diff options
-rw-r--r-- | CMakeLists.txt | 2 | ||||
-rw-r--r-- | CSVParser.cpp | 4 | ||||
-rw-r--r-- | CSVParser.h | 4 | ||||
-rw-r--r-- | Canvas.cpp | 2 | ||||
-rw-r--r-- | File.cpp | 9 | ||||
-rw-r--r-- | FileReader.cpp | 10 | ||||
-rw-r--r-- | FileReader.h | 12 | ||||
-rw-r--r-- | FileStrategy.cpp | 9 | ||||
-rw-r--r-- | FileStrategy.h (renamed from File.h) | 10 | ||||
-rw-r--r-- | HTTPFile.cpp | 3 | ||||
-rw-r--r-- | HTTPFile.h | 6 | ||||
-rw-r--r-- | LocalFile.cpp | 6 | ||||
-rw-r--r-- | LocalFile.h | 6 | ||||
-rw-r--r-- | Parser.cpp | 2 | ||||
-rw-r--r-- | Parser.h | 4 | ||||
-rw-r--r-- | ParserStrategy.h | 6 | ||||
-rw-r--r-- | TXTParser.cpp | 4 | ||||
-rw-r--r-- | TXTParser.h | 4 | ||||
-rw-r--r-- | View.h | 4 | ||||
-rw-r--r-- | XMLParser.cpp | 4 | ||||
-rw-r--r-- | XMLParser.h | 4 | ||||
-rw-r--r-- | docs/class-diag.puml | 28 | ||||
-rw-r--r-- | main.cpp | 14 |
23 files changed, 85 insertions, 72 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index ed1ad24..d8fe9a5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,7 +17,7 @@ project(main C CXX) add_executable(main main.cpp util.cpp - File.cpp + FileStrategy.cpp HTTPFile.cpp LocalFile.cpp Exception.cpp diff --git a/CSVParser.cpp b/CSVParser.cpp index 3fbe804..7d52cd7 100644 --- a/CSVParser.cpp +++ b/CSVParser.cpp @@ -15,7 +15,7 @@ CSVParser::CSVParser() { Parser::register_strategy(this); } -unsigned int CSVParser::heuristic(File & f) { +unsigned int CSVParser::heuristic(FileStrategy & f) { const string content = f.read(); int global_columns = 0; int columns = 1; @@ -41,7 +41,7 @@ static size_t header_idx(vector<string> header, string field) { return iter - header.begin(); } -void CSVParser::parse(File & f, Deserializer & d) { +void CSVParser::parse(FileStrategy & f, Deserializer & d) { vector<vector<string>> table = {}; istringstream rows(f.read()); diff --git a/CSVParser.h b/CSVParser.h index aa091a7..6698645 100644 --- a/CSVParser.h +++ b/CSVParser.h @@ -4,8 +4,8 @@ class CSVParser : public ParserStrategy { public: - virtual void parse(File & f, Deserializer & d); - virtual unsigned int heuristic(File & f); + virtual void parse(FileStrategy & f, Deserializer & d); + virtual unsigned int heuristic(FileStrategy & f); private: static CSVParser instance; @@ -48,7 +48,7 @@ string Canvas::to_string(bool truecolor) { if (type_str.length() == 0) type_str = "."; if (truecolor) - out += stringf("\e[48;2;%d;%d;%dm", + out += stringf("\e[38;2;0;0;0;48;2;%d;%d;%dm", tile.color.red, tile.color.green, tile.color.blue); out += stringf("%-2s ", type_str.c_str()); diff --git a/File.cpp b/File.cpp deleted file mode 100644 index bc31b1c..0000000 --- a/File.cpp +++ /dev/null @@ -1,9 +0,0 @@ -#include <string> - -#include "File.h" -#include "FileReader.h" - -File::File(const std::string protocol) { - FileReader::assign(protocol, this); -} - diff --git a/FileReader.cpp b/FileReader.cpp index 503aee5..2c7a50e 100644 --- a/FileReader.cpp +++ b/FileReader.cpp @@ -1,13 +1,13 @@ #include "FileReader.h" -#include "File.h" +#include "FileStrategy.h" -File & FileReader::open(const std::string url) { - File * reader = find_reader(url)->clone(); +FileStrategy & FileReader::open(const std::string url) { + FileStrategy * reader = find_reader(url)->clone(); reader->open(url); return *reader; } -void FileReader::assign(const std::string type, const File * node) { +void FileReader::register_strategy(const std::string type, const FileStrategy * node) { static FactoryMap & map = get_map(); map[type] = node; } @@ -17,7 +17,7 @@ FactoryMap & FileReader::get_map() { return map; } -const File * FileReader::find_reader(const std::string type) { +const FileStrategy * FileReader::find_reader(const std::string type) { static FactoryMap & map = get_map(); // try to find protocol by prefix diff --git a/FileReader.h b/FileReader.h index b2d8b37..90d5e32 100644 --- a/FileReader.h +++ b/FileReader.h @@ -3,24 +3,24 @@ #include <string> #include <map> -#include "File.h" +#include "FileStrategy.h" -using FactoryMap = std::map<std::string, const File *>; +using FactoryMap = std::map<std::string, const FileStrategy *>; class FileReader { public: - static File & open(const std::string url); + static FileStrategy & open(const std::string url); private: FileReader() = default; virtual ~FileReader() = default; private: - static void assign(const std::string type, const File * node); + static void register_strategy(const std::string type, const FileStrategy * node); static FactoryMap & get_map(); - static const File * find_reader(const std::string type); + static const FileStrategy * find_reader(const std::string type); private: - friend File; + friend FileStrategy; }; diff --git a/FileStrategy.cpp b/FileStrategy.cpp new file mode 100644 index 0000000..777aef5 --- /dev/null +++ b/FileStrategy.cpp @@ -0,0 +1,9 @@ +#include <string> + +#include "FileStrategy.h" +#include "FileReader.h" + +FileStrategy::FileStrategy(const std::string protocol) { + FileReader::register_strategy(protocol, this); +} + @@ -4,7 +4,7 @@ class FileReader; #include <string> -class File { +class FileStrategy { protected: virtual void open(const std::string url) = 0; public: @@ -12,14 +12,14 @@ public: virtual const std::string read() = 0; public: - virtual ~File() = default; + virtual ~FileStrategy() = default; protected: - File() = default; - virtual File * clone() const = 0; + FileStrategy() = default; + virtual FileStrategy * clone() const = 0; protected: - File(const std::string protocol); + FileStrategy(const std::string protocol); friend FileReader; }; diff --git a/HTTPFile.cpp b/HTTPFile.cpp index d488ab0..fded2d3 100644 --- a/HTTPFile.cpp +++ b/HTTPFile.cpp @@ -1,7 +1,6 @@ #include <cpr/cpr.h> #include "HTTPFile.h" -#include "Exception.h" HTTPFile HTTPFile::instance(protocol); @@ -23,5 +22,5 @@ HTTPFile * HTTPFile::clone() const { return new HTTPFile(this); } -HTTPFile::HTTPFile(const HTTPFile *) : File() { } +HTTPFile::HTTPFile(const HTTPFile *) : FileStrategy() { } @@ -2,9 +2,9 @@ #include <cpr/cpr.h> -#include "File.h" +#include "FileStrategy.h" -class HTTPFile : File { +class HTTPFile : FileStrategy { protected: virtual void open(const std::string url); public: @@ -19,7 +19,7 @@ private: virtual HTTPFile * clone() const; private: - using File::File; + using FileStrategy::FileStrategy; constexpr static const std::string protocol = "https://"; static HTTPFile instance; diff --git a/LocalFile.cpp b/LocalFile.cpp index ee5bf74..c21988e 100644 --- a/LocalFile.cpp +++ b/LocalFile.cpp @@ -28,10 +28,10 @@ const std::string LocalFile::read() { return *this->content; if (this->file == nullptr) - throw Exception("File read after destructor\n"); + throw Exception("FileStrategy read after destructor\n"); if (!this->file->is_open()) - throw Exception("File read after close\n"); + throw Exception("FileStrategy read after close\n"); this->content = new std::string( std::istreambuf_iterator<char>(*this->file), @@ -59,5 +59,5 @@ LocalFile * LocalFile::clone() const { return new LocalFile(this); } -LocalFile::LocalFile(const LocalFile *) : File() { } +LocalFile::LocalFile(const LocalFile *) : FileStrategy() { } diff --git a/LocalFile.h b/LocalFile.h index 5a67dab..c80d0f4 100644 --- a/LocalFile.h +++ b/LocalFile.h @@ -2,9 +2,9 @@ #include <fstream> -#include "File.h" +#include "FileStrategy.h" -class LocalFile : File { +class LocalFile : FileStrategy { protected: virtual void open(const std::string url); public: @@ -19,7 +19,7 @@ private: virtual LocalFile * clone() const; private: - using File::File; + using FileStrategy::FileStrategy; constexpr static const std::string protocol = "file://"; static LocalFile instance; @@ -3,7 +3,7 @@ #include "Parser.h" #include "Exception.h" -void Parser::parse(File & file, Deserializer & deserializer) { +void Parser::parse(FileStrategy & file, Deserializer & deserializer) { auto & col = Parser::get_collection(); if (col.size() < 1) throw Exception("no parsers registered"); @@ -1,6 +1,6 @@ #pragma once -#include "File.h" +#include "FileStrategy.h" #include "Deserializer.h" #include "ParserStrategy.h" @@ -8,7 +8,7 @@ class Parser { typedef std::vector<ParserStrategy*> ParserCollection; public: - static void parse(File & f, Deserializer & d); + static void parse(FileStrategy & f, Deserializer & d); static void register_strategy(ParserStrategy * p); private: diff --git a/ParserStrategy.h b/ParserStrategy.h index 54e853a..26dc347 100644 --- a/ParserStrategy.h +++ b/ParserStrategy.h @@ -1,11 +1,11 @@ #pragma once -#include "File.h" +#include "FileStrategy.h" #include "Deserializer.h" class ParserStrategy { public: - virtual void parse(File & f, Deserializer & d) = 0; - virtual unsigned int heuristic(File & f) = 0; + virtual void parse(FileStrategy & f, Deserializer & d) = 0; + virtual unsigned int heuristic(FileStrategy & f) = 0; }; diff --git a/TXTParser.cpp b/TXTParser.cpp index c9de4ca..5aea092 100644 --- a/TXTParser.cpp +++ b/TXTParser.cpp @@ -6,11 +6,11 @@ TXTParser::TXTParser() { Parser::register_strategy(this); } -unsigned int TXTParser::heuristic(File & f) { +unsigned int TXTParser::heuristic(FileStrategy & f) { return 0; } -void TXTParser::parse(File & f, Deserializer & d) { +void TXTParser::parse(FileStrategy & f, Deserializer & d) { printf("%s\n", __PRETTY_FUNCTION__); } diff --git a/TXTParser.h b/TXTParser.h index 1db9e9d..d67a7fb 100644 --- a/TXTParser.h +++ b/TXTParser.h @@ -4,8 +4,8 @@ class TXTParser : public ParserStrategy { public: - virtual void parse(File & f, Deserializer & d); - virtual unsigned int heuristic(File & f); + virtual void parse(FileStrategy & f, Deserializer & d); + virtual unsigned int heuristic(FileStrategy & f); private: static TXTParser instance; @@ -1,2 +1,6 @@ #pragma once +class View { + +}; + diff --git a/XMLParser.cpp b/XMLParser.cpp index 8dda552..3d6e6cf 100644 --- a/XMLParser.cpp +++ b/XMLParser.cpp @@ -13,7 +13,7 @@ XMLParser::XMLParser() { Parser::register_strategy(this); } -unsigned int XMLParser::heuristic(File & f) { +unsigned int XMLParser::heuristic(FileStrategy & f) { const string content = f.read(); int open_backets = 0; int close_brackets = 0; @@ -26,7 +26,7 @@ unsigned int XMLParser::heuristic(File & f) { return (open_backets + close_brackets) / penalty; } -void XMLParser::parse(File & f, Deserializer & d) { +void XMLParser::parse(FileStrategy & f, Deserializer & d) { using namespace pugi; const string content = f.read(); diff --git a/XMLParser.h b/XMLParser.h index 7e3ff09..dab7290 100644 --- a/XMLParser.h +++ b/XMLParser.h @@ -4,8 +4,8 @@ class XMLParser : public ParserStrategy { public: - virtual void parse(File & f, Deserializer & d); - virtual unsigned int heuristic(File & f); + virtual void parse(FileStrategy & f, Deserializer & d); + virtual unsigned int heuristic(FileStrategy & f); private: static XMLParser instance; diff --git a/docs/class-diag.puml b/docs/class-diag.puml index b8e8a63..3502aa5 100644 --- a/docs/class-diag.puml +++ b/docs/class-diag.puml @@ -26,14 +26,14 @@ exception Exception { rectangle Group_FileReading as "File reading" <<group>> { class FileReader <<Factory>> { - +open(url) : File& + +open(url) : FileStrategy& } - interface File { + interface FileStrategy { + read() : string + close() -- # open(string url) - # clone() : File* <<const>> + # clone() : FileStrategy* <<const>> } class LocalFile { - instance : LocalFile <<static>> @@ -46,11 +46,11 @@ rectangle Group_FileReading as "File reading" <<group>> { class Url <<irrelevant>> } - File <|.. LocalFile - File <|.. HTTPFile + FileStrategy <|.. LocalFile + FileStrategy <|.. HTTPFile - File <. FileReader - File .> FileReader + FileStrategy <. FileReader + FileStrategy .> FileReader HTTPFile --> CPR.Response CPR.Response - CPR.Url @@ -145,7 +145,9 @@ rectangle Group_Model as "Model" <<group>> { Color <.. TileAppearance } rectangle Group_Tile_Behavior as "Tile behavior" <<group>> { - interface TileBehavior + interface TileBehaviorStrategy + + class TileBehavior together { class GrayTileBehavior @@ -158,10 +160,10 @@ rectangle Group_Model as "Model" <<group>> { BlueTileBehavior -d[hidden]- YellowTileBehavior } - TileBehavior <|.. GrayTileBehavior - TileBehavior <|.. RedTileBehavior - TileBehavior <|.. BlueTileBehavior - TileBehavior <|.. YellowTileBehavior + TileBehaviorStrategy <|.. GrayTileBehavior + TileBehaviorStrategy <|.. RedTileBehavior + TileBehaviorStrategy <|.. BlueTileBehavior + TileBehaviorStrategy <|.. YellowTileBehavior Tile --> "state" TileBehavior } @@ -198,7 +200,7 @@ rectangle "Visualization" <<group>> { ' CanvasDeserializer <... Canvas ' Deserializer <... Museum -ParserStrategy ..> File +ParserStrategy ..> FileStrategy ' Museum .> Deserializer : friend Deserializer .> Museum @@ -5,6 +5,7 @@ #include "Museum.h" #include "Deserializer.h" #include "Parser.h" +#include "View.h" using namespace std; @@ -14,12 +15,12 @@ void load_museum(Museum & m, int argc, char** argv) { for (int i = 1; i < argc; i++) { char * url = argv[i]; - File * f = nullptr; + FileStrategy * f = nullptr; try { f = &FileReader::open(url); } catch (Exception & e) { - printf("File open error: %s\n", e.what()); + printf("FileStrategy open error: %s\n", e.what()); exit(EXIT_FAILURE); } @@ -36,11 +37,18 @@ void load_museum(Museum & m, int argc, char** argv) { d.finalize(); } +void load_view(View & v, Museum & m) { + // v.set_target(&m); + +} + int main(int argc, char** argv) { Museum m {}; - load_museum(m, argc, argv); + View v {}; + load_view(v, m); + printf("%s", m.canvas.to_string(true).c_str()); return EXIT_SUCCESS; |