Mon, 18 Mar 2013 12:15:23 +0200
Deleted scanner.cpp (don't need it), merged model.cpp into io.cpp. Renamed LDForgeWindow to just ForgeWindow since I want the LD* prefix only be given to LDObject derivatives.
--- a/common.h Mon Mar 18 04:03:05 2013 +0200 +++ b/common.h Mon Mar 18 12:15:23 2013 +0200 @@ -31,7 +31,7 @@ using std::vector; -class LDForgeWindow; +class ForgeWindow; class LDObject; class bbox; class OpenFile; @@ -114,7 +114,7 @@ extern OpenFile* g_CurrentFile; extern bbox g_BBox; -extern LDForgeWindow* g_qWindow; +extern ForgeWindow* g_qWindow; extern vector<OpenFile*> g_LoadedFiles; #ifndef unix
--- a/gui.cpp Mon Mar 18 04:03:05 2013 +0200 +++ b/gui.cpp Mon Mar 18 12:15:23 2013 +0200 @@ -3,12 +3,11 @@ #include "common.h" #include "draw.h" #include "gui.h" -#include "model.h" #include "io.h" #include "zz_setContentsDialog.h" -LDForgeWindow::LDForgeWindow () { +ForgeWindow::ForgeWindow () { R = new renderer; qObjList = new QTreeWidget; @@ -47,7 +46,7 @@ qAct_##OBJECT->setStatusTip (tr (DESCR)); \ connect (qAct_##OBJECT, SIGNAL (triggered ()), this, SLOT (slot_##OBJECT ())); -void LDForgeWindow::createMenuActions () { +void ForgeWindow::createMenuActions () { // Long menu names go here so my cool action definition table doesn't get out of proportions const char* sNewCdLineText = "New Conditional Line", *sNewQuadText = "New Quadrilateral", @@ -109,7 +108,7 @@ qaDisabledActions[i]->setEnabled (false); } -void LDForgeWindow::createMenus () { +void ForgeWindow::createMenus () { // File menu qFileMenu = menuBar ()->addMenu (tr ("&File")); qFileMenu->addAction (qAct_new); // New @@ -145,7 +144,7 @@ qHelpMenu->addAction (qAct_aboutQt); // About Qt } -void LDForgeWindow::createToolbars () { +void ForgeWindow::createToolbars () { qFileToolBar = new QToolBar ("File"); qFileToolBar->addAction (qAct_new); qFileToolBar->addAction (qAct_open); @@ -174,7 +173,7 @@ addToolBar (qEditToolBar); } -void LDForgeWindow::setTitle () { +void ForgeWindow::setTitle () { str zTitle = APPNAME_DISPLAY " v" VERSION_STRING; // Append our current file if we have one @@ -193,38 +192,34 @@ setWindowTitle (zTitle.chars()); } -void LDForgeWindow::slot_new () { - printf ("new file\n"); - - closeModel (); - newModel (); +void ForgeWindow::slot_new () { + newFile (); } -void LDForgeWindow::slot_open () { +void ForgeWindow::slot_open () { str name = QFileDialog::getOpenFileName (this, "Open File", "", "LDraw files (*.dat *.ldr)").toStdString().c_str(); - openModel (name); -} - -void LDForgeWindow::slot_save () { - saveModel (); + openMainFile (name); } -void LDForgeWindow::slot_saveAs () { - printf ("save as file\n"); +void ForgeWindow::slot_save () { + g_CurrentFile->save (); } -void LDForgeWindow::slot_exit () { - printf ("exit\n"); +void ForgeWindow::slot_saveAs () { + +} + +void ForgeWindow::slot_exit () { exit (0); } -void LDForgeWindow::slot_newSubfile () { +void ForgeWindow::slot_newSubfile () { } -void LDForgeWindow::slot_newLine () { +void ForgeWindow::slot_newLine () { LDLine* line = new LDLine; const ulong ulSpot = getInsertionPoint (); @@ -237,55 +232,55 @@ R->hardRefresh (); } -void LDForgeWindow::slot_newTriangle () { +void ForgeWindow::slot_newTriangle () { } -void LDForgeWindow::slot_newQuad () { +void ForgeWindow::slot_newQuad () { } -void LDForgeWindow::slot_newCondLine () { +void ForgeWindow::slot_newCondLine () { } -void LDForgeWindow::slot_newComment () { +void ForgeWindow::slot_newComment () { } -void LDForgeWindow::slot_about () { +void ForgeWindow::slot_about () { } -void LDForgeWindow::slot_aboutQt () { +void ForgeWindow::slot_aboutQt () { QMessageBox::aboutQt (this); } -void LDForgeWindow::slot_cut () { +void ForgeWindow::slot_cut () { } -void LDForgeWindow::slot_copy () { +void ForgeWindow::slot_copy () { } -void LDForgeWindow::slot_paste () { +void ForgeWindow::slot_paste () { } -void LDForgeWindow::slot_newVector () { +void ForgeWindow::slot_newVector () { } -void LDForgeWindow::slot_newVertex () { +void ForgeWindow::slot_newVertex () { } -void LDForgeWindow::slot_inline () { +void ForgeWindow::slot_inline () { } -void LDForgeWindow::slot_splitQuads () { +void ForgeWindow::slot_splitQuads () { if (qObjList->selectedItems().size() == 0) return; @@ -321,7 +316,7 @@ R->hardRefresh (); } -void LDForgeWindow::slot_setContents () { +void ForgeWindow::slot_setContents () { if (qObjList->selectedItems().size() != 1) return; @@ -379,7 +374,7 @@ return QIcon (); } -void LDForgeWindow::buildObjList () { +void ForgeWindow::buildObjList () { if (!g_CurrentFile) return; @@ -389,7 +384,6 @@ for (ulong i = 0; i < g_CurrentFile->objects.size(); ++i) { LDObject* obj = g_CurrentFile->objects[i]; - printf ("%lu: %p\n", i, obj); str zText; switch (obj->getType ()) { @@ -471,7 +465,7 @@ qObjList->insertTopLevelItems (0, qaItems); } -void LDForgeWindow::slot_selectionChanged () { +void ForgeWindow::slot_selectionChanged () { // If the selection isn't 1 exact, disable setting contents qAct_setContents->setEnabled (qObjList->selectedItems().size() == 1); @@ -484,7 +478,7 @@ // // Returns the index of where a new item should be placed at. // ============================================================================= -ulong LDForgeWindow::getInsertionPoint () { +ulong ForgeWindow::getInsertionPoint () { ulong ulIndex; if (qObjList->selectedItems().size() == 1) {
--- a/gui.h Mon Mar 18 04:03:05 2013 +0200 +++ b/gui.h Mon Mar 18 12:15:23 2013 +0200 @@ -10,7 +10,7 @@ #include <QTextEdit> #include "draw.h" -class LDForgeWindow : public QMainWindow { +class ForgeWindow : public QMainWindow { Q_OBJECT public: @@ -37,7 +37,7 @@ QAction* qAct_splitQuads, *qAct_setContents, *qAct_inline; QAction* qAct_about, *qAct_aboutQt; - LDForgeWindow (); + ForgeWindow (); void buildObjList (); void setTitle ();
--- a/io.cpp Mon Mar 18 04:03:05 2013 +0200 +++ b/io.cpp Mon Mar 18 12:15:23 2013 +0200 @@ -4,15 +4,14 @@ #include "io.h" #include "misc.h" #include "bbox.h" +#include "gui.h" vector<str> g_zaFileLoadPaths; // ============================================================================= -// IO_FindLoadedFile (str) -// -// Returns a pointer to the first found open file with the given name. +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // ============================================================================= -OpenFile* IO_FindLoadedFile (str name) { +OpenFile* findLoadedFile (str name) { for (ulong i = 0; i < g_LoadedFiles.size(); i++) { OpenFile* const file = g_LoadedFiles[i]; if (file->zFileName == name) @@ -23,11 +22,9 @@ } // ============================================================================= -// IO_OpenLDrawFile (str) -// -// Opens the given file and parses the LDraw code within. +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // ============================================================================= -OpenFile* IO_OpenLDrawFile (str path) { +OpenFile* openDATFile (str path) { logf ("Opening %s...\n", path.chars()); FILE* fp = fopen (path.chars (), "r"); @@ -69,10 +66,10 @@ fclose (fp); for (ulong i = 0; i < lines.size(); ++i) { - LDObject* obj = ParseLine (lines[i]); + LDObject* obj = parseLine (lines[i]); load->objects.push_back (obj); - // Check for warnings + // Check for parse errors and warn abotu tthem if (obj->getType() == OBJ_Gibberish) { logf (LOG_Warning, "Couldn't parse line #%lu: %s\n", i, static_cast<LDGibberish*> (obj)->zReason.chars()); @@ -90,44 +87,96 @@ } // ============================================================================= -// isNumber (char*) [static] -// -// Returns whether a given string represents a floating point number -// TODO: Does LDraw support scientific notation? +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // ============================================================================= -static bool isNumber (char* sToken) { - char* sPointer = &sToken[0]; - bool bGotDot = false; - - // Allow leading hyphen for negatives - if (*sPointer == '-') - sPointer++; +// Clear everything from the model +void OpenFile::close () { + for (ulong j = 0; j < objects.size(); ++j) + delete objects[j]; - while (*sPointer != '\0') { - if (*sPointer == '.' && !bGotDot) { - // Decimal point - bGotDot = true; - sPointer++; - continue; - } - - if (*sPointer >= '0' && *sPointer <= '9') { - sPointer++; - continue; // Digit - } - - // If the above cases didn't catch this character, it was - // illegal and this is therefore not a number. - return false; + delete this; +} + +// ============================================================================= +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +// ============================================================================= +void closeAll () { + if (!g_LoadedFiles.size()) + return; + + // Remove all loaded files and the objects they contain + for (ushort i = 0; i < g_LoadedFiles.size(); i++) { + OpenFile* f = g_LoadedFiles[i]; + f->close (); } - return true; + // Clear the array + g_LoadedFiles.clear(); + g_CurrentFile = NULL; + + g_qWindow->R->hardRefresh(); } // ============================================================================= -// ParseLine (str) -// -// Parses a string line containing an LDraw object and returns the object parsed. +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +// ============================================================================= +void newFile () { + // Create a new anonymous file and set it to our current + closeAll (); + + OpenFile* f = new OpenFile; + f->zFileName = ""; + g_LoadedFiles.push_back (f); + g_CurrentFile = f; + + g_BBox.calculate(); + g_qWindow->buildObjList (); + g_qWindow->R->hardRefresh(); +} + +// ============================================================================= +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +// ============================================================================= +void openMainFile (str zPath) { + closeAll (); + + OpenFile* pFile = openDATFile (zPath); + g_CurrentFile = pFile; + + // Recalculate the bounding box + g_BBox.calculate(); + + // Rebuild the object tree view now. + g_qWindow->buildObjList (); + g_qWindow->setTitle (); +} + +// ============================================================================= +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +// ============================================================================= +void OpenFile::save (str zPath) { + if (!~zPath) + zPath = zFileName; + + FILE* fp = fopen (zPath, "w"); + if (!fp) + return; + + // Write all entries now + for (ulong i = 0; i < objects.size(); ++i) { + LDObject* obj = objects[i]; + + // LDraw requires lines to have DOS line endings + str zLine = str::mkfmt ("%s\r\n",obj->getContents ().chars ()); + + fwrite (zLine.chars(), 1, ~zLine, fp); + } + + fclose (fp); +} + +// ============================================================================= +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // ============================================================================= #define CHECK_TOKEN_COUNT(N) \ if (tokens.size() != N) \ @@ -139,9 +188,9 @@ return new LDGibberish (zLine, str::mkfmt ("Token #%u was `%s`, expected a number", \ (i + 1), tokens[i].chars())); -LDObject* ParseLine (str zLine) { +LDObject* parseLine (str zLine) { str zNoWhitespace = zLine; - StripWhitespace (zNoWhitespace); + stripWhitespace (zNoWhitespace); if (!~zNoWhitespace) { // Line was empty, or only consisted of whitespace return new LDEmpty; @@ -177,9 +226,9 @@ #endif // WIN32 // Try open the file - OpenFile* pFile = IO_FindLoadedFile (tokens[14]); + OpenFile* pFile = findLoadedFile (tokens[14]); if (!pFile) - pFile = IO_OpenLDrawFile (tokens[14]); + pFile = openDATFile (tokens[14]); // If we cannot open the file, mark it an error if (!pFile) @@ -187,7 +236,7 @@ LDSubfile* obj = new LDSubfile; obj->dColor = atoi (tokens[1]); - obj->vPosition = ParseVertex (zLine, 2); // 2 - 4 + obj->vPosition = parseVertex (zLine, 2); // 2 - 4 for (short i = 0; i < 9; ++i) obj->faMatrix[i] = atof (tokens[i + 5]); // 5 - 13 @@ -204,9 +253,9 @@ // Line LDLine* obj = new LDLine; - obj->dColor = GetWordInt (zLine, 1); + obj->dColor = getWordInt (zLine, 1); for (short i = 0; i < 2; ++i) - obj->vaCoords[i] = ParseVertex (zLine, 2 + (i * 3)); // 2 - 7 + obj->vaCoords[i] = parseVertex (zLine, 2 + (i * 3)); // 2 - 7 return obj; } @@ -217,10 +266,10 @@ // Triangle LDTriangle* obj = new LDTriangle; - obj->dColor = GetWordInt (zLine, 1); + obj->dColor = getWordInt (zLine, 1); for (short i = 0; i < 3; ++i) - obj->vaCoords[i] = ParseVertex (zLine, 2 + (i * 3)); // 2 - 10 + obj->vaCoords[i] = parseVertex (zLine, 2 + (i * 3)); // 2 - 10 return obj; } @@ -232,10 +281,10 @@ // Quadrilateral LDQuad* obj = new LDQuad; - obj->dColor = GetWordInt (zLine, 1); + obj->dColor = getWordInt (zLine, 1); for (short i = 0; i < 4; ++i) - obj->vaCoords[i] = ParseVertex (zLine, 2 + (i * 3)); // 2 - 13 + obj->vaCoords[i] = parseVertex (zLine, 2 + (i * 3)); // 2 - 13 return obj; } @@ -247,13 +296,13 @@ // Conditional line LDCondLine* obj = new LDCondLine; - obj->dColor = GetWordInt (zLine, 1); + obj->dColor = getWordInt (zLine, 1); for (short i = 0; i < 2; ++i) - obj->vaCoords[i] = ParseVertex (zLine, 2 + (i * 3)); // 2 - 7 + obj->vaCoords[i] = parseVertex (zLine, 2 + (i * 3)); // 2 - 7 for (short i = 0; i < 2; ++i) - obj->vaControl[i] = ParseVertex (zLine, 8 + (i * 3)); // 8 - 13 + obj->vaControl[i] = parseVertex (zLine, 8 + (i * 3)); // 8 - 13 return obj; }
--- a/io.h Mon Mar 18 04:03:05 2013 +0200 +++ b/io.h Mon Mar 18 12:15:23 2013 +0200 @@ -5,16 +5,42 @@ #include "ldtypes.h" #include "str.h" +// ============================================================================= +// OpenFile +// +// The OpenFile class stores a file opened in LDForge either as a editable file +// for the user or for subfile caching. Its methods handle file input and output. +// ============================================================================= class OpenFile { public: str zFileName, zTitle; vector<LDObject*> objects; + + // Closes this OpenFile. The object is deleted in the process. + void close (); + + // Saves this file to disk. + void save (str zPath = ""); }; -// PROTOTYPES -OpenFile* IO_FindLoadedFile (str name); -OpenFile* IO_OpenLDrawFile (str path); -LDObject* ParseLine (str zLine); +// Close all current loaded files and start off blank. +void newFile (); + +// Opens the given file as the main file. Everything is closed first. +void openMainFile (str zPath); + +// Finds an OpenFile by name or nullptr if not open +OpenFile* findLoadedFile (str name); + +// Opens the given file and parses the LDraw code within. Returns a pointer +// to the opened file or nullptr on error. +OpenFile* openDATFile (str path); + +// Close all open files, whether user-opened or subfile caches. +void closeAll (); + +// Parses a string line containing an LDraw object and returns the object parsed. +LDObject* parseLine (str zLine); extern vector<str> g_zaFileLoadPaths; extern vector<OpenFile*> g_LoadedFiles;
--- a/ldforge.pro Mon Mar 18 04:03:05 2013 +0200 +++ b/ldforge.pro Mon Mar 18 12:15:23 2013 +0200 @@ -16,8 +16,6 @@ io.h \ ldtypes.h \ misc.h \ - model.h \ - scanner.h \ str.h \ config.h \ cfgdef.h \ @@ -30,8 +28,6 @@ ldtypes.cpp \ main.cpp \ misc.cpp \ - model.cpp \ - scanner.cpp \ str.cpp \ config.cpp \ zz_setContentsDialog.cpp
--- a/ldtypes.h Mon Mar 18 04:03:05 2013 +0200 +++ b/ldtypes.h Mon Mar 18 12:15:23 2013 +0200 @@ -19,8 +19,8 @@ // Object type codes // ============================================================================= enum LDObjectType_e { - OBJ_Unidentified, // Object is uninitialized (LDObject) - OBJ_Gibberish, // Object is unknown (LDUnknown; bad code number) + OBJ_Unidentified, // Object is an uninitialized (LDObject) (SHOULD NEVER HAPPEN) + OBJ_Gibberish, // Object is the result of failed parsing (LDGibberish) OBJ_Empty, // Object represents an empty line (LDEmpty) OBJ_Comment, // Object represents a comment (LDComment, code: 0) OBJ_Subfile, // Object represents a sub-file reference (LDSubfile, code: 1)
--- a/main.cpp Mon Mar 18 04:03:05 2013 +0200 +++ b/main.cpp Mon Mar 18 12:15:23 2013 +0200 @@ -6,18 +6,18 @@ vector<OpenFile*> g_LoadedFiles; OpenFile* g_CurrentFile = NULL; -LDForgeWindow* g_qWindow = NULL; +ForgeWindow* g_qWindow = NULL; bbox g_BBox; -int main (int argc, char** argv) { +int main (int dArgC, char* saArgV[]) { // 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; + QApplication app (dArgC, saArgV); + ForgeWindow* win = new ForgeWindow; g_qWindow = win; win->show (); return app.exec ();
--- a/misc.cpp Mon Mar 18 04:03:05 2013 +0200 +++ b/misc.cpp Mon Mar 18 12:15:23 2013 +0200 @@ -1,24 +1,24 @@ #include "common.h" #include <math.h> -double GetWordFloat (str& s, const ushort n) { +double getWordFloat (str& s, const ushort n) { return atof ((s / " ")[n]); } -long GetWordInt (str& s, const ushort n) { +long getWordInt (str& s, const ushort n) { return atol ((s / " ")[n]); } -vertex ParseVertex (str& s, const ushort n) { +vertex parseVertex (str& s, const ushort n) { vertex v; - v.x = GetWordFloat (s, n); - v.y = GetWordFloat (s, n + 1); - v.z = GetWordFloat (s, n + 2); + v.x = getWordFloat (s, n); + v.y = getWordFloat (s, n + 1); + v.z = getWordFloat (s, n + 2); return v; } -void StripWhitespace (str& s) { +void stripWhitespace (str& s) { str other; for (size_t i = 0; i < ~s; i++) @@ -54,4 +54,39 @@ zRep -= 1; return zRep; +} + +// ============================================================================= +// isNumber (str&) +// +// Returns whether a given string represents a floating point number +// TODO: Does LDraw support scientific notation? +// ============================================================================= +bool isNumber (str& zToken) { + char* cpPointer = &zToken[0]; + bool bGotDot = false; + + // Allow leading hyphen for negatives + if (*cpPointer == '-') + cpPointer++; + + while (*cpPointer != '\0') { + if (*cpPointer == '.' && !bGotDot) { + // Decimal point + bGotDot = true; + cpPointer++; + continue; + } + + if (*cpPointer >= '0' && *cpPointer <= '9') { + cpPointer++; + continue; // Digit + } + + // If the above cases didn't catch this character, it was + // illegal and this is therefore not a number. + return false; + } + + return true; } \ No newline at end of file
--- a/misc.h Mon Mar 18 04:03:05 2013 +0200 +++ b/misc.h Mon Mar 18 12:15:23 2013 +0200 @@ -8,10 +8,11 @@ return (zString / " ")[ulIndex]; } -double GetWordFloat (str& s, const ushort n); -long GetWordInt (str& s, const ushort n); -vertex ParseVertex (str& s, const ushort n); -void StripWhitespace (str& s); +double getWordFloat (str& s, const ushort n); +long getWordInt (str& s, const ushort n); +vertex parseVertex (str& s, const ushort n); +void stripWhitespace (str& s); +bool isNumber (str& zToken); // Float to string str ftoa (double fCoord);
--- a/model.cpp Mon Mar 18 04:03:05 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,75 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include "common.h" -#include "io.h" -#include "gui.h" -#include "draw.h" -#include "bbox.h" - -// Clear everything from the model -void closeModel () { - // Remove all loaded files and the objects they contain - for (ushort i = 0; i < g_LoadedFiles.size(); i++) { - OpenFile* f = g_LoadedFiles[i]; - - for (ushort j = 0; j < f->objects.size(); ++j) - delete (LDObject*)f->objects[j]; - - delete f; - } - - // Clear the array - g_LoadedFiles.clear(); - g_CurrentFile = NULL; - - g_qWindow->R->hardRefresh(); -} - -void newModel () { - // Create a new anonymous file and set it to our current - if (g_LoadedFiles.size()) - closeModel (); // Close any open file first, though - - OpenFile* f = new OpenFile; - f->zFileName = ""; - g_LoadedFiles.push_back (f); - g_CurrentFile = f; - - 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; - - FILE* fp = fopen (g_CurrentFile->zFileName, "w"); - if (!fp) - return; - - // Write all entries now - for (ulong i = 0; i < g_CurrentFile->objects.size(); ++i) { - LDObject* obj = g_CurrentFile->objects[i]; - - // LDraw requires lines to have DOS line endings - str zLine = str::mkfmt ("%s\r\n",obj->getContents ().chars ()); - - fwrite (zLine.chars(), 1, ~zLine, fp); - } - - fclose (fp); -} \ No newline at end of file
--- a/model.h Mon Mar 18 04:03:05 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,4 +0,0 @@ -void closeModel (); -void newModel (); -void saveModel (); -void openModel (str zPath); \ No newline at end of file
--- a/scanner.cpp Mon Mar 18 04:03:05 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,409 +0,0 @@ -/* - * botc source code - * Copyright (C) 2012 Santeri `azimuth` Piippo - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. Neither the name of the developer nor the names of its contributors may - * be used to endorse or promote products derived from this software without - * specific prior written permission. - * 4. Redistributions in any form must be accompanied by information on how to - * obtain complete source code for the software and any accompanying - * software that uses the software. The source code must either be included - * in the distribution or be available for no more than the cost of - * distribution plus a nominal fee, and must be freely redistributable - * under reasonable conditions. For an executable file, complete source - * code means the source code for all modules it contains. It does not - * include source code for modules or files that typically accompany the - * major components of the operating system on which the executable file - * runs. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include <stdio.h> -#include <stdlib.h> -#include "string.h" -#include "str.h" -#include "common.h" -#include "scanner.h" -#include "stdarg.h" - -#define STORE_POSITION \ - const bool _atnewline = bAtNewLine; \ - const ulong ulStoredLineNumber = ulaLineNumber[fc]; \ - const ulong ulStoredCurChar = ulaCurChar[fc]; - -#define RESTORE_POSITION \ - bAtNewLine = _atnewline; \ - ulaLineNumber[fc] = ulStoredLineNumber; \ - ulaCurChar[fc] = ulStoredCurChar; - -// ============================================================================ -Scanner::Scanner (str path) { - token = ""; - zPrevToken = ""; - lPrevPos = 0; - fc = -1; - - for (unsigned int u = 0; u < MAX_FILESTACK; u++) - fp[u] = NULL; - - OpenFile (path); - dCommentMode = 0; -} - -// ============================================================================ -Scanner::~Scanner () { - // If comment mode is 2 by the time the file ended, the - // comment was left unterminated. 1 is no problem, since - // it's terminated by newlines anyway. - if (dCommentMode == 2) - ParserError ("unterminated `/*`-style comment"); - - for (unsigned int u = 0; u < MAX_FILESTACK; u++) { - if (fp[u]) { - ParserWarning ("file idx %u remained open after parsing", u); - CloseFile (u); - } - } -} - -// ============================================================================ -// Opens a file and pushes its pointer to stack -void Scanner::OpenFile (str path) { - if (fc+1 >= MAX_FILESTACK) - ParserError ("supposed to open file `%s` but file stack is full! do you have recursive `#include` directives?", - path.chars()); - - // Save the position first. - if (fc != -1) { - laSavedPos[fc] = ftell (fp[fc]); - } - - fc++; - - fp[fc] = fopen (path.chars(), "r"); - if (!fp[fc]) { - ParserError ("couldn't open %s for reading!\n", path.chars ()); - exit (1); - } - - fseek (fp[fc], 0, SEEK_SET); - saFilePath[fc] = path.chars(); - ulaLineNumber[fc] = 1; - ulaCurChar[fc] = 1; - ulaPosition[fc] = 0; - bAtNewLine = 0; -} - -// ============================================================================ -// Closes the current file -void Scanner::CloseFile (unsigned int u) { - if (u >= MAX_FILESTACK) - u = fc; - - if (!fp[u]) - return; - - fclose (fp[u]); - fp[u] = NULL; - fc--; - - if (fc != -1) - fseek (fp[fc], laSavedPos[fc], SEEK_SET); -} - -// ============================================================================ -char Scanner::ReadChar () { - if (feof (fp[fc])) - return 0; - - char c; - if (!fread (&c, 1, 1, fp[fc])) - return 0; - - // We're at a newline, thus next char read will begin the next line - if (bAtNewLine) { - bAtNewLine = false; - ulaLineNumber[fc]++; - ulaCurChar[fc] = 0; // gets incremented to 1 - } - - if (c == '\n') - bAtNewLine = true; - - ulaCurChar[fc]++; - return c; -} - -// ============================================================================ -// Peeks the next character -char Scanner::PeekChar (int offset) { - // Store current position - long curpos = ftell (fp[fc]); - STORE_POSITION - - // Forward by offset - fseek (fp[fc], offset, SEEK_CUR); - - // Read the character - char* c = (char*)malloc (sizeof (char)); - - if (!fread (c, sizeof (char), 1, fp[fc])) { - fseek (fp[fc], curpos, SEEK_SET); - return 0; - } - - // Rewind back - fseek (fp[fc], curpos, SEEK_SET); - RESTORE_POSITION - - return c[0]; -} - -// ============================================================================ -// Read a token from the file buffer. Returns true if token was found, false if not. -bool Scanner::Next (bool peek) { - lPrevPos = ftell (fp[fc]); - str tmp = ""; - - while (1) { - // Check end-of-file - if (feof (fp[fc])) { - // If we're just peeking, we shouldn't - // actually close anything.. - if (peek) - break; - - CloseFile (); - if (fc == -1) - break; - } - - // Check if the next token possibly starts a comment. - if (PeekChar () == '/' && !tmp.len()) { - char c2 = PeekChar (1); - // C++-style comment - if (c2 == '/') - dCommentMode = 1; - else if (c2 == '*') - dCommentMode = 2; - - // We don't need to actually read in the - // comment characters, since they will get - // ignored due to comment mode anyway. - } - - c = ReadChar (); - - // If this is a comment we're reading, check if this character - // gets the comment terminated, otherwise ignore it. - if (dCommentMode > 0) { - if (dCommentMode == 1 && c == '\n') { - // C++-style comments are terminated by a newline - dCommentMode = 0; - continue; - } else if (dCommentMode == 2 && c == '*') { - // C-style comments are terminated by a `*/` - if (PeekChar() == '/') { - dCommentMode = 0; - ReadChar (); - } - } - - // Otherwise, ignore it. - continue; - } - - // Non-alphanumber characters (sans underscore) break the word too. - // If there was prior data, the delimeter pushes the cursor back so - // that the next character will be the same delimeter. If there isn't, - // the delimeter itself is included (and thus becomes a token itself.) - if ((c >= 33 && c <= 47) || - (c >= 58 && c <= 64) || - (c >= 91 && c <= 96 && c != '_') || - (c >= 123 && c <= 126)) { - if (tmp.len()) - fseek (fp[fc], ftell (fp[fc]) - 1, SEEK_SET); - else - tmp += c; - break; - } - - if (c <= 32 || c >= 127) { - // Don't break if we haven't gathered anything yet. - if (tmp.len()) - break; - } else { - tmp += c; - } - } - - // If we got nothing here, read failed. This should - // only happen in the case of EOF. - if (!tmp.len()) { - token = ""; - return false; - } - - ulaPosition[fc]++; - zPrevToken = token; - token = tmp; - return true; -} - -// ============================================================================ -// Returns the next token without advancing the cursor. -str Scanner::PeekNext (int offset) { - // Store current information - str storedtoken = token; - int cpos = ftell (fp[fc]); - STORE_POSITION - - // Advance on the token. - while (offset >= 0) { - if (!Next (true)) - return ""; - offset--; - } - - str tmp = token; - - // Restore position - fseek (fp[fc], cpos, SEEK_SET); - ulaPosition[fc]--; - token = storedtoken; - RESTORE_POSITION - return tmp; -} - -// ============================================================================ -void Scanner::Seek (unsigned int n, int origin) { - switch (origin) { - case SEEK_SET: - fseek (fp[fc], 0, SEEK_SET); - ulaPosition[fc] = 0; - break; - case SEEK_CUR: - break; - case SEEK_END: - printf ("ScriptReader::Seek: SEEK_END not yet supported.\n"); - break; - } - - for (unsigned int i = 0; i < n+1; i++) - Next(); -} - -// ============================================================================ -void Scanner::MustNext (const char* c) { - if (!Next()) { - if (strlen (c)) - ParserError ("expected `%s`, reached end of file instead\n", c); - else - ParserError ("expected a token, reached end of file instead\n"); - } - - if (strlen (c)) - MustThis (c); -} - -// ============================================================================ -void Scanner::MustThis (const char* c) { - if (token.compare (c) != 0) - ParserError ("expected `%s`, got `%s` instead", c, token.chars()); -} - -// ============================================================================ -void Scanner::ParserError (const char* message, ...) { - PERFORM_FORMAT (message, outmessage); - ParserMessage ("\nError: ", outmessage); - exit (1); -} - -// ============================================================================ -void Scanner::ParserWarning (const char* message, ...) { - PERFORM_FORMAT (message, outmessage); - ParserMessage ("Warning: ", outmessage); -} - -// ============================================================================ -void Scanner::ParserMessage (const char* header, char* message) { - if (fc >= 0 && fc < MAX_FILESTACK) - fprintf (stderr, "%s%s:%lu:%lu: %s\n", - header, saFilePath[fc], ulaLineNumber[fc], ulaCurChar[fc], message); - else - fprintf (stderr, "%s%s\n", header, message); -} - -// ============================================================================ -// if gotquote == 1, the current token already holds the quotation mark. -void Scanner::MustString (bool gotquote) { - if (gotquote) - MustThis ("\""); - else - MustNext ("\""); - - str string; - // Keep reading characters until we find a terminating quote. - while (1) { - // can't end here! - if (feof (fp[fc])) - ParserError ("unterminated string"); - - char c = ReadChar (); - if (c == '"') - break; - - string += c; - } - - token = string; -} - -// ============================================================================ -void Scanner::MustNumber (bool fromthis) { - if (!fromthis) - MustNext (); - - str num = token; - if (!num.compare ("-")) { - MustNext (); - num += token; - } - - // "true" and "false" are valid numbers - if (!token.icompare ("true")) - token = "1"; - else if (!token.icompare ("false")) - token = "0"; - else { - if (!token.isnumber()) - ParserError ("expected a number, got `%s`", num.chars()); - - str check; - check.appendformat ("%d", atoi (num.chars ())); - if (token.compare (check) != 0) - ParserWarning ("integer too large: %s -> %s", num.chars(), check.chars()); - - token = num; - } -} \ No newline at end of file
--- a/scanner.h Mon Mar 18 04:03:05 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,102 +0,0 @@ -/* - * botc source code - * Copyright (C) 2012 Santeri `azimuth` Piippo - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. Neither the name of the developer nor the names of its contributors may - * be used to endorse or promote products derived from this software without - * specific prior written permission. - * 4. Redistributions in any form must be accompanied by information on how to - * obtain complete source code for the software and any accompanying - * software that uses the software. The source code must either be included - * in the distribution or be available for no more than the cost of - * distribution plus a nominal fee, and must be freely redistributable - * under reasonable conditions. For an executable file, complete source - * code means the source code for all modules it contains. It does not - * include source code for modules or files that typically accompany the - * major components of the operating system on which the executable file - * runs. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef __SCRIPTREADER_H__ -#define __SCRIPTREADER_H__ - -#include <stdio.h> -#include "str.h" - -#define MAX_FILESTACK 8 -#define MAX_SCOPE 32 -#define MAX_CASE 64 - -#define PERFORM_FORMAT(in, out) \ - va_list v; \ - va_start (v, in); \ - char* out = vdynformat (in, v, 256); \ - va_end (v); - -class Scanner { -public: - // ==================================================================== - // MEMBERS - FILE* fp[MAX_FILESTACK]; - char* saFilePath[MAX_FILESTACK]; - int fc; - - ulong ulaPosition[MAX_FILESTACK]; - ulong ulaLineNumber[MAX_FILESTACK]; - ulong ulaCurChar[MAX_FILESTACK]; - long laSavedPos[MAX_FILESTACK]; // filepointer cursor position - str token; - short dCommentMode; - long lPrevPos; - str zPrevToken; - - // ==================================================================== - // METHODS - Scanner (str path); - ~Scanner (); - void OpenFile (str path); - void CloseFile (unsigned int u = MAX_FILESTACK); - char ReadChar (); - char PeekChar (int offset = 0); - bool Next (bool peek = false); - void Prev (); - str PeekNext (int offset = 0); - void Seek (unsigned int n, int origin); - void MustNext (const char* c = ""); - void MustThis (const char* c); - void MustString (bool gotquote = false); - void MustNumber (bool fromthis = false); - void MustBool (); - bool BoolValue (); - - void ParserError (const char* message, ...); - void ParserWarning (const char* message, ...); - -private: - bool bAtNewLine; - char c; - void ParserMessage (const char* header, char* message); -}; - -#endif // __SCRIPTREADER_H__ \ No newline at end of file
--- a/str.cpp Mon Mar 18 04:03:05 2013 +0200 +++ b/str.cpp Mon Mar 18 12:15:23 2013 +0200 @@ -82,7 +82,7 @@ // ============================================================================ // Adds a new character at the end of the string. -void str::append (char c) { +void str::append (const char c) { // Out of space, thus resize if (curs == alloclen) resize (alloclen + 1); @@ -103,6 +103,10 @@ append (c.chars()); } +void str::append (QString c) { + append (c.toUtf8 ().constData ()); +} + // ============================================================================ void str::appendformat (const char* c, ...) { va_list v; @@ -256,6 +260,7 @@ } // ============================================================================ +// It works otherwise but I'm having trouble with the initializer_list /* void str::strip (char c) { strip ({c});
--- a/str.h Mon Mar 18 04:03:05 2013 +0200 +++ b/str.h Mon Mar 18 12:15:23 2013 +0200 @@ -6,10 +6,7 @@ #include <stdarg.h> // #include <initializer_list> #include <vector> - -#ifdef QT_VERSION - #include <QString> -#endif // QT_VERSION +#include <QString> char* vdynformat (const char* csFormat, va_list vArgs, long int lSize); @@ -36,6 +33,7 @@ str (); str (const char* c); str (char c); + str (const QString c); ~str (); static str mkfmt (const char* fmt, ...) { @@ -69,9 +67,10 @@ void dump (); // Appends text to the string - void append (char c); + void append (const char c); void append (const char* c); void append (str c); + void append (QString c); // Formats text to the string. void format (const char* fmt, ...); @@ -97,6 +96,9 @@ // Removes a given index from the string, optionally more characters than just 1. void remove (unsigned int idx, unsigned int dellen=1); + // Trims the given amount of characters. If negative, the characters + // are removed from the beginning of the string, if positive, from the + // end of the string. str trim (int dellen); // Inserts a substring into a certain position. @@ -166,6 +168,11 @@ return *this; } + str& operator+= (const QString c) { + append (c); + return *this; + } + str& operator+= (vertex vrt); str operator* (const int repcount) { @@ -245,11 +252,9 @@ return text; } -#ifdef QT_VERSION operator QString () const { return text; } -#endif // QT_VERSION operator int () const { return atoi (text);
--- a/zz_setContentsDialog.cpp Mon Mar 18 04:03:05 2013 +0200 +++ b/zz_setContentsDialog.cpp Mon Mar 18 12:15:23 2013 +0200 @@ -55,7 +55,7 @@ // are exposed to the user and is reinterpreted if the user accepts the new // contents. // ============================================================================= -void Dialog_SetContents::staticDialog (LDObject* obj, LDForgeWindow* parent) { +void Dialog_SetContents::staticDialog (LDObject* obj, ForgeWindow* parent) { if (!obj) return; @@ -64,7 +64,7 @@ LDObject* oldobj = obj; // Reinterpret it from the text of the input field - obj = ParseLine (dlg.qContents->text ().toStdString ().c_str ()); + obj = parseLine (dlg.qContents->text ().toStdString ().c_str ()); // Remove the old object delete oldobj;
--- a/zz_setContentsDialog.h Mon Mar 18 04:03:05 2013 +0200 +++ b/zz_setContentsDialog.h Mon Mar 18 12:15:23 2013 +0200 @@ -13,7 +13,7 @@ LDObject* patient; Dialog_SetContents (LDObject* obj, QWidget* parent = nullptr); - static void staticDialog (LDObject* obj, LDForgeWindow* parent); + static void staticDialog (LDObject* obj, ForgeWindow* parent); private slots: void slot_handleButtons (QAbstractButton* qButton);