# HG changeset patch # User Santeri Piippo # Date 1363570731 -7200 # Node ID 69a91c1ff583fe90fc6e6182cdbc94d323d7b75a # Parent 335e430a6b4f188928dad2512729a2388b28b33c begin work on subfile caching diff -r 335e430a6b4f -r 69a91c1ff583 gui.cpp --- a/gui.cpp Sat Mar 16 17:50:13 2013 +0200 +++ b/gui.cpp Mon Mar 18 03:38:51 2013 +0200 @@ -200,10 +200,7 @@ str name = QFileDialog::getOpenFileName (this, "Open File", "", "LDraw files (*.dat *.ldr)").toStdString().c_str(); - if (g_LoadedFiles.size()) - closeModel (); - - IO_OpenLDrawFile (name); + openModel (name); } void LDForgeWindow::slot_save () { diff -r 335e430a6b4f -r 69a91c1ff583 icons/inline.png Binary file icons/inline.png has changed diff -r 335e430a6b4f -r 69a91c1ff583 io.cpp --- a/io.cpp Sat Mar 16 17:50:13 2013 +0200 +++ b/io.cpp Mon Mar 18 03:38:51 2013 +0200 @@ -6,21 +6,21 @@ #include "gui.h" #include "bbox.h" +vector g_zaFileLoadPaths; + // ============================================================================= // IO_FindLoadedFile (str) // // Returns a pointer to the first found open file with the given name. // ============================================================================= OpenFile* IO_FindLoadedFile (str name) { - OpenFile* file; - - for (uint i = 0; i < g_LoadedFiles.size(); i++) { - file = g_LoadedFiles[i]; - if (!file->zFileName.icompare (name)) + for (ulong i = 0; i < g_LoadedFiles.size(); i++) { + OpenFile* const file = g_LoadedFiles[i]; + if (file->zFileName == name) return file; } - return NULL; + return nullptr; } // ============================================================================= @@ -34,8 +34,19 @@ FILE* fp = fopen (path.chars (), "r"); if (!fp) { + for (ushort i = 0; i < g_zaFileLoadPaths.size(); ++i) { + str zFilePath = str::mkfmt ("%s/%s", g_zaFileLoadPaths[i].chars(), path.chars()); + printf ("try open %s\n", zFilePath.chars ()); + fp = fopen (zFilePath.chars (), "r"); + + if (fp) + break; + } + } + + if (!fp) { logf (LOG_Error, "Couldn't open %s: %s\n", path.chars (), strerror (errno)); - return NULL; + return nullptr; } OpenFile* load = new OpenFile; @@ -73,23 +84,15 @@ } g_LoadedFiles.push_back (load); - g_CurrentFile = g_LoadedFiles[g_LoadedFiles.size() - 1]; - - // Recalculate the bounding box - g_BBox.calculate(); - - // Rebuild the object tree view now. - g_qWindow->buildObjList (); - g_qWindow->setTitle (); logf (LOG_Success, "File %s parsed successfully (%lu warning%s).\n", path.chars(), numWarnings, PLURAL (numWarnings)); - return g_CurrentFile; + return load; } // ============================================================================= -// isNumber (char*) +// isNumber (char*) [static] // // Returns whether a given string represents a floating point number // TODO: Does LDraw support scientific notation? @@ -146,12 +149,16 @@ return new LDEmpty; } - char c = zLine[0]; vector tokens = zLine / " "; + // Rid leading all-whitespace tokens + while (tokens.size() && !(~tokens[0])) + tokens.erase (tokens.begin()); + if (~tokens[0] != 1) return new LDGibberish (zLine, "Illogical line code"); + char const c = tokens[0][0]; switch (c - '0') { case 0: { @@ -163,10 +170,23 @@ case 1: { + // Subfile CHECK_TOKEN_COUNT (15) CHECK_TOKEN_NUMBERS (1, 13) - // Subfile +#ifndef WIN32 + tokens[14].replace ("\\", "/"); +#endif // WIN32 + + // Try open the file + OpenFile* pFile = IO_FindLoadedFile (tokens[14]); + if (!pFile) + pFile = IO_OpenLDrawFile (tokens[14]); + + // If we cannot open the file, mark it an error + if (!pFile) + return new LDGibberish (zLine, "Could not open referred file"); + LDSubfile* obj = new LDSubfile; obj->dColor = atoi (tokens[1]); obj->vPosition = ParseVertex (zLine, 2); // 2 - 4 @@ -175,6 +195,7 @@ obj->faMatrix[i] = atof (tokens[i + 5]); // 5 - 13 obj->zFileName = tokens[14]; + obj->pFile = pFile; return obj; } diff -r 335e430a6b4f -r 69a91c1ff583 io.h --- a/io.h Sat Mar 16 17:50:13 2013 +0200 +++ b/io.h Mon Mar 18 03:38:51 2013 +0200 @@ -16,6 +16,7 @@ OpenFile* IO_OpenLDrawFile (str path); LDObject* ParseLine (str zLine); +extern vector g_zaFileLoadPaths; extern vector g_LoadedFiles; #endif \ No newline at end of file diff -r 335e430a6b4f -r 69a91c1ff583 ldforge.pro --- a/ldforge.pro Sat Mar 16 17:50:13 2013 +0200 +++ b/ldforge.pro Mon Mar 18 03:38:51 2013 +0200 @@ -6,6 +6,7 @@ TARGET = DEPENDPATH += . INCLUDEPATH += . +OBJECTS_DIR = build/ # Input HEADERS += bbox.h \ diff -r 335e430a6b4f -r 69a91c1ff583 ldtypes.h --- a/ldtypes.h Sat Mar 16 17:50:13 2013 +0200 +++ b/ldtypes.h Mon Mar 18 03:38:51 2013 +0200 @@ -118,6 +118,7 @@ vertex vPosition; // Position of the subpart double faMatrix[9]; // Transformation matrix for the subpart str zFileName; // Filename of the subpart + OpenFile* pFile; // Pointer to opened file for this subfile. nullptr if unopened. }; // ============================================================================= diff -r 335e430a6b4f -r 69a91c1ff583 main.cpp --- a/main.cpp Sat Mar 16 17:50:13 2013 +0200 +++ b/main.cpp Mon Mar 18 03:38:51 2013 +0200 @@ -10,11 +10,11 @@ bbox g_BBox; int main (int argc, char** argv) { - if (g_CurrentFile) { - printf ("bbox: (%.3f, %.3f, %.3f), (%.3f, %.3f, %.3f)\n", - FVERTEX (g_BBox.v0), FVERTEX (g_BBox.v1)); - printf ("%u objects\n", g_CurrentFile->objects.size()); - } + // TODO + g_zaFileLoadPaths.push_back ("."); + g_zaFileLoadPaths.push_back ("/home/arezey/ldraw/parts"); + g_zaFileLoadPaths.push_back ("/home/arezey/ldraw/parts/s"); + g_zaFileLoadPaths.push_back ("/home/arezey/ldraw/p"); QApplication app (argc, argv); LDForgeWindow* win = new LDForgeWindow; @@ -46,52 +46,52 @@ // Common code for the two logfs // ============================================================================= static void logVA (logtype_e eType, const char* fmt, va_list va) { + // Log it to standard output + vprintf (fmt, va); + + return; + str zText; char* sBuffer; - // Log it to standard output - vprintf (fmt, va); - sBuffer = vdynformat (fmt, va, 128); zText = sBuffer; delete[] sBuffer; - // Replace some things out with HTML entities zText.replace ("<", "<"); zText.replace (">", ">"); zText.replace ("\n", "
"); - str zHTML; + str* zpHTML = &g_qWindow->zMessageLogHTML; switch (eType) { case LOG_Normal: - zHTML = zText; + zpHTML->append (zText); break; case LOG_Error: - zHTML.format ("[ERROR] %s", + zpHTML->appendformat ("[ERROR] %s", zText.chars()); break; case LOG_Info: - zHTML.format ("[INFO] %s", + zpHTML->appendformat ("[INFO] %s", zText.chars()); break; case LOG_Success: - zHTML.format ("[SUCCESS] %s", + zpHTML->appendformat ("[SUCCESS] %s", zText.chars()); break; case LOG_Warning: - zHTML.format ("[WARNING] %s", + zpHTML->appendformat ("[WARNING] %s", zText.chars()); break; } - g_qWindow->zMessageLogHTML += zHTML; - g_qWindow->qMessageLog->setHtml (g_qWindow->zMessageLogHTML); + g_qWindow->qMessageLog->setHtml (*zpHTML); } diff -r 335e430a6b4f -r 69a91c1ff583 model.cpp --- a/model.cpp Sat Mar 16 17:50:13 2013 +0200 +++ b/model.cpp Mon Mar 18 03:38:51 2013 +0200 @@ -4,6 +4,7 @@ #include "io.h" #include "gui.h" #include "draw.h" +#include "bbox.h" // Clear everything from the model void closeModel () { @@ -37,6 +38,21 @@ g_qWindow->R->hardRefresh(); } +void openModel (str zPath) { + if (g_CurrentFile) + closeModel (); + + OpenFile* pFile = IO_OpenLDrawFile (zPath); + g_CurrentFile = pFile; + + // Recalculate the bounding box + g_BBox.calculate(); + + // Rebuild the object tree view now. + g_qWindow->buildObjList (); + g_qWindow->setTitle (); +} + void saveModel () { if (!g_CurrentFile) return; diff -r 335e430a6b4f -r 69a91c1ff583 model.h --- a/model.h Sat Mar 16 17:50:13 2013 +0200 +++ b/model.h Mon Mar 18 03:38:51 2013 +0200 @@ -1,3 +1,4 @@ void closeModel (); void newModel (); -void saveModel (); \ No newline at end of file +void saveModel (); +void openModel (str zPath); \ No newline at end of file