enumerated types for config.c
authorMarkus Bröker <mbroeker@largo.dyndns.tv>
Thu, 28 May 2009 14:08:06 +0200
changeset 36 6889aacd038e
parent 35 3fedae1f6ceb
child 37 85891f91096c
enumerated types for config.c The config section gets an enumerated type for constant values. This makes the code more readable and less error prone. committer: Markus Bröker <mbroeker@largo.homelinux.org>
src/config.c
--- a/src/config.c
+++ b/src/config.c
@@ -12,6 +12,10 @@
 #include <config.h>
 #include "common.h"
 
+enum config_map {
+    NICK, PASSWORD, SERVER, PORT, CHANNEL, TOPIC,
+};
+
 const
 char *CONFIG_OPTIONS[] = {
     "NICK", "PASSWORD", "SERVER", "PORT",
@@ -25,15 +29,15 @@
     char **line;
     char *token;
     char *value;
-    int i = 0;
+    enum config_map map = 0;
 
     if ((f = fopen (fname, "r")) == NULL)
         return -1;
 
-    while (CONFIG_OPTIONS[i] != NULL)
-        i++;
+    while (CONFIG_OPTIONS[map] != NULL)
+        map++;
 
-    line = calloc ((size_t) (i + 1), sizeof (char *));
+    line = calloc ((size_t) (map + 1), sizeof (char *));
 
     /*
      * We can easily provide default values ...
@@ -54,13 +58,13 @@
 
         if (token != NULL) {
             value = strtok (NULL, "\n");
-            i = 0;
-            while (CONFIG_OPTIONS[i] != NULL) {
-                if (!strcmp (CONFIG_OPTIONS[i], token)) {
+            map = 0;
+            while (CONFIG_OPTIONS[map] != NULL) {
+                if (!strcmp (CONFIG_OPTIONS[map], token)) {
                     if (value)
-                        line[i] = compat_strdup (value);
+                        line[map] = compat_strdup (value);
                 }
-                i++;
+                map++;
             }
         }
     }
@@ -68,36 +72,36 @@
     if (fclose (f) != 0)
         return -1;
 
-    i = 0;
-    while (CONFIG_OPTIONS[i] != NULL) {
-        if (line[i] != NULL)
-            switch (i) {
-            case 0:            /* NICK */
-                uc->nick = compat_strdup (line[i]);
-                free (line[i]);
+    map = 0;
+    while (CONFIG_OPTIONS[map] != NULL) {
+        if (line[map] != NULL)
+            switch (map) {
+            case NICK:
+                uc->nick = compat_strdup (line[map]);
+                free (line[map]);
                 break;
-            case 1:            /* PASSWORD */
-                uc->pass = compat_strdup (line[i]);
-                free (line[i]);
+            case PASSWORD:
+                uc->pass = compat_strdup (line[map]);
+                free (line[map]);
                 break;
-            case 2:            /* SERVER */
-                uc->server = compat_strdup (line[i]);
-                free (line[i]);
+            case SERVER:
+                uc->server = compat_strdup (line[map]);
+                free (line[map]);
                 break;
-            case 3:            /* PORT */
-                uc->port = atoi (line[i]);
-                free (line[i]);
+            case PORT:
+                uc->port = atoi (line[map]);
+                free (line[map]);
                 break;
-            case 4:            /* CHANNEL */
-                uc->channel = compat_strdup (line[i]);
-                free (line[i]);
+            case CHANNEL:
+                uc->channel = compat_strdup (line[map]);
+                free (line[map]);
                 break;
-            case 5:            /* TOPIC */
-                uc->topic = compat_strdup (line[i]);
-                free (line[i]);
+            case TOPIC:
+                uc->topic = compat_strdup (line[map]);
+                free (line[map]);
                 break;
             }
-        i++;
+        map++;
     }
 
     if (line != NULL)