DEADSOFTWARE

sound: move sound/music configuration to sound driver
[flatwaifu.git] / src / config.c
index ed611c3b47772c3525f4ebaa2daf7588501c72fa..1980bc61ec2986f13e9d82bfe4100a47abecfa9e 100644 (file)
@@ -35,15 +35,20 @@ static int ch;
 
 const cfg_t *CFG_find_entry (const char *key, const cfg_t *cfg) {
   assert(key != NULL);
-  assert(cfg != NULL);
-  int i = 0;
-  while (cfg[i].cfg && strcasecmp(cfg[i].cfg, key) != 0) {
-    i++;
+  if (cfg != NULL) {
+    int i = 0;
+    while (cfg[i].cfg && strcasecmp(cfg[i].cfg, key) != 0) {
+      i++;
+    }
+    return cfg[i].cfg ? &cfg[i] : NULL;
+  } else {
+    return NULL;
   }
-  return cfg[i].cfg ? &cfg[i] : NULL;
 }
 
 int CFG_update_key (const char *key, const char *value, const cfg_t *cfg) {
+  assert(key != NULL);
+  assert(value != NULL);
   const cfg_t *entry = CFG_find_entry(key, cfg);
   if (entry != NULL) {
     void *p = entry->p;
@@ -53,11 +58,12 @@ int CFG_update_key (const char *key, const char *value, const cfg_t *cfg) {
       case Y_DWORD: *(dword*)p = atoi(value); break;
       case Y_STRING: strcpy(p, value); break; // TODO fix this security problem
       case Y_SW_ON: *(byte*)p = strcasecmp(value, "on") == 0 ? 1 : 0; break;
-      case Y_SW_OFF: *(byte*)p = strcasecmp(value, "off") == 0 ? 0 : 1; break;
+      case Y_SW_OFF: *(byte*)p = strcasecmp(value, "off") == 0 ? 1 : 0; break;
       case Y_FILES: F_addwad(value); break;
       case Y_KEY: *(int*)p = I_string_to_key(value); break;
       default: assert(0); // unknown type -> something broken
     }
+    //logo("CFG_update_key: [%s] = [%s]\n", key, value);
     return 1;
   } else {
     return 0;
@@ -140,13 +146,20 @@ void CFG_close_iterator (void) {
 
 /* --- reader --- */
 
-int CFG_read_config (const char *name, const cfg_t *cfg) {
+int CFG_read_config (const char *name, int n, const cfg_t **cfg) {
+  assert(name != NULL);
+  assert(n >= 0);
+  assert(cfg != NULL);
+  int i;
   char key[64];
   char value[64];
   assert(name != NULL);
   if (CFG_open_iterator(name)) {
     while (CFG_scan_iterator(key, 64, value, 64)) {
-      CFG_update_key(key, value, cfg);
+      i = 0;
+      while (i < n && CFG_update_key(key, value, cfg[i]) == 0) {
+        i++;
+      }
     }
     CFG_close_iterator();
     return 1;
@@ -172,21 +185,21 @@ static void CFG_write_key_value (FILE *f, const char *key, const char *value) {
 static int CFG_write_entry (FILE *f, const cfg_t *entry) {
   assert(f != NULL);
   assert(entry != NULL);
-  char buf[64];
+  char buf[16];
   const char *str;
   const char *key = entry->cfg;
   if (key != NULL) {
     switch (entry->t) {
       case Y_BYTE:
-        snprintf(buf, 64, "%i", *(byte*)entry->p);
+        snprintf(buf, 16, "%i", *(byte*)entry->p);
         CFG_write_key_value(f, key, buf);
         break;
       case Y_WORD:
-        snprintf(buf, 64, "%i", *(word*)entry->p);
+        snprintf(buf, 16, "%i", *(word*)entry->p);
         CFG_write_key_value(f, key, buf);
         break;
       case Y_DWORD:
-        snprintf(buf, 64, "%i", *(dword*)entry->p);
+        snprintf(buf, 16, "%i", *(dword*)entry->p);
         CFG_write_key_value(f, key, buf);
         break;
       case Y_STRING:
@@ -237,9 +250,11 @@ int CFG_update_config (const char *old, const char *new, int n, const cfg_t **cf
       CFG_close_iterator();
     }
     for (j = 0; j < n; j++) {
-      i = 0;
-      while (CFG_write_entry(nf, &cfg[j][i])) {
-        i++;
+      if (cfg[j] != NULL) {
+        i = 0;
+        while (CFG_write_entry(nf, &cfg[j][i])) {
+          i++;
+        }
       }
     }
     fclose(nf);