baum

changeset 33:2e564bf8599c

new error code 6; more readable printTree; cleanups
author meillo@marmaro.de
date Sat, 01 Mar 2008 17:36:59 +0100
parents 3903effae5ef
children e986c6abed2b
files baum.1 baum.c
diffstat 2 files changed, 22 insertions(+), 29 deletions(-) [+]
line diff
     1.1 --- a/baum.1	Fri Feb 22 15:58:25 2008 +0100
     1.2 +++ b/baum.1	Sat Mar 01 17:36:59 2008 +0100
     1.3 @@ -90,6 +90,10 @@
     1.4  indention over more than one level
     1.5  
     1.6  .TP
     1.7 +.BI 6
     1.8 +node name too long, or no value given
     1.9 +
    1.10 +.TP
    1.11  .BI 127
    1.12  invalid command line options
    1.13  
     2.1 --- a/baum.c	Fri Feb 22 15:58:25 2008 +0100
     2.2 +++ b/baum.c	Sat Mar 01 17:36:59 2008 +0100
     2.3 @@ -23,8 +23,8 @@
     2.4  struct Stackitem* stack = NULL;
     2.5  
     2.6  
     2.7 -void printNode(struct Node* node);
     2.8 -void printTree(struct Node* root);
     2.9 +void printNode(struct Node* node, int level);
    2.10 +void printTree(struct Node* root, int level);
    2.11  struct Node* lastNode(struct Node* node);
    2.12  void delete(struct Node* node);
    2.13  
    2.14 @@ -69,41 +69,29 @@
    2.15  	if (node == NULL) {
    2.16  		return;
    2.17  	}
    2.18 -	if (node->down != NULL) {
    2.19 -		delete(node->down);
    2.20 -	}
    2.21 -	if (node->right != NULL) {
    2.22 -		delete(node->right);
    2.23 -	}
    2.24 +	delete(node->down);
    2.25 +	delete(node->right);
    2.26  	free(node); node=0;
    2.27  }
    2.28  
    2.29  
    2.30  /* print */
    2.31 -void printNode(struct Node* node) {
    2.32 +void printNode(struct Node* node, int level) {
    2.33  	if (node != NULL) {
    2.34 -		fprintf(stderr, "Node: %10s (%d|%c)\n", node->name, node->value, node->value);
    2.35 +		while (level-- > 0) {
    2.36 +			fprintf(stderr, "\t");
    2.37 +		}
    2.38 +		fprintf(stderr, "%s (%d|%c)\n", node->name, node->value, node->value);
    2.39  	}
    2.40  }
    2.41  
    2.42 -void printTree(struct Node* root) {
    2.43 +void printTree(struct Node* root, int level) {
    2.44  	if (root == NULL) {
    2.45  		return;
    2.46  	}
    2.47 -
    2.48 -	printNode(root);
    2.49 -	fprintf(stderr, "  down: ");
    2.50 -	if (root->down != NULL) {
    2.51 -		printTree(root->down);
    2.52 -	} else {
    2.53 -		fprintf(stderr, "NULL\n");
    2.54 -	}
    2.55 -	fprintf(stderr, "  right: ");
    2.56 -	if (root->right != NULL) {
    2.57 -		printTree(root->right);
    2.58 -	} else {
    2.59 -		fprintf(stderr, "NULL\n");
    2.60 -	}
    2.61 +	printNode(root, level);
    2.62 +	printTree(root->down, level+1);
    2.63 +	printTree(root->right, level);
    2.64  }
    2.65  
    2.66  
    2.67 @@ -175,7 +163,7 @@
    2.68  				node = newNode((char*) name, value);
    2.69  				if (indent > last_indent) { /* down */
    2.70  					/* if it goes more than one level down -> error */
    2.71 -					if (indent - last_indent > 1) {
    2.72 +					if (indent > last_indent + 1) {
    2.73  						fprintf(stderr, "error: Indention over more than one level. Only indent by one!\n");
    2.74  						exit(5);
    2.75  					}
    2.76 @@ -206,7 +194,8 @@
    2.77  				name[i] = (char) c;
    2.78  				i++;
    2.79  				if (i > 255) {
    2.80 -					break;
    2.81 +					fprintf(stderr, "error: node name too long, or no value given\n");
    2.82 +					exit(6);
    2.83  				}
    2.84  			}
    2.85  			name[i] = '\0';
    2.86 @@ -270,13 +259,13 @@
    2.87  	read_input(argv[0]);
    2.88  
    2.89  	if (option_verbose) {
    2.90 -		printTree(root);
    2.91 +		printTree(root, 0);
    2.92  	}
    2.93  
    2.94  	shell_return = action(root);
    2.95  
    2.96  	if (option_verbose) {
    2.97 -		printTree(root);
    2.98 +		printTree(root, 0);
    2.99  	}
   2.100  
   2.101  	delete(root);