-
Notifications
You must be signed in to change notification settings - Fork 0
/
dictcc.cc
104 lines (103 loc) · 3.29 KB
/
dictcc.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <iostream>
#include "dict.h"
int main(int argc, char **argv)
{
setlocale(LC_ALL, "");
int c;
char* lang_chr = nullptr;
char* search_chr = nullptr;
int nwords = -1;
// TODO: Add new languages here.
std::ostringstream langs;
langs << " DESV" << std::endl
<< " DEEN" << std::endl
<< " DENL" << std::endl;
// Help stringstream
std::ostringstream help;
help << "dictcc [OPTION] [ARGUMENT]" << std::endl
<< "-h Print this help message." << std::endl
<< "-l Specify language." << std::endl
<< " Currently supported:" << std::endl
<< langs.str()
<< "-s Word to search for." << std::endl
<< "-n Number of words to be printed"<< std::endl;
// Settings getopt.
opterr = 0;
// Get arguments.
while ( (c = getopt (argc, argv, "hl:s:n:") ) != -1) {
switch (c)
{
case 'h':
std::cout << help.str();
exit(EXIT_SUCCESS);
case 'l':
lang_chr = optarg;
break;
case 's':
search_chr = optarg;
break;
case 'n':
nwords = std::stol(optarg);
break;
case '?':
if (isprint (optopt))
std::cerr << "Unknown option " << optopt << std::endl;
else
std::cerr << "Unknown option character." << std::endl;
exit(EXIT_FAILURE);
default:
abort ();
}
}
// Sanity checks.
if (argc < 2) {
std::cout << "Please use this CLI the following way.\n" << std::endl;
std::cout << help.str();
exit(EXIT_FAILURE);
}
if (lang_chr == nullptr) {
std::cerr << "Language has to specified." << std::endl;
exit(EXIT_FAILURE);
}
if (search_chr == nullptr) {
std::cerr << "String to search for has to be specified." << std::endl;
exit(EXIT_FAILURE);
}
// Make new didctionary and search.
std::string lang(lang_chr);
std::shared_ptr<dictcc::dict> dictionary = nullptr;
dictcc::search_ptr_t result;
try {
dictionary = dictcc::creator::create(lang);
result = dictionary->search(search_chr);
} catch (const dictcc::dict_exception& e) {
std::cout << "Unfortunately something went wrong. We are very sorry about that :(" << std::endl
<< "See below for further information on the error occured." << std::endl
<< e.what() << std::endl;
exit(EXIT_FAILURE);
}
// A search returns a pair of word lists.
int max_len = 0;
// Find longest strting for formatting purposes.
for (const std::string& str: result->first) {
if (dictcc::dict::actual_size(str) > max_len) {
max_len = dictcc::dict::actual_size(str);
}
}
// Default number of words to be printed is 8.
if (nwords < 0) {
nwords = (result->first).size() > 8 ? nwords = 8 : nwords = (result->first).size();
}
// Avoid segmentation fault.
if (nwords > (result->first).size()) {
nwords = (result->first).size();
}
// Print on console.
for (int i = 0; i < nwords; i++) {
std::cout << (result->first)[i];
for (int j=0; j < ( max_len - dictcc::dict::actual_size( (result->first)[i] ) + 10); j++) {
std::cout << ".";
}
std::cout << (result->second)[i] << std::endl;
}
}