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