Skip to content

Idea: testing for existence of a particular section #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
krychu opened this issue Mar 6, 2019 · 1 comment
Open

Idea: testing for existence of a particular section #2

krychu opened this issue Mar 6, 2019 · 1 comment

Comments

@krychu
Copy link

krychu commented Mar 6, 2019

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 krychu changed the title Idea: testing for existence of particular section Idea: testing for existence of a particular section Mar 6, 2019
@jjbailey
Copy link

jjbailey commented Aug 2, 2022

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);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants