- doc
[oweals/gnunet.git] / src / util / configuration.c
index 7d5dc10c1046d2cab86512465b7bc12bb8ed05a3..72fe0e7d02de532364bfe63dc660d8acf7c45f5c 100644 (file)
@@ -157,6 +157,7 @@ GNUNET_CONFIGURATION_deserialize (struct GNUNET_CONFIGURATION_Handle *cfg,
                                  int allow_inline)
 {
   char *line;
+  char *line_orig;
   size_t line_size;
   char *pos;
   unsigned int nr;
@@ -175,26 +176,26 @@ GNUNET_CONFIGURATION_deserialize (struct GNUNET_CONFIGURATION_Handle *cfg,
   section = GNUNET_strdup ("");
   nr = 0;
   r_bytes = 0;
+  line_orig = NULL;
   while (r_bytes < size)
   {
+    GNUNET_free_non_null (line_orig);
     /* fgets-like behaviour on buffer */
     to_read = size - r_bytes;
     pos = memchr (&mem[r_bytes], '\n', to_read);
     if (NULL == pos)
     {
-      line = GNUNET_strndup (&mem[r_bytes], line_size = to_read);
+      line_orig = GNUNET_strndup (&mem[r_bytes], line_size = to_read);
       r_bytes += line_size;    
     }
     else
     {
-      line = GNUNET_strndup (&mem[r_bytes], line_size = (pos - &mem[r_bytes]));
+      line_orig = GNUNET_strndup (&mem[r_bytes], line_size = (pos - &mem[r_bytes]));
       r_bytes += line_size + 1;
     }
+    line = line_orig;
     /* increment line number */
     nr++;
-    /* ignore comments */
-    if ( ('#' == line[0]) || ('%' == line[0]) )
-      continue; 
     /* tabs and '\r' are whitespace */
     emptyline = GNUNET_YES;
     for (i = 0; i < line_size; i++)
@@ -214,6 +215,13 @@ GNUNET_CONFIGURATION_deserialize (struct GNUNET_CONFIGURATION_Handle *cfg,
     for (i = line_size - 1; (i >= 1) && (isspace ((unsigned char) line[i]));i--)
       line[i] = '\0';
 
+    /* remove leading whitespace */
+    for (; line[0] != '\0' && (isspace ((unsigned char) line[0])); line++);
+
+    /* ignore comments */
+    if ( ('#' == line[0]) || ('%' == line[0]) )
+      continue;
+
     /* handle special "@INLINE@" directive */
     if (0 == strncasecmp (line, 
                          "@INLINE@ ",
@@ -275,6 +283,7 @@ GNUNET_CONFIGURATION_deserialize (struct GNUNET_CONFIGURATION_Handle *cfg,
       }
       LOG (GNUNET_ERROR_TYPE_DEBUG, "Config value %s=\"%s\"\n", tag, value);
       GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, &value[i]);
+      GNUNET_free (tag);
       continue;
     }
     /* parse error */
@@ -284,7 +293,8 @@ GNUNET_CONFIGURATION_deserialize (struct GNUNET_CONFIGURATION_Handle *cfg,
     ret = GNUNET_SYSERR;
     break;
   }
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished deserializing config\n", tag);
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished deserializing config\n");
+  GNUNET_free_non_null (line_orig);
   GNUNET_free (section);  
   GNUNET_assert ( (GNUNET_OK != ret) || (r_bytes == size) );
   return ret;
@@ -726,8 +736,8 @@ findEntry (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section,
  * @param value value to copy (of the default conf.)
  */
 static void
-compareEntries (void *cls, const char *section, const char *option,
-                const char *value)
+compare_entries (void *cls, const char *section, const char *option,
+                const char *value)
 {
   struct DiffHandle *dh = cls;
   struct ConfigEntry *entNew;
@@ -739,6 +749,28 @@ compareEntries (void *cls, const char *section, const char *option,
 }
 
 
+/**
+ * Compute configuration with only entries that have been changed
+ *
+ * @param cfgDefault original configuration
+ * @param cfgNew new configuration
+ * @return configuration with only the differences, never NULL
+ */
+struct GNUNET_CONFIGURATION_Handle *
+GNUNET_CONFIGURATION_get_diff (const struct GNUNET_CONFIGURATION_Handle
+                              *cfgDefault,
+                              const struct GNUNET_CONFIGURATION_Handle
+                              *cfgNew)
+{
+  struct DiffHandle diffHandle;
+
+  diffHandle.cfgDiff = GNUNET_CONFIGURATION_create ();
+  diffHandle.cfgDefault = cfgDefault;
+  GNUNET_CONFIGURATION_iterate (cfgNew, &compare_entries, &diffHandle);
+  return diffHandle.cfgDiff;
+}
+
+
 /**
  * Write only configuration entries that have been changed to configuration file
  * @param cfgDefault default configuration
@@ -753,13 +785,11 @@ GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle
                                   *cfgNew, const char *filename)
 {
   int ret;
-  struct DiffHandle diffHandle;
+  struct GNUNET_CONFIGURATION_Handle *diff;
 
-  diffHandle.cfgDiff = GNUNET_CONFIGURATION_create ();
-  diffHandle.cfgDefault = cfgDefault;
-  GNUNET_CONFIGURATION_iterate (cfgNew, compareEntries, &diffHandle);
-  ret = GNUNET_CONFIGURATION_write (diffHandle.cfgDiff, filename);
-  GNUNET_CONFIGURATION_destroy (diffHandle.cfgDiff);
+  diff = GNUNET_CONFIGURATION_get_diff (cfgDefault, cfgNew);
+  ret = GNUNET_CONFIGURATION_write (diff, filename);
+  GNUNET_CONFIGURATION_destroy (diff);
   return ret;
 }