/* ** container_file.coh ** ** Made by yann koeth ** Wed Apr 07 15:03:18 2010 */ class ContainerFile { private: var name; var content; GetNextHierarchyObject(op, stop); GetParameters(op); SetParameters(op); public: ContainerFile(name); Write(parent); Apply(parent); }; ContainerFile::ContainerFile(name) { this->name = name; } ContainerFile::GetNextHierarchyObject(op, stop) { if (!op) return NULL; var next; if ((next = op->GetDown())) return next; if ((next = op->GetNext())) return (op == stop ? NULL : next); if (op == stop) return NULL; var prev = op; while (prev = prev->GetUp()) { if (prev == stop) return NULL; if ((next = prev->GetNext())) return next; } return NULL; } ContainerFile::GetParameters(op) { var p = "<" + op->GetName() + ">"; var mg = op->GetMg(); var v = mg->GetV0(); p += tostring(v.x) + ","; p += tostring(v.y) + ","; p += tostring(v.z) + ";"; v = mg->GetV1(); p += tostring(v.x) + ","; p += tostring(v.y) + ","; p += tostring(v.z) + ";"; v = mg->GetV2(); p += tostring(v.x) + ","; p += tostring(v.y) + ","; p += tostring(v.z) + ";"; v = mg->GetV3(); p += tostring(v.x) + ","; p += tostring(v.y) + ","; p += tostring(v.z); p += ""; return p; } ContainerFile::Write(parent) { if (!parent) return FALSE; var f = GeGetRootFilename(); f->RemoveLast(); f->AddLast(name); var bf = new(BaseFile); if (bf->Open(f, GE_WRITE) == FALSE) return FALSE; var op; for (op = parent; op; op = GetNextHierarchyObject(op, parent)) { bf->WriteString(GetParameters(op)); bf->WriteChar('\n'); } bf->Close(); return TRUE; } ContainerFile::SetParameters(op) { var b1 = "<" + op->GetName() + ">"; var cpos; if ((cpos = strstr(content, b1)) == NOTOK) { println(op->GetName() + " not found."); return TRUE; } cpos += sizeof(b1); var params = new(array, 12); var i, sep; for (i = 0; i < 12; i++) { if (i == 2 || i == 5 || i == 8) sep = ";"; else if (i == 11) sep = ""; else sep = ","; var tmp = strstr(content, sep, cpos); if (tmp == NOTOK) return FALSE; params[i] = strmid(content, cpos, tmp - cpos); cpos += tmp - cpos + 1; } var mg = op->GetMg(); mg->SetV0(vector(evaluate(params[0]), evaluate(params[1]), evaluate(params[2]))); mg->SetV1(vector(evaluate(params[3]), evaluate(params[4]), evaluate(params[5]))); mg->SetV2(vector(evaluate(params[6]), evaluate(params[7]), evaluate(params[8]))); mg->SetV3(vector(evaluate(params[9]), evaluate(params[10]), evaluate(params[11]))); op->SetMg(mg); return TRUE; } ContainerFile::Apply(parent) { if (!parent) return FALSE; var f = GeGetRootFilename(); f->RemoveLast(); f->AddLast(name); var bf = new(BaseFile); if (bf->Open(f, GE_READ, FILE_DIALOG) == FALSE) return FALSE; this->content = bf->ReadString(bf->GetLength()); var op; for (op = parent; op; op = GetNextHierarchyObject(op, parent)) { if (SetParameters(op) == FALSE) { println("Parse error."); return FALSE; } } bf->Close(); EventAdd(); return TRUE; }