-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfixionary.cpp
92 lines (73 loc) · 1.84 KB
/
fixionary.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include "fixionary.h"
#include "split.h"
#include <fstream>
#include <ios>
#include <iostream>
#include <iterator>
#include <sstream>
#include <utility>
#include "affix.h"
#include "vos_node.h"
namespace timlan {
bool fixionary::load_affixes(const std::string &filename)
{
std::fstream file{filename};
file >> *this;
return true;
}
std::optional<std::reference_wrapper<const affix>> fixionary::find_prefix(
const std::string& input)
{
for (const auto &a: affixes_) {
if (input.size() >= a.timlan_.size()
&& input.compare(0, a.timlan_.size(), a.timlan_) == 0
&& a.prefix_)
return {std::cref(a)};
}
return {};
}
std::optional<std::reference_wrapper<const affix>> fixionary::find_suffix(
const std::string& input)
{
for (const auto &a : affixes_) {
if (input.size() >= a.timlan_.size()
&& input.compare(
input.size() - a.timlan_.size(),
a.timlan_.size(),
a.timlan_) == 0
&& !a.prefix_)
return {std::cref(a)};
}
return {};
}
std::istream &operator>>(std::istream &is, fixionary &fix)
{
fix.affixes_.clear();
std::string line_str{
std::istreambuf_iterator<char>{is},
std::istreambuf_iterator<char>{}};
std::vector<std::string> lines{split(line_str, "<br />")};
for (const auto &affix_line : lines) {
if (affix_line == "")
continue;
try {
affix new_affix{affix_line};
fix.affixes_.emplace_back(std::move(new_affix));
}
catch (affix_format_exception) {
std::cerr << "Failed to load line " << affix_line << "\n";
is.setstate(std::ios::failbit);
}
}
return is;
}
std::ostream &operator<<(std::ostream &os, const fixionary &fix)
{
os << "START OF FIXIONARY\n";
for (const auto &affix : fix.affixes_) {
os << " " << affix << "\n";
}
os << "END OF FIXIONARY";
return os;
}
}