aboutsummaryrefslogtreecommitdiff
path: root/FileReader.cpp
blob: d2d888f6d895ec3f1cf362d487b44ca03d292a15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <memory>

#include "FileReader.h"
#include "FileStrategy.h"

using namespace std;

unique_ptr<FileStrategy> FileReader::open(const std::string url) {
	FileStrategy * reader = find_reader(url)->clone();
	reader->open(url);
	return unique_ptr<FileStrategy>(reader);
}

void FileReader::register_strategy(const std::string type, const FileStrategy * node) {
	static FactoryMap & map = get_map();
	map[type] = node;
}

FactoryMap & FileReader::get_map() {
	static FactoryMap map;
	return map;
}

const FileStrategy * FileReader::find_reader(const std::string type) {
	static FactoryMap & map = get_map();

	// try to find protocol by prefix
	for (auto item : map) {
		if (!type.starts_with(item.first)) continue;
		return item.second;
	}

	// fallback is local file
	return map.find("file://")->second;
}