Look for LDraw files in the part's directory first

Fri, 17 May 2013 11:53:28 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Fri, 17 May 2013 11:53:28 +0300
changeset 213
a4113545242c
parent 212
79c5205b807c
child 214
28e0b37156be

Look for LDraw files in the part's directory first

src/addObjectDialog.cpp file | annotate | diff | comparison | revisions
src/file.cpp file | annotate | diff | comparison | revisions
src/file.h file | annotate | diff | comparison | revisions
--- a/src/addObjectDialog.cpp	Fri May 17 03:11:47 2013 +0300
+++ b/src/addObjectDialog.cpp	Fri May 17 11:53:28 2013 +0300
@@ -481,8 +481,10 @@
 				return; // no subfile filename
 			
 			OpenFile* file = loadSubfile (name);
-			if (!file)
+			if (!file) {
+				critical (fmt ("Couldn't open `%s': %s", name.c (), strerror (errno)));
 				return;
+			}
 			
 			LDSubfile* ref = initObj<LDSubfile> (obj);
 			ref->color = dlg.dColor;
--- a/src/file.cpp	Fri May 17 03:11:47 2013 +0300
+++ b/src/file.cpp	Fri May 17 11:53:28 2013 +0300
@@ -19,6 +19,7 @@
 #include <QMessageBox>
 #include <QDir>
 
+#include <stdlib.h>
 #include "common.h"
 #include "config.h"
 #include "file.h"
@@ -118,7 +119,22 @@
 	relpath.replace ("\\", "/");
 #endif // WIN32
 	
-	printf ("Trying %s\n", relpath.chars ());
+	if (g_curfile != null) {
+		long lastpos;
+		for (lastpos = g_curfile->m_filename.len () - 1; lastpos >= 0; --lastpos)
+			if (g_curfile->m_filename[(size_t) lastpos] == '/')
+				break;
+		
+		if (lastpos > 0) {
+			str dirname = g_curfile->m_filename.substr (0, lastpos);
+			str partpath = fmt ("%s" DIRSLASH "%s", dirname.c (), relpath.c ());
+			FILE* fp = fopen (partpath, "r");
+			
+			if (fp != null)
+				return fp;
+		}
+	}
+	
 	FILE* fp = fopen (relpath.chars (), "r");
 	str fullPath;
 	
@@ -128,7 +144,6 @@
 	if (~io_ldpath.value) {
 		// Try with just the LDraw path first
 		fullPath = fmt ("%s" DIRSLASH "%s", io_ldpath.value.chars(), relpath.chars());
-		printf ("Trying %s\n", fullPath.chars());
 		
 		fp = fopen (fullPath, "r");
 		if (fp != null)
@@ -143,7 +158,6 @@
 			for (char const* sSubdir : saSubdirectories) {
 				fullPath = fmt ("%s" DIRSLASH "%s" DIRSLASH "%s",
 					io_ldpath.value.chars(), sSubdir, relpath.chars());
-				printf ("Trying %s\n", fullPath.chars());
 				
 				fp = fopen (fullPath.chars (), "r");
 				
@@ -198,7 +212,7 @@
 // =============================================================================
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
-OpenFile* openDATFile (str path, bool search) {
+OpenFile* openDATFile (str path, bool search, bool mainfile) {
 	logf ("Opening %s...\n", path.chars());
 	
 	// Convert the file name to lowercase since some parts contain uppercase
@@ -217,6 +231,10 @@
 	
 	OpenFile* load = new OpenFile;
 	load->m_filename = path;
+	
+	if (mainfile)
+		g_curfile = load;
+	
 	ulong numWarnings;
 	std::vector<LDObject*> objs = loadFileContents (fp, &numWarnings);
 	
@@ -346,7 +364,7 @@
 void openMainFile (str path) {
 	closeAll ();
 	
-	OpenFile* file = openDATFile (path, false);
+	OpenFile* file = openDATFile (path, false, true);
 	
 	if (!file) {
 		// Tell the user loading failed.
@@ -620,7 +638,7 @@
 	// Try open the file
 	OpenFile* pFile = findLoadedFile (zFile);
 	if (!pFile)
-		pFile = openDATFile (zFile, true);
+		pFile = openDATFile (zFile, true, false);
 	
 	return pFile;
 }
--- a/src/file.h	Fri May 17 03:11:47 2013 +0300
+++ b/src/file.h	Fri May 17 11:53:28 2013 +0300
@@ -86,7 +86,7 @@
 
 // Opens the given file and parses the LDraw code within. Returns a pointer
 // to the opened file or null on error.
-OpenFile* openDATFile (str path, bool search);
+OpenFile* openDATFile (str path, bool search, bool mainfile);
 
 // Opens the given file and returns a pointer to it, potentially looking in /parts and /p
 FILE* openLDrawFile (str path, bool bSubDirectories);

mercurial