FOX Community

Recent site activity

Cookbook‎ > ‎

Iterating over entries in FXSettings

Problem

You want to loop over the entries in an FXSettings object.

Solution

Since the FXSettings class is derived from FXDict, the procedure for iterating over the contents of a settings object is similar to that for iterating over the contents of any dictionary. The important thing to recognize is that each key in a settings object is the name of a different section of the settings, and the "data" for that section is itself another dictionary of (key, value) pairs. So, to iterate over the sections in a settings object, you'd begin with some code like this:

FXint s;
FXStringDict *dict;

for (s = settings->first(); s < settings->size(); s = settings->next(s))
{
    dict = settings->data(s);
}

Here, settings is a pointer to an FXSettings object and dict is assigned a pointer to a section of the settings. Now we can build on this by adding an inner loop that loops over the contents of each section (which is just an FXStringDict, a dictionary that maps string keys to string values):

FXint s;
FXStringDict *dict;

for (s = settings->first(); s < settings->size(); s = settings->next(s))
{
    dict = settings->data(s);
    for (FXint e = dict->first(); e < dict->size(); e = dict->next(e))
    {
        const FXchar *key = dict->key(s);
        const FXchar *data = dict->data(s);
        FXbool mark = dict->mark(s);
    }
}

Of course, if you know the name of the section you're interested in, you don't have to loop over the entire settings object to find it; that's what the find() function is for:

FXStringDict *prefs;


prefs = settings->find("PREFERENCES");
for (FXint e = prefs->first(); e < prefs->size(); e = prefs->next(e))
{
    const FXchar *key = prefs->key(e);
    const FXchar *data = prefs->data(e);
}

And (last but not least) if you know the name of the specific setting (and which section it's in) you can go straight to it:


const FXchar *data = settings->find("PREFERENCES")->find("IceCreamFlavor");

Contributor(s)

Lyle Johnson