[ Thread Index |
Date Index
| More lists.liballeg.org/allegro-developers Archives
]
Attached again a patch I sent some months ago, against new_api_branch..
I'm not sure how well it will fit into the new config system, but I find
it useful right now.
--
Elias Pschernig
Index: src/config.c
===================================================================
RCS file: /cvsroot/alleg/allegro/src/config.c,v
retrieving revision 1.26.2.4
diff -r1.26.2.4 config.c
1403a1404,1492
>
>
> /* add_unique_name
> * Helper to add a name to a list of names.
> */
> static int add_unique_name(AL_CONST char ***names, int n, char const *name)
> {
> int i;
> /* FIXME: use better search algorithm */
> for (i = 0; i < n; i++)
> if (!ustrcmp((*names)[i], name))
> return n;
>
> *names = _al_sane_realloc((void *)*names, (n + 1) * sizeof **names);
> (*names)[n] = name;
> return n + 1;
> }
>
>
>
> /* attach_config_entries
> * Helper function to attach key or section names to a list of strings.
> */
> static int attach_config_entries(CONFIG *conf, AL_CONST char *section,
> int n, AL_CONST char ***names, int list_sections)
> {
> CONFIG_ENTRY *p;
> char section_name[256];
> prettify_section_name(section, section_name, sizeof(section_name));
> int in_section;
>
> if (conf) {
> p = conf->head;
>
> /* If section is NULL, only initial, section-less entries are used. */
> if (ugetc(section_name))
> in_section = FALSE;
> else
> in_section = TRUE;
>
> while (p) {
> if (p->name) {
> /* a section start is just a list entry enclosed in [] */
> if (ugetc(p->name) == '[' && ugetat(p->name, -1) == ']') {
> if (list_sections) {
> n = add_unique_name(names, n, p->name);
> }
> in_section = (ustricmp(section_name, p->name) == 0);
> }
> else if (in_section && !list_sections) {
> n = add_unique_name(names, n, p->name);
> }
> }
> p = p->next;
> }
> }
> return n;
> }
>
>
>
> /* list_config_entires:
> * Returns the names of all config entries in a section. The names parameter is
> * a pointer to a strings array that will contain the config keys. If it points to
> * a NULL pointer, it will be allocated, or else re-allocated accordingly. The
> * return value tells how many valid string pointers it contains after the
> * function returns.
> */
> int list_config_entries(AL_CONST char *section, AL_CONST char ***names)
> {
> int n = 0;
> n = attach_config_entries(config_override, section, n, names, 0);
> n = attach_config_entries(config[0], section, n, names, 0);
> return n;
> }
>
>
>
> /* list_config_sections:
> * Returns the names of all current config sections, enclodes in []. The names
> * parameter and return value is like in list_config_entires above.
> */
> int list_config_sections(AL_CONST char ***names)
> {
> int n = 0;
> n = attach_config_entries(config_override, NULL, n, names, 1);
> n = attach_config_entries(config[0], NULL, n, names, 1);
> return n;
> }
Index: docs/src/allegro._tx
===================================================================
RCS file: /cvsroot/alleg/allegro/docs/src/allegro._tx,v
retrieving revision 1.249.2.13
diff -r1.249.2.13 allegro._tx
2157a2158,2198
> @@int @list_config_entries(const char *section, const char ***names);
> @xref set_config_file, get_config_string
> @shortdesc Lists the names of all entries in a config section
> This function can be used to get a list of all entries in the given config
> section. The names parameter is a pointer to an array of strings. If it
> points to a NULL pointer, the list will be allocated, else it will be
> re-allocated. You should free the list yourself with free if you don't need
> it anymore. See the following example for how you can use it, it will print
> out the complete contents of the current configuration:
> <codeblock>
> int i, n;
> char const **sections = NULL;
> char const **entries = NULL;
>
> n = list_config_sections(§ions);
> /* loop through all section names */
> for (i = 0; i < n; i++)
> {
> int j, m;
> printf("%s\n", sections[i]);
> m = list_config_entries(sections[i], &entries);
> /* loop through all entries in the section */
> for (j = 0; j < m; j++)
> {
> printf(" %s=\"%s\"\n", entries[j], get_config_string(
> sections[i], entries[j], "-"));
> }
> }
> free(sections);
> free(entries);<endblock>
> @retval
> Returns the number of valid strings in the names array.
>
> @@int @list_config_sections(const char ***names);
> @xref list_config_entries, set_config_file, get_config_string
> @shortdesc Lists the names of all sections available in the current configuration.
> The names parameter is a pointer to an array of strings. See
> list_config_entries for more information and an example how to use it.
> @retval
> Returns the number of valid strings in the names array.
>