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
24 #include "container.h"
25 #include "dimension.h"
29 How to handle connected objects
30 -------------------------------
32 Every Object has a vector<Object *> which enumerates all Objects connected to
33 the one we're looking at. So it looks like we'll have to take a two pass
34 approach to loading and saving.
36 Basically, in the saving case, first we write out all objects, keeping a
37 pointer-to-index-number record. Second, we loop through all the objects we
38 wrote out, writing connection lists. Format (indices are 1-based):
46 In the reading case, we do pretty much the same: we construct pointer-to-index-
47 number list, then read the connection list. The PTIN connects the index to the
48 Object pointers we've created. Then we simply call the Object's Connect()
49 function to connect the objects.
51 Small problem though: How does a Dimension know which points on a Line it's
52 connected to? This approach tells the Dimension it's connected to the Line and
53 the Line that it's connected to the Dimension, but not which points.
55 How to handle them then? Do we list the point with the Object pointed at? A
56 Line can contain an infinite number of points to connect with besides its
59 So with each connection Object in the vector, there would also have to be a
60 corresponding point to go with it, that would be gotten from the other Object's
61 Connect() function. Or, instead of a point, a parameter value?
63 Doing that, with the Line and a parameter "t", if t == 0 we have endpoint 1.
64 if t == 1, then we have endpoint 2. With a Circle, the parameter is a number
65 between 0 and 1 (scaled to 0 to 2π). With an Arc, the parameter goes from 0 to
66 1, 0 being enpoint 1 and 1 being endpoint 2.
68 How does this work for moving objects that are connected? Again, with the Line
69 and Dimension. The Line's connections looks like this:
74 Dimension looks like this:
76 Object *: line1, t = 0
77 Object *: line1, t = 1
79 For Dimensions, it can query the connected object (if any) using something like
80 GetPointForParameter(). That way it can figure out where its endpoints are. If
81 there is no connected point, then it uses its internal point.
84 Dimensions are special cases of lines: They have exactly *two* points and none
85 in between. Therefore, the Dimension object only needs to have two points. But
86 those points can be connected to multiple objects. The can also be connected to
87 no points/Objects too.
89 How to describe them and their connections (or lack thereof)?
91 Would have to be a 2nd pass, after all objects have been written out in order.
92 Then you could do something like:
95 8 (the Dimension #): 1 (the Object # for point 1) 1 (the Object # for point 2)
100 Connection attributes: E.g., between a Line a Circle, it can be tangent,
101 perpendicular, or an arbitrary angle. How to encode that information? It's not
102 intrinsic to either the Line or the Circle, but is a function of the
103 relationship between them by virtue of their connection.
106 OTHER CONSIDERATIONS:
107 ---------------------
109 - Need to figure out how to store the Layer list (should layer list be optional?)
110 - Need to figure out how to store the Block list and blocks
115 enum ObjectTypeFile { OTFContainer, OTFContainerEnd, OTFLine, OTFCircle, OTFArc, OTFDimension,
116 OTFPolygon, OTFText, OTFImage, OTFBlock, OTFEndOfFile };
119 /*static*/ bool FileIO::SaveAtnsFile(FILE * file, Container * object)
121 /* Approach: loop through the container, doing a depth-first traversal. Any extra
122 containers found are looped through until there aren't any more down, then
123 ordinary objects are described. This can be handled by a virtual Object function
124 that reports the object by itself if it's a non-Container, otherwise it
125 enumerates all objects within itself. */
127 fprintf(file, "ARCHITEKTONAS DRAWING V1.0\n");
129 object->Enumerate(file);
130 fprintf(file, "END\n");
138 /*static*/ bool FileIO::LoadAtnsFile(FILE * file, Container * drawing)
143 fscanf(file, "ARCHITEKTONAS DRAWING V%f", &version);
145 //printf("Load: version = %f\n", version);
148 /* Approach: read each object in the file, one by one. If the object is a Container,
149 add objects to it until an "endContainer" marker is found. This will require a
150 stack to maintain the current Container. */
153 std::vector<Container *> containerStack;
154 Container * currentTopContainer = drawing;//new Container(Vector(0, 0));
156 // ObjectType objectType;
161 if (FileIO::GetObjectFromFile(file, currentTopContainer, &object, &objectType) == false)
164 // object->type down below can be replaced with objType.
165 // Above could be: bool FileIO::GetObjectFromFile(FILE *, Object *, ObjectType *);
166 // where the return value tells if it's a valid object, Object * returns the
167 // reconstructed object and ObjectType * returns the object type.
169 if (objectType == OTFEndOfFile)
171 //printf("Load: container size = %li\n", drawing->objects.size());
174 else if (objectType == OTFContainer)
176 containerStack.push_back(currentTopContainer);
177 currentTopContainer = new Container(Vector(0, 0), currentTopContainer);
179 else if (objectType == OTFContainerEnd)
181 Container * containerToAdd = currentTopContainer;
182 currentTopContainer = containerStack.back();
183 containerStack.pop_back();
184 currentTopContainer->Add(containerToAdd);
188 currentTopContainer->Add(object);
189 //printf("Load: Adding object. Container size = %li (%li)\n", drawing->objects.size(), currentTopContainer->objects.size());
200 /*static*/ bool FileIO::GetObjectFromFile(FILE * file, Object * parent, Object ** object, int * objectType)
204 int num = fscanf(file, "%s", buffer);
205 bool recognized = false;
207 //printf("FileIO: fscanf returned %i, buffer = \"%s\"\n", num, buffer);
209 // The following fugliness is for troubleshooting. Can remove later.
210 if ((strcmp(buffer, "END") != 0) && (strcmp(buffer, "ENDCONTAINER") != 0))
213 num = fscanf(file, " %i ", &foundLayer);
214 //printf("FileIO: fscanf returned %i, foundLayer = %i\n", num, foundLayer);
219 else if (errno == EBADF)
221 else if (errno == EILSEQ)
223 else if (errno == EINTR)
225 else if (errno == EINVAL)
227 else if (errno == ENOMEM)
229 else if (errno == ERANGE)
232 printf("errno = %i\n", errno);
236 if (strcmp(buffer, "LINE") == 0)
238 //printf(" Found LINE.\n");
241 fscanf(file, "(%lf,%lf) (%lf,%lf)", &v1.x, &v1.y, &v2.x, &v2.y);
242 //printf(" Number of params recognized: %i\n", n);
243 *object = new Line(v1, v2, parent);
244 *objectType = OTFLine;
246 else if (strcmp(buffer, "CIRCLE") == 0)
251 fscanf(file, "(%lf,%lf) %lf", &v.x, &v.y, &r);
252 *object = new Circle(v, r, parent);
253 *objectType = OTFCircle;
255 else if (strcmp(buffer, "ARC") == 0)
260 fscanf(file, "(%lf,%lf) %lf, %lf, %lf", &v.x, &v.y, &r, &a1, &a2);
261 *object = new Arc(v, r, a1, a2, parent);
262 *objectType = OTFArc;
264 else if (strcmp(buffer, "DIMENSION") == 0)
269 fscanf(file, "(%lf,%lf) (%lf,%lf) %i", &v1.x, &v1.y, &v2.x, &v2.y, &type);
270 *object = new Dimension(v1, v2, type, parent);
271 *objectType = OTFDimension;
273 else if (strcmp(buffer, "CONTAINER") == 0)
276 *objectType = OTFContainer;
278 else if (strcmp(buffer, "ENDCONTAINER") == 0)
281 *objectType = OTFContainerEnd;
283 else if (strcmp(buffer, "END") == 0)
286 *objectType = OTFEndOfFile;
290 (*object)->layer = foundLayer;