#include "ivnode.h" #include "ivreader.h" IVField::IVField() { next = NULL; typ = IV_STRING; } IVField::~IVField() { if (value != NULL) free(value); if (name != NULL) delete [] name; // if (next != NULL) delete next; } IVNode::IVNode() { parent = children = next = actual = NULL; name = NULL; fields = NULL; } IVNode::IVNode(const char *s) { name = new char[strlen(s)+1]; strcpy(name, s); parent = children = next = actual = NULL; fields = NULL; } IVNode::~IVNode() { if (name != NULL) delete[] name; if (fields != NULL) { IVField *help = fields, *delme = NULL; while (help->next != NULL) { delme = help; help = help->next; delete delme; } delete help; } if (children != NULL) { IVNode *help = children, *delme = NULL; while (help->next != NULL) { delme = help; help = help->next; delete delme; } delete help; } // if (next != NULL) delete next; } void IVNode::attachNode(IVNode *child) { if (children == NULL) children = child; else { IVNode *help = children; while (help->next != NULL) help = help->next; help->next = child; } child->parent = this; } void IVNode::print(int ident) { if (IVReader::IVLog != NULL) { std::string msg = ""; // for (int i=0; i < ident; i++) fprintf(stdout, " "); for (int i=0; i < ident; i++) msg = msg + " "; // fprintf(stdout, "%s:\n", name); msg = msg + name + ":"; IVReader::IVLog->logMessage(msg); IVField *help = fields; while (help != NULL) { // for (int i=0; i < ident; i++) fprintf(stdout, " "); msg = ""; for (int i=0; i < ident; i++) msg = msg + " "; if (help->typ == IV_STRING) { // fprintf(stdout, "- %s: %s\n", help->name, (char *)help->value); msg = msg + "- " + help->name + ": " + (char *)help->value; IVReader::IVLog->logMessage(msg); } if (help->typ == IV_INT) { // fprintf(stdout, "- %s (%d): ", help->name, help->cnt); msg = msg + "- " + help->name + "(" + IVReader::intToStr(help->cnt) + "): "; // for (int i=0; i < help->cnt; i++) fprintf(stdout, " %d", ((int *)help->value)[i]); for (int i=0; i < help->cnt; i++) msg = msg + " " + IVReader::realToStr(((float *)help->value)[i]); // fprintf(stdout, "\n"); IVReader::IVLog->logMessage(msg); } if (help->typ == IV_REAL) { // fprintf(stdout, "- %s (%d):\n", help->name, help->cnt); msg = msg + "- " + help->name + "(" + IVReader::intToStr(help->cnt) + "): "; for (int i=0; i < help->cnt; i++) { // fprintf(stdout, " %.3f", ((float *)help->value)[i]); msg = msg + " " + IVReader::realToStr(((float *)help->value)[i]); // if (i % 3 == 2) fprintf(stdout, "\n"); if (i % 3 == 2) msg = msg + "\n"; } // fprintf(stdout, "\n"); IVReader::IVLog->logMessage(msg); } help = help->next; } if (children != NULL) { children->print(ident+3); } if (next != NULL) next->print(ident); } } void IVNode::addField(const char *name, void *value, int cnt, IVType typ) { IVField *newone = new IVField(); newone->name = new char[strlen(name)+1]; strcpy(newone->name, name); /* if (typ == IV_STRING) { newone->value = new char[strlen((char *)value)+1]; strcpy((char *)newone->value, (char *)value); } else if ((typ == IV_INT) || (typ == IV_REAL)) { newone->value = new Ogre::Real[cnt]; memcpy(newone->value, value, cnt*sizeof(Ogre::Real)); } */ newone->value = value; newone->typ = typ; newone->cnt = cnt; newone->next = NULL; if (fields == NULL) fields = newone; else { IVField *help = fields; while (help->next != NULL) help = help->next; help->next = newone; } } void IVNode::addField(const char *name, char *value) { addField(name, value, 1, IV_STRING); } IVNode *IVNode::getNodeRecursive(const char *name) { if (strcmp(name, this->name) == 0) return this; if (this->children != NULL) { IVNode *help = this->children->getNodeRecursive(name); if (help != NULL) return help; } if (this->next != NULL) return this->next->getNodeRecursive(name); else return NULL; } IVNode *IVNode::getNextChildNode(bool reset) { if (reset) actual = this->children; else if (actual == NULL) actual = this->children; else actual = actual->next; return actual; } IVField *IVNode::getField(const char *name) { IVField *help = fields; while (help != NULL) { if (strcmp(name, help->name) == 0) return help; help = help->next; } return NULL; } void *IVNode::getField(const char *name, IVType *typ, int *cnt) { IVField *help = fields; while (help != NULL) { if (strcmp(name, help->name) == 0) { *typ = help->typ; *cnt = help->cnt; return help->value; } help = help->next; } *typ = IV_INVALID; *cnt = 0; return NULL; }