Thread: INI Praser

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    3
    Rep Power
    0

    INI Praser


    Hi,
    I am new to standard libs of C.I used to program hardware where these libs dont come in any use.

    I was trying to develop a simple ini praser.
    My INI Syntax is simple:
    [HEADER]
    ITEM=VALUE
    ITEM2=VALUE

    I want a function that would return the VALUE of an item in he spefic section or header.

    Somewhat lyk this
    char * text = GetItemValue(char * FileName,char * section,char * item);

    Thanks for ur consideration
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    34
    The keywords to search for are ini, parser, and C.

    If you want to write your own function, I'd pass a FILE * pointing to an opened stream rather than a char * pointing to a filename. Passing a pointer o a filename, you'd need to open (and close) the file everytime you call the function.

    Also, I'd rather pass in the destination address rather than return a pointer to the result. The return value could then indicate success or some error ("section not exist", "key not exist", "value too long for destination length", ...)
    Code:
    int GetItemValue(char *dst, size_t dstlen, FILE *inihandle, const char *section, const char *item);
    Happy Coding!
  4. #3
  5. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    Well the usual way to read a text file is (no error checks for brevity)
    Code:
    FILE *fp = fopen("file.txt","r");
    char buff[BUFSIZ];
    while ( fgets(buff,BUFSIZ,fp) != NULL ) {
        // do something with each line
    }
    fclose(fp);
    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
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    3
    Rep Power
    0

    Reply


    I find difficult to start using these libs.However, I will try developing the code and paste the code here if I go successful.

    Thanks & Regards
  8. #5
  9. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,645
    Rep Power
    4248
    If you're using Windows, why write your own when there is a function already provided by the Windows API to do this. See GetPrivateProfileString() for more details.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    3
    Rep Power
    0
    Originally Posted by Scorpions4ever
    If you're using Windows, why write your own when there is a function already provided by the Windows API to do this.
    I am running windows but VC++ or so,I am using a Game Development SOftware having the language syntax as C and also few libs of C.

    ================================
    I have come so far, but I have trouble.

    char * GetItemValue(char * filename,char * section,char * item)
    {
    char value[1024];
    char sectionstr[256];
    char itemstr[256];
    char tmp;
    char * errormsg;
    int i;
    FILE * inifile;

    if((inifile=fopen(filename,"r")) == NULL)
    {
    errormsg = str_cat(str_create("Failed to open "),str_create(filename));
    WriteINILog("GetItemValue",errormsg);
    return -1;
    }
    //File Found
    goto find;
    find:
    while(!feof(inifile))
    {
    find_section:
    if((getc(inifile)) == '[')
    {
    i = 0;
    while(1)
    {
    tmp = getc(inifile);
    if(tmp == ']')
    {
    if(str_cmp(section,sectionstr))
    {
    i = 0;
    while(1)
    {
    tmp = getc(inifile);
    if(tmp == '=')
    {
    if(str_cmp(item,itemstr))
    {
    WriteINILog("GetItemValue","Requested Item Found");
    while(1)
    {
    i = 0;
    tmp = getc(inifile);
    if(tmp == ';') { return value; }
    if(feof(inifile)) { WriteINILog("GetItemValue","End Of File while refering a value"); return -3; }
    value[i] = tmp;
    i++;
    wait(1);
    }
    }
    }
    if(tmp == '[') { WriteINILog("GetItemValue","Item not found in the given section/Non-terminating Item"); return -2; }
    if(feof(inifile)){ WriteINILog("GetItemValue","Item not found in the given section/Non-terminating Item"); return -2; }
    itemstr[i] =tmp;
    i++;
    wait(1);
    }
    }
    else
    {
    i = 0;
    goto find_section;
    }
    }
    if(tmp == '\n') { continue; }
    if(feof(inifile)){ WriteINILog("GetItemValue","Section opened is never closed"); return -1; }
    sectionstr[i] = tmp;
    i++;
    wait(1);
    }

    }
    wait(1);
    }
    WriteINILog("GetItemValue","Required section not found or no Sections EXIST:End of File(EOF)");
    }



    My INI File
    [TestSectionMain]
    TestItem=12345;
    TestItem2=12345;
    [TestSection]
    TestItem=123;
  12. #7
  13. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,117
    Rep Power
    1803
    Originally Posted by Yashas
    I am running windows but VC++ or so,I am using a Game Development SOftware having the language syntax as C and also few libs of C.
    None of which precludes you from using GetPrivateProfileString(). Why do you think it does?

    Either way, source code to do this is commonly available; why waste your time developing yet another? For example.
    Last edited by clifford; October 14th, 2012 at 04:14 AM.
  14. #8
  15. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,117
    Rep Power
    1803
    Originally Posted by Yashas
    I have come so far, but I have trouble.
    1. Post the code in code tags.
    2. Tell us exactly what "trouble" you are having so we don't have to guess.

IMN logo majestic logo threadwatch logo seochat tools logo