+// The following fugliness is for troubleshooting. Can remove later.
+ if ((strcmp(buffer, "END") != 0) && (strcmp(buffer, "ENDCONTAINER") != 0))
+{
+errno = 0;
+ /*num =*/ fscanf(file, " %i ", &foundLayer);
+//printf("FileIO: fscanf returned %i, foundLayer = %i\n", num, foundLayer);
+if (errno)
+{
+ if (errno == EAGAIN)
+ printf("EAGAIN\n");
+ else if (errno == EBADF)
+ printf("EBADF\n");
+ else if (errno == EILSEQ)
+ printf("EILSEQ\n");
+ else if (errno == EINTR)
+ printf("EINTR\n");
+ else if (errno == EINVAL)
+ printf("EINVAL\n");
+ else if (errno == ENOMEM)
+ printf("ENOMEM\n");
+ else if (errno == ERANGE)
+ printf("ERANGE\n");
+ else
+ printf("errno = %i\n", errno);
+}
+}
+
+ // Need to add pen attributes as well... do that for v1.1 :-P
+ // And fill attributes... do that for v1.3 (1.2 added layers)
+ if (strcmp(buffer, "LINE") == 0)
+ {
+ Point p1, p2;
+ fscanf(file, "(%lf,%lf) (%lf,%lf)", &p1.x, &p1.y, &p2.x, &p2.y);
+ obj = (Object *)new Line(p1, p2);
+ }
+ else if (strcmp(buffer, "CIRCLE") == 0)
+ {
+ Point p;
+ double r;
+ fscanf(file, "(%lf,%lf) %lf", &p.x, &p.y, &r);
+ obj = (Object *)new Circle(p, r);
+ }
+ else if (strcmp(buffer, "ARC") == 0)
+ {
+ Point p;
+ double r, a1, a2;
+ fscanf(file, "(%lf,%lf) %lf, %lf, %lf", &p.x, &p.y, &r, &a1, &a2);
+ obj = (Object *)new Arc(p, r, a1, a2);
+ }
+ else if (strcmp(buffer, "TEXT") == 0)
+ {
+ Point p;
+ fscanf(file, "(%lf,%lf) \"%[^\"]\"", &p.x, &p.y, textBuffer);
+ obj = (Object *)new Text(p, textBuffer);
+ }
+ else if (strcmp(buffer, "DIMENSION") == 0)
+ {
+ Point p1, p2;
+ DimensionType type;
+ double offset = 0;
+ fscanf(file, "(%lf,%lf) (%lf,%lf) %i", &p1.x, &p1.y, &p2.x, &p2.y, (int *)&type);
+
+ if (ext2)
+ fscanf(file, " %lf", &offset);
+
+ obj = (Object *)new Dimension(p1, p2, type, offset);
+ }
+ else if (strcmp(buffer, "CONTAINER") == 0)
+ {
+ obj = (Object *)new Container();
+// objectFileType = OTFContainer;
+ }
+ else if (strcmp(buffer, "ENDCONTAINER") == 0)
+ {
+ objectFileType = OTFContainerEnd;
+ }
+ else if (strcmp(buffer, "END") == 0)
+ {
+ objectFileType = OTFEndOfFile;
+ }
+ else
+ printf("Unknown object type '%s'...\n", buffer);
+
+ if (obj != NULL)
+ {
+ obj->layer = foundLayer;
+
+ if (extended && (obj->type != OTContainer))
+ {
+ fscanf(file, " (%i, %f, %i)\n", &obj->color, &obj->thickness, &obj->style);
+ }
+ }
+
+ return obj;
+}
+
+/*static*/ bool FileIO::WriteObjectToFile(FILE * file, Object * obj)
+{
+ // Sanity check
+ if (obj == NULL)
+ return false;
+
+ switch (obj->type)
+ {
+ case OTLine:
+ 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);
+ break;
+ case OTCircle:
+ fprintf(file, "CIRCLE %i (%lf,%lf) %lf", obj->layer, obj->p[0].x, obj->p[0].y, obj->radius[0]);
+ break;
+ case OTEllipse:
+ break;
+ case OTArc:
+ 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]);
+ break;
+ case OTPolygon:
+ break;
+ case OTDimension:
+// fprintf(file, "DIMENSION %i (%lf,%lf) (%lf,%lf) %i\n", layer, position.x, position.y, endpoint.x, endpoint.y, dimensionType);
+ 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);
+ break;
+ case OTSpline:
+ break;
+ case OTText:
+ fprintf(file, "TEXT %i (%lf,%lf) \"%s\"", obj->layer, obj->p[0].x, obj->p[0].y, ((Text *)obj)->s.c_str());
+ break;
+ case OTContainer:
+ {
+ Container * c = (Container *)obj;
+
+ if (c->topLevel == false)
+ fprintf(file, "CONTAINER %i\n", obj->layer);
+
+ std::vector<void *>::iterator i;
+
+ for(i=c->objects.begin(); i!=c->objects.end(); i++)
+ WriteObjectToFile(file, (Object *)*i);
+
+ if (c->topLevel == false)
+ fprintf(file, "ENDCONTAINER\n");
+
+ break;
+ }
+ default:
+ break;
+ }
+
+ if (obj->type != OTContainer)
+ fprintf(file, " (%i, %f, %i)\n", obj->color, obj->thickness, obj->style);
+
+ return true;
+}