ini icon indicating copy to clipboard operation
ini copied to clipboard

Idea: testing for existence of a particular section

Open krychu opened this issue 5 years ago • 1 comments

It'd be useful to be able to test if a particular section exists before trying to read anything from it. This would allow for creating relevant structures in advance. And it would be especially useful in situations when you cannot rely on any key/value pair being always present if a section exists.

Is this something that could be supported without expanding existing API? e.g.,

ini_get(config, "owner", NULL) == NULL // section doesn't exist ini_get(config, "owner", NULL) != NULL // section exists

If that sounds OK to you I could create a PR.

krychu avatar Mar 06 '19 16:03 krychu

I wrote a function to return a list of sections. Perhaps this will be useful.

int get_sections(ini_t *inidata, int maxsect, char *sections[]) 
{       
    /* returns a list of INI sections */
    
    char   *p;
    int     i = 0; 
        
    for(p = inidata->data; p < inidata->end; p++)
        if(*p == '[') {
            if(p > inidata->data && *(p - 1))       /* line does not start with [ */
                continue;

            sections[i++] = strdup(p + 1);
            
            if(i == maxsect)
                break;
        }

    return (i);
}

jjbailey avatar Aug 02 '22 22:08 jjbailey