Mercurial > baum
changeset 55:6279e5b14d9e
added error handling for fopen and malloc; code cleanups
author | meillo@marmaro.de |
---|---|
date | Sun, 16 Mar 2008 10:40:53 +0100 (2008-03-16) |
parents | 6e46b106c334 |
children | 7adeee76ce3e |
files | actions.c baum.1 baum.c |
diffstat | 3 files changed, 25 insertions(+), 9 deletions(-) [+] |
line wrap: on
line diff
--- a/actions.c Sun Mar 02 16:43:07 2008 +0100 +++ b/actions.c Sun Mar 16 10:40:53 2008 +0100 @@ -66,10 +66,8 @@ struct Node* tp; node->value = 0; - tp = node->down; - while (tp != NULL) { + for (tp = node->down; tp != NULL; tp = tp->right) { node->value += action(tp); - tp = tp->right; } return node->value; } @@ -102,6 +100,7 @@ unsigned char action_times(struct Node* node) { unsigned char i; + for (i = 0; i < node->value; i++) { insertLast(node, copyTree(node->down)); }
--- a/baum.1 Sun Mar 02 16:43:07 2008 +0100 +++ b/baum.1 Sun Mar 16 10:40:53 2008 +0100 @@ -136,6 +136,10 @@ a node has not the required amount of sons .TP +.BI 10 +unable to open input file, unable to allocate memory, or something similar + +.TP .BI 126 invalid command line options
--- 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') { } }