1 | #include "ivnode.h"
|
---|
2 | #include "ivreader.h"
|
---|
3 |
|
---|
4 | IVField::IVField()
|
---|
5 | {
|
---|
6 | next = NULL;
|
---|
7 | typ = IV_STRING;
|
---|
8 | }
|
---|
9 |
|
---|
10 |
|
---|
11 | IVField::~IVField()
|
---|
12 | {
|
---|
13 | if (value != NULL) free(value);
|
---|
14 | if (name != NULL) delete [] name;
|
---|
15 | // if (next != NULL) delete next;
|
---|
16 | }
|
---|
17 |
|
---|
18 |
|
---|
19 | IVNode::IVNode()
|
---|
20 | {
|
---|
21 | parent = children = next = actual = NULL;
|
---|
22 | name = NULL;
|
---|
23 | fields = NULL;
|
---|
24 | }
|
---|
25 |
|
---|
26 |
|
---|
27 | IVNode::IVNode(const char *s)
|
---|
28 | {
|
---|
29 | name = new char[strlen(s)+1];
|
---|
30 | strcpy(name, s);
|
---|
31 | parent = children = next = actual = NULL;
|
---|
32 | fields = NULL;
|
---|
33 | }
|
---|
34 |
|
---|
35 | IVNode::~IVNode()
|
---|
36 | {
|
---|
37 | if (name != NULL) delete[] name;
|
---|
38 | if (fields != NULL)
|
---|
39 | {
|
---|
40 | IVField *help = fields, *delme = NULL;
|
---|
41 | while (help->next != NULL)
|
---|
42 | {
|
---|
43 | delme = help; help = help->next; delete delme;
|
---|
44 | }
|
---|
45 | delete help;
|
---|
46 | }
|
---|
47 | if (children != NULL)
|
---|
48 | {
|
---|
49 | IVNode *help = children, *delme = NULL;
|
---|
50 | while (help->next != NULL)
|
---|
51 | {
|
---|
52 | delme = help; help = help->next; delete delme;
|
---|
53 | }
|
---|
54 | delete help;
|
---|
55 | }
|
---|
56 | // if (next != NULL) delete next;
|
---|
57 | }
|
---|
58 |
|
---|
59 |
|
---|
60 |
|
---|
61 |
|
---|
62 | void IVNode::attachNode(IVNode *child)
|
---|
63 | {
|
---|
64 | if (children == NULL) children = child;
|
---|
65 | else
|
---|
66 | {
|
---|
67 | IVNode *help = children;
|
---|
68 | while (help->next != NULL) help = help->next;
|
---|
69 | help->next = child;
|
---|
70 | }
|
---|
71 | child->parent = this;
|
---|
72 | }
|
---|
73 |
|
---|
74 |
|
---|
75 | void IVNode::print(int ident)
|
---|
76 | {
|
---|
77 | if (IVReader::IVLog != NULL)
|
---|
78 | {
|
---|
79 | std::string msg = "";
|
---|
80 |
|
---|
81 | // for (int i=0; i < ident; i++) fprintf(stdout, " ");
|
---|
82 | for (int i=0; i < ident; i++) msg = msg + " ";
|
---|
83 | // fprintf(stdout, "%s:\n", name);
|
---|
84 | msg = msg + name + ":";
|
---|
85 | IVReader::IVLog->logMessage(msg);
|
---|
86 |
|
---|
87 | IVField *help = fields;
|
---|
88 | while (help != NULL)
|
---|
89 | {
|
---|
90 | // for (int i=0; i < ident; i++) fprintf(stdout, " ");
|
---|
91 | msg = "";
|
---|
92 | for (int i=0; i < ident; i++) msg = msg + " ";
|
---|
93 |
|
---|
94 | if (help->typ == IV_STRING)
|
---|
95 | {
|
---|
96 | // fprintf(stdout, "- %s: %s\n", help->name, (char *)help->value);
|
---|
97 | msg = msg + "- " + help->name + ": " + (char *)help->value;
|
---|
98 | IVReader::IVLog->logMessage(msg);
|
---|
99 | }
|
---|
100 | if (help->typ == IV_INT)
|
---|
101 | {
|
---|
102 | // fprintf(stdout, "- %s (%d): ", help->name, help->cnt);
|
---|
103 | msg = msg + "- " + help->name + "(" + IVReader::intToStr(help->cnt) + "): ";
|
---|
104 | // for (int i=0; i < help->cnt; i++) fprintf(stdout, " %d", ((int *)help->value)[i]);
|
---|
105 | for (int i=0; i < help->cnt; i++) msg = msg + " " + IVReader::realToStr(((float *)help->value)[i]);
|
---|
106 | // fprintf(stdout, "\n");
|
---|
107 | IVReader::IVLog->logMessage(msg);
|
---|
108 | }
|
---|
109 | if (help->typ == IV_REAL)
|
---|
110 | {
|
---|
111 | // fprintf(stdout, "- %s (%d):\n", help->name, help->cnt);
|
---|
112 | msg = msg + "- " + help->name + "(" + IVReader::intToStr(help->cnt) + "): ";
|
---|
113 | for (int i=0; i < help->cnt; i++)
|
---|
114 | {
|
---|
115 | // fprintf(stdout, " %.3f", ((float *)help->value)[i]);
|
---|
116 | msg = msg + " " + IVReader::realToStr(((float *)help->value)[i]);
|
---|
117 | // if (i % 3 == 2) fprintf(stdout, "\n");
|
---|
118 | if (i % 3 == 2) msg = msg + "\n";
|
---|
119 | }
|
---|
120 | // fprintf(stdout, "\n");
|
---|
121 | IVReader::IVLog->logMessage(msg);
|
---|
122 | }
|
---|
123 | help = help->next;
|
---|
124 | }
|
---|
125 | if (children != NULL) { children->print(ident+3); }
|
---|
126 | if (next != NULL) next->print(ident);
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | void IVNode::addField(const char *name, void *value, int cnt, IVType typ)
|
---|
131 | {
|
---|
132 | IVField *newone = new IVField();
|
---|
133 | newone->name = new char[strlen(name)+1];
|
---|
134 | strcpy(newone->name, name);
|
---|
135 | /*
|
---|
136 | if (typ == IV_STRING)
|
---|
137 | {
|
---|
138 | newone->value = new char[strlen((char *)value)+1];
|
---|
139 | strcpy((char *)newone->value, (char *)value);
|
---|
140 | } else
|
---|
141 | if ((typ == IV_INT) || (typ == IV_REAL))
|
---|
142 | {
|
---|
143 | newone->value = new Ogre::Real[cnt];
|
---|
144 | memcpy(newone->value, value, cnt*sizeof(Ogre::Real));
|
---|
145 | }
|
---|
146 | */
|
---|
147 | newone->value = value;
|
---|
148 | newone->typ = typ;
|
---|
149 | newone->cnt = cnt;
|
---|
150 | newone->next = NULL;
|
---|
151 |
|
---|
152 | if (fields == NULL) fields = newone;
|
---|
153 | else
|
---|
154 | {
|
---|
155 | IVField *help = fields;
|
---|
156 | while (help->next != NULL) help = help->next;
|
---|
157 | help->next = newone;
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 |
|
---|
162 | void IVNode::addField(const char *name, char *value)
|
---|
163 | {
|
---|
164 | addField(name, value, 1, IV_STRING);
|
---|
165 | }
|
---|
166 |
|
---|
167 |
|
---|
168 | IVNode *IVNode::getNodeRecursive(const char *name)
|
---|
169 | {
|
---|
170 | if (strcmp(name, this->name) == 0) return this;
|
---|
171 | if (this->children != NULL)
|
---|
172 | {
|
---|
173 | IVNode *help = this->children->getNodeRecursive(name);
|
---|
174 | if (help != NULL) return help;
|
---|
175 | }
|
---|
176 | if (this->next != NULL) return this->next->getNodeRecursive(name); else return NULL;
|
---|
177 | }
|
---|
178 |
|
---|
179 |
|
---|
180 | IVNode *IVNode::getNextChildNode(bool reset)
|
---|
181 | {
|
---|
182 | if (reset) actual = this->children;
|
---|
183 | else if (actual == NULL) actual = this->children; else actual = actual->next;
|
---|
184 | return actual;
|
---|
185 | }
|
---|
186 |
|
---|
187 |
|
---|
188 | IVField *IVNode::getField(const char *name)
|
---|
189 | {
|
---|
190 | IVField *help = fields;
|
---|
191 | while (help != NULL)
|
---|
192 | {
|
---|
193 | if (strcmp(name, help->name) == 0) return help;
|
---|
194 | help = help->next;
|
---|
195 | }
|
---|
196 | return NULL;
|
---|
197 | }
|
---|
198 |
|
---|
199 | void *IVNode::getField(const char *name, IVType *typ, int *cnt)
|
---|
200 | {
|
---|
201 | IVField *help = fields;
|
---|
202 | while (help != NULL)
|
---|
203 | {
|
---|
204 | if (strcmp(name, help->name) == 0)
|
---|
205 | {
|
---|
206 | *typ = help->typ;
|
---|
207 | *cnt = help->cnt;
|
---|
208 | return help->value;
|
---|
209 | }
|
---|
210 | help = help->next;
|
---|
211 | }
|
---|
212 | *typ = IV_INVALID;
|
---|
213 | *cnt = 0;
|
---|
214 | return NULL;
|
---|
215 | } |
---|