Mercurial > baum
diff 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 |
line wrap: on
line diff
--- a/baum.c Sun Mar 02 16:43:07 2008 +0100 +++ b/baum.c Sun Mar 16 10:40:53 2008 +0100 @@ -44,6 +44,10 @@ struct Node* newNode(char* name, unsigned char value) { struct Node* node; node = (struct Node*) malloc(sizeof(struct Node)); + if (node == NULL) { + perror("unable to allocate memory"); + exit(10); + } strcpy(node->name, name); node->value = value; node->right = 0; @@ -91,12 +95,13 @@ /* print */ void printNode(struct Node* node, int level) { - if (node != NULL) { - while (level-- > 0) { - fprintf(stderr, "\t"); - } - fprintf(stderr, "%s (%d|%c)\n", node->name, node->value, node->value); + if (node == NULL) { + return; } + while (level-- > 0) { + fprintf(stderr, "\t"); + } + fprintf(stderr, "%s (%d|%c)\n", node->name, node->value, node->value); } void printTree(struct Node* root, int level) { @@ -117,6 +122,10 @@ struct Stackitem* tmp; struct Stackitem* new; new = (struct Stackitem*) malloc(sizeof(struct Stackitem)); + if (new == NULL) { + perror("unable to allocate memory"); + exit(10); + } new->node = node; tmp = stack; stack = new; @@ -154,9 +163,13 @@ last_indent = -1; last_node = NULL; file = fopen(filename, "r"); + if (file == NULL) { + perror("unable to open file"); + exit(10); + } while ((c = getc(file)) != EOF) { - if (c == '#') { /* comment */ + if (c == '#') { /* comment */ while ((c = getc(file)) != '\n') { } }