2 // fileio.cpp: Architektonas file save/load support
4 // Part of the Architektonas Project
5 // (C) 2013 Underground Software
6 // See the README and GPLv3 files for licensing and warranty information
8 // JLH = James Hammons <jlhamm@acm.org>
11 // --- ---------- -------------------------------------------------------------
12 // JLH 02/20/2013 Created this file
23 #include "container.h"
24 #include "dimension.h"
27 enum ObjectType { OTContainer, OTContainerEnd, OTLine, OTCircle, OTArc, OTDimension,
28 OTPolygon, OTText, OTImage, OTBlock, OTEndOfFile };
31 /*static*/ bool FileIO::SaveAtnsFile(FILE * file, Container * object)
33 /* Approach: loop through the container, doing a depth-first traversal. Any extra
34 containers found are looped through until there aren't any more down, then
35 ordinary objects are described. This can be handled by a virtual Object function
36 that reports the object by itself if it's a non-Container, otherwise it
37 enumerates all objects within itself. */
39 fprintf(file, "ARCHITEKTONAS DRAWING V1.0\n");
41 object->Enumerate(file);
42 fprintf(file, "END\n");
50 /*static*/ bool FileIO::LoadAtnsFile(FILE * file, Container * drawing)
55 fscanf(file, "ARCHITEKTONAS DRAWING V%f", &version);
57 //printf("Load: version = %f\n", version);
60 /* Approach: read each object in the file, one by one. If the object is a Container,
61 add objects to it until an "endContainer" marker is found. This will require a
62 stack to maintain the current Container. */
65 std::vector<Container *> containerStack;
66 Container * currentTopContainer = drawing;//new Container(Vector(0, 0));
68 // ObjectType objectType;
73 if (FileIO::GetObjectFromFile(file, currentTopContainer, &object, &objectType) == false)
76 // object->type down below can be replaced with objType.
77 // Above could be: bool FileIO::GetObjectFromFile(FILE *, Object *, ObjectType *);
78 // where the return value tells if it's a valid object, Object * returns the
79 // reconstructed object and ObjectType * returns the object type.
81 if (objectType == OTEndOfFile)
83 printf("Load: container size = %li\n", drawing->objects.size());
86 else if (objectType == OTContainer)
88 containerStack.push_back(currentTopContainer);
89 currentTopContainer = new Container(Vector(0, 0), currentTopContainer);
91 else if (objectType == OTContainerEnd)
93 Container * containerToAdd = currentTopContainer;
94 currentTopContainer = containerStack.back();
95 containerStack.pop_back();
96 currentTopContainer->Add(containerToAdd);
100 currentTopContainer->Add(object);
101 printf("Load: Adding object. Container size = %li (%li)\n", drawing->objects.size(), currentTopContainer->objects.size());
112 /*static*/ bool FileIO::GetObjectFromFile(FILE * file, Object * parent, Object ** object, int * objectType)
115 fscanf(file, "%s ", buffer);
116 bool recognized = false;
117 //printf("Load: buffer = \"%s\"\n", buffer);
119 if (strcmp(buffer, "LINE") == 0)
121 //printf(" Found LINE.\n");
124 fscanf(file, "(%lf,%lf) (%lf,%lf)", &v1.x, &v1.y, &v2.x, &v2.y);
125 //printf(" Number of params recognized: %i\n", n);
126 *object = new Line(v1, v2, parent);
127 *objectType = OTLine;
129 else if (strcmp(buffer, "CIRCLE") == 0)
134 fscanf(file, "(%lf,%lf) %lf", &v.x, &v.y, &r);
135 *object = new Circle(v, r, parent);
136 *objectType = OTCircle;
138 else if (strcmp(buffer, "ARC") == 0)
143 fscanf(file, "(%lf,%lf) %lf, %lf, %lf", &v.x, &v.y, &r, &a1, &a2);
144 *object = new Arc(v, r, a1, a2, parent);
147 else if (strcmp(buffer, "DIMENSION") == 0)
152 fscanf(file, "(%lf,%lf) (%lf,%lf) %i", &v1.x, &v1.y, &v2.x, &v2.y, &type);
153 *object = new Dimension(v1, v2, type, parent);
154 *objectType = OTDimension;
156 else if (strcmp(buffer, "CONTAINER") == 0)
159 *objectType = OTContainer;
161 else if (strcmp(buffer, "ENDCONTAINER") == 0)
164 *objectType = OTContainerEnd;
166 else if (strcmp(buffer, "END") == 0)
169 *objectType = OTEndOfFile;