file.cpp

changeset 29
55406ce7446e
parent 28
208adc847377
child 30
31ff9aabd506
equal deleted inserted replaced
28:208adc847377 29:55406ce7446e
4 #include "file.h" 4 #include "file.h"
5 #include "misc.h" 5 #include "misc.h"
6 #include "bbox.h" 6 #include "bbox.h"
7 #include "gui.h" 7 #include "gui.h"
8 8
9 vector<str> g_zaFileLoadPaths; 9 // =============================================================================
10 10 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
11 // ============================================================================= 11 // =============================================================================
12 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 12 OpenFile* findLoadedFile (str zName) {
13 // ============================================================================= 13 printf ("finding %s in files\n", zName.chars());
14 OpenFile* findLoadedFile (str name) { 14
15 for (ulong i = 0; i < g_LoadedFiles.size(); i++) { 15 for (ulong i = 0; i < g_LoadedFiles.size(); i++) {
16 OpenFile* const file = g_LoadedFiles[i]; 16 OpenFile* const file = g_LoadedFiles[i];
17 if (file->zFileName == name) 17 printf ("%s <-> %s\n", file->zFileName.chars(), zName.chars());
18 if (file->zFileName == zName)
18 return file; 19 return file;
19 } 20 }
20 21
22 printf ("failed to find given file\n");
21 return nullptr; 23 return nullptr;
22 } 24 }
23 25
24 // ============================================================================= 26 // =============================================================================
25 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 27 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
27 OpenFile* openDATFile (str path) { 29 OpenFile* openDATFile (str path) {
28 logf ("Opening %s...\n", path.chars()); 30 logf ("Opening %s...\n", path.chars());
29 31
30 FILE* fp = fopen (path.chars (), "r"); 32 FILE* fp = fopen (path.chars (), "r");
31 33
32 if (!fp) { 34 if (!fp && ~io_ldpath.value) {
33 for (ushort i = 0; i < g_zaFileLoadPaths.size(); ++i) { 35 char const* saSubdirectories[] = {
34 str zFilePath = str::mkfmt ("%s/%s", g_zaFileLoadPaths[i].chars(), path.chars()); 36 "parts",
37 "p",
38 };
39
40 for (ushort i = 0; i < sizeof saSubdirectories / sizeof *saSubdirectories; ++i) {
41 str zFilePath = str::mkfmt ("%s" DIRSLASH "%s" DIRSLASH "%s",
42 io_ldpath.value.chars(), saSubdirectories[i], path.chars());
43
44 printf ("trying %s...\n", zFilePath.chars ());
35 fp = fopen (zFilePath.chars (), "r"); 45 fp = fopen (zFilePath.chars (), "r");
36 46
37 if (fp) 47 if (fp)
38 break; 48 break;
39 } 49 }
112 122
113 // Clear the array 123 // Clear the array
114 g_LoadedFiles.clear(); 124 g_LoadedFiles.clear();
115 g_CurrentFile = NULL; 125 g_CurrentFile = NULL;
116 126
117 g_qWindow->R->hardRefresh(); 127 g_qWindow->refresh ();
118 } 128 }
119 129
120 // ============================================================================= 130 // =============================================================================
121 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 131 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
122 // ============================================================================= 132 // =============================================================================
128 f->zFileName = ""; 138 f->zFileName = "";
129 g_LoadedFiles.push_back (f); 139 g_LoadedFiles.push_back (f);
130 g_CurrentFile = f; 140 g_CurrentFile = f;
131 141
132 g_BBox.calculate(); 142 g_BBox.calculate();
133 g_qWindow->buildObjList (); 143 g_qWindow->refresh ();
134 g_qWindow->R->hardRefresh();
135 } 144 }
136 145
137 // ============================================================================= 146 // =============================================================================
138 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 147 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
139 // ============================================================================= 148 // =============================================================================
145 154
146 // Recalculate the bounding box 155 // Recalculate the bounding box
147 g_BBox.calculate(); 156 g_BBox.calculate();
148 157
149 // Rebuild the object tree view now. 158 // Rebuild the object tree view now.
150 g_qWindow->buildObjList (); 159 g_qWindow->refresh ();
151 g_qWindow->setTitle ();
152 } 160 }
153 161
154 // ============================================================================= 162 // =============================================================================
155 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 163 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
156 // ============================================================================= 164 // =============================================================================
225 #ifndef WIN32 233 #ifndef WIN32
226 tokens[14].replace ("\\", "/"); 234 tokens[14].replace ("\\", "/");
227 #endif // WIN32 235 #endif // WIN32
228 236
229 // Try open the file 237 // Try open the file
230 OpenFile* pFile = findLoadedFile (tokens[14]); 238 OpenFile* pFile = loadSubfile (tokens[14]);
231 if (!pFile)
232 pFile = openDATFile (tokens[14]);
233 239
234 // If we cannot open the file, mark it an error 240 // If we cannot open the file, mark it an error
235 if (!pFile) 241 if (!pFile)
236 return new LDGibberish (zLine, "Could not open referred file"); 242 return new LDGibberish (zLine, "Could not open referred file");
237 243
309 315
310 default: // Strange line we couldn't parse 316 default: // Strange line we couldn't parse
311 return new LDGibberish (zLine, "Unknown line code number"); 317 return new LDGibberish (zLine, "Unknown line code number");
312 } 318 }
313 } 319 }
320
321 // =============================================================================
322 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
323 // =============================================================================
324 OpenFile* loadSubfile (str zFile) {
325 // Try open the file
326 OpenFile* pFile = findLoadedFile (zFile);
327 if (!pFile)
328 pFile = openDATFile (zFile);
329
330 return pFile;
331 }
332
333 // =============================================================================
334 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
335 // =============================================================================
336 void reloadAllSubfiles () {
337 // First, close all but the current open file.
338 for (ushort i = 0; i < g_LoadedFiles.size(); ++i)
339 if (g_LoadedFiles[i] != g_CurrentFile)
340 g_LoadedFiles[i]->close();
341
342 g_LoadedFiles.clear ();
343 g_LoadedFiles.push_back (g_CurrentFile);
344
345 // Go through all the current file and reload the subfiles
346 for (ulong i = 0; i < g_CurrentFile->objects.size(); ++i) {
347 LDObject* obj = g_CurrentFile->objects[i];
348
349 // Reload subfiles
350 if (obj->getType() == OBJ_Subfile) {
351 LDSubfile* ref = static_cast<LDSubfile*> (obj);
352 OpenFile* pFile = loadSubfile (ref->zFileName);
353
354 if (pFile)
355 ref->pFile = pFile;
356 else {
357 // Couldn't load the file, mark it an error
358 ref->replace (new LDGibberish (ref->getContents (), "Could not open referred file"));
359 }
360 }
361
362 // Reparse gibberish files. It could be that they are invalid because
363 // the file could not be opened. Circumstances may be different now.
364 if (obj->getType() == OBJ_Gibberish)
365 obj->replace (parseLine (static_cast<LDGibberish*> (obj)->zContents));
366 }
367 }

mercurial