-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cc
48 lines (41 loc) · 1017 Bytes
/
main.cc
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
//
// Copyright (c) 2023 Adam Lichtl.
//
#include <cstring>
#include <fstream>
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
#include "Parser.h"
using parser::Parser;
using std::cout;
using std::endl;
using std::ifstream;
using std::runtime_error;
using std::string;
int main(int argc, char* argv[]) {
if (argc != 2) {
cout << "Usage: " << argv[0] << " FILE" << endl;
return EXIT_FAILURE;
}
const string filepath{argv[1]};
try {
ifstream file(filepath);
if (!file) {
throw runtime_error("Unable to open " + filepath + ": " + strerror(errno));
}
const auto index = Parser().parse(file);
for (const auto& entry : index) {
cout << entry.first;
for (const auto word_index : entry.second) {
cout << " " << word_index;
}
cout << endl;
}
} catch (const runtime_error& e) {
cout << "ERROR: Unable to parse " << filepath << ": " << e.what() << endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}