Mercurial > baum
comparison baum.c @ 55:6279e5b14d9e
added error handling for fopen and malloc; code cleanups
author | meillo@marmaro.de |
---|---|
date | Sun, 16 Mar 2008 10:40:53 +0100 |
parents | 6e46b106c334 |
children | b7544f23673b 73de2151aebd |
comparison
equal
deleted
inserted
replaced
54:6e46b106c334 | 55:6279e5b14d9e |
---|---|
42 | 42 |
43 /* new */ | 43 /* new */ |
44 struct Node* newNode(char* name, unsigned char value) { | 44 struct Node* newNode(char* name, unsigned char value) { |
45 struct Node* node; | 45 struct Node* node; |
46 node = (struct Node*) malloc(sizeof(struct Node)); | 46 node = (struct Node*) malloc(sizeof(struct Node)); |
47 if (node == NULL) { | |
48 perror("unable to allocate memory"); | |
49 exit(10); | |
50 } | |
47 strcpy(node->name, name); | 51 strcpy(node->name, name); |
48 node->value = value; | 52 node->value = value; |
49 node->right = 0; | 53 node->right = 0; |
50 node->down = 0; | 54 node->down = 0; |
51 return node; | 55 return node; |
89 } | 93 } |
90 | 94 |
91 | 95 |
92 /* print */ | 96 /* print */ |
93 void printNode(struct Node* node, int level) { | 97 void printNode(struct Node* node, int level) { |
94 if (node != NULL) { | 98 if (node == NULL) { |
95 while (level-- > 0) { | 99 return; |
96 fprintf(stderr, "\t"); | 100 } |
97 } | 101 while (level-- > 0) { |
98 fprintf(stderr, "%s (%d|%c)\n", node->name, node->value, node->value); | 102 fprintf(stderr, "\t"); |
99 } | 103 } |
104 fprintf(stderr, "%s (%d|%c)\n", node->name, node->value, node->value); | |
100 } | 105 } |
101 | 106 |
102 void printTree(struct Node* root, int level) { | 107 void printTree(struct Node* root, int level) { |
103 if (root == NULL) { | 108 if (root == NULL) { |
104 return; | 109 return; |
115 /* stack for read_input */ | 120 /* stack for read_input */ |
116 void push(struct Node* node) { | 121 void push(struct Node* node) { |
117 struct Stackitem* tmp; | 122 struct Stackitem* tmp; |
118 struct Stackitem* new; | 123 struct Stackitem* new; |
119 new = (struct Stackitem*) malloc(sizeof(struct Stackitem)); | 124 new = (struct Stackitem*) malloc(sizeof(struct Stackitem)); |
125 if (new == NULL) { | |
126 perror("unable to allocate memory"); | |
127 exit(10); | |
128 } | |
120 new->node = node; | 129 new->node = node; |
121 tmp = stack; | 130 tmp = stack; |
122 stack = new; | 131 stack = new; |
123 stack->next = tmp; | 132 stack->next = tmp; |
124 } | 133 } |
152 strcpy(name, ""); | 161 strcpy(name, ""); |
153 value = 0; | 162 value = 0; |
154 last_indent = -1; | 163 last_indent = -1; |
155 last_node = NULL; | 164 last_node = NULL; |
156 file = fopen(filename, "r"); | 165 file = fopen(filename, "r"); |
166 if (file == NULL) { | |
167 perror("unable to open file"); | |
168 exit(10); | |
169 } | |
157 | 170 |
158 while ((c = getc(file)) != EOF) { | 171 while ((c = getc(file)) != EOF) { |
159 if (c == '#') { /* comment */ | 172 if (c == '#') { /* comment */ |
160 while ((c = getc(file)) != '\n') { | 173 while ((c = getc(file)) != '\n') { |
161 } | 174 } |
162 } | 175 } |
163 | 176 |
164 if (c == ' ' || c == '\t') { /* indent if at start of line */ | 177 if (c == ' ' || c == '\t') { /* indent if at start of line */ |