src/file.cpp

changeset 232
4e44c92e21dd
parent 227
e13cd69b16dd
child 234
79b120b0aa51
equal deleted inserted replaced
231:6ee9917b79f8 232:4e44c92e21dd
83 str prims () { return pathInfo.primsPath; } 83 str prims () { return pathInfo.primsPath; }
84 str parts () { return pathInfo.partsPath; } 84 str parts () { return pathInfo.partsPath; }
85 } 85 }
86 86
87 // ============================================================================= 87 // =============================================================================
88 OpenFile::OpenFile () { 88 LDOpenFile::LDOpenFile () {
89 m_implicit = true; 89 m_implicit = true;
90 savePos = -1; 90 savePos = -1;
91 } 91 }
92 92
93 // ============================================================================= 93 // =============================================================================
94 OpenFile::~OpenFile () { 94 LDOpenFile::~LDOpenFile () {
95 // Clear everything from the model 95 // Clear everything from the model
96 for (LDObject* obj : m_objs) 96 for (LDObject* obj : m_objs)
97 delete obj; 97 delete obj;
98 98
99 // Clear the cache as well 99 // Clear the cache as well
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 OpenFile* findLoadedFile (str zName) { 105 LDOpenFile* findLoadedFile (str zName) {
106 for (OpenFile* file : g_loadedFiles) 106 for (LDOpenFile* file : g_loadedFiles)
107 if (file->m_filename == zName) 107 if (file->m_filename == zName)
108 return file; 108 return file;
109 109
110 return null; 110 return null;
111 } 111 }
212 } 212 }
213 213
214 // ============================================================================= 214 // =============================================================================
215 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 215 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
216 // ============================================================================= 216 // =============================================================================
217 OpenFile* openDATFile (str path, bool search, bool mainfile) { 217 LDOpenFile* openDATFile (str path, bool search, bool mainfile) {
218 logf ("Opening %s...\n", path.chars()); 218 logf ("Opening %s...\n", path.chars());
219 219
220 // Convert the file name to lowercase since some parts contain uppercase 220 // Convert the file name to lowercase since some parts contain uppercase
221 // file names. I'll assume here that the library will always use lowercase 221 // file names. I'll assume here that the library will always use lowercase
222 // file names for the actual parts.. 222 // file names for the actual parts..
229 if (!fp) { 229 if (!fp) {
230 logf (LOG_Error, "Couldn't open %s: %s\n", path.chars (), strerror (errno)); 230 logf (LOG_Error, "Couldn't open %s: %s\n", path.chars (), strerror (errno));
231 return null; 231 return null;
232 } 232 }
233 233
234 OpenFile* load = new OpenFile; 234 LDOpenFile* load = new LDOpenFile;
235 load->m_filename = path; 235 load->m_filename = path;
236 236
237 if (mainfile) 237 if (mainfile)
238 g_curfile = load; 238 g_curfile = load;
239 239
253 } 253 }
254 254
255 // ============================================================================= 255 // =============================================================================
256 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 256 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
257 // ============================================================================= 257 // =============================================================================
258 bool OpenFile::safeToClose () { 258 bool LDOpenFile::safeToClose () {
259 setlocale (LC_ALL, "C"); 259 setlocale (LC_ALL, "C");
260 260
261 // If we have unsaved changes, warn and give the option of saving. 261 // If we have unsaved changes, warn and give the option of saving.
262 if (!m_implicit && History::pos () != savePos) { 262 if (!m_implicit && History::pos () != savePos) {
263 switch (QMessageBox::question (g_win, "Unsaved Changes", 263 switch (QMessageBox::question (g_win, "Unsaved Changes",
307 void closeAll () { 307 void closeAll () {
308 if (!g_loadedFiles.size()) 308 if (!g_loadedFiles.size())
309 return; 309 return;
310 310
311 // Remove all loaded files and the objects they contain 311 // Remove all loaded files and the objects they contain
312 for (OpenFile* file : g_loadedFiles) 312 for (LDOpenFile* file : g_loadedFiles)
313 delete file; 313 delete file;
314 314
315 // Clear the array 315 // Clear the array
316 g_loadedFiles.clear(); 316 g_loadedFiles.clear();
317 g_curfile = NULL; 317 g_curfile = NULL;
324 // ============================================================================= 324 // =============================================================================
325 void newFile () { 325 void newFile () {
326 // Create a new anonymous file and set it to our current 326 // Create a new anonymous file and set it to our current
327 closeAll (); 327 closeAll ();
328 328
329 OpenFile* f = new OpenFile; 329 LDOpenFile* f = new LDOpenFile;
330 f->m_filename = ""; 330 f->m_filename = "";
331 f->m_implicit = false; 331 f->m_implicit = false;
332 g_loadedFiles.push_back (f); 332 g_loadedFiles.push_back (f);
333 g_curfile = f; 333 g_curfile = f;
334 334
380 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 380 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
381 // ============================================================================= 381 // =============================================================================
382 void openMainFile (str path) { 382 void openMainFile (str path) {
383 closeAll (); 383 closeAll ();
384 384
385 OpenFile* file = openDATFile (path, false, true); 385 LDOpenFile* file = openDATFile (path, false, true);
386 386
387 if (!file) { 387 if (!file) {
388 // Tell the user loading failed. 388 // Tell the user loading failed.
389 setlocale (LC_ALL, "C"); 389 setlocale (LC_ALL, "C");
390 critical (fmt ("Failed to open %s: %s", path.chars(), strerror (errno))); 390 critical (fmt ("Failed to open %s: %s", path.chars(), strerror (errno)));
409 } 409 }
410 410
411 // ============================================================================= 411 // =============================================================================
412 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 412 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
413 // ============================================================================= 413 // =============================================================================
414 bool OpenFile::save (str path) { 414 bool LDOpenFile::save (str path) {
415 if (!~path) 415 if (!~path)
416 path = m_filename; 416 path = m_filename;
417 417
418 FILE* fp = fopen (path, "w"); 418 FILE* fp = fopen (path, "w");
419 419
585 // Subfile 585 // Subfile
586 CHECK_TOKEN_COUNT (15) 586 CHECK_TOKEN_COUNT (15)
587 CHECK_TOKEN_NUMBERS (1, 13) 587 CHECK_TOKEN_NUMBERS (1, 13)
588 588
589 // Try open the file 589 // Try open the file
590 OpenFile* pFile = loadSubfile (tokens[14]); 590 LDOpenFile* pFile = loadSubfile (tokens[14]);
591 591
592 // If we cannot open the file, mark it an error 592 // If we cannot open the file, mark it an error
593 if (!pFile) 593 if (!pFile)
594 return new LDGibberish (line, "Could not open referred file"); 594 return new LDGibberish (line, "Could not open referred file");
595 595
669 } 669 }
670 670
671 // ============================================================================= 671 // =============================================================================
672 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 672 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
673 // ============================================================================= 673 // =============================================================================
674 OpenFile* loadSubfile (str zFile) { 674 LDOpenFile* loadSubfile (str zFile) {
675 // Try open the file 675 // Try open the file
676 OpenFile* pFile = findLoadedFile (zFile); 676 LDOpenFile* pFile = findLoadedFile (zFile);
677 if (!pFile) 677 if (!pFile)
678 pFile = openDATFile (zFile, true, false); 678 pFile = openDATFile (zFile, true, false);
679 679
680 return pFile; 680 return pFile;
681 } 681 }
686 void reloadAllSubfiles () { 686 void reloadAllSubfiles () {
687 if (!g_curfile) 687 if (!g_curfile)
688 return; 688 return;
689 689
690 // First, close all but the current open file. 690 // First, close all but the current open file.
691 for (OpenFile* file : g_loadedFiles) 691 for (LDOpenFile* file : g_loadedFiles)
692 if (file != g_curfile) 692 if (file != g_curfile)
693 delete file; 693 delete file;
694 694
695 g_loadedFiles.clear (); 695 g_loadedFiles.clear ();
696 g_loadedFiles.push_back (g_curfile); 696 g_loadedFiles.push_back (g_curfile);
698 // Go through all objects in the current file and reload the subfiles 698 // Go through all objects in the current file and reload the subfiles
699 for (LDObject* obj : g_curfile->m_objs) { 699 for (LDObject* obj : g_curfile->m_objs) {
700 if (obj->getType() == LDObject::Subfile) { 700 if (obj->getType() == LDObject::Subfile) {
701 // Note: ref->pFile is invalid right now since all subfiles were closed. 701 // Note: ref->pFile is invalid right now since all subfiles were closed.
702 LDSubfile* ref = static_cast<LDSubfile*> (obj); 702 LDSubfile* ref = static_cast<LDSubfile*> (obj);
703 OpenFile* pFile = loadSubfile (ref->fileName); 703 LDOpenFile* pFile = loadSubfile (ref->fileName);
704 704
705 if (pFile) 705 if (pFile)
706 ref->fileInfo = pFile; 706 ref->fileInfo = pFile;
707 else { 707 else {
708 // Couldn't load the file, mark it an error 708 // Couldn't load the file, mark it an error
718 } 718 }
719 719
720 // ============================================================================= 720 // =============================================================================
721 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 721 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
722 // ============================================================================= 722 // =============================================================================
723 ulong OpenFile::addObject (LDObject* obj) { 723 ulong LDOpenFile::addObject (LDObject* obj) {
724 m_objs.push_back (obj); 724 m_objs.push_back (obj);
725 725
726 if (this == g_curfile) 726 if (this == g_curfile)
727 g_BBox.calcObject (obj); 727 g_BBox.calcObject (obj);
728 728
730 } 730 }
731 731
732 // ============================================================================= 732 // =============================================================================
733 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 733 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
734 // ============================================================================= 734 // =============================================================================
735 void OpenFile::insertObj (const ulong pos, LDObject* obj) { 735 void LDOpenFile::insertObj (const ulong pos, LDObject* obj) {
736 m_objs.insert (m_objs.begin () + pos, obj); 736 m_objs.insert (m_objs.begin () + pos, obj);
737 737
738 if (this == g_curfile) 738 if (this == g_curfile)
739 g_BBox.calcObject (obj); 739 g_BBox.calcObject (obj);
740 } 740 }
741 741
742 // ============================================================================= 742 // =============================================================================
743 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 743 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
744 // ============================================================================= 744 // =============================================================================
745 void OpenFile::forgetObject (LDObject* obj) { 745 void LDOpenFile::forgetObject (LDObject* obj) {
746 // Find the index for the given object 746 // Find the index for the given object
747 ulong ulIndex; 747 ulong ulIndex;
748 for (ulIndex = 0; ulIndex < (ulong)m_objs.size(); ++ulIndex) 748 for (ulIndex = 0; ulIndex < (ulong)m_objs.size(); ++ulIndex)
749 if (m_objs[ulIndex] == obj) 749 if (m_objs[ulIndex] == obj)
750 break; // found it 750 break; // found it
815 } 815 }
816 } 816 }
817 817
818 // ============================================================================= 818 // =============================================================================
819 bool safeToCloseAll () { 819 bool safeToCloseAll () {
820 for (OpenFile* f : g_loadedFiles) 820 for (LDOpenFile* f : g_loadedFiles)
821 if (!f->safeToClose ()) 821 if (!f->safeToClose ())
822 return false; 822 return false;
823 823
824 return true; 824 return true;
825 } 825 }

mercurial