diff --git a/src/map/style/mapcssparser.cpp b/src/map/style/mapcssparser.cpp index fd0971a..e3365d5 100644 --- a/src/map/style/mapcssparser.cpp +++ b/src/map/style/mapcssparser.cpp @@ -1,131 +1,131 @@ /* Copyright (C) 2020 Volker Krause This program is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include "mapcssparser.h" #include "mapcssparser_p.h" #include "mapcssscanner.h" #include "mapcssstyle.h" #include #include #include #include #include char* unquoteString(const char *str) { const auto size = strlen(str) - 2; if (size <= 0) return nullptr; auto out = (char*)malloc(size + 1); memset(out, 0, size + 1); auto outIt = out; for (auto it = str + 1; it < str + size + 1; ++it, ++outIt) { if (*it == '\\') { ++it; switch (*it) { case '\\': case '"': *outIt = *it; break; case 'n': *outIt = '\n'; break; case 't': *outIt = '\t'; break; default: *outIt++ = '\\'; *outIt = *it; } } else { *outIt = *it; } } return out; } using namespace KOSMIndoorMap; bool MapCSSParser::hasError() const { return m_error; } QString MapCSSParser::fileName() const { return m_currentFileName; } MapCSSStyle MapCSSParser::parse(const QString &fileName) { m_error = true; MapCSSStyle style; parse(&style, fileName); if (m_error) { - return {}; + return MapCSSStyle(); } return style; } void MapCSSParser::parse(MapCSSStyle *style, const QString &fileName) { QFile f(fileName); if (!f.open(QFile::ReadOnly)) { qWarning() << f.fileName() << f.errorString(); return; } m_currentFileName = fileName; m_currentStyle = style; yyscan_t scanner; if (yylex_init(&scanner)) { return; } const auto lexerCleanup = qScopeGuard([&scanner]{ yylex_destroy(scanner); }); const auto b = f.readAll(); YY_BUFFER_STATE state; state = yy_scan_string(b.constData(), scanner); if (yyparse(this, scanner)) { return; } yy_delete_buffer(state, scanner); m_error = false; m_currentStyle = nullptr; } void MapCSSParser::addImport(char* fileName) { auto cssFile = QString::fromUtf8(fileName); free(fileName); if (QFileInfo(cssFile).isRelative()) { cssFile = QFileInfo(m_currentFileName).absolutePath() + QLatin1Char('/') + cssFile; } MapCSSParser p; p.parse(m_currentStyle, cssFile); } void MapCSSParser::addRule(MapCSSRule *rule) { m_currentStyle->m_rules.push_back(std::unique_ptr(rule)); } diff --git a/src/map/style/mapcssstyle.h b/src/map/style/mapcssstyle.h index 60bd3e0..f6eb001 100644 --- a/src/map/style/mapcssstyle.h +++ b/src/map/style/mapcssstyle.h @@ -1,77 +1,80 @@ /* Copyright (C) 2020 Volker Krause This program is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #ifndef KOSMINDOORMAP_MAPCSSSTYLE_H #define KOSMINDOORMAP_MAPCSSSTYLE_H #include "mapcssrule.h" #include #include #include class QIODevice; namespace OSM { class DataSet; } namespace KOSMIndoorMap { class MapCSSParser; class MapCSSResult; -/** A parsed MapCSS style sheet. */ +/** A parsed MapCSS style sheet. + * @see MapCSSParser::parse for how to obtain a valid instance + */ class MapCSSStyle { public: + /** Creates an invalid/empty style. */ + explicit MapCSSStyle(); MapCSSStyle(const MapCSSStyle&) = delete; MapCSSStyle(MapCSSStyle&&); ~MapCSSStyle(); + MapCSSStyle& operator=(const MapCSSStyle&) = delete; MapCSSStyle& operator=(MapCSSStyle&&); /** Optimizes style sheet rules for application against @p dataSet. * This does resolve tag keys and is therefore mandatory when changing the data set. */ void compile(const OSM::DataSet &dataSet); /** Evaluates the style sheet for a given state @p state (OSM element, view state, element state, etc). * The result is not returned but added to @p result for reusing allocated memory * between evaluations. */ void evaluate(const MapCSSState &state, MapCSSResult &result) const; /** Evaluate canvas style rules. */ void evaluateCanvas(MapCSSResult &result) const; /** Write this style as MapCSS to @p out. * Mainly used for testing. */ void write(QIODevice *out) const; private: friend class MapCSSParser; - MapCSSStyle(); - std::vector> m_rules; }; } #endif // KOSMINDOORMAP_MAPCSSSTYLE_H