-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
68 lines (57 loc) · 1.76 KB
/
main.cpp
File metadata and controls
68 lines (57 loc) · 1.76 KB
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
#include <fstream>
#include <iostream>
#include <map>
#include <sstream>
#include <stdexcept>
#include <string>
// testing
std::map<std::string, std::string> buildMap(std::ifstream& codes) {
std::map<std::string, std::string> transmap;
std::string key, line;
while (codes >> key && getline(codes, line)) {
if (line.size() > 1 && line.find_first_not_of(" \t") != std::string::npos) {
if (!transmap.insert({key, line.substr(1)}).second) {
std::cerr << "Duplicate key found!" << std::endl;
}
} else {
std::cerr << "Rule has been set with no value" << std::endl;
}
}
return transmap;
}
std::string translation(const std::map<std::string, std::string>& transMap, std::string& word) {
auto target = transMap.find(word);
if (target != transMap.end()) {
return target->second;
} else {
return word;
}
}
void manage_translation(std::ifstream& codes, std::ifstream& text) {
auto transMap = buildMap(codes);
std::string line;
while (getline(text, line)) {
std::string word, msg;
std::stringstream s(line);
while (s >> word) {
msg = translation(transMap, word);
std::cout << msg << " ";
}
std::cout << std::endl;
}
};
int main() {
std::ifstream codes("./out1.txt");
std::ifstream message("./out2.txt");
try {
if (!codes) {
throw std::runtime_error("ERROR: Failed to open file. ['./out1.txt']");
} else if (!message) {
throw std::runtime_error("ERROR: Failed to open file. '[./out2.txt']");
}
} catch (std::runtime_error& e) {
std::cout << e.what() << std::endl;
};
manage_translation(codes, message);
return 0;
}