baum
diff 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 |
line diff
1.1 --- a/baum.c Sat Feb 09 11:57:16 2008 +0100 1.2 +++ b/baum.c Sat Feb 09 12:42:11 2008 +0100 1.3 @@ -181,59 +181,57 @@ 1.4 1.5 1.6 /* read input */ 1.7 -void read_input(char* filename) { 1.8 - FILE* file; 1.9 +void read_input() { 1.10 int c; 1.11 - char* cp; 1.12 - int indent = 0; 1.13 + int indent; 1.14 char name[256]; 1.15 int value; 1.16 1.17 - file = fopen(filename, "r"); 1.18 + indent = 0; 1.19 + strcpy(name, ""); 1.20 + value = 0; 1.21 1.22 - 1.23 - while ((c = getc(file)) != EOF) { 1.24 - if (c == '#') { 1.25 - printf("c\n"); 1.26 - while ((c = getc(file)) != '\n') { 1.27 + while ((c = getchar()) != EOF) { 1.28 + if (c == '#') { /* comment */ 1.29 + while ((c = getchar()) != '\n') { 1.30 } 1.31 } 1.32 1.33 - if (c == ' ' || c == '\t') { 1.34 - indent++; 1.35 + if (c == ' ' || c == '\t') { /* indent if at start of line */ 1.36 + if (strlen(name) == 0) { 1.37 + indent++; 1.38 + } 1.39 } 1.40 1.41 - if (c == '\n') { 1.42 + if (c == '\n') { /* end of line: create node */ 1.43 if (strlen(name) > 0) { 1.44 printf(" %d - %s - %d\n", indent, name, value); 1.45 - /* 1.46 - */ 1.47 - } else { 1.48 - printf("comment\n"); 1.49 + /* create node */ 1.50 } 1.51 indent = 0; 1.52 strcpy(name, ""); 1.53 value = 0; 1.54 } 1.55 1.56 - if (c >= 'a' && c <= 'z') { 1.57 + if (c >= 'a' && c <= 'z') { /* name */ 1.58 int i = 1; 1.59 name[0] = (char) c; 1.60 - while ((c = getc(file)) != '(') { 1.61 - /*putc(c, stdout);*/ 1.62 + while ((c = getchar()) != '(') { 1.63 name[i] = (char) c; 1.64 i++; 1.65 + if (i > 255) { 1.66 + break; 1.67 + } 1.68 } 1.69 name[i] = '\0'; 1.70 } 1.71 1.72 - if (c == '(') { 1.73 - fscanf(file, "%d)", &value); 1.74 + if (c == '(') { /* value */ 1.75 + scanf("%d)", &value); 1.76 } 1.77 1.78 } 1.79 1.80 - fclose(file); 1.81 } 1.82 1.83 /* main */ 1.84 @@ -253,6 +251,6 @@ 1.85 delete(root); 1.86 */ 1.87 1.88 - read_input("./input_addition"); 1.89 + read_input(); 1.90 exit(shell_return); 1.91 }