1 | #include "readcfg.h" |
---|
2 | |
---|
3 | #ifndef __STRING_H |
---|
4 | #include <string.h> |
---|
5 | #endif |
---|
6 | #ifndef __STDLIB_H |
---|
7 | #include <stdlib.h> |
---|
8 | #endif |
---|
9 | #ifndef __ERRNO_H |
---|
10 | #include <errno.h> |
---|
11 | #endif |
---|
12 | |
---|
13 | #define COMMENT ';' |
---|
14 | #define MAXLEN 2048 |
---|
15 | |
---|
16 | static cstr error_message; /* Kann von decode gesetzt werden */ |
---|
17 | /* und wird dann als Fehlermeldung */ |
---|
18 | /* ausgegeben */ |
---|
19 | |
---|
20 | /* -------------------------------------------------------------------------- */ |
---|
21 | /* static void scanKeywords(struct S_cfgLine line[], int *lineanz, int... */ |
---|
22 | /* ------------------------------------------------------ 18.05.95 02.10 ---- */ |
---|
23 | /* */ |
---|
24 | /* Anzahl und maximale Lnge der Keywords bestimmen */ |
---|
25 | /* */ |
---|
26 | static void scanKeywords(struct S_cfgLine line[], int *lineanz, int *maxlen) |
---|
27 | { |
---|
28 | while (1) |
---|
29 | { |
---|
30 | cstr keyword = line[*lineanz].keyword; |
---|
31 | int keywordlen; |
---|
32 | |
---|
33 | if (!keyword) break; |
---|
34 | keywordlen = strlen(keyword); |
---|
35 | if (keywordlen>*maxlen) *maxlen = keywordlen; |
---|
36 | (*lineanz)++; |
---|
37 | } |
---|
38 | } |
---|
39 | /* -------------------------------------------------------------------------- */ |
---|
40 | /* static void cfgReadWarning(cstr fname, int lineno) */ |
---|
41 | /* ------------------------------------------------------ 18.05.95 02.23 ---- */ |
---|
42 | static void cfgReadWarning(cstr fname, int lineno) |
---|
43 | { |
---|
44 | warningf("Fehler in Zeile %i von '%s'", lineno, fname); |
---|
45 | } |
---|
46 | /* -------------------------------------------------------------------------- */ |
---|
47 | /* int readCfg(cstr fname, struct S_cfgLine line[]) */ |
---|
48 | /* ------------------------------------------------------ 17.05.95 21:23 ---- */ |
---|
49 | /* */ |
---|
50 | /* liest und dekodiert CFG-Datei */ |
---|
51 | /* */ |
---|
52 | int readCfg(cstr fname, struct S_cfgLine line[]) |
---|
53 | { |
---|
54 | FILE *in = fopen(fname, "r"); |
---|
55 | int ok = in!=NULL; |
---|
56 | |
---|
57 | if (!ok && errno==ENOENT) /* Datei nicht vorhanden */ |
---|
58 | { |
---|
59 | FILE *out = fopen(fname, "w"); |
---|
60 | |
---|
61 | if (out) |
---|
62 | { |
---|
63 | fprintf(out, ";\n; CFG-Datei '%s'\n;\n\n", fname); |
---|
64 | fclose(out); |
---|
65 | |
---|
66 | warningf("'%s' wurde erzeugt.", fname); |
---|
67 | |
---|
68 | in = fopen(fname, "r"); |
---|
69 | ok = in!=NULL; |
---|
70 | } |
---|
71 | } |
---|
72 | |
---|
73 | if (ok) |
---|
74 | { |
---|
75 | char linebuf[MAXLEN]; |
---|
76 | int readLines = 0, /* Anzahl gelesener Zeilen */ |
---|
77 | keywords = 0, /* Anzahl keywords */ |
---|
78 | maxKeywordlen = 0, /* Laenge des laengsten keywords */ |
---|
79 | lineno = 0, /* fortlaufende Zeilennummer */ |
---|
80 | *wordRead, /* welche Worte wurden in welcher Zeile gelesen */ |
---|
81 | x; |
---|
82 | |
---|
83 | scanKeywords(line, &keywords, &maxKeywordlen); |
---|
84 | |
---|
85 | wordRead = (int*)malloc(keywords*sizeof(int)); |
---|
86 | for (x = 0; x<keywords; x++) wordRead[x] = 0; |
---|
87 | |
---|
88 | /* /----------------------------\ */ |
---|
89 | /* | CFG lesen und decodieren | */ |
---|
90 | /* \----------------------------/ */ |
---|
91 | |
---|
92 | while (1) |
---|
93 | { |
---|
94 | str firstWord; |
---|
95 | |
---|
96 | if (!fgets(linebuf, MAXLEN, in)) break; |
---|
97 | lineno++; |
---|
98 | |
---|
99 | firstWord = strtok(linebuf, " \n"); |
---|
100 | |
---|
101 | if (firstWord && firstWord[0]!=COMMENT && firstWord[0]!=0) |
---|
102 | { |
---|
103 | int search; |
---|
104 | |
---|
105 | for (search = 0; search<keywords; search++) |
---|
106 | { |
---|
107 | if (strcmp(line[search].keyword, firstWord)==0) |
---|
108 | { |
---|
109 | str restDerZeile = strtok(NULL, "\n"); |
---|
110 | int decoded; |
---|
111 | |
---|
112 | error_message = NULL; |
---|
113 | decoded = line[search].decode(restDerZeile, line[search].varPointer); |
---|
114 | |
---|
115 | if (!decoded) |
---|
116 | { |
---|
117 | cfgReadWarning(fname, lineno); |
---|
118 | if (error_message) warning(error_message); |
---|
119 | else warningf("Angabe bei '%s' kann nicht interpretiert werden.", line[search].keyword); |
---|
120 | ok = 0; |
---|
121 | } |
---|
122 | else |
---|
123 | { |
---|
124 | if (wordRead[search]) /* schonmal gelesen? */ |
---|
125 | { |
---|
126 | if (decoded!=2) /* mehrfache Verwendung erlaubt? */ |
---|
127 | { |
---|
128 | cfgReadWarning(fname, lineno); |
---|
129 | warningf("Schluesselwort '%s' doppelt definiert (Erste Definition in Zeile %i)", firstWord, wordRead[search]); |
---|
130 | ok = 0; |
---|
131 | } |
---|
132 | } |
---|
133 | else /* nein, dann merken! */ |
---|
134 | { |
---|
135 | wordRead[search] = lineno; |
---|
136 | readLines++; |
---|
137 | } |
---|
138 | } |
---|
139 | |
---|
140 | break; |
---|
141 | } |
---|
142 | } |
---|
143 | |
---|
144 | if (search==keywords) /* keyword nicht gefunden! */ |
---|
145 | { |
---|
146 | cfgReadWarning(fname, lineno); |
---|
147 | warningf("Unbekanntes Schluesselwort '%s'", firstWord); |
---|
148 | ok = 0; |
---|
149 | } |
---|
150 | } |
---|
151 | } |
---|
152 | |
---|
153 | fclose(in); |
---|
154 | |
---|
155 | /* /----------------------------------------------\ */ |
---|
156 | /* | Waren alle keywords im CFG-File vorhanden? | */ |
---|
157 | /* \----------------------------------------------/ */ |
---|
158 | |
---|
159 | if (ok && readLines<keywords) /* nein :-( */ |
---|
160 | { |
---|
161 | FILE *out = fopen(fname, "a"); |
---|
162 | |
---|
163 | if (out) |
---|
164 | { |
---|
165 | for (x = 0; x<keywords; x++) |
---|
166 | { |
---|
167 | if (!wordRead[x]) |
---|
168 | { |
---|
169 | fprintf(out, ";%s\n" |
---|
170 | "\n" |
---|
171 | "%-*s %s\n" |
---|
172 | "\n", |
---|
173 | line[x].description, |
---|
174 | maxKeywordlen, |
---|
175 | line[x].keyword, |
---|
176 | line[x].defaultVal); |
---|
177 | } |
---|
178 | } |
---|
179 | |
---|
180 | fclose(out); |
---|
181 | warningf("Fehlende Keywords an '%s' angehngt.", fname); |
---|
182 | } |
---|
183 | |
---|
184 | ok = 0; |
---|
185 | } |
---|
186 | |
---|
187 | free(wordRead); |
---|
188 | } |
---|
189 | |
---|
190 | return ok; |
---|
191 | } |
---|
192 | /* -------------------------------------------------------------------------- */ |
---|
193 | /* void setCfgError(cstr message) */ |
---|
194 | /* ------------------------------------------------------ 17.05.95 22.01 ---- */ |
---|
195 | void setCfgError(cstr message) |
---|
196 | { |
---|
197 | error_message = message; |
---|
198 | } |
---|
199 | |
---|
200 | |
---|