rockstiff
rockstiff

Reputation: 383

Read .ini file and return specific value in C

So, I need to access a .ini file like:

[alpha]
colour=green
size=78
style=italic
[beta]
colour=black
size=94
mode=xyz
[gamma]
black=0231
blue=127
red=0x35876f

I need to find a section [likethis], then a parameter (one of the follow three) and then return its value. So, section alpha, param size, I return "78". Section gamma, param red, I return "0x35876f". section beta, param red doesn't exist.

char *sFile: Name of file
char *sSec:  Section where the parameter is
char *sPar:  Parametro wanted
char *sRet:  Array where I store the value
int  ilen:  Lenght of the array

i open the file with fp = fopen (sFile,"r");, but then it gets complicated to find and return the value and I dont know if this is the best way to do it.

char *strAux, *strAux2;

while(!feof(fp)) //While the file doesnt end
{
    fscanf (fp,"%s",strAux); //Read a line of the file
    if(!strcmp(strAux,sSec)) //If I found the section
    {
        for(j=0; j<3; j++)
        {
            fscanf(fp,"%s",strAux); //Read a line
            strAux2 = strtok (strAux,"="); //store the param in strAux2 from the start to the =
            if (!strcmp(strAux2,sPar))
            {
                strAux2 = strtok (NULL,"\r\n"); //store in strAux2 frmo the = to end of line
                if(strlen(strAux2)>ilen)
                {
                    fclose(fp);
                    return (-3);  //Error, Lenght Value > ilen
                }else{
                    strncpy(sRet,strAux2,ilen);
                    fclose(fp);
                    return (sRet);   //Return the value
                }
            }
        }
    }           
}
fclose(fp);
return (-2);  //Error, Parameter not found

Is this ok?

Upvotes: 4

Views: 1022

Answers (2)

Sridhar Nagarajan
Sridhar Nagarajan

Reputation: 1105

Dont use while(!feof). Instead you could use while(fscanf(fp,"%s",buf) ==1) and you can use same token delimiter for strtok for both the calls as =. You should allocate memory for strAux before using it in fscanf.

Upvotes: 0

&#212;rel
&#212;rel

Reputation: 7632

I think a more manual approach is easier

open the file

search lines starting with [

when done look for a line with the param name and a = (If you find a [ search has failed)

extra skip space around '='

FILE *f = fopen("test.ini", "r");                                          
char *sSec = "beta";                                                       
char *sPar = "size";                                                       
char *sRet = NULL;                                                         
char *p;                                                                   
char buf[1024];                                                            
bool section_search = true;                                                

while (p = fgets(buf, 1024, f)) {                                          
    if (section_search) {                                                  
        const char *q = sSec;                                              
        if (*p != '[') {                                                   
            continue;                                                      
        }                                                                  
        p++; /* read [ */                                                  
        while (*q && *p == *q) {                                           
            p++;                                                           
            q++;                                                           
        }                                                                  
        if (*q == '\0' && *p == ']') {                                     
            section_search = false;                                        
            continue;                                                      
        }                                                                  
    } else {                                                               
        const char *q = sPar;                                              

        if (*p == '[') {                                                   
            break;                                                         
        }                                                                  
        while (*q && *p == *q) {                                           
            p++;                                                           
            q++;                                                           
        }                                                                  
        if (*q != '\0')                                                    
            continue;                                                      
        while (*p == ' ') p++;                                             
        if (*p != '=')                                                     
            continue;                                                      
        p++; /*read =*/                                                    
        while (*p == ' ') p++;                                             
        sRet = p;                                                          
        break;                                                             

    }                                                                      
}                                                                          
if (sRet) {                                                                
    printf("value found %s", sRet);                                        
} else {                                                                   
    printf("value not found");                                             
}   

Upvotes: 1

Related Questions