Mercurial > baum
annotate actions.c @ 39:96e2d58bc346
added new example program; added comments to example programs
author | meillo@marmaro.de |
---|---|
date | Sat, 01 Mar 2008 20:10:02 +0100 |
parents | ff01f0f076e4 |
children | 1ad3d7305e5d |
rev | line source |
---|---|
2 | 1 #include <stdio.h> |
2 #include <stdlib.h> | |
3 #include <string.h> | |
4 #include "baum.h" | |
5 #include "actions.h" | |
6 | |
26
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
16
diff
changeset
|
7 unsigned char action_print(struct Node* node); |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
16
diff
changeset
|
8 unsigned char action_sum(struct Node* node); |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
16
diff
changeset
|
9 unsigned char action_number(struct Node* node); |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
16
diff
changeset
|
10 unsigned char action_input(struct Node* node); |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
16
diff
changeset
|
11 unsigned char action_times(struct Node* node); |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
16
diff
changeset
|
12 |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
16
diff
changeset
|
13 |
2 | 14 |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
15 unsigned char action(struct Node* node) { |
5
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
16 if (node == NULL) { |
38
ff01f0f076e4
option_verbose is now global; stuff about warning when (expected) nodes are not there
meillo@marmaro.de
parents:
36
diff
changeset
|
17 if (option_verbose) { |
ff01f0f076e4
option_verbose is now global; stuff about warning when (expected) nodes are not there
meillo@marmaro.de
parents:
36
diff
changeset
|
18 fprintf(stderr, "warning: action of non existing node\n"); |
ff01f0f076e4
option_verbose is now global; stuff about warning when (expected) nodes are not there
meillo@marmaro.de
parents:
36
diff
changeset
|
19 } |
5
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
20 return 0; |
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
21 } |
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
22 |
26
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
16
diff
changeset
|
23 logit(node->name); |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
16
diff
changeset
|
24 |
2 | 25 if (strcmp(node->name, "print") == 0) { |
26 return action_print(node); | |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
27 |
2 | 28 } else if (strcmp(node->name, "sum") == 0) { |
29 return action_sum(node); | |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
30 |
2 | 31 } else if (strcmp(node->name, "number") == 0) { |
32 return action_number(node); | |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
33 |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
34 } else if (strcmp(node->name, "input") == 0) { |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
35 return action_input(node); |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
36 |
5
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
37 } else if (strcmp(node->name, "times") == 0) { |
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
38 return action_times(node); |
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
39 |
2 | 40 } else { |
31
4e60d96265f0
removed -c option completely; updated man page; new error code 5
meillo@marmaro.de
parents:
30
diff
changeset
|
41 fprintf(stderr, "unknown kind of node\n"); |
16
b62288419c1c
added README and LICENSE; changed error code of invalid node
meillo@marmaro.de
parents:
15
diff
changeset
|
42 exit(4); |
2 | 43 } |
44 } | |
45 | |
46 | |
47 | |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
48 unsigned char action_print(struct Node* node) { |
5
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
49 unsigned char result; |
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
50 result = action(node->down); |
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
51 if (node->value == 'c') { |
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
52 printf("%c", result); |
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
53 } else { |
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
54 printf("%d", result); |
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
55 } |
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
56 return result; |
2 | 57 } |
58 | |
59 | |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
60 unsigned char action_sum(struct Node* node) { |
2 | 61 struct Node* tp; |
62 tp = node->down; | |
63 while (tp != NULL) { | |
64 node->value += action(tp); | |
65 tp = tp->right; | |
66 } | |
67 return node->value; | |
68 } | |
69 | |
70 | |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
71 unsigned char action_number(struct Node* node) { |
38
ff01f0f076e4
option_verbose is now global; stuff about warning when (expected) nodes are not there
meillo@marmaro.de
parents:
36
diff
changeset
|
72 if (node->down != NULL) { |
ff01f0f076e4
option_verbose is now global; stuff about warning when (expected) nodes are not there
meillo@marmaro.de
parents:
36
diff
changeset
|
73 action(node->down); |
ff01f0f076e4
option_verbose is now global; stuff about warning when (expected) nodes are not there
meillo@marmaro.de
parents:
36
diff
changeset
|
74 } |
2 | 75 return node->value; |
76 } | |
77 | |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
78 |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
79 unsigned char action_input(struct Node* node) { |
5
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
80 /* reads a number which is treated as ASCII value */ |
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
81 int input; |
14 | 82 printf("input: "); |
5
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
83 scanf("%d", &input); |
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
84 input = input % 256; |
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
85 insertLast(node, newNode("number", (char) input)); |
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
86 |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
87 return 0; |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
88 } |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
89 |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
90 |
5
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
91 unsigned char action_times(struct Node* node) { |
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
92 struct Node* tp; |
15
e2048e569891
insertLast returns now new inserted node; very dumb implementation for action_times
meillo@marmaro.de
parents:
14
diff
changeset
|
93 struct Node* last; |
5
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
94 unsigned char i; |
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
95 tp = node->down; |
10 | 96 for (i = 0; i < node->value; i++) { |
30
cd979b979610
fixed multiple (un)indentions in read_input; some better comments
meillo@marmaro.de
parents:
26
diff
changeset
|
97 /* FIXME deep copy */ |
15
e2048e569891
insertLast returns now new inserted node; very dumb implementation for action_times
meillo@marmaro.de
parents:
14
diff
changeset
|
98 last = insertLast(node, newNode(tp->name, tp->value)); |
e2048e569891
insertLast returns now new inserted node; very dumb implementation for action_times
meillo@marmaro.de
parents:
14
diff
changeset
|
99 if (tp->down != NULL) { |
e2048e569891
insertLast returns now new inserted node; very dumb implementation for action_times
meillo@marmaro.de
parents:
14
diff
changeset
|
100 last->down = newNode(tp->down->name, tp->down->value); |
e2048e569891
insertLast returns now new inserted node; very dumb implementation for action_times
meillo@marmaro.de
parents:
14
diff
changeset
|
101 } |
e2048e569891
insertLast returns now new inserted node; very dumb implementation for action_times
meillo@marmaro.de
parents:
14
diff
changeset
|
102 |
5
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
103 } |
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
104 return 0; |
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
105 } |