src/ldproject.cpp

branch
projects
changeset 937
d5a58674ccd1
parent 936
aee883858c90
child 938
cc8920320184
--- a/src/ldproject.cpp	Wed Mar 04 15:37:21 2015 +0200
+++ b/src/ldproject.cpp	Thu Mar 05 02:24:15 2015 +0200
@@ -31,9 +31,7 @@
 
 	archive* arc = archive_read_new();
 	archive_read_support_filter_all (arc);
-	archive_read_support_format_all (arc);
-	// archive_read_support_format_zip (arc);
-	archive_entry* arcent;
+	archive_read_support_format_zip (arc);
 	int result = archive_read_open_filename (arc, filename.toLocal8Bit().constData(), 0x4000);
 
 	if (result != ARCHIVE_OK)
@@ -42,16 +40,20 @@
 		return LDProjectPtr();
 	}
 
-	while (archive_read_next_header(arc, &arcent) == ARCHIVE_OK)
+	for (archive_entry* arcent; archive_read_next_header(arc, &arcent) == ARCHIVE_OK;)
 	{
 		QString pathname = archive_entry_pathname (arcent);
-		char buffer[1024];
-		int size = archive_read_data(arc, buffer, sizeof buffer);
+		QVector<char> buffer;
+		buffer.resize (archive_entry_size (arcent));
+		int size = archive_read_data(arc, buffer.data(), buffer.size());
 
-		if (size < 0)
-			fprint (fp, "Error reading entry %1: %2", pathname, archive_error_string (arc));
+		if (size >= 0)
+		{
+			if (pathname.startsWith ("dat/"))
+				loadBinaryDocument (pathname.right (4), QByteArray (buffer.constData(), buffer.size()));
+		}
 		else
-			fprint (fp, "%1 contains: %2 (%3 bytes)\n", pathname, static_cast<const char*>(buffer), size);
+			fprint (fp, "Unable to read %1: %2", pathname, archive_error_string (arc));
 	}
 
 	if ((result = archive_read_free(arc)) != ARCHIVE_OK)
@@ -63,6 +65,68 @@
 	return LDProjectPtr();
 }
 
+#include "ldDocument.h"
+void LDProject::loadBinaryDocument(const QString &name, const QByteArray &data)
+{
+	QDataStream ds (&data, QIODevice::ReadOnly);
+	ds.setVersion (QDataStream::Qt_4_8);
+	enum { CurrentVersion = 0 };
+
+	quint16 version;
+	ds << version;
+
+	if (version > CurrentVersion)
+		return; // too new
+
+	qint8 header;
+	quint32 color;
+	LDDocumentPtr doc = LDDocument::createNew();
+	LDObjectPtr obj;
+	struct XYZ { double x, y, z; Vertex toVertex() const { return Vertex (x,y,z); }};
+	XYZ verts[4];
+
+	while ((ds << header) != -1)
+	{
+		switch (header)
+		{
+		case 0:
+			{
+				QString message;
+				ds >> message;
+				doc->addObject (LDSpawn<LDComment> (message));
+			}
+			break;
+
+		case 2:
+			obj = LDSpawn<LDLine>();
+			goto polyobject;
+
+		case 3:
+			obj = LDSpawn<LDTriangle>();
+			goto polyobject;
+
+		case 4:
+			obj = LDSpawn<LDQuad>();
+			goto polyobject;
+
+		case 5:
+			obj = LDSpawn<LDCondLine>();
+		polyobject:
+			ds >> color;
+			for (int i = 0; i < obj->numVertices(); ++i)
+			{
+				XYZ v;
+				ds >> v.x >> v.y >> v.z;
+				obj->setVertex (i, Vertex (v.x, v.y, v.z));
+			}
+
+			doc->addObject (obj);
+			break;
+		}
+	}
+
+}
+
 LDProjectPtr LDProject::NewProject()
 {
 	return LDProjectPtr (new LDProject());

mercurial