diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-22 14:00:41 +0200 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-22 14:00:41 +0200 |
commit | bc02054d56118110a36aea72d21f9d5e73d07d1f (patch) | |
tree | 2fc49dd3f93307df6242b8e63bca0e26b2e62168 /FileReaderFactory.cpp | |
parent | fab0fccc0aaa18e915bcd08e81e5a04177e435cd (diff) |
refactor file reading factory
Diffstat (limited to 'FileReaderFactory.cpp')
-rw-r--r-- | FileReaderFactory.cpp | 40 |
1 files changed, 17 insertions, 23 deletions
diff --git a/FileReaderFactory.cpp b/FileReaderFactory.cpp index 4b72b11..e40dbb9 100644 --- a/FileReaderFactory.cpp +++ b/FileReaderFactory.cpp @@ -3,34 +3,28 @@ #include "FileReaderFactory.h" #include "FileReader.h" +#include "LocalFileReader.h" +#include "HTTPFileReader.h" + using namespace std; -unique_ptr<FileReader> FileReaderFactory::open(const std::string url) { - FileReader * reader = find_reader(url)->clone(); - reader->open(url); - return unique_ptr<FileReader>(reader); -} +unique_ptr<FileReader> FileReaderFactory::create(const string & url) { + FileReader * reader = nullptr; -void FileReaderFactory::register_strategy(const std::string type, const FileReader * node) { - static FactoryMap & map = get_map(); - map[type] = node; -} + // protocol handlers + if (url.starts_with("file://")) + reader = new LocalFileReader(url); + else if (url.starts_with("http://")) + reader = new HTTPFileReader(url); + else if (url.starts_with("https://")) + reader = new HTTPFileReader(url); -FactoryMap & FileReaderFactory::get_map() { - static FactoryMap map; - return map; -} - -const FileReader * FileReaderFactory::find_reader(const std::string type) { - static FactoryMap & map = get_map(); + // no protocol = treat as file:// + if (reader == nullptr) + reader = new LocalFileReader(url); - // try to find protocol by prefix - for (auto item : map) { - if (!type.starts_with(item.first)) continue; - return item.second; - } + reader->open(); - // fallback is local file - return map.find("file://")->second; + return unique_ptr<FileReader>(reader); } |