src/config.c
changeset 0 586472add385
child 6 7eb12be31bb5
equal deleted inserted replaced
-1:000000000000 0:586472add385
       
     1 /**
       
     2  *  $Id: config.c 51 2008-01-10 00:19:39Z mbroeker $
       
     3  * $URL: http://localhost/svn/c/mcbot/trunk/src/config.c $
       
     4  *
       
     5  */
       
     6 
       
     7 #include <stdio.h>
       
     8 #include <stdlib.h>
       
     9 #include <string.h>
       
    10 
       
    11 #include <config.h>
       
    12 
       
    13 const
       
    14 char *CONFIG_OPTIONS[] = {
       
    15     "NICK", "PASSWORD", "SERVER", "PORT",
       
    16     "CHANNEL", "TOPIC",
       
    17     NULL
       
    18 };
       
    19 
       
    20 int config (UC * uc, char *fname)
       
    21 {
       
    22     FILE *f;
       
    23     char buffer[513];
       
    24     static char **line;
       
    25     char *token;
       
    26     char *value;
       
    27     int i = 0;
       
    28 
       
    29     if ((f = fopen (fname, "r")) == NULL)
       
    30         return -1;
       
    31 
       
    32     while (CONFIG_OPTIONS[i] != NULL)
       
    33         i++;
       
    34 
       
    35     line = calloc (i, sizeof (char *));
       
    36 
       
    37     /*
       
    38      * We can easily provide default values ...
       
    39      */
       
    40     uc->nick = uc->pass = uc->server = uc->channel = uc->topic = 0;
       
    41     uc->port = 6667;
       
    42 
       
    43     fgets (buffer, 512, f);
       
    44 
       
    45     token = buffer;
       
    46 
       
    47     while (!feof (f)) {
       
    48         /*
       
    49          * eat trailing tabs
       
    50          */
       
    51         while (*token == '\t')
       
    52             token++;
       
    53 
       
    54         /*
       
    55          * eat trailing whitespaces
       
    56          */
       
    57         while (*token == ' ')
       
    58             token++;
       
    59 
       
    60         token = strtok (token, ":");
       
    61 
       
    62         if (token != NULL) {
       
    63             value = strtok (NULL, "\n");
       
    64             i = 0;
       
    65             while (CONFIG_OPTIONS[i] != NULL) {
       
    66                 if (!strcmp (token, CONFIG_OPTIONS[i])) {
       
    67                     line[i] = malloc (strlen (value) + 1);
       
    68                     strncpy (line[i], value, strlen (value));
       
    69                 }
       
    70                 i++;
       
    71             }
       
    72         }
       
    73         *buffer = 0;
       
    74         fgets (buffer, 512, f);
       
    75     }
       
    76 
       
    77     if (fclose (f) != 0)
       
    78         return -1;
       
    79 
       
    80     i = 0;
       
    81     while (CONFIG_OPTIONS[i] != NULL) {
       
    82         if (line[i] != NULL)
       
    83             switch (i) {
       
    84             case 0:            /* NICK */
       
    85                 uc->nick = line[i];
       
    86                 break;
       
    87             case 1:            /* PASSWORD */
       
    88                 uc->pass = line[i];
       
    89                 break;
       
    90             case 2:            /* SERVER */
       
    91                 uc->server = line[i];
       
    92                 break;
       
    93             case 3:            /* PORT */
       
    94                 uc->port = atoi (line[i]);
       
    95                 break;
       
    96             case 4:            /* CHANNEL */
       
    97                 uc->channel = line[i];
       
    98                 break;
       
    99             case 5:            /* TOPIC */
       
   100                 uc->topic = line[i];
       
   101                 break;
       
   102             }
       
   103         i++;
       
   104     }
       
   105 
       
   106     if (!(uc->nick && uc->pass && uc->server && uc->channel))
       
   107         return -2;
       
   108 
       
   109     return 0;
       
   110 }