-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
53 lines (44 loc) · 1.41 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
#include "textmodel.h"
#include "tokenizer.h"
#include <fstream>
#include <iostream>
int main(int argc, char *argv[])
{
if (argc != 3)
{
std::cout << "Usage: " << argv[0] << " filename min_length" << std::endl;
return EXIT_FAILURE;
}
const char *const filename = argv[1];
std::ifstream fin(filename);
if (!fin.good())
{
std::cerr << "Failed to open file: " << filename << std::endl;
return EXIT_FAILURE;
}
unsigned minLength = 0;
try {
const std::string minLengthStr = argv[2];
std::size_t pos;
const unsigned long value = std::stoul(minLengthStr, &pos);
if (pos < minLengthStr.length())
throw std::invalid_argument(minLengthStr);
else if (value > std::numeric_limits<unsigned>::max())
throw std::out_of_range(minLengthStr);
minLength = static_cast<unsigned>(value);
} catch (const std::invalid_argument &) {
std::cerr << "Bad min_length value. Positive integer value is expected." << std::endl;
return EXIT_FAILURE;
} catch (const std::out_of_range &) {
std::cerr << "min_length value is out of range." << std::endl;
return EXIT_FAILURE;
}
Tokenizer tokenizer(fin);
TextModel model;
model.buildFrom(tokenizer);
#ifdef PRINT_TABLE
model.print(std::cout);
#endif
model.generateTo(std::cout, minLength);
return EXIT_SUCCESS;
}