author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Sat, 13 Dec 2008 15:40:14 +0100 | |
changeset 11 | a769385a59c6 |
parent 10 | 311ea5fa60dd |
child 13 | d3554afaa768 |
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", |
|
11
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
16 |
"CHANNEL", "TOPIC", NULL |
0 | 17 |
}; |
18 |
||
19 |
int config (UC * uc, char *fname) |
|
20 |
{ |
|
21 |
FILE *f; |
|
22 |
char buffer[513]; |
|
10
311ea5fa60dd
Code cleanups and debian changelogs
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
6
diff
changeset
|
23 |
char **line; |
0 | 24 |
char *token; |
25 |
char *value; |
|
26 |
int i = 0; |
|
27 |
||
28 |
if ((f = fopen (fname, "r")) == NULL) |
|
29 |
return -1; |
|
30 |
||
31 |
while (CONFIG_OPTIONS[i] != NULL) |
|
32 |
i++; |
|
33 |
||
11
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
34 |
line = calloc ((size_t) (i + 1), sizeof (char *)); |
0 | 35 |
|
36 |
/* |
|
37 |
* We can easily provide default values ... |
|
38 |
*/ |
|
6
7eb12be31bb5
Various Bug Fixes including
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
39 |
uc->nick = uc->pass = uc->server = uc->channel = uc->topic = NULL; |
0 | 40 |
uc->port = 6667; |
41 |
||
11
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
42 |
while (!feof (f)) { |
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
43 |
*buffer = '\0'; |
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
44 |
(void)fgets (buffer, 512, f); |
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
45 |
token = buffer; |
0 | 46 |
|
11
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
47 |
while (*token == '\t') /* Eat trailing tabs */ |
0 | 48 |
token++; |
11
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
49 |
while (*token == ' ') /* Eat trailing whitespaces */ |
0 | 50 |
token++; |
51 |
||
52 |
token = strtok (token, ":"); |
|
53 |
||
54 |
if (token != NULL) { |
|
55 |
value = strtok (NULL, "\n"); |
|
56 |
i = 0; |
|
57 |
while (CONFIG_OPTIONS[i] != NULL) { |
|
11
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
58 |
if (!strcmp (CONFIG_OPTIONS[i], token)) { |
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
59 |
if (value) |
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
60 |
line[i] = strdup (value); |
0 | 61 |
} |
62 |
i++; |
|
63 |
} |
|
64 |
} |
|
65 |
} |
|
66 |
||
67 |
if (fclose (f) != 0) |
|
68 |
return -1; |
|
69 |
||
70 |
i = 0; |
|
71 |
while (CONFIG_OPTIONS[i] != NULL) { |
|
72 |
if (line[i] != NULL) |
|
73 |
switch (i) { |
|
74 |
case 0: /* NICK */ |
|
11
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
75 |
uc->nick = strdup (line[i]); |
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
76 |
free (line[i]); |
0 | 77 |
break; |
78 |
case 1: /* PASSWORD */ |
|
11
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
79 |
uc->pass = strdup (line[i]); |
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
80 |
free (line[i]); |
0 | 81 |
break; |
82 |
case 2: /* SERVER */ |
|
11
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
83 |
uc->server = strdup (line[i]); |
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
84 |
free (line[i]); |
0 | 85 |
break; |
86 |
case 3: /* PORT */ |
|
87 |
uc->port = atoi (line[i]); |
|
11
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
88 |
free (line[i]); |
0 | 89 |
break; |
90 |
case 4: /* CHANNEL */ |
|
11
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
91 |
uc->channel = strdup (line[i]); |
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
92 |
free (line[i]); |
0 | 93 |
break; |
94 |
case 5: /* TOPIC */ |
|
11
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
95 |
uc->topic = strdup (line[i]); |
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
96 |
free (line[i]); |
0 | 97 |
break; |
98 |
} |
|
99 |
i++; |
|
100 |
} |
|
101 |
||
11
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
102 |
if (line != NULL) |
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
103 |
free (line); |
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
104 |
|
0 | 105 |
if (!(uc->nick && uc->pass && uc->server && uc->channel)) |
106 |
return -2; |
|
107 |
||
108 |
return 0; |
|
109 |
} |