author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Sat, 13 Dec 2008 15:38:48 +0100 | |
changeset 7 | b04ef0b1cd3b |
parent 6 | 7eb12be31bb5 |
child 10 | 311ea5fa60dd |
permissions | -rw-r--r-- |
0 | 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 |
*/ |
|
6
7eb12be31bb5
Various Bug Fixes including
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
40 |
uc->nick = uc->pass = uc->server = uc->channel = uc->topic = NULL; |
0 | 41 |
uc->port = 6667; |
42 |
||
6
7eb12be31bb5
Various Bug Fixes including
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
43 |
*buffer = 0; |
0 | 44 |
fgets (buffer, 512, f); |
45 |
||
46 |
token = buffer; |
|
47 |
||
48 |
while (!feof (f)) { |
|
49 |
/* |
|
50 |
* eat trailing tabs |
|
51 |
*/ |
|
52 |
while (*token == '\t') |
|
53 |
token++; |
|
54 |
||
55 |
/* |
|
56 |
* eat trailing whitespaces |
|
57 |
*/ |
|
58 |
while (*token == ' ') |
|
59 |
token++; |
|
60 |
||
61 |
token = strtok (token, ":"); |
|
62 |
||
63 |
if (token != NULL) { |
|
64 |
value = strtok (NULL, "\n"); |
|
65 |
i = 0; |
|
66 |
while (CONFIG_OPTIONS[i] != NULL) { |
|
67 |
if (!strcmp (token, CONFIG_OPTIONS[i])) { |
|
68 |
line[i] = malloc (strlen (value) + 1); |
|
69 |
strncpy (line[i], value, strlen (value)); |
|
70 |
} |
|
71 |
i++; |
|
72 |
} |
|
73 |
} |
|
74 |
*buffer = 0; |
|
75 |
fgets (buffer, 512, f); |
|
76 |
} |
|
77 |
||
78 |
if (fclose (f) != 0) |
|
79 |
return -1; |
|
80 |
||
81 |
i = 0; |
|
82 |
while (CONFIG_OPTIONS[i] != NULL) { |
|
83 |
if (line[i] != NULL) |
|
84 |
switch (i) { |
|
85 |
case 0: /* NICK */ |
|
86 |
uc->nick = line[i]; |
|
87 |
break; |
|
88 |
case 1: /* PASSWORD */ |
|
89 |
uc->pass = line[i]; |
|
90 |
break; |
|
91 |
case 2: /* SERVER */ |
|
92 |
uc->server = line[i]; |
|
93 |
break; |
|
94 |
case 3: /* PORT */ |
|
95 |
uc->port = atoi (line[i]); |
|
96 |
break; |
|
97 |
case 4: /* CHANNEL */ |
|
98 |
uc->channel = line[i]; |
|
99 |
break; |
|
100 |
case 5: /* TOPIC */ |
|
101 |
uc->topic = line[i]; |
|
102 |
break; |
|
103 |
} |
|
104 |
i++; |
|
105 |
} |
|
106 |
||
107 |
if (!(uc->nick && uc->pass && uc->server && uc->channel)) |
|
108 |
return -2; |
|
109 |
||
110 |
return 0; |
|
111 |
} |