39 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
39 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
40 // ============================================================================= |
40 // ============================================================================= |
41 OpenFile* openDATFile (str path) { |
41 OpenFile* openDATFile (str path) { |
42 logf ("Opening %s...\n", path.chars()); |
42 logf ("Opening %s...\n", path.chars()); |
43 |
43 |
|
44 // Convert the file name to lowercase since some parts contain uppercase |
|
45 // file names. I'll assume here that the library will always use lowercase |
|
46 // file names for the actual parts.. |
|
47 str zTruePath = -path; |
|
48 #ifndef WIN32 |
|
49 zTruePath.replace ("\\", "/"); |
|
50 #endif // WIN32 |
|
51 |
44 FILE* fp = fopen (path.chars (), "r"); |
52 FILE* fp = fopen (path.chars (), "r"); |
45 |
53 |
46 if (!fp && ~io_ldpath.value) { |
54 if (!fp && ~io_ldpath.value) { |
47 char const* saSubdirectories[] = { |
55 char const* saSubdirectories[] = { |
48 "parts", |
56 "parts", |
49 "p", |
57 "p", |
50 }; |
58 }; |
51 |
59 |
52 for (ushort i = 0; i < sizeof saSubdirectories / sizeof *saSubdirectories; ++i) { |
60 for (ushort i = 0; i < sizeof saSubdirectories / sizeof *saSubdirectories; ++i) { |
53 str zFilePath = str::mkfmt ("%s" DIRSLASH "%s" DIRSLASH "%s", |
61 str zFilePath = str::mkfmt ("%s" DIRSLASH "%s" DIRSLASH "%s", |
54 io_ldpath.value.chars(), saSubdirectories[i], path.chars()); |
62 io_ldpath.value.chars(), saSubdirectories[i], zTruePath.chars()); |
55 |
63 |
56 printf ("trying %s...\n", zFilePath.chars ()); |
|
57 fp = fopen (zFilePath.chars (), "r"); |
64 fp = fopen (zFilePath.chars (), "r"); |
58 |
65 |
59 if (fp) |
66 if (fp) |
60 break; |
67 break; |
61 } |
68 } |
111 // ============================================================================= |
118 // ============================================================================= |
112 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
119 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
113 // ============================================================================= |
120 // ============================================================================= |
114 // Clear everything from the model |
121 // Clear everything from the model |
115 void OpenFile::close () { |
122 void OpenFile::close () { |
116 FOREACH (LDObject, *, obj, objects) |
123 for (ulong j = 0; j < objects.size(); ++j) |
117 delete obj; |
124 delete objects[j]; |
118 |
125 |
119 delete this; |
126 delete this; |
120 } |
127 } |
121 |
128 |
122 // ============================================================================= |
129 // ============================================================================= |
125 void closeAll () { |
132 void closeAll () { |
126 if (!g_LoadedFiles.size()) |
133 if (!g_LoadedFiles.size()) |
127 return; |
134 return; |
128 |
135 |
129 // Remove all loaded files and the objects they contain |
136 // Remove all loaded files and the objects they contain |
130 FOREACH (OpenFile, *, f, g_LoadedFiles) |
137 for (ushort i = 0; i < g_LoadedFiles.size(); i++) { |
|
138 OpenFile* f = g_LoadedFiles[i]; |
131 f->close (); |
139 f->close (); |
|
140 } |
132 |
141 |
133 // Clear the array |
142 // Clear the array |
134 g_LoadedFiles.clear(); |
143 g_LoadedFiles.clear(); |
135 g_CurrentFile = NULL; |
144 g_CurrentFile = NULL; |
136 |
145 |
183 FILE* fp = fopen (zPath, "w"); |
192 FILE* fp = fopen (zPath, "w"); |
184 if (!fp) |
193 if (!fp) |
185 return false; |
194 return false; |
186 |
195 |
187 // Write all entries now |
196 // Write all entries now |
188 FOREACH (LDObject, *, obj, objects) { |
197 for (ulong i = 0; i < objects.size(); ++i) { |
|
198 LDObject* obj = objects[i]; |
|
199 |
189 // LDraw requires lines to have DOS line endings |
200 // LDraw requires lines to have DOS line endings |
190 str zLine = str::mkfmt ("%s\r\n",obj->getContents ().chars ()); |
201 str zLine = str::mkfmt ("%s\r\n",obj->getContents ().chars ()); |
191 |
202 |
192 fwrite (zLine.chars(), 1, ~zLine, fp); |
203 fwrite (zLine.chars(), 1, ~zLine, fp); |
193 } |
204 } |
271 { |
282 { |
272 // Subfile |
283 // Subfile |
273 CHECK_TOKEN_COUNT (15) |
284 CHECK_TOKEN_COUNT (15) |
274 CHECK_TOKEN_NUMBERS (1, 13) |
285 CHECK_TOKEN_NUMBERS (1, 13) |
275 |
286 |
276 #ifndef WIN32 |
|
277 tokens[14].replace ("\\", "/"); |
|
278 #endif // WIN32 |
|
279 |
|
280 // Try open the file |
287 // Try open the file |
281 OpenFile* pFile = loadSubfile (tokens[14]); |
288 OpenFile* pFile = loadSubfile (tokens[14]); |
282 |
289 |
283 // If we cannot open the file, mark it an error |
290 // If we cannot open the file, mark it an error |
284 if (!pFile) |
291 if (!pFile) |