aboutsummaryrefslogtreecommitdiff
path: root/FileReaderFactory.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'FileReaderFactory.cpp')
-rw-r--r--FileReaderFactory.cpp40
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);
}