Mercurial > baum
comparison baum.c @ 8:495a56e706dc
input is now read from stdin
author | meillo@marmaro.de |
---|---|
date | Sat, 09 Feb 2008 12:42:11 +0100 |
parents | 6a6152cf63f7 |
children | c020b0d1cfca |
comparison
equal
deleted
inserted
replaced
7:6a6152cf63f7 | 8:495a56e706dc |
---|---|
179 | 179 |
180 } | 180 } |
181 | 181 |
182 | 182 |
183 /* read input */ | 183 /* read input */ |
184 void read_input(char* filename) { | 184 void read_input() { |
185 FILE* file; | |
186 int c; | 185 int c; |
187 char* cp; | 186 int indent; |
188 int indent = 0; | |
189 char name[256]; | 187 char name[256]; |
190 int value; | 188 int value; |
191 | 189 |
192 file = fopen(filename, "r"); | 190 indent = 0; |
193 | 191 strcpy(name, ""); |
194 | 192 value = 0; |
195 while ((c = getc(file)) != EOF) { | 193 |
196 if (c == '#') { | 194 while ((c = getchar()) != EOF) { |
197 printf("c\n"); | 195 if (c == '#') { /* comment */ |
198 while ((c = getc(file)) != '\n') { | 196 while ((c = getchar()) != '\n') { |
199 } | 197 } |
200 } | 198 } |
201 | 199 |
202 if (c == ' ' || c == '\t') { | 200 if (c == ' ' || c == '\t') { /* indent if at start of line */ |
203 indent++; | 201 if (strlen(name) == 0) { |
204 } | 202 indent++; |
205 | 203 } |
206 if (c == '\n') { | 204 } |
205 | |
206 if (c == '\n') { /* end of line: create node */ | |
207 if (strlen(name) > 0) { | 207 if (strlen(name) > 0) { |
208 printf(" %d - %s - %d\n", indent, name, value); | 208 printf(" %d - %s - %d\n", indent, name, value); |
209 /* | 209 /* create node */ |
210 */ | |
211 } else { | |
212 printf("comment\n"); | |
213 } | 210 } |
214 indent = 0; | 211 indent = 0; |
215 strcpy(name, ""); | 212 strcpy(name, ""); |
216 value = 0; | 213 value = 0; |
217 } | 214 } |
218 | 215 |
219 if (c >= 'a' && c <= 'z') { | 216 if (c >= 'a' && c <= 'z') { /* name */ |
220 int i = 1; | 217 int i = 1; |
221 name[0] = (char) c; | 218 name[0] = (char) c; |
222 while ((c = getc(file)) != '(') { | 219 while ((c = getchar()) != '(') { |
223 /*putc(c, stdout);*/ | |
224 name[i] = (char) c; | 220 name[i] = (char) c; |
225 i++; | 221 i++; |
222 if (i > 255) { | |
223 break; | |
224 } | |
226 } | 225 } |
227 name[i] = '\0'; | 226 name[i] = '\0'; |
228 } | 227 } |
229 | 228 |
230 if (c == '(') { | 229 if (c == '(') { /* value */ |
231 fscanf(file, "%d)", &value); | 230 scanf("%d)", &value); |
232 } | 231 } |
233 | 232 |
234 } | 233 } |
235 | 234 |
236 fclose(file); | |
237 } | 235 } |
238 | 236 |
239 /* main */ | 237 /* main */ |
240 int main(int argc, char* argv[]) { | 238 int main(int argc, char* argv[]) { |
241 unsigned char shell_return = 0; | 239 unsigned char shell_return = 0; |
251 printTree(root); | 249 printTree(root); |
252 | 250 |
253 delete(root); | 251 delete(root); |
254 */ | 252 */ |
255 | 253 |
256 read_input("./input_addition"); | 254 read_input(); |
257 exit(shell_return); | 255 exit(shell_return); |
258 } | 256 } |