changeset 9:c020b0d1cfca

read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
author meillo@marmaro.de
date Sat, 09 Feb 2008 16:41:41 +0100
parents 495a56e706dc
children 0e15841ae111
files baum.c baum.h
diffstat 2 files changed, 52 insertions(+), 84 deletions(-) [+]
line wrap: on
line diff
--- a/baum.c	Sat Feb 09 12:42:11 2008 +0100
+++ b/baum.c	Sat Feb 09 16:41:41 2008 +0100
@@ -16,6 +16,7 @@
 
 
 struct Node* root = 0;
+struct Listitem* list = NULL;
 
 
 void logit(char* text) {
@@ -27,7 +28,7 @@
 struct Node* newNode(char* name, unsigned char value) {
 	struct Node* node;
 	node = (struct Node*) malloc(sizeof(struct Node));
-	node->name = name;
+	strcpy(node->name, name);
 	node->value = value;
 	node->right = 0;
 	node->down = 0;
@@ -73,7 +74,7 @@
 /* print */
 void printNode(struct Node* node) {
 	if (node != NULL) {
-		fprintf(stderr, "Node: %20s (%d|%c)\n", node->name, node->value, node->value);
+		fprintf(stderr, "Node: %10s (%d|%c)\n", node->name, node->value, node->value);
 	}
 }
 
@@ -103,81 +104,31 @@
 	action(root);
 }
 
-/* init */
-void init() {
-	/* add some numbers
-	root = newNode("print", 'n');
-	root->down = newNode("sum", 0);
 
-	root->down->down = newNode("number", 60);
-
-	root->down->down->right = newNode("number", 50);
-
-	root->down->down->right->right = newNode("sum", 0);
-	root->down->down->right->right->down = newNode("number", 1);
-	root->down->down->right->right->down->right = newNode("number", 5);
-	*/
-
-	/* input numbers and add them
-	root = newNode("print", 'n');
-	root->down = newNode("sum", 0);
-	root->down->down = newNode("input", 0);
-	root->down->down->right = newNode("input", 0);
-	root->down->down->right->right = newNode("input", 0);
-	*/
-
-	/* prints HelloWorld
-	*/
-	struct Node* np = 0;
-
-	root = newNode("blackhole", 0);
-	root->down = newNode("sum", 0);
-	root->down->down = newNode("print", 'c');
-	np = root->down->down;
-	np->down = newNode("number", 'H');
-
-	np->right = newNode("print", 'c');
-	np->right->down = newNode("number", 'e');
-	np = np->right;
-
-	np->right = newNode("print", 'c');
-	np->right->down = newNode("number", 'l');
-	np = np->right;
-
-	np->right = newNode("print", 'c');
-	np->right->down = newNode("number", 'l');
-	np = np->right;
-
-	np->right = newNode("print", 'c');
-	np->right->down = newNode("number", 'o');
-	np = np->right;
-
-	np->right = newNode("print", 'c');
-	np->right->down = newNode("number", ' ');
-	np = np->right;
-
-	np->right = newNode("print", 'c');
-	np->right->down = newNode("number", 'L');
-	np = np->right;
-
-	np->right = newNode("print", 'c');
-	np->right->down = newNode("number", 'y');
-	np = np->right;
-
-	np->right = newNode("print", 'c');
-	np->right->down = newNode("number", 'd');
-	np = np->right;
-
-	np->right = newNode("print", 'c');
-	np->right->down = newNode("number", 'i');
-	np = np->right;
-
-	np->right = newNode("print", 'c');
-	np->right->down = newNode("number", 10);
-	np = np->right;
 
 
+void push(struct Node* node) {
+	struct Listitem* tmp;
+	struct Listitem* new;
+	new = (struct Listitem*) malloc(sizeof(struct Listitem));
+	new->node = node;
+	tmp = list;
+	list = new;
+	list->next = tmp;
 }
+struct Node* pull() {
+	if (list == NULL) {
+		return NULL;
+	}
+	struct Listitem* tmp;
+	struct Node* node;
+	tmp = list;
+	list = list->next;
+	node = tmp->node;
+	free(tmp); tmp=0;
+	return node;
+}
+
 
 
 /* read input */
@@ -186,10 +137,16 @@
 	int indent;
 	char name[256];
 	int value;
+	int last_indent;
+	struct Node* last_node;
+	struct Node* node;
 
 	indent = 0;
 	strcpy(name, "");
 	value = 0;
+	last_indent = -1;
+	root = newNode("blackhole", 0);
+	last_node = root;
 
 	while ((c = getchar()) != EOF) {
 		if (c == '#') { /* comment */
@@ -205,8 +162,21 @@
 
 		if (c == '\n') {  /* end of line: create node */
 			if (strlen(name) > 0) {
-				printf(" %d - %s - %d\n", indent, name, value);
+				fprintf(stderr, " %d - %s - %d\n", indent, name, value);
 				/* create node */
+				node = newNode((char*) name, value);
+				if (indent > last_indent) { /* down */
+					last_node->down = node;
+					push(last_node);
+				} else if (indent == last_indent) { /* right */
+					last_node->right = node;
+				} else if (indent < last_indent) { /* up */
+					/* FIXME what if it goes more than one level up? */
+					last_node = pull();
+					last_node->right = node;
+				}
+				last_indent = indent;
+				last_node = node;
 			}
 			indent = 0;
 			strcpy(name, "");
@@ -237,20 +207,14 @@
 /* main */
 int main(int argc, char* argv[]) {
 	unsigned char shell_return = 0;
-	/*
-	init();
-
+	
+	read_input();
 	printTree(root);
-	fprintf(stderr, "\n\n");
 
 	shell_return = action(root);
 
-	fprintf(stderr, "\n\n");
 	printTree(root);
-
 	delete(root);
-	*/
-	
-	read_input();
+
 	exit(shell_return);
 }
--- a/baum.h	Sat Feb 09 12:42:11 2008 +0100
+++ b/baum.h	Sat Feb 09 16:41:41 2008 +0100
@@ -14,10 +14,14 @@
 
 
 struct Node {
-	char* name;
+	char name[256];
 	unsigned char value;
 	struct Node* down;
 	struct Node* right;
 };
 
 
+struct Listitem {
+	struct Node* node;
+	struct Listitem* next;
+};