src/file.cpp

changeset 213
a4113545242c
parent 211
8d35e631bef3
child 214
28e0b37156be
equal deleted inserted replaced
212:79c5205b807c 213:a4113545242c
17 */ 17 */
18 18
19 #include <QMessageBox> 19 #include <QMessageBox>
20 #include <QDir> 20 #include <QDir>
21 21
22 #include <stdlib.h>
22 #include "common.h" 23 #include "common.h"
23 #include "config.h" 24 #include "config.h"
24 #include "file.h" 25 #include "file.h"
25 #include "misc.h" 26 #include "misc.h"
26 #include "bbox.h" 27 #include "bbox.h"
116 FILE* openLDrawFile (str relpath, bool subdirs) { 117 FILE* openLDrawFile (str relpath, bool subdirs) {
117 #ifndef WIN32 118 #ifndef WIN32
118 relpath.replace ("\\", "/"); 119 relpath.replace ("\\", "/");
119 #endif // WIN32 120 #endif // WIN32
120 121
121 printf ("Trying %s\n", relpath.chars ()); 122 if (g_curfile != null) {
123 long lastpos;
124 for (lastpos = g_curfile->m_filename.len () - 1; lastpos >= 0; --lastpos)
125 if (g_curfile->m_filename[(size_t) lastpos] == '/')
126 break;
127
128 if (lastpos > 0) {
129 str dirname = g_curfile->m_filename.substr (0, lastpos);
130 str partpath = fmt ("%s" DIRSLASH "%s", dirname.c (), relpath.c ());
131 FILE* fp = fopen (partpath, "r");
132
133 if (fp != null)
134 return fp;
135 }
136 }
137
122 FILE* fp = fopen (relpath.chars (), "r"); 138 FILE* fp = fopen (relpath.chars (), "r");
123 str fullPath; 139 str fullPath;
124 140
125 if (fp != null) 141 if (fp != null)
126 return fp; 142 return fp;
127 143
128 if (~io_ldpath.value) { 144 if (~io_ldpath.value) {
129 // Try with just the LDraw path first 145 // Try with just the LDraw path first
130 fullPath = fmt ("%s" DIRSLASH "%s", io_ldpath.value.chars(), relpath.chars()); 146 fullPath = fmt ("%s" DIRSLASH "%s", io_ldpath.value.chars(), relpath.chars());
131 printf ("Trying %s\n", fullPath.chars());
132 147
133 fp = fopen (fullPath, "r"); 148 fp = fopen (fullPath, "r");
134 if (fp != null) 149 if (fp != null)
135 return fp; 150 return fp;
136 151
141 }; 156 };
142 157
143 for (char const* sSubdir : saSubdirectories) { 158 for (char const* sSubdir : saSubdirectories) {
144 fullPath = fmt ("%s" DIRSLASH "%s" DIRSLASH "%s", 159 fullPath = fmt ("%s" DIRSLASH "%s" DIRSLASH "%s",
145 io_ldpath.value.chars(), sSubdir, relpath.chars()); 160 io_ldpath.value.chars(), sSubdir, relpath.chars());
146 printf ("Trying %s\n", fullPath.chars());
147 161
148 fp = fopen (fullPath.chars (), "r"); 162 fp = fopen (fullPath.chars (), "r");
149 163
150 if (fp) 164 if (fp)
151 return fp; 165 return fp;
196 } 210 }
197 211
198 // ============================================================================= 212 // =============================================================================
199 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 213 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
200 // ============================================================================= 214 // =============================================================================
201 OpenFile* openDATFile (str path, bool search) { 215 OpenFile* openDATFile (str path, bool search, bool mainfile) {
202 logf ("Opening %s...\n", path.chars()); 216 logf ("Opening %s...\n", path.chars());
203 217
204 // Convert the file name to lowercase since some parts contain uppercase 218 // Convert the file name to lowercase since some parts contain uppercase
205 // file names. I'll assume here that the library will always use lowercase 219 // file names. I'll assume here that the library will always use lowercase
206 // file names for the actual parts.. 220 // file names for the actual parts..
215 return null; 229 return null;
216 } 230 }
217 231
218 OpenFile* load = new OpenFile; 232 OpenFile* load = new OpenFile;
219 load->m_filename = path; 233 load->m_filename = path;
234
235 if (mainfile)
236 g_curfile = load;
237
220 ulong numWarnings; 238 ulong numWarnings;
221 std::vector<LDObject*> objs = loadFileContents (fp, &numWarnings); 239 std::vector<LDObject*> objs = loadFileContents (fp, &numWarnings);
222 240
223 for (LDObject* obj : objs) 241 for (LDObject* obj : objs)
224 load->m_objs.push_back (obj); 242 load->m_objs.push_back (obj);
344 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 362 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
345 // ============================================================================= 363 // =============================================================================
346 void openMainFile (str path) { 364 void openMainFile (str path) {
347 closeAll (); 365 closeAll ();
348 366
349 OpenFile* file = openDATFile (path, false); 367 OpenFile* file = openDATFile (path, false, true);
350 368
351 if (!file) { 369 if (!file) {
352 // Tell the user loading failed. 370 // Tell the user loading failed.
353 setlocale (LC_ALL, "C"); 371 setlocale (LC_ALL, "C");
354 critical (fmt ("Failed to open %s: %s", path.chars(), strerror (errno))); 372 critical (fmt ("Failed to open %s: %s", path.chars(), strerror (errno)));
618 // ============================================================================= 636 // =============================================================================
619 OpenFile* loadSubfile (str zFile) { 637 OpenFile* loadSubfile (str zFile) {
620 // Try open the file 638 // Try open the file
621 OpenFile* pFile = findLoadedFile (zFile); 639 OpenFile* pFile = findLoadedFile (zFile);
622 if (!pFile) 640 if (!pFile)
623 pFile = openDATFile (zFile, true); 641 pFile = openDATFile (zFile, true, false);
624 642
625 return pFile; 643 return pFile;
626 } 644 }
627 645
628 // ============================================================================= 646 // =============================================================================

mercurial