From bc02054d56118110a36aea72d21f9d5e73d07d1f Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 22 Oct 2024 14:00:41 +0200 Subject: refactor file reading factory --- FileReaderFactory.cpp | 40 +++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 23 deletions(-) (limited to 'FileReaderFactory.cpp') 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 FileReaderFactory::open(const std::string url) { - FileReader * reader = find_reader(url)->clone(); - reader->open(url); - return unique_ptr(reader); -} +unique_ptr 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(reader); } -- cgit v1.2.3