-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
71 lines (58 loc) · 1.78 KB
/
main.cpp
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <QApplication>
#include <QMessageBox>
#include "MainWindow.hpp"
#include "MemDictionary.hpp"
#include "FileDictionary.hpp"
struct AppArguments{
static constexpr const char* DONT_STORE_OPT = "--dont-store";
bool do_not_store;
QString dictionary_path;
inline AppArguments()
: do_not_store(false)
, dictionary_path("words.txt")
{
auto list = QApplication::arguments();
size_t size = list.size();
switch (size) {
case 1:
break;
case 2:
if (list.at(1) == DONT_STORE_OPT)
do_not_store = true;
else // if isn't "dont_store", try as dictionary name
dictionary_path = list.at(1);
break;
case 3:
if (list.at(1) == DONT_STORE_OPT)
do_not_store = true;
else
throw std::runtime_error("Unknown argument");
dictionary_path = list.at(2);
break;
default:
throw std::runtime_error("Incorrect number of arguments");
}
}
};
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
qRegisterMetaType<Dictionary::State>("Dictionary::State");
qRegisterMetaType<Dictionary::SearchType>("Dictionary::SearchType");
// Dictionary
Dictionary* dictionary;
try {
AppArguments args;
if (args.do_not_store)
dictionary = new FileDictionary(args.dictionary_path);
else
dictionary = new MemDictionary(args.dictionary_path);
}
catch (std::runtime_error& error) {
QMessageBox::critical(nullptr, "Exception was thrown", error.what());
return -1;
}
auto* mainWindow = new MainWindow(dictionary);
mainWindow->show();
return QApplication::exec();
}