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
parents 6e46b106c334
children 7adeee76ce3e
files actions.c baum.1 baum.c
diffstat 3 files changed, 25 insertions(+), 9 deletions(-) [+]
line diff
     1.1 --- a/actions.c	Sun Mar 02 16:43:07 2008 +0100
     1.2 +++ b/actions.c	Sun Mar 16 10:40:53 2008 +0100
     1.3 @@ -66,10 +66,8 @@
     1.4  	struct Node* tp;
     1.5  
     1.6  	node->value = 0;
     1.7 -	tp = node->down;
     1.8 -	while (tp != NULL) {
     1.9 +	for (tp = node->down; tp != NULL; tp = tp->right) {
    1.10  		node->value += action(tp);
    1.11 -		tp = tp->right;
    1.12  	}
    1.13  	return node->value;
    1.14  }
    1.15 @@ -102,6 +100,7 @@
    1.16  
    1.17  unsigned char action_times(struct Node* node) {
    1.18  	unsigned char i;
    1.19 +
    1.20  	for (i = 0; i < node->value; i++) {
    1.21  		insertLast(node, copyTree(node->down));
    1.22  	}
     2.1 --- a/baum.1	Sun Mar 02 16:43:07 2008 +0100
     2.2 +++ b/baum.1	Sun Mar 16 10:40:53 2008 +0100
     2.3 @@ -136,6 +136,10 @@
     2.4  a node has not the required amount of sons
     2.5  
     2.6  .TP
     2.7 +.BI 10
     2.8 +unable to open input file, unable to allocate memory, or something similar
     2.9 +
    2.10 +.TP
    2.11  .BI 126
    2.12  invalid command line options
    2.13  
     3.1 --- a/baum.c	Sun Mar 02 16:43:07 2008 +0100
     3.2 +++ b/baum.c	Sun Mar 16 10:40:53 2008 +0100
     3.3 @@ -44,6 +44,10 @@
     3.4  struct Node* newNode(char* name, unsigned char value) {
     3.5  	struct Node* node;
     3.6  	node = (struct Node*) malloc(sizeof(struct Node));
     3.7 +	if (node == NULL) {
     3.8 +		perror("unable to allocate memory");
     3.9 +		exit(10);
    3.10 +	}
    3.11  	strcpy(node->name, name);
    3.12  	node->value = value;
    3.13  	node->right = 0;
    3.14 @@ -91,12 +95,13 @@
    3.15  
    3.16  /* print */
    3.17  void printNode(struct Node* node, int level) {
    3.18 -	if (node != NULL) {
    3.19 -		while (level-- > 0) {
    3.20 -			fprintf(stderr, "\t");
    3.21 -		}
    3.22 -		fprintf(stderr, "%s (%d|%c)\n", node->name, node->value, node->value);
    3.23 +	if (node == NULL) {
    3.24 +		return;
    3.25  	}
    3.26 +	while (level-- > 0) {
    3.27 +		fprintf(stderr, "\t");
    3.28 +	}
    3.29 +	fprintf(stderr, "%s (%d|%c)\n", node->name, node->value, node->value);
    3.30  }
    3.31  
    3.32  void printTree(struct Node* root, int level) {
    3.33 @@ -117,6 +122,10 @@
    3.34  	struct Stackitem* tmp;
    3.35  	struct Stackitem* new;
    3.36  	new = (struct Stackitem*) malloc(sizeof(struct Stackitem));
    3.37 +	if (new == NULL) {
    3.38 +		perror("unable to allocate memory");
    3.39 +		exit(10);
    3.40 +	}
    3.41  	new->node = node;
    3.42  	tmp = stack;
    3.43  	stack = new;
    3.44 @@ -154,9 +163,13 @@
    3.45  	last_indent = -1;
    3.46  	last_node = NULL;
    3.47  	file = fopen(filename, "r");
    3.48 +	if (file == NULL) {
    3.49 +		perror("unable to open file");
    3.50 +		exit(10);
    3.51 +	}
    3.52  
    3.53  	while ((c = getc(file)) != EOF) {
    3.54 -		if (c == '#') { /* comment */
    3.55 +		if (c == '#') {  /* comment */
    3.56  			while ((c = getc(file)) != '\n') {
    3.57  			}
    3.58  		}