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
22 #include "applicationwindow.h"
23 #include "drawingview.h"
28 How to handle connected objects
29 -------------------------------
31 Every Object has a vector<Object *> which enumerates all Objects connected to
32 the one we're looking at. So it looks like we'll have to take a two pass
33 approach to loading and saving.
35 Basically, in the saving case, first we write out all objects, keeping a
36 pointer-to-index-number record. Second, we loop through all the objects we
37 wrote out, writing connection lists. Format (indices are 1-based):
45 In the reading case, we do pretty much the same: we construct pointer-to-index-
46 number list, then read the connection list. The PTIN connects the index to the
47 Object pointers we've created. Then we simply call the Object's Connect()
48 function to connect the objects.
50 Small problem though: How does a Dimension know which points on a Line it's
51 connected to? This approach tells the Dimension it's connected to the Line and
52 the Line that it's connected to the Dimension, but not which points.
54 How to handle them then? Do we list the point with the Object pointed at? A
55 Line can contain an infinite number of points to connect with besides its
58 So with each connection Object in the vector, there would also have to be a
59 corresponding point to go with it, that would be gotten from the other Object's
60 Connect() function. Or, instead of a point, a parameter value?
62 Doing that, with the Line and a parameter "t", if t == 0 we have endpoint 1.
63 if t == 1, then we have endpoint 2. With a Circle, the parameter is a number
64 between 0 and 1 (scaled to 0 to tau). With an Arc, the parameter goes from 0 to
65 1, 0 being enpoint 1 and 1 being endpoint 2.
67 How does this work for moving objects that are connected? Again, with the Line
68 and Dimension. The Line's connections looks like this:
73 Dimension looks like this:
75 Object *: line1, t = 0
76 Object *: line1, t = 1
78 For Dimensions, it can query the connected object (if any) using something like
79 GetPointForParameter(). That way it can figure out where its endpoints are. If
80 there is no connected point, then it uses its internal point.
83 Dimensions are special cases of lines: They have exactly *two* points and none
84 in between. Therefore, the Dimension object only needs to have two points. But
85 those points can be connected to multiple objects. The can also be connected to
86 no points/Objects too.
88 How to describe them and their connections (or lack thereof)?
90 Would have to be a 2nd pass, after all objects have been written out in order.
91 Then you could do something like:
94 8 (the Dimension #): 1 (the Object # for point 1) 1 (the Object # for point 2)
99 Connection attributes: E.g., between a Line a Circle, it can be tangent,
100 perpendicular, or an arbitrary angle. How to encode that information? It's not
101 intrinsic to either the Line or the Circle, but is a function of the
102 relationship between them by virtue of their connection.
105 OTHER CONSIDERATIONS:
106 ---------------------
108 - Need to figure out how to store the Layer list (should layer list be optional?)
109 - Need to figure out how to store the Block list and blocks
114 //enum ObjectTypeFile { OTFContainer, OTFContainerEnd, OTFLine, OTFCircle, OTFArc,
115 // OTFDimension, OTFPolygon, OTFText, OTFImage, OTFBlock, OTFEndOfFile };
116 enum ObjectTypeFile { OTFContainer, OTFContainerEnd, OTFObject, OTFEndOfFile };
118 // Instantiate class variables
119 /*static*/ int FileIO::objectFileType = OTFObject;
121 /*static*/ bool FileIO::SaveAtnsFile(FILE * file, Container * c)
123 /* Approach: loop through the container, doing a depth-first traversal. Any
124 extra containers found are looped through until there aren't any more
125 down, then ordinary objects are described. This can be handled by a
126 virtual Object function that reports the object by itself if it's a non-
127 Container, otherwise it enumerates all objects within itself. */
129 fprintf(file, "ARCHITEKTONAS DRAWING V1.2\n");
130 fprintf(file, "LAYERS %i\n", Global::numLayers);
132 for(int i=0; i<Global::numLayers; i++)
133 fprintf(file, "%i %i \"%s\"\n", (Global::layerHidden[i] ? 1 : 0), (Global::layerLocked[i] ? 1 : 0), Global::layerName[i].c_str());
135 fprintf(file, "ACTIVE %i\n", Global::activeLayer);
136 WriteObjectToFile(file, (Object *)c);
137 fprintf(file, "END\n");
141 /*static*/ bool FileIO::LoadAtnsFile(FILE * file, Container * drawing)
145 fscanf(file, "ARCHITEKTONAS DRAWING V%f\n", &version);
147 // Clear out layer vectors
148 Global::layerHidden.clear();
149 Global::layerLocked.clear();
150 Global::layerName.clear();
153 return LoadVersion1_0(file, drawing);
154 else if (version == 1.1f)
155 return LoadVersion1_1(file, drawing);
156 else if (version == 1.2f)
157 return LoadVersion1_2(file, drawing);
159 //printf("LoadAtnsFile: Could not locate version! (version=%f)\n", version);
163 /*static*/ void FileIO::ResetLayerVectors(void)
165 // Set up layer vectors
166 Global::layerHidden.insert(Global::layerHidden.begin(), false);
167 Global::layerLocked.insert(Global::layerLocked.begin(), false);
168 Global::layerName.insert(Global::layerName.begin(), "Background");
169 Global::numLayers = 1;
170 Global::activeLayer = 0;
173 /*static*/ bool FileIO::LoadVersion1_0(FILE * file, Container * drawing)
175 // Approach: read each object in the file, one by one. If the object is a
176 // Container, add objects to it until an "endContainer" marker is found.
177 // This will require a stack to maintain the current Container.
178 std::vector<Container *> containerStack;
179 Container * currentTopContainer = drawing;
184 // Reconstruct the object
185 Object * obj = GetObjectFromFile(file);
187 // objectFileType is set in GetObjectFromFile()...
188 if (objectFileType == OTFObject)
193 currentTopContainer->objects.push_back(obj);
194 //printf("Load: Adding object. Container size = %li (%li)\n", drawing->objects.size(), currentTopContainer->objects.size());
196 // If the object is a container, push current TLC on the stack and
197 // set it as the new TLC
198 if (obj->type == OTContainer)
200 containerStack.push_back(currentTopContainer);
201 currentTopContainer = (Container *)obj;
204 else if (objectFileType == OTFContainerEnd)
206 // Container is done, so pop the stack to get back the previous TLC
207 currentTopContainer = containerStack.back();
208 containerStack.pop_back();
210 else if (objectFileType == OTFEndOfFile)
212 //printf("Load: container size = %li\n", drawing->objects.size());
220 /*static*/ bool FileIO::LoadVersion1_1(FILE * file, Container * drawing)
222 // Approach: read each object in the file, one by one. If the object is a
223 // Container, add objects to it until an "endContainer" marker is found.
224 // This will require a stack to maintain the current Container.
225 std::vector<Container *> containerStack;
226 Container * currentTopContainer = drawing;
231 // Reconstruct the object (extended format!)
232 Object * obj = GetObjectFromFile(file, true);
234 // objectFileType is set in GetObjectFromFile()...
235 if (objectFileType == OTFObject)
240 currentTopContainer->objects.push_back(obj);
241 //printf("Load: Adding object. Container size = %li (%li)\n", drawing->objects.size(), currentTopContainer->objects.size());
243 // If the object is a container, push current TLC on the stack and
244 // set it as the new TLC
245 if (obj->type == OTContainer)
247 containerStack.push_back(currentTopContainer);
248 currentTopContainer = (Container *)obj;
251 else if (objectFileType == OTFContainerEnd)
253 // Add the extents of the current container
254 Rect r = ApplicationWindow::drawing->GetObjectExtents((Object *)currentTopContainer);
255 currentTopContainer->p[0] = r.TopLeft();
256 currentTopContainer->p[1] = r.BottomRight();
257 //printf("Container extents: <%lf, %lf>, <%lf, %lf>\n", r.l, r.t, r.r, r.b);
259 // Container is done, so pop the stack to get back the previous TLC
260 currentTopContainer = containerStack.back();
261 containerStack.pop_back();
263 else if (objectFileType == OTFEndOfFile)
265 //printf("Load: container size = %li\n", drawing->objects.size());
273 /*static*/ bool FileIO::LoadVersion1_2(FILE * file, Container * drawing)
276 char textBuffer[65536];
278 // Load layer information first
279 fscanf(file, "LAYERS %i\n", &Global::numLayers);
281 for(int i=0; i<Global::numLayers; i++)
283 fscanf(file, "%i %i \"%[^\"]\"\n", &hidden, &locked, textBuffer);
284 Global::layerHidden.push_back(hidden ? true : false);
285 Global::layerLocked.push_back(locked ? true : false);
286 Global::layerName.push_back(textBuffer);
289 fscanf(file, "ACTIVE %i\n", &Global::activeLayer);
291 // Approach: read each object in the file, one by one. If the object is a
292 // Container, add objects to it until an "endContainer" marker is found.
293 // This will require a stack to maintain the current Container.
294 std::vector<Container *> containerStack;
295 Container * currentTopContainer = drawing;
299 // Reconstruct the object (extended format!)
300 Object * obj = GetObjectFromFile(file, true, true);
302 // objectFileType is set in GetObjectFromFile()...
303 if (objectFileType == OTFObject)
308 currentTopContainer->objects.push_back(obj);
309 //printf("Load: Adding object. Container size = %li (%li)\n", drawing->objects.size(), currentTopContainer->objects.size());
311 // If the object is a container, push current TLC on the stack and
312 // set it as the new TLC
313 if (obj->type == OTContainer)
315 containerStack.push_back(currentTopContainer);
316 currentTopContainer = (Container *)obj;
319 else if (objectFileType == OTFContainerEnd)
321 // Add the extents of the current container
322 Rect r = ApplicationWindow::drawing->GetObjectExtents((Object *)currentTopContainer);
323 currentTopContainer->p[0] = r.TopLeft();
324 currentTopContainer->p[1] = r.BottomRight();
325 //printf("Container extents: <%lf, %lf>, <%lf, %lf>\n", r.l, r.t, r.r, r.b);
327 // Container is done, so pop the stack to get back the previous TLC
328 currentTopContainer = containerStack.back();
329 containerStack.pop_back();
331 else if (objectFileType == OTFEndOfFile)
333 //printf("Load: container size = %li\n", drawing->objects.size());
341 /*static*/ Object * FileIO::GetObjectFromFile(FILE * file, bool extended/*= false*/, bool ext2/*= false*/)
344 char textBuffer[65536];
346 /*int num =*/ fscanf(file, "%s", buffer);
348 objectFileType = OTFObject;
349 //printf("FileIO: fscanf returned %i, buffer = \"%s\"\n", num, buffer);
351 // The following fugliness is for troubleshooting. Can remove later.
352 if ((strcmp(buffer, "END") != 0) && (strcmp(buffer, "ENDCONTAINER") != 0))
355 /*num =*/ fscanf(file, " %i ", &foundLayer);
356 //printf("FileIO: fscanf returned %i, foundLayer = %i\n", num, foundLayer);
361 else if (errno == EBADF)
363 else if (errno == EILSEQ)
365 else if (errno == EINTR)
367 else if (errno == EINVAL)
369 else if (errno == ENOMEM)
371 else if (errno == ERANGE)
374 printf("errno = %i\n", errno);
378 // Need to add pen attributes as well... do that for v1.1 :-P
379 // And fill attributes... do that for v1.3 (1.2 added layers)
380 if (strcmp(buffer, "LINE") == 0)
383 fscanf(file, "(%lf,%lf) (%lf,%lf)", &p1.x, &p1.y, &p2.x, &p2.y);
384 obj = (Object *)new Line(p1, p2);
386 else if (strcmp(buffer, "CIRCLE") == 0)
390 fscanf(file, "(%lf,%lf) %lf", &p.x, &p.y, &r);
391 obj = (Object *)new Circle(p, r);
393 else if (strcmp(buffer, "ARC") == 0)
397 fscanf(file, "(%lf,%lf) %lf, %lf, %lf", &p.x, &p.y, &r, &a1, &a2);
398 obj = (Object *)new Arc(p, r, a1, a2);
400 else if (strcmp(buffer, "TEXT") == 0)
403 fscanf(file, "(%lf,%lf) \"%[^\"]\"", &p.x, &p.y, textBuffer);
404 obj = (Object *)new Text(p, textBuffer);
406 else if (strcmp(buffer, "DIMENSION") == 0)
411 fscanf(file, "(%lf,%lf) (%lf,%lf) %i", &p1.x, &p1.y, &p2.x, &p2.y, (int *)&type);
414 fscanf(file, " %lf", &offset);
416 obj = (Object *)new Dimension(p1, p2, type, offset);
418 else if (strcmp(buffer, "CONTAINER") == 0)
420 obj = (Object *)new Container();
421 // objectFileType = OTFContainer;
423 else if (strcmp(buffer, "ENDCONTAINER") == 0)
425 objectFileType = OTFContainerEnd;
427 else if (strcmp(buffer, "END") == 0)
429 objectFileType = OTFEndOfFile;
432 printf("Unknown object type '%s'...\n", buffer);
436 obj->layer = foundLayer;
438 if (extended && (obj->type != OTContainer))
440 fscanf(file, " (%i, %f, %i)\n", &obj->color, &obj->thickness, &obj->style);
447 /*static*/ bool FileIO::WriteObjectToFile(FILE * file, Object * obj)
456 fprintf(file, "LINE %i (%lf,%lf) (%lf,%lf)", obj->layer, obj->p[0].x, obj->p[0].y, obj->p[1].x, obj->p[1].y);
459 fprintf(file, "CIRCLE %i (%lf,%lf) %lf", obj->layer, obj->p[0].x, obj->p[0].y, obj->radius[0]);
464 fprintf(file, "ARC %i (%lf,%lf) %lf, %lf, %lf", obj->layer, obj->p[0].x, obj->p[0].y, obj->radius[0], obj->angle[0], obj->angle[1]);
469 // fprintf(file, "DIMENSION %i (%lf,%lf) (%lf,%lf) %i\n", layer, position.x, position.y, endpoint.x, endpoint.y, dimensionType);
470 fprintf(file, "DIMENSION %i (%lf,%lf) (%lf,%lf) %i %lf", obj->layer, obj->p[0].x, obj->p[0].y, obj->p[1].x, obj->p[1].y, ((Dimension *)obj)->subtype, ((Dimension *)obj)->offset);
475 fprintf(file, "TEXT %i (%lf,%lf) \"%s\"", obj->layer, obj->p[0].x, obj->p[0].y, ((Text *)obj)->s.c_str());
479 Container * c = (Container *)obj;
481 if (c->topLevel == false)
482 fprintf(file, "CONTAINER %i\n", obj->layer);
484 std::vector<void *>::iterator i;
486 for(i=c->objects.begin(); i!=c->objects.end(); i++)
487 WriteObjectToFile(file, (Object *)*i);
489 if (c->topLevel == false)
490 fprintf(file, "ENDCONTAINER\n");
498 if (obj->type != OTContainer)
499 fprintf(file, " (%i, %f, %i)\n", obj->color, obj->thickness, obj->style);