src/file.cpp

changeset 227
e13cd69b16dd
parent 226
3dd9e63698cd
child 232
4e44c92e21dd
equal deleted inserted replaced
226:3dd9e63698cd 227:e13cd69b16dd
100 for (LDObject* obj : m_objCache) 100 for (LDObject* obj : m_objCache)
101 delete obj; 101 delete obj;
102 } 102 }
103 103
104 // ============================================================================= 104 // =============================================================================
105 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
106 // =============================================================================
107 OpenFile* findLoadedFile (str zName) { 105 OpenFile* findLoadedFile (str zName) {
108 for (OpenFile* file : g_loadedFiles) 106 for (OpenFile* file : g_loadedFiles)
109 if (file->m_filename == zName) 107 if (file->m_filename == zName)
110 return file; 108 return file;
111 109
112 return null; 110 return null;
113 } 111 }
114 112
115 // ============================================================================= 113 // =============================================================================
116 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 114 str dirname (str path) {
115 long lastpos; // FIXME: why doesn't str::last () work here?
116 for (lastpos = path.len () - 1; lastpos >= 0; --lastpos)
117 if (path[(size_t) lastpos] == DIRSLASH_CHAR)
118 break;
119
120 if (lastpos > 0)
121 return path.substr (0, lastpos);
122
123 return "";
124 }
125
117 // ============================================================================= 126 // =============================================================================
118 FILE* openLDrawFile (str relpath, bool subdirs) { 127 FILE* openLDrawFile (str relpath, bool subdirs) {
128 printf ("%s: Try to open %s\n", __func__, relpath.c ());
119 #ifndef WIN32 129 #ifndef WIN32
120 relpath.replace ("\\", "/"); 130 relpath.replace ("\\", "/");
121 #endif // WIN32 131 #endif // WIN32
122 132
123 if (g_curfile != null) { 133 if (g_curfile != null) {
124 long lastpos; 134 str partpath = fmt ("%s" DIRSLASH "%s", dirname (g_curfile->m_filename).c (), relpath.c ());
125 for (lastpos = g_curfile->m_filename.len () - 1; lastpos >= 0; --lastpos) 135 printf ("try %s\n", partpath.c ());
126 if (g_curfile->m_filename[(size_t) lastpos] == '/') 136 FILE* fp = fopen (partpath, "r");
127 break; 137
128 138 if (fp != null)
129 if (lastpos > 0) { 139 return fp;
130 str dirname = g_curfile->m_filename.substr (0, lastpos);
131 str partpath = fmt ("%s" DIRSLASH "%s", dirname.c (), relpath.c ());
132 printf ("try %s\n", partpath.c ());
133 FILE* fp = fopen (partpath, "r");
134
135 if (fp != null)
136 return fp;
137 }
138 } 140 }
139 141
140 printf ("try %s\n", relpath.chars ()); 142 printf ("try %s\n", relpath.chars ());
141 FILE* fp = fopen (relpath, "r"); 143 FILE* fp = fopen (relpath, "r");
142 str fullPath; 144 str fullPath;
265 { 267 {
266 case QMessageBox::Yes: 268 case QMessageBox::Yes:
267 // If we don't have a file path yet, we have to ask the user for one. 269 // If we don't have a file path yet, we have to ask the user for one.
268 if (m_filename.len () == 0) { 270 if (m_filename.len () == 0) {
269 str path = QFileDialog::getSaveFileName (g_win, "Save As", 271 str path = QFileDialog::getSaveFileName (g_win, "Save As",
270 "", "LDraw files (*.dat *.ldr)"); 272 g_curfile->m_filename, "LDraw files (*.dat *.ldr)");
271 273
272 if (path.len () == 0) 274 if (path.len () == 0)
273 return false; 275 return false;
274 276
275 m_filename = path; 277 m_filename = path;
418 if (!fp) { 420 if (!fp) {
419 lastError = errno; 421 lastError = errno;
420 return false; 422 return false;
421 } 423 }
422 424
425 // If the second object in the list holds the file name, update that now.
426 // Only do this if the file is explicitly open. If it's saved into a directory
427 // called "s" or "48", prepend that into the name.
428 LDComment* fpathComment = null;
429 if (m_implicit == false && m_objs.size () >= 2 && object (1)->getType () == LDObject::Comment) {
430 fpathComment = static_cast<LDComment*> (object (1));
431
432 if (fpathComment->text.substr (0, 6) == "Name: ") {
433 str newfname;
434 str dir = basename (dirname (path));
435
436 if (dir == "s" || dir == "48")
437 newfname = fmt ("%s\\", dir.c ());
438
439 newfname += basename (path);
440 fpathComment->text = fmt ("Name: %s", newfname.c ());
441 g_win->buildObjList ();
442 }
443 }
444
423 // Write all entries now 445 // Write all entries now
424 for (LDObject* obj : m_objs) { 446 for (LDObject* obj : m_objs) {
425 // LDraw requires lines to have DOS line endings 447 // LDraw requires files to have DOS line endings
426 str zLine = fmt ("%s\r\n", obj->getContents ().chars ()); 448 str line = fmt ("%s\r\n", obj->getContents ().chars ());
427 449 fwrite (line.chars(), 1, line.len (), fp);
428 fwrite (zLine.chars(), 1, ~zLine, fp);
429 } 450 }
430 451
431 fclose (fp); 452 fclose (fp);
432 453
433 // We have successfully saved, update the save position now. 454 // We have successfully saved, update the save position now.
434 savePos = History::pos (); 455 savePos = History::pos ();
456 m_filename = path;
435 457
436 g_win->updateTitle (); 458 g_win->updateTitle ();
437
438 return true; 459 return true;
439 } 460 }
440 461
441 #define CHECK_TOKEN_COUNT(N) \ 462 #define CHECK_TOKEN_COUNT(N) \
442 if (tokens.size() != N) \ 463 if (tokens.size() != N) \

mercurial