aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CMakeLists.txt7
-rw-r--r--HTTPFile.cpp1
-rw-r--r--LocalFile.cpp25
-rw-r--r--LocalFile.h2
-rw-r--r--docs/class-diag.puml46
-rw-r--r--main.cpp2
6 files changed, 62 insertions, 21 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 67c5607..fbec2b8 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -5,8 +5,11 @@ set(CMAKE_CXX_STANDARD 20)
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
set(CMAKE_BUILD_TYPE Debug)
-find_package(SDL2 REQUIRED)
-find_package(cpr REQUIRED)
+# find_package(SDL2 REQUIRED)
+# find_package(cpr REQUIRED)
+
+add_subdirectory(lib/SDL)
+add_subdirectory(lib/cpr)
project(main C CXX)
diff --git a/HTTPFile.cpp b/HTTPFile.cpp
index a1d9185..d488ab0 100644
--- a/HTTPFile.cpp
+++ b/HTTPFile.cpp
@@ -7,7 +7,6 @@ HTTPFile HTTPFile::instance(protocol);
void HTTPFile::open(const std::string url) {
_res = cpr::Get(cpr::Url{url});
-
}
void HTTPFile::close() { }
diff --git a/LocalFile.cpp b/LocalFile.cpp
index 2f4140d..1ff595a 100644
--- a/LocalFile.cpp
+++ b/LocalFile.cpp
@@ -11,21 +11,38 @@ void LocalFile::open(const std::string url) {
if (path.starts_with(protocol))
path = path.substr(protocol.size());
- std::ifstream _file(path);
- if (!_file.is_open())
+ _file = new std::ifstream(path, std::ios::in);
+ if (_file->fail() || !_file->is_open())
throw Exception("Cannot open file://%s\n", path.c_str());
}
void LocalFile::close() {
- if (_file.is_open()) _file.close();
+ if (_file == nullptr) return;
+
+ if (_file->is_open())
+ _file->close();
}
const std::string LocalFile::read() {
- return std::string(std::istreambuf_iterator<char>(_file), std::istreambuf_iterator<char>());
+ if (_file == nullptr)
+ throw Exception("File read after destructor\n");
+
+ if (!_file->is_open())
+ throw Exception("File read after close\n");
+
+ return std::string(
+ std::istreambuf_iterator<char>(*_file),
+ std::istreambuf_iterator<char>()
+ );
}
LocalFile::~LocalFile() {
close();
+
+ if (_file != nullptr) {
+ delete _file;
+ _file = nullptr;
+ }
}
LocalFile * LocalFile::clone() const {
diff --git a/LocalFile.h b/LocalFile.h
index 9529053..df9a479 100644
--- a/LocalFile.h
+++ b/LocalFile.h
@@ -24,6 +24,6 @@ private:
static LocalFile instance;
private:
- std::ifstream _file;
+ std::ifstream * _file = nullptr;
};
diff --git a/docs/class-diag.puml b/docs/class-diag.puml
index 6de5851..7f537ea 100644
--- a/docs/class-diag.puml
+++ b/docs/class-diag.puml
@@ -3,21 +3,37 @@
!theme plain
skinparam linetype ortho
skinparam classAttributeIconSize 0
-' skinparam packageStyle rectangle
+skinparam roundCorner 0
+skinparam packageStyle rectangle
class main as "main()"
hide main circle
hide main members
-class FileReader {
- + open(path)
- + read()
- + close()
- - protocol
+exception Exception {
+ Exception(const char* fmt, ...)
+ +what() : const char*
+ #error : char*
+ #va_format(va_list args, const char* fmt)
}
-class LocalFile
-class HTTPFile
+class FileReader <<Factory>> {
+ +open(url) : File&
+}
+
+interface File {
+ #open(string url)
+ +close()
+ +read() : string
+ #clone() const : File*
+}
+
+class LocalFile {
+ -static instance : LocalFile
+}
+class HTTPFile {
+ -static instance : HTTPFile
+}
class Canvas {
+ get_tile(x, y)
@@ -37,6 +53,7 @@ class YellowTileBehavior
class Artist
+class Deserializer
class ArtistDeserializer
class CanvasDeserializer
@@ -44,15 +61,17 @@ package CPR { }
package SDL2 { }
interface Parser {
- + parse(f: FileReader)
+ +parse(File)
}
class CSVParser
class XMLParser
class TXTParser
-FileReader <|-- LocalFile
-FileReader <|-- HTTPFile
+File <|.. LocalFile
+File <|.. HTTPFile
+File <. FileReader
+File .> FileReader
Canvas "1" -> "*" Tile
TileBehavior <|-- GrayTileBehavior
TileBehavior <|-- RedTileBehavior
@@ -63,9 +82,12 @@ Parser <|-- CSVParser
Parser <|-- TXTParser
Parser <|-- XMLParser
+Deserializer <|-- ArtistDeserializer
+Deserializer <|-- CanvasDeserializer
+
HTTPFile ..> CPR
-TileFactory --> Tile : create
+TileFactory --> Tile
diff --git a/main.cpp b/main.cpp
index 3ee5b4c..dfb0a9c 100644
--- a/main.cpp
+++ b/main.cpp
@@ -5,7 +5,7 @@
int main(int argc, char** argv) {
for (int i = 1; i < argc; i++) {
File & r = FileReader::open(argv[i]);
- printf("-- %s --\n%s\n", argv[i], r.read().c_str());
+ printf("\n-- %s --\n%s\n", argv[i], r.read().c_str());
r.close();
}