//#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
+#include <vector>
+#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<Container *> 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;
+}
+