X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Ffileio.cpp;fp=src%2Ffileio.cpp;h=fedddea4fea6729a917fe15510420aa829bef802;hb=eb0057e8a8145032152e4c417fcd102ef5a21484;hp=7cd6816d4766f63690d14d7ce0dbca13c7bc31b3;hpb=baf67656b97e3d61e9223e66ebe4f554e364cd4a;p=architektonas diff --git a/src/fileio.cpp b/src/fileio.cpp index 7cd6816..fedddea 100644 --- a/src/fileio.cpp +++ b/src/fileio.cpp @@ -16,24 +16,159 @@ //#include #include +#include +#include +#include "arc.h" +#include "circle.h" #include "container.h" +#include "dimension.h" +#include "line.h" + +enum ObjectType { OTContainer, OTContainerEnd, OTLine, OTCircle, OTArc, OTDimension, + OTPolygon, OTText, OTImage, OTBlock, OTEndOfFile }; /*static*/ bool FileIO::SaveAtnsFile(FILE * file, Container * object) { -// QString filename2 = QFileDialog::getSaveFileName(NULL, tr("Save Drawing"), -// "", tr("Architektonas files (*.drawing)")); + /* Approach: loop through the container, doing a depth-first traversal. Any extra + containers found are looped through until there aren't any more down, then + ordinary objects are described. This can be handled by a virtual Object function + that reports the object by itself if it's a non-Container, otherwise it + enumerates all objects within itself. */ + fprintf(file, "ARCHITEKTONAS DRAWING V1.0\n"); +#if 1 + object->Enumerate(file); + fprintf(file, "END\n"); + return true; +#else return false; +#endif } -/*static*/ bool FileIO::LoadAtnsFile(FILE * file, Container * object) +/*static*/ bool FileIO::LoadAtnsFile(FILE * file, Container * drawing) { -// QString filename2 = QFileDialog::getOpenFileName(NULL, tr("Open Drawing"), -// "", tr("Architektonas files (*.drawing)")); +// char buffer[256]; + float version; + + fscanf(file, "ARCHITEKTONAS DRAWING V%f", &version); + +//printf("Load: version = %f\n", version); + if (version != 1.0) + return false; + /* Approach: read each object in the file, one by one. If the object is a Container, + add objects to it until an "endContainer" marker is found. This will require a + stack to maintain the current Container. */ + +#if 1 + std::vector containerStack; + Container * currentTopContainer = drawing;//new Container(Vector(0, 0)); + Object * object; +// ObjectType objectType; + int objectType; + + while (!feof(file)) + { + if (FileIO::GetObjectFromFile(file, currentTopContainer, &object, &objectType) == false) + return false; + + // object->type down below can be replaced with objType. + // Above could be: bool FileIO::GetObjectFromFile(FILE *, Object *, ObjectType *); + // where the return value tells if it's a valid object, Object * returns the + // reconstructed object and ObjectType * returns the object type. + + if (objectType == OTEndOfFile) + { +printf("Load: container size = %li\n", drawing->objects.size()); + return true; + } + else if (objectType == OTContainer) + { + containerStack.push_back(currentTopContainer); + currentTopContainer = new Container(Vector(0, 0), currentTopContainer); + } + else if (objectType == OTContainerEnd) + { + Container * containerToAdd = currentTopContainer; + currentTopContainer = containerStack.back(); + containerStack.pop_back(); + currentTopContainer->Add(containerToAdd); + } + else + { + currentTopContainer->Add(object); +printf("Load: Adding object. Container size = %li (%li)\n", drawing->objects.size(), currentTopContainer->objects.size()); + } + } return false; +#else + return false; +#endif } +/*static*/ bool FileIO::GetObjectFromFile(FILE * file, Object * parent, Object ** object, int * objectType) +{ + char buffer[256]; + fscanf(file, "%s ", buffer); + bool recognized = false; +//printf("Load: buffer = \"%s\"\n", buffer); + + if (strcmp(buffer, "LINE") == 0) + { +//printf(" Found LINE.\n"); + recognized = true; + Vector v1, v2; + fscanf(file, "(%lf,%lf) (%lf,%lf)", &v1.x, &v1.y, &v2.x, &v2.y); +//printf(" Number of params recognized: %i\n", n); + *object = new Line(v1, v2, parent); + *objectType = OTLine; + } + else if (strcmp(buffer, "CIRCLE") == 0) + { + recognized = true; + Vector v; + double r; + fscanf(file, "(%lf,%lf) %lf", &v.x, &v.y, &r); + *object = new Circle(v, r, parent); + *objectType = OTCircle; + } + else if (strcmp(buffer, "ARC") == 0) + { + recognized = true; + Vector v; + double r, a1, a2; + fscanf(file, "(%lf,%lf) %lf, %lf, %lf", &v.x, &v.y, &r, &a1, &a2); + *object = new Arc(v, r, a1, a2, parent); + *objectType = OTArc; + } + else if (strcmp(buffer, "DIMENSION") == 0) + { + recognized = true; + Vector v1, v2; + DimensionType type; + fscanf(file, "(%lf,%lf) (%lf,%lf) %i", &v1.x, &v1.y, &v2.x, &v2.y, &type); + *object = new Dimension(v1, v2, type, parent); + *objectType = OTDimension; + } + else if (strcmp(buffer, "CONTAINER") == 0) + { + recognized = true; + *objectType = OTContainer; + } + else if (strcmp(buffer, "ENDCONTAINER") == 0) + { + recognized = true; + *objectType = OTContainerEnd; + } + else if (strcmp(buffer, "END") == 0) + { + recognized = true; + *objectType = OTEndOfFile; + } + + return recognized; +} +