begin work on subfile caching

Mon, 18 Mar 2013 03:38:51 +0200

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Mon, 18 Mar 2013 03:38:51 +0200
changeset 23
69a91c1ff583
parent 22
335e430a6b4f
child 24
d2d4d0154338

begin work on subfile caching

gui.cpp file | annotate | diff | comparison | revisions
icons/inline.png file | annotate | diff | comparison | revisions
io.cpp file | annotate | diff | comparison | revisions
io.h file | annotate | diff | comparison | revisions
ldforge.pro file | annotate | diff | comparison | revisions
ldtypes.h file | annotate | diff | comparison | revisions
main.cpp file | annotate | diff | comparison | revisions
model.cpp file | annotate | diff | comparison | revisions
model.h file | annotate | diff | comparison | revisions
--- a/gui.cpp	Sat Mar 16 17:50:13 2013 +0200
+++ b/gui.cpp	Mon Mar 18 03:38:51 2013 +0200
@@ -200,10 +200,7 @@
 	str name = QFileDialog::getOpenFileName (this, "Open File",
 		"", "LDraw files (*.dat *.ldr)").toStdString().c_str();
 	
-	if (g_LoadedFiles.size())
-		closeModel ();
-	
-	IO_OpenLDrawFile (name);
+	openModel (name);
 }
 
 void LDForgeWindow::slot_save () {
Binary file icons/inline.png has changed
--- a/io.cpp	Sat Mar 16 17:50:13 2013 +0200
+++ b/io.cpp	Mon Mar 18 03:38:51 2013 +0200
@@ -6,21 +6,21 @@
 #include "gui.h"
 #include "bbox.h"
 
+vector<str> g_zaFileLoadPaths;
+
 // =============================================================================
 // IO_FindLoadedFile (str)
 //
 // Returns a pointer to the first found open file with the given name.
 // =============================================================================
 OpenFile* IO_FindLoadedFile (str name) {
-	OpenFile* file;
-	
-	for (uint i = 0; i < g_LoadedFiles.size(); i++) {
-		file = g_LoadedFiles[i];
-		if (!file->zFileName.icompare (name))
+	for (ulong i = 0; i < g_LoadedFiles.size(); i++) {
+		OpenFile* const file = g_LoadedFiles[i];
+		if (file->zFileName == name)
 			return file;
 	}
 	
-	return NULL;
+	return nullptr;
 }
 
 // =============================================================================
@@ -34,8 +34,19 @@
 	FILE* fp = fopen (path.chars (), "r");
 	
 	if (!fp) {
+		for (ushort i = 0; i < g_zaFileLoadPaths.size(); ++i) {
+			str zFilePath = str::mkfmt ("%s/%s", g_zaFileLoadPaths[i].chars(), path.chars());
+			printf ("try open %s\n", zFilePath.chars ());
+			fp = fopen (zFilePath.chars (), "r");
+			
+			if (fp)
+				break;
+		}
+	}
+	
+	if (!fp) {
 		logf (LOG_Error, "Couldn't open %s: %s\n", path.chars (), strerror (errno));
-		return NULL;
+		return nullptr;
 	}
 	
 	OpenFile* load = new OpenFile;
@@ -73,23 +84,15 @@
 	}
 	
 	g_LoadedFiles.push_back (load);
-	g_CurrentFile = g_LoadedFiles[g_LoadedFiles.size() - 1];
-	
-	// Recalculate the bounding box
-	g_BBox.calculate();
-	
-	// Rebuild the object tree view now.
-	g_qWindow->buildObjList ();
-	g_qWindow->setTitle ();
 	
 	logf (LOG_Success, "File %s parsed successfully (%lu warning%s).\n",
 		path.chars(), numWarnings, PLURAL (numWarnings));
 	
-	return g_CurrentFile;
+	return load;
 }
 
 // =============================================================================
-// isNumber (char*)
+// isNumber (char*) [static]
 //
 // Returns whether a given string represents a floating point number
 // TODO: Does LDraw support scientific notation?
@@ -146,12 +149,16 @@
 		return new LDEmpty;
 	}
 	
-	char c = zLine[0];
 	vector<str> tokens = zLine / " ";
 	
+	// Rid leading all-whitespace tokens
+	while (tokens.size() && !(~tokens[0]))
+		tokens.erase (tokens.begin());
+	
 	if (~tokens[0] != 1)
 		return new LDGibberish (zLine, "Illogical line code");
 	
+	char const c = tokens[0][0];
 	switch (c - '0') {
 	case 0:
 		{
@@ -163,10 +170,23 @@
 	
 	case 1:
 		{
+			// Subfile
 			CHECK_TOKEN_COUNT (15)
 			CHECK_TOKEN_NUMBERS (1, 13)
 			
-			// Subfile
+#ifndef WIN32
+			tokens[14].replace ("\\", "/");
+#endif // WIN32
+			
+			// Try open the file
+			OpenFile* pFile = IO_FindLoadedFile (tokens[14]);
+			if (!pFile)
+				pFile = IO_OpenLDrawFile (tokens[14]);
+			
+			// If we cannot open the file, mark it an error
+			if (!pFile)
+				return new LDGibberish (zLine, "Could not open referred file");
+			
 			LDSubfile* obj = new LDSubfile;
 			obj->dColor = atoi (tokens[1]);
 			obj->vPosition = ParseVertex (zLine, 2); // 2 - 4
@@ -175,6 +195,7 @@
 				obj->faMatrix[i] = atof (tokens[i + 5]); // 5 - 13
 			
 			obj->zFileName = tokens[14];
+			obj->pFile = pFile;
 			return obj;
 		}
 	
--- a/io.h	Sat Mar 16 17:50:13 2013 +0200
+++ b/io.h	Mon Mar 18 03:38:51 2013 +0200
@@ -16,6 +16,7 @@
 OpenFile* IO_OpenLDrawFile (str path);
 LDObject* ParseLine (str zLine);
 
+extern vector<str> g_zaFileLoadPaths;
 extern vector<OpenFile*> g_LoadedFiles;
 
 #endif
\ No newline at end of file
--- a/ldforge.pro	Sat Mar 16 17:50:13 2013 +0200
+++ b/ldforge.pro	Mon Mar 18 03:38:51 2013 +0200
@@ -6,6 +6,7 @@
 TARGET = 
 DEPENDPATH += .
 INCLUDEPATH += .
+OBJECTS_DIR = build/
 
 # Input
 HEADERS += bbox.h \
--- a/ldtypes.h	Sat Mar 16 17:50:13 2013 +0200
+++ b/ldtypes.h	Mon Mar 18 03:38:51 2013 +0200
@@ -118,6 +118,7 @@
 	vertex vPosition; // Position of the subpart
 	double faMatrix[9]; // Transformation matrix for the subpart
 	str zFileName; // Filename of the subpart
+	OpenFile* pFile; // Pointer to opened file for this subfile. nullptr if unopened.
 };
 
 // =============================================================================
--- a/main.cpp	Sat Mar 16 17:50:13 2013 +0200
+++ b/main.cpp	Mon Mar 18 03:38:51 2013 +0200
@@ -10,11 +10,11 @@
 bbox g_BBox;
 
 int main (int argc, char** argv) {
-	if (g_CurrentFile) {
-		printf ("bbox: (%.3f, %.3f, %.3f), (%.3f, %.3f, %.3f)\n",
-			FVERTEX (g_BBox.v0), FVERTEX (g_BBox.v1));
-		printf ("%u objects\n", g_CurrentFile->objects.size());
-	}
+	// TODO
+	g_zaFileLoadPaths.push_back (".");
+	g_zaFileLoadPaths.push_back ("/home/arezey/ldraw/parts");
+	g_zaFileLoadPaths.push_back ("/home/arezey/ldraw/parts/s");
+	g_zaFileLoadPaths.push_back ("/home/arezey/ldraw/p");
 	
 	QApplication app (argc, argv);
 	LDForgeWindow* win = new LDForgeWindow;
@@ -46,52 +46,52 @@
 // Common code for the two logfs
 // =============================================================================
 static void logVA (logtype_e eType, const char* fmt, va_list va) {
+	// Log it to standard output
+	vprintf (fmt, va);
+	
+	return;
+	
 	str zText;
 	char* sBuffer;
 	
-	// Log it to standard output
-	vprintf (fmt, va);
-	
 	sBuffer = vdynformat (fmt, va, 128);
 	zText = sBuffer;
 	delete[] sBuffer;
 	
-	
 	// Replace some things out with HTML entities
 	zText.replace ("<", "&lt;");
 	zText.replace (">", "&gt;");
 	zText.replace ("\n", "<br />");
 	
-	str zHTML;
+	str* zpHTML = &g_qWindow->zMessageLogHTML;
 	
 	switch (eType) {
 	case LOG_Normal:
-		zHTML = zText;
+		zpHTML->append (zText);
 		break;
 	
 	case LOG_Error:
-		zHTML.format ("<span style=\"color: #F8F8F8; background-color: #800\"><b>[ERROR]</b> %s</span>",
+		zpHTML->appendformat ("<span style=\"color: #F8F8F8; background-color: #800\"><b>[ERROR]</b> %s</span>",
 			zText.chars());
 		break;
 	
 	case LOG_Info:
-		zHTML.format ("<span style=\"color: #04F\"><b>[INFO]</b> %s</span>",
+		zpHTML->appendformat ("<span style=\"color: #04F\"><b>[INFO]</b> %s</span>",
 			zText.chars());
 		break;
 	
 	case LOG_Success:
-		zHTML.format ("<span style=\"color: #6A0\"><b>[SUCCESS]</b> %s</span>",
+		zpHTML->appendformat ("<span style=\"color: #6A0\"><b>[SUCCESS]</b> %s</span>",
 			zText.chars());
 		break;
 	
 	case LOG_Warning:
-		zHTML.format ("<span style=\"color: #C50\"><b>[WARNING]</b> %s</span>",
+		zpHTML->appendformat ("<span style=\"color: #C50\"><b>[WARNING]</b> %s</span>",
 			zText.chars());
 		break;
 	}
 	
-	g_qWindow->zMessageLogHTML += zHTML;
-	g_qWindow->qMessageLog->setHtml (g_qWindow->zMessageLogHTML);
+	g_qWindow->qMessageLog->setHtml (*zpHTML);
 }
 
 
--- a/model.cpp	Sat Mar 16 17:50:13 2013 +0200
+++ b/model.cpp	Mon Mar 18 03:38:51 2013 +0200
@@ -4,6 +4,7 @@
 #include "io.h"
 #include "gui.h"
 #include "draw.h"
+#include "bbox.h"
 
 // Clear everything from the model
 void closeModel () {
@@ -37,6 +38,21 @@
 	g_qWindow->R->hardRefresh();
 }
 
+void openModel (str zPath) {
+	if (g_CurrentFile)
+		closeModel ();
+	
+	OpenFile* pFile = IO_OpenLDrawFile (zPath);
+	g_CurrentFile = pFile;
+	
+	// Recalculate the bounding box
+	g_BBox.calculate();
+	
+	// Rebuild the object tree view now.
+	g_qWindow->buildObjList ();
+	g_qWindow->setTitle ();
+}
+
 void saveModel () {
 	if (!g_CurrentFile)
 		return;
--- a/model.h	Sat Mar 16 17:50:13 2013 +0200
+++ b/model.h	Mon Mar 18 03:38:51 2013 +0200
@@ -1,3 +1,4 @@
 void closeModel ();
 void newModel ();
-void saveModel ();
\ No newline at end of file
+void saveModel ();
+void openModel (str zPath);
\ No newline at end of file

mercurial