aboutsummaryrefslogtreecommitdiff
path: root/LoadFilesCommand.cpp
blob: 76cd197767bd8232081dd1a9ace7372e3b444d06 (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <memory>
#include <vector>
#include <string>

#include "LoadFilesCommand.h"
#include "MuseumDeserializer.h"
#include "Exception.h"
#include "FileReaderFactory.h"
#include "FileReader.h"
#include "ToggleMuseumPauseCommand.h"
#include "ParserFactory.h"

using namespace std;

LoadFilesCommand::LoadFilesCommand(Museum & m, int argc, char ** argv) : museum(m) {
	vector<string> files = {};
	for (size_t i = 0; i < argc; i++) {
		files.push_back(string(argv[i]));
	}
	this->files = files;
}

LoadFilesCommand::LoadFilesCommand(Museum & m, vector<string> files) : museum(m) {
	this->files = files;
}

void LoadFilesCommand::load_files() {
	ToggleMuseumPauseCommand(this->museum, true).execute();

	MuseumDeserializer deserializer { this->museum };
	ParserFactory factory {};

	for (string url : files) {
		try {
			unique_ptr<FileReader> file = FileReaderFactory().create(url);
			Parser & parser = factory.get_parser(*file);
			parser.parse(deserializer);
		} catch (Exception & e) {
			throw Exception("%s (%s)", e.what(), url.c_str());
		}
	}
}

void LoadFilesCommand::execute() {
	try {
		this->load_files();
	} catch (Exception & e) {
		throw Exception("LoadFilesCommand: %s", e.what());
	}
}