-eliminating duplicate code
[oweals/gnunet.git] / src / util / configuration.c
index 41a4e1975ad242da6ab3afe1bd55a7d8fe5b599b..d006ed3ef3e10ba24968a4c881d754140b11f345 100644 (file)
@@ -197,19 +197,19 @@ GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
     for (i = strlen (line) - 1; (i >= 0) && (isspace ((unsigned char) line[i]));
          i--)
       line[i] = '\0';
-    if (1 == sscanf (line, "@INLINE@ %191[^\n]", value))
+    if (1 == SSCANF (line, "@INLINE@ %191[^\n]", value))
     {
       /* @INLINE@ value */
       if (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, value))
         ret = GNUNET_SYSERR;    /* failed to parse included config */
     }
-    else if (1 == sscanf (line, "[%99[^]]]", value))
+    else if (1 == SSCANF (line, "[%99[^]]]", value))
     {
       /* [value] */
       GNUNET_free (section);
       section = GNUNET_strdup (value);
     }
-    else if (2 == sscanf (line, " %63[^= ] = %191[^\n]", tag, value))
+    else if (2 == SSCANF (line, " %63[^= ] = %191[^\n]", tag, value))
     {
       /* tag = value */
       /* Strip LF */
@@ -233,7 +233,7 @@ GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
       }
       GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, &value[i]);
     }
-    else if (1 == sscanf (line, " %63[^= ] =[^\n]", tag))
+    else if (1 == SSCANF (line, " %63[^= ] =[^\n]", tag))
     {
       /* tag = */
       GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, "");
@@ -248,7 +248,7 @@ GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
       break;
     }
   }
-  GNUNET_assert (0 == fclose (fp));
+  GNUNET_assert (0 == FCLOSE (fp));
   /* restore dirty flag - anything we set in the meantime
    * came from disk */
   cfg->dirty = dirty;
@@ -309,7 +309,7 @@ GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
   sec = cfg->sections;
   while (sec != NULL)
   {
-    if (0 > fprintf (fp, "[%s]\n", sec->name))
+    if (0 > FPRINTF (fp, "[%s]\n", sec->name))
     {
       error = 1;
       break;
@@ -327,7 +327,7 @@ GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
           pos[0] = '\\';
           pos[1] = 'n';
         }
-        if (0 > fprintf (fp, "%s = %s\n", ent->key, val))
+        if (0 > FPRINTF (fp, "%s = %s\n", ent->key, val))
         {
           error = 1;
           GNUNET_free (val);
@@ -339,7 +339,7 @@ GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
     }
     if (error != 0)
       break;
-    if (0 > fprintf (fp, "\n"))
+    if (0 > FPRINTF (fp, "%s\n", ""))
     {
       error = 1;
       break;
@@ -348,7 +348,7 @@ GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
   }
   if (error != 0)
     LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fprintf", filename);
-  GNUNET_assert (0 == fclose (fp));
+  GNUNET_assert (0 == FCLOSE (fp));
   if (error != 0)
   {
     cfg->dirty = GNUNET_SYSERR; /* last write failed */
@@ -407,7 +407,7 @@ GNUNET_CONFIGURATION_iterate_section_values (const struct
   struct ConfigEntry *epos;
 
   spos = cfg->sections;
-  while ((spos != NULL) && (0 != strcmp (spos->name, section)))
+  while ((spos != NULL) && (0 != strcasecmp (spos->name, section)))
     spos = spos->next;
 
   if (spos == NULL)
@@ -465,7 +465,7 @@ GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg,
   spos = cfg->sections;
   while (spos != NULL)
   {
-    if (0 == strcmp (section, spos->name))
+    if (0 == strcasecmp (section, spos->name))
     {
       if (prev == NULL)
         cfg->sections = spos->next;
@@ -1235,6 +1235,25 @@ parse_configuration_file (void *cls, const char *filename)
 }
 
 
+/**
+ * Load default configuration.  This function will parse the
+ * defaults from the given defaults_d directory.
+ *
+ * @param cfg configuration to update
+ * @param defaults_d directory with the defaults
+ * @return GNUNET_OK on success, GNUNET_SYSERR on error
+ */
+int
+GNUNET_CONFIGURATION_load_from (struct GNUNET_CONFIGURATION_Handle *cfg,
+                               const char *defaults_d)
+{
+  if (GNUNET_SYSERR ==
+      GNUNET_DISK_directory_scan (defaults_d, &parse_configuration_file, cfg))
+    return GNUNET_SYSERR;       /* no configuration at all found */
+  return GNUNET_OK;
+}
+
+
 /**
  * Load configuration (starts with defaults, then loads
  * system-specific configuration).