src/config.c
changeset 36 6889aacd038e
parent 33 56571d34d754
child 47 a689b6a8e6ed
equal deleted inserted replaced
35:3fedae1f6ceb 36:6889aacd038e
    10 
    10 
    11 #include <compat.h>
    11 #include <compat.h>
    12 #include <config.h>
    12 #include <config.h>
    13 #include "common.h"
    13 #include "common.h"
    14 
    14 
       
    15 enum config_map {
       
    16     NICK, PASSWORD, SERVER, PORT, CHANNEL, TOPIC,
       
    17 };
       
    18 
    15 const
    19 const
    16 char *CONFIG_OPTIONS[] = {
    20 char *CONFIG_OPTIONS[] = {
    17     "NICK", "PASSWORD", "SERVER", "PORT",
    21     "NICK", "PASSWORD", "SERVER", "PORT",
    18     "CHANNEL", "TOPIC", NULL
    22     "CHANNEL", "TOPIC", NULL
    19 };
    23 };
    23     FILE *f;
    27     FILE *f;
    24     char buffer[DEFAULT_BUF_SIZE];
    28     char buffer[DEFAULT_BUF_SIZE];
    25     char **line;
    29     char **line;
    26     char *token;
    30     char *token;
    27     char *value;
    31     char *value;
    28     int i = 0;
    32     enum config_map map = 0;
    29 
    33 
    30     if ((f = fopen (fname, "r")) == NULL)
    34     if ((f = fopen (fname, "r")) == NULL)
    31         return -1;
    35         return -1;
    32 
    36 
    33     while (CONFIG_OPTIONS[i] != NULL)
    37     while (CONFIG_OPTIONS[map] != NULL)
    34         i++;
    38         map++;
    35 
    39 
    36     line = calloc ((size_t) (i + 1), sizeof (char *));
    40     line = calloc ((size_t) (map + 1), sizeof (char *));
    37 
    41 
    38     /*
    42     /*
    39      * We can easily provide default values ...
    43      * We can easily provide default values ...
    40      */
    44      */
    41     uc->nick = uc->pass = uc->server = uc->channel = uc->topic = NULL;
    45     uc->nick = uc->pass = uc->server = uc->channel = uc->topic = NULL;
    52 
    56 
    53         token = strtok (token, ":");
    57         token = strtok (token, ":");
    54 
    58 
    55         if (token != NULL) {
    59         if (token != NULL) {
    56             value = strtok (NULL, "\n");
    60             value = strtok (NULL, "\n");
    57             i = 0;
    61             map = 0;
    58             while (CONFIG_OPTIONS[i] != NULL) {
    62             while (CONFIG_OPTIONS[map] != NULL) {
    59                 if (!strcmp (CONFIG_OPTIONS[i], token)) {
    63                 if (!strcmp (CONFIG_OPTIONS[map], token)) {
    60                     if (value)
    64                     if (value)
    61                         line[i] = compat_strdup (value);
    65                         line[map] = compat_strdup (value);
    62                 }
    66                 }
    63                 i++;
    67                 map++;
    64             }
    68             }
    65         }
    69         }
    66     }
    70     }
    67 
    71 
    68     if (fclose (f) != 0)
    72     if (fclose (f) != 0)
    69         return -1;
    73         return -1;
    70 
    74 
    71     i = 0;
    75     map = 0;
    72     while (CONFIG_OPTIONS[i] != NULL) {
    76     while (CONFIG_OPTIONS[map] != NULL) {
    73         if (line[i] != NULL)
    77         if (line[map] != NULL)
    74             switch (i) {
    78             switch (map) {
    75             case 0:            /* NICK */
    79             case NICK:
    76                 uc->nick = compat_strdup (line[i]);
    80                 uc->nick = compat_strdup (line[map]);
    77                 free (line[i]);
    81                 free (line[map]);
    78                 break;
    82                 break;
    79             case 1:            /* PASSWORD */
    83             case PASSWORD:
    80                 uc->pass = compat_strdup (line[i]);
    84                 uc->pass = compat_strdup (line[map]);
    81                 free (line[i]);
    85                 free (line[map]);
    82                 break;
    86                 break;
    83             case 2:            /* SERVER */
    87             case SERVER:
    84                 uc->server = compat_strdup (line[i]);
    88                 uc->server = compat_strdup (line[map]);
    85                 free (line[i]);
    89                 free (line[map]);
    86                 break;
    90                 break;
    87             case 3:            /* PORT */
    91             case PORT:
    88                 uc->port = atoi (line[i]);
    92                 uc->port = atoi (line[map]);
    89                 free (line[i]);
    93                 free (line[map]);
    90                 break;
    94                 break;
    91             case 4:            /* CHANNEL */
    95             case CHANNEL:
    92                 uc->channel = compat_strdup (line[i]);
    96                 uc->channel = compat_strdup (line[map]);
    93                 free (line[i]);
    97                 free (line[map]);
    94                 break;
    98                 break;
    95             case 5:            /* TOPIC */
    99             case TOPIC:
    96                 uc->topic = compat_strdup (line[i]);
   100                 uc->topic = compat_strdup (line[map]);
    97                 free (line[i]);
   101                 free (line[map]);
    98                 break;
   102                 break;
    99             }
   103             }
   100         i++;
   104         map++;
   101     }
   105     }
   102 
   106 
   103     if (line != NULL)
   107     if (line != NULL)
   104         free (line);
   108         free (line);
   105 
   109