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
25 How to handle connected objects
26 -------------------------------
28 Every Object has a vector<Object *> which enumerates all Objects connected to
29 the one we're looking at. So it looks like we'll have to take a two pass
30 approach to loading and saving.
32 Basically, in the saving case, first we write out all objects, keeping a
33 pointer-to-index-number record. Second, we loop through all the objects we
34 wrote out, writing connection lists. Format (indices are 1-based):
42 In the reading case, we do pretty much the same: we construct pointer-to-index-
43 number list, then read the connection list. The PTIN connects the index to the
44 Object pointers we've created. Then we simply call the Object's Connect()
45 function to connect the objects.
47 Small problem though: How does a Dimension know which points on a Line it's
48 connected to? This approach tells the Dimension it's connected to the Line and
49 the Line that it's connected to the Dimension, but not which points.
51 How to handle them then? Do we list the point with the Object pointed at? A
52 Line can contain an infinite number of points to connect with besides its
55 So with each connection Object in the vector, there would also have to be a
56 corresponding point to go with it, that would be gotten from the other Object's
57 Connect() function. Or, instead of a point, a parameter value?
59 Doing that, with the Line and a parameter "t", if t == 0 we have endpoint 1.
60 if t == 1, then we have endpoint 2. With a Circle, the parameter is a number
61 between 0 and 1 (scaled to 0 to 2π). With an Arc, the parameter goes from 0 to
62 1, 0 being enpoint 1 and 1 being endpoint 2.
64 How does this work for moving objects that are connected? Again, with the Line
65 and Dimension. The Line's connections looks like this:
70 Dimension looks like this:
72 Object *: line1, t = 0
73 Object *: line1, t = 1
75 For Dimensions, it can query the connected object (if any) using something like
76 GetPointForParameter(). That way it can figure out where its endpoints are. If
77 there is no connected point, then it uses its internal point.
80 Dimensions are special cases of lines: They have exactly *two* points and none
81 in between. Therefore, the Dimension object only needs to have two points. But
82 those points can be connected to multiple objects. The can also be connected to
83 no points/Objects too.
85 How to describe them and their connections (or lack thereof)?
87 Would have to be a 2nd pass, after all objects have been written out in order.
88 Then you could do something like:
91 8 (the Dimension #): 1 (the Object # for point 1) 1 (the Object # for point 2)
96 Connection attributes: E.g., between a Line a Circle, it can be tangent,
97 perpendicular, or an arbitrary angle. How to encode that information? It's not
98 intrinsic to either the Line or the Circle, but is a function of the
99 relationship between them by virtue of their connection.
102 OTHER CONSIDERATIONS:
103 ---------------------
105 - Need to figure out how to store the Layer list (should layer list be optional?)
106 - Need to figure out how to store the Block list and blocks
111 //enum ObjectTypeFile { OTFContainer, OTFContainerEnd, OTFLine, OTFCircle, OTFArc,
112 // OTFDimension, OTFPolygon, OTFText, OTFImage, OTFBlock, OTFEndOfFile };
113 enum ObjectTypeFile { OTFContainer, OTFContainerEnd, OTFObject, OTFEndOfFile };
116 // Instantiate class variables
117 /*static*/ int FileIO::objectFileType = OTFObject;
120 /*static*/ bool FileIO::SaveAtnsFile(FILE * file, Container * c)
122 /* Approach: loop through the container, doing a depth-first traversal. Any
123 extra containers found are looped through until there aren't any more
124 down, then ordinary objects are described. This can be handled by a
125 virtual Object function that reports the object by itself if it's a non-
126 Container, otherwise it enumerates all objects within itself. */
128 fprintf(file, "ARCHITEKTONAS DRAWING V1.1\n");
129 WriteObjectToFile(file, (Object *)c);
130 fprintf(file, "END\n");
135 /*static*/ bool FileIO::LoadAtnsFile(FILE * file, Container * drawing)
139 fscanf(file, "ARCHITEKTONAS DRAWING V%f", &version);
142 return LoadVersion1_0(file, drawing);
143 else if (version == 1.1f)
144 return LoadVersion1_1(file, drawing);
146 //printf("LoadAtnsFile: Could not locate version! (version=%f)\n", version);
151 /*static*/ bool FileIO::LoadVersion1_0(FILE * file, Container * drawing)
153 // Approach: read each object in the file, one by one. If the object is a
154 // Container, add objects to it until an "endContainer" marker is found.
155 // This will require a stack to maintain the current Container.
156 std::vector<Container *> containerStack;
157 Container * currentTopContainer = drawing;
161 // Reconstruct the object
162 Object * obj = GetObjectFromFile(file);
164 // objectFileType is set in GetObjectFromFile()...
165 if (objectFileType == OTFObject)
170 currentTopContainer->objects.push_back(obj);
171 //printf("Load: Adding object. Container size = %li (%li)\n", drawing->objects.size(), currentTopContainer->objects.size());
173 // If the object is a container, push current TLC on the stack and
174 // set it as the new TLC
175 if (obj->type == OTContainer)
177 containerStack.push_back(currentTopContainer);
178 currentTopContainer = (Container *)obj;
181 else if (objectFileType == OTFContainerEnd)
183 // Container is done, so pop the stack to get back the previous TLC
184 currentTopContainer = containerStack.back();
185 containerStack.pop_back();
187 else if (objectFileType == OTFEndOfFile)
189 //printf("Load: container size = %li\n", drawing->objects.size());
198 /*static*/ bool FileIO::LoadVersion1_1(FILE * file, Container * drawing)
200 // Approach: read each object in the file, one by one. If the object is a
201 // Container, add objects to it until an "endContainer" marker is found.
202 // This will require a stack to maintain the current Container.
203 std::vector<Container *> containerStack;
204 Container * currentTopContainer = drawing;
208 // Reconstruct the object (extended format!)
209 Object * obj = GetObjectFromFile(file, true);
211 // objectFileType is set in GetObjectFromFile()...
212 if (objectFileType == OTFObject)
217 currentTopContainer->objects.push_back(obj);
218 //printf("Load: Adding object. Container size = %li (%li)\n", drawing->objects.size(), currentTopContainer->objects.size());
220 // If the object is a container, push current TLC on the stack and
221 // set it as the new TLC
222 if (obj->type == OTContainer)
224 containerStack.push_back(currentTopContainer);
225 currentTopContainer = (Container *)obj;
228 else if (objectFileType == OTFContainerEnd)
230 // Container is done, so pop the stack to get back the previous TLC
231 currentTopContainer = containerStack.back();
232 containerStack.pop_back();
234 else if (objectFileType == OTFEndOfFile)
236 //printf("Load: container size = %li\n", drawing->objects.size());
245 /*static*/ Object * FileIO::GetObjectFromFile(FILE * file, bool extended/*= false*/)
248 char textBuffer[65536];
250 /*int num =*/ fscanf(file, "%s", buffer);
252 objectFileType = OTFObject;
253 //printf("FileIO: fscanf returned %i, buffer = \"%s\"\n", num, buffer);
255 // The following fugliness is for troubleshooting. Can remove later.
256 if ((strcmp(buffer, "END") != 0) && (strcmp(buffer, "ENDCONTAINER") != 0))
259 /*num =*/ fscanf(file, " %i ", &foundLayer);
260 //printf("FileIO: fscanf returned %i, foundLayer = %i\n", num, foundLayer);
265 else if (errno == EBADF)
267 else if (errno == EILSEQ)
269 else if (errno == EINTR)
271 else if (errno == EINVAL)
273 else if (errno == ENOMEM)
275 else if (errno == ERANGE)
278 printf("errno = %i\n", errno);
282 // Need to add pen attributes as well... do that for v1.1 :-P
283 if (strcmp(buffer, "LINE") == 0)
286 fscanf(file, "(%lf,%lf) (%lf,%lf)", &p1.x, &p1.y, &p2.x, &p2.y);
287 obj = (Object *)new Line(p1, p2);
289 else if (strcmp(buffer, "CIRCLE") == 0)
293 fscanf(file, "(%lf,%lf) %lf", &p.x, &p.y, &r);
294 obj = (Object *)new Circle(p, r);
296 else if (strcmp(buffer, "ARC") == 0)
300 fscanf(file, "(%lf,%lf) %lf, %lf, %lf", &p.x, &p.y, &r, &a1, &a2);
301 obj = (Object *)new Arc(p, r, a1, a2);
303 else if (strcmp(buffer, "TEXT") == 0)
306 fscanf(file, "(%lf,%lf) \"%[^\"]\"", &p.x, &p.y, textBuffer);
307 obj = (Object *)new Text(p, textBuffer);
309 else if (strcmp(buffer, "DIMENSION") == 0)
313 fscanf(file, "(%lf,%lf) (%lf,%lf) %i", &p1.x, &p1.y, &p2.x, &p2.y, (int *)&type);
314 obj = (Object *)new Dimension(p1, p2, type);
316 else if (strcmp(buffer, "CONTAINER") == 0)
318 obj = (Object *)new Container();
319 // objectFileType = OTFContainer;
321 else if (strcmp(buffer, "ENDCONTAINER") == 0)
323 objectFileType = OTFContainerEnd;
325 else if (strcmp(buffer, "END") == 0)
327 objectFileType = OTFEndOfFile;
330 printf("Unknown object type '%s'...\n", buffer);
334 obj->layer = foundLayer;
336 if (extended && (obj->type != OTContainer))
338 fscanf(file, " (%i, %f, %i)", &obj->color, &obj->thickness, &obj->style);
346 /*static*/ bool FileIO::WriteObjectToFile(FILE * file, Object * obj)
355 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);
358 fprintf(file, "CIRCLE %i (%lf,%lf) %lf", obj->layer, obj->p[0].x, obj->p[0].y, obj->radius[0]);
363 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]);
368 // fprintf(file, "DIMENSION %i (%lf,%lf) (%lf,%lf) %i\n", layer, position.x, position.y, endpoint.x, endpoint.y, dimensionType);
369 fprintf(file, "DIMENSION %i (%lf,%lf) (%lf,%lf) %i", obj->layer, obj->p[0].x, obj->p[0].y, obj->p[1].x, obj->p[1].y, ((Dimension *)obj)->subtype);
374 fprintf(file, "TEXT %i (%lf,%lf) \"%s\"", obj->layer, obj->p[0].x, obj->p[0].y, ((Text *)obj)->s.c_str());
378 Container * c = (Container *)obj;
380 if (c->topLevel == false)
381 fprintf(file, "CONTAINER %i\n", obj->layer);
383 std::vector<void *>::iterator i;
385 for(i=c->objects.begin(); i!=c->objects.end(); i++)
386 WriteObjectToFile(file, (Object *)*i);
388 if (c->topLevel == false)
389 fprintf(file, "ENDCONTAINER\n");
397 if (obj->type != OTContainer)
398 fprintf(file, " (%i, %f, %i)\n", obj->color, obj->thickness, obj->style);