Thread: Structures , Callback Functions , LibCurl - Need Some Help!

  1. #1
    Registered User
    Join Date
    Nov 2014
    Location
    Centurion, Gauteng, South Africa
    Posts
    28

    Wink Structures , Callback Functions , LibCurl - Need Some Help!

    printf("%s",MemBlock->Data[0]);So Playing Around with structures :
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    struct MemoryBlock
    {
        char *Data;
        size_t Size; //size_t is a unsigned integer
    };
    
    
    int LoadFromFile(char *FileName,void *GenericPointer)
    {
        FILE *Source;
        size_t Size = 0;
        Source = fopen(FileName,"r");
        struct MemoryBlock *MemBlock;
        MemBlock = GenericPointer;
        if (Source!=NULL)
        {
            char ch;
            while((ch = fgetc(Source))!=EOF)
            {
            Size++;
            MemBlock->Data=MemBlock->Data+ch; //Fills my structure with data
            };
            MemBlock->Size=Size;
        }
        return 1; //This line will only be reached if the file could not be found.
    }
    int PrintMemBlock(void *GenericPointer)
    {
    struct MemoryBlock *MemBlock;
    MemBlock=GenericPointer;
    printf("%s\n","Memory Block:");
    printf("\nSize :%i\n",MemBlock->Size);
    
    
    printf("%s",MemBlock->Data[0]);
    return 0;
    }
    
    
    int main()
    {
        struct MemoryBlock MemBlock;
        MemBlock.Size = 3;
        MemBlock.Data="a";
        LoadFromFile("BlobFile.txt",&MemBlock);
        PrintMemBlock(&MemBlock);
        return 0;
    }
    This is bugging me - printf("%s",MemBlock->Data[0]);
    1. ) How do I work with the first element of the Data variable?


    On to callback functions :
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    struct Rectangle
    {
       int Width,Length;
       float Area;
       char *Name;
    };
    
    
    void Construct_Rectangle(struct Rectangle *StructPnt,char *AName,int ALength,int AWidth)
    {
        StructPnt->Name = AName; //-> is basically just dereferancing the struct like &StructPnt....
        StructPnt->Width = AWidth;
        StructPnt->Length = ALength;
        StructPnt->Area = AWidth * ALength;
    }
    
    
    void Print_Rectangle(struct Rectangle *StructPnt)
    {
        printf("Name : %s\n",StructPnt->Name);
        printf("Width : %i\n",StructPnt->Width);
        printf("Length : %i\n",StructPnt->Length);
        printf("Area : %f\n",StructPnt->Area);
    
    
    }
    
    
    void Count(void(*Referance)())
    {
        int i = 1 ;
        for(i;i!=10;i++)
        {
            printf("%i\n",i);
            Referance(i);
        }
    }
    void CallBack(int a)
    {
        if (a%2==0) printf("%s\n","Called A");
            else printf("%s\n","Called B");
    }
    
    
    
    
    int main()
    {
        //Void pointer
        int AString = 5;
        void *Pntr; //Generic Pointer
        Pntr = AString;
        printf("%i\n",Pntr);
        //Structures
        struct Rectangle Test;
        Construct_Rectangle(&Test,"Bobby",5,10);
        Print_Rectangle(&Test);
        Count(CallBack);
        return 0;
    
    
        //Callbacks
    
    
    }
    Just any help with this , if I'm going wrong somewhere. General Tips!

    And then libcurl :O

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <include/curl/curl.h>
    #include <include/curl/easy.h> //#includes are funny because i had no idea what i was doing setting up curl
    
    
    static int CBFWrite(void *Contents,size_t Size,size_t NumberOfUnits,void *UserPnter);
    typedef struct Memblock_t;
    struct Memblock_t
    {
        char *Data;
        size_t Size;
    };
    
    
    
    
    static int CBFWrite(void *Contents,size_t Size,size_t NumberOfUnits,void *UserPnter)
    //Callback structure  : Void Pointer , size_t , size_t , void Pointer
    {
        size_t RawSize = Size * NumberOfUnits;
        struct MemBlock_t *Chunk;
        Chunk = (struct Memblock_t *)UserPnter;
    
    
        return 0;
    }
    
    
    
    
    
    
    int main()
    {
        CURL *CurlHandle;
        CURLcode CurlResult;
        CurlHandle = curl_easy_init();
        struct MemBlock_t DataChunk;
    
    
        curl_easy_setopt(CurlHandle,CURLOPT_URL,"http://www.youtube.com");
        curl_easy_setopt(CurlHandle,CURLOPT_WRITEFUNCTION,CBFWrite);
        curl_easy_setopt(CurlHandle,CURLOPT_WRITEDATA,(void *)&DataChunk);
    
    
        CurlResult = curl_easy_perform(CurlHandle);
        if (CurlResult == CURLE_OK)
        {
            printf("%s\n","Executed With No Errors");
        }
        else
        {
            printf("%s\n","Error");
        }
    
    
    
    
        curl_global_cleanup();
        return 0;
    }
    I get an error on this line
    Chunk = (struct Memblock_t *)UserPnter;
    Error : dereferencing pointer to incomplete type


    Aaaaaaand this line
    struct MemBlock_t DataChunk;
    Error : Size of datachunk is unknown.



    As I understand it instead of pushing everything to the stdout by using curl_easy_opt(...CURLOPT_WRITEFUNCTION,CallbackFun ction)
    curl will now send it's output to that particular function.

    Now this
    Code:
     static int CBFWrite(void *Contents,size_t Size,size_t NumberOfUnits,void *UserPnter)
    is the function Curl is expecting ; That being said if I were to lets say remove a parameter - would my program simply output garbash or just skip that variable (like if what curl is expecting in the callback function was set up in a parallel array ) Eg:

    My main function expects int a , char b , int c
    but my callback function only provides int a and char b
    what happens to int c?

    As of now i feel the structures and callback functions are much more important than learning to use libcurl. Please help!

    Saw Someting else:

    I guess this comes from delphi XD
    MemBlock->Data=MemBlock->Data+ch; //Fills my structure with data
    Why does this line work? Isnt it the same to lets say AString = AString + 'c'
    Which again in delphi is fine but in c shouldnt i use strcat()
    Also. Shouldnt i be allocating memory somewhere . im confused.
    Last edited by RagingGrim; 11-28-2014 at 06:22 AM. Reason: Noticed Errors , Saw Something!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,408
    Let's look at your first example with corrections.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct MemoryBlock
    {
        char *Data;
        size_t Size; //size_t is a unsigned integer
    };
    
    int LoadFromFile(char *FileName,void *GenericPointer)
    {
        FILE *Source;
        size_t Size = 0;
        Source = fopen(FileName,"r");
        struct MemoryBlock *MemBlock;
        MemBlock = GenericPointer;
        if (Source!=NULL)
        {
            int ch;
            //!! read the file, while there is space to store the result
            while( Size < MemBlock->Size &&
                  (ch = fgetc(Source))!=EOF)
            {
                //!! this actually appends to a buffer
                MemBlock->Data[Size++] = ch;
            }
            //!! update the max size, with the actual amount used.
            MemBlock->Size=Size;
            fclose(Source);
        }
        return 1;
    }
    
    int PrintMemBlock(void *GenericPointer)
    {
      struct MemoryBlock *MemBlock;
      MemBlock=GenericPointer;
      printf("%s\n","Memory Block:");
      printf("\nSize :%zd\n",MemBlock->Size);
      //!! restrict printing to only the valid length
      printf("%*s",(int)MemBlock->Size,MemBlock->Data);
      return 0;
    }
    
    
    int main()
    {
        struct MemoryBlock MemBlock;
        MemBlock.Size = 50;
        MemBlock.Data = malloc(50); //!! Make it point at usable memory, for the size needed
        LoadFromFile("bar.c",&MemBlock);
        PrintMemBlock(&MemBlock);
        return 0;
    }
    Now you could adopt the convention that if .Size is 0 and .Data is NULL, then LoadFromFile() should call malloc/realloc itself until either memory is exhausted or the whole file has been read. Either approach is fine.

    As for your curl program,
    > struct Memblock_t
    ...
    > struct MemBlock_t *Chunk;
    C is case sensitive.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Callback Functions
    By valaris in forum C Programming
    Replies: 11
    Last Post: 07-31-2008, 09:20 PM
  2. CallBack Functions
    By manofsteel972 in forum Windows Programming
    Replies: 4
    Last Post: 11-04-2004, 04:04 PM
  3. Callback functions from dll
    By Calthun in forum Windows Programming
    Replies: 2
    Last Post: 07-09-2004, 04:13 PM
  4. Callback functions
    By linuxdude in forum C Programming
    Replies: 2
    Last Post: 01-03-2004, 06:23 PM
  5. Callback functions
    By nbo10 in forum C# Programming
    Replies: 1
    Last Post: 10-08-2003, 07:51 AM

Tags for this Thread