RE: [AD] al_list_config_entries

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


On Wed, 2005-06-22 at 14:50 +0200, Elias Pschernig wrote:
> New patch attached, it works now with the override files as well, and

Attach to this, I mean..

-- 
Elias Pschernig
Index: src/config.c
===================================================================
RCS file: /cvsroot/alleg/allegro/src/config.c,v
retrieving revision 1.30
diff -u -p -r1.30 config.c
--- src/config.c	25 May 2005 21:04:26 -0000	1.30
+++ src/config.c	22 Jun 2005 12:16:45 -0000
@@ -1401,3 +1401,92 @@ AL_CONST char *get_config_text(AL_CONST 
    return ret;
 }
 
+
+
+/* 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 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.339
diff -u -p -r1.339 allegro._tx
--- docs/src/allegro._tx	10 Jun 2005 13:32:13 -0000	1.339
+++ docs/src/allegro._tx	22 Jun 2005 12:17:21 -0000
@@ -2063,6 +2063,47 @@ your game.
    Writes a 4-letter driver ID variable to the current config file. See the 
    comments about set_config_string().
 
+@@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(&sections);
+   /* 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.
+
 @hnode Standard config variables
 @xref set_config_file, set_config_string, get_config_string
 Allegro uses these standard variables from the configuration file:


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/