Open Chinese Convert 1.0.1 A project for conversion between Traditional and Simplified Chinese

- Main Page
- Modules
- Classes
- Examples

All Classes Functions Typedefs Modules
DictEntry.hpp
1 /*
2 * Open Chinese Convert
3 *
4 * Copyright 2010-2014 BYVoid <byvoid@byvoid.com>
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 #pragma once
20
21 #include "Common.hpp"
22 #include "UTF8Util.hpp"
23 #include "Segments.hpp"
24
25 namespace opencc {
30 class OPENCC_EXPORT DictEntry {
31 public:
32 virtual ~DictEntry() {
33 }
34
35 virtual const char* Key() const = 0;
36
37 virtual const char* GetDefault() const = 0;
38
39 virtual size_t NumValues() const = 0;
40
41 virtual string ToString() const = 0;
42
43 size_t KeyLength() const {
44 return strlen(Key());
45 }
46
47 bool operator<(const DictEntry& that) const {
48 return strcmp(Key(), that.Key()) < 0;
49 }
50
51 bool operator==(const DictEntry& that) const {
52 return strcmp(Key(), that.Key()) == 0;
53 }
54
55 static bool PtrLessThan(const DictEntry* a, const DictEntry* b) {
56 return *a < *b;
57 }
58 };
59
60 class OPENCC_EXPORT NoValueDictEntry : public DictEntry {
61 public:
62 NoValueDictEntry(const string& _key) : key(_key) {
63 }
64
65 virtual ~NoValueDictEntry() {
66 }
67
68 virtual const char* Key() const {
69 return key.c_str();
70 }
71
72 virtual const char* GetDefault() const {
73 return Key();
74 }
75
76 virtual size_t NumValues() const {
77 return 0;
78 }
79
80 virtual string ToString() const {
81 return key;
82 }
83
84 private:
85 string key;
86 };
87
88 class OPENCC_EXPORT SingleValueDictEntry : public DictEntry {
89 public:
90 virtual const char* Value() const = 0;
91
92 virtual const char* GetDefault() const {
93 return Value();
94 }
95
96 virtual size_t NumValues() const {
97 return 1;
98 }
99
100 virtual string ToString() const {
101 return string(Key()) + "\t" + Value();
102 }
103 };
104
105 class OPENCC_EXPORT StrSingleValueDictEntry : public SingleValueDictEntry {
106 public:
107 StrSingleValueDictEntry(const string& _key, const string& _value)
108 : key(_key), value(_value) {
109 }
110
111 virtual ~StrSingleValueDictEntry() {
112 }
113
114 virtual const char* Key() const {
115 return key.c_str();
116 }
117
118 virtual const char* Value() const {
119 return value.c_str();
120 }
121
122 private:
123 string key;
124 string value;
125 };
126
127 class OPENCC_EXPORT MultiValueDictEntry : public DictEntry {
128 public:
129 virtual vector<const char*> Values() const = 0;
130
131 virtual const char* GetDefault() const {
132 if (NumValues() > 0) {
133 return Values().at(0);
134 } else {
135 return Key();
136 }
137 }
138
139 virtual string ToString() const;
140 };
141
142 class OPENCC_EXPORT StrMultiValueDictEntry : public MultiValueDictEntry {
143 public:
144 StrMultiValueDictEntry(const string& _key, const vector<string>& _values)
145 : key(_key), values(_values) {
146 }
147
148 StrMultiValueDictEntry(const string& _key, const vector<const char*>& _values)
149 : key(_key) {
150 values.reserve(_values.size());
151 for (const char* str : _values) {
152 values.push_back(str);
153 }
154 }
155
156 virtual ~StrMultiValueDictEntry() {
157 }
158
159 virtual const char* Key() const {
160 return key.c_str();
161 }
162
163 size_t NumValues() const {
164 return values.size();
165 }
166
167 vector<const char*> Values() const {
168 vector<const char*> values;
169 for (const string& value : this->values) {
170 values.push_back(value.c_str());
171 }
172 return values;
173 }
174
175 private:
176 string key;
177 vector<string> values;
178 };
179
180 class OPENCC_EXPORT PtrDictEntry : public MultiValueDictEntry {
181 public:
182 PtrDictEntry(const char* _key, const vector<const char*>& _values)
183 : key(_key), values(_values) {
184 }
185
186 virtual ~PtrDictEntry() {
187 }
188
189 virtual const char* Key() const {
190 return key;
191 }
192
193 size_t NumValues() const {
194 return values.size();
195 }
196
197 vector<const char*> Values() const {
198 return values;
199 }
200
201 private:
202 const char* key;
203 vector<const char*> values;
204 };
205
206 class OPENCC_EXPORT DictEntryFactory {
207 public:
208 static DictEntry* New(const string& key) {
209 return new NoValueDictEntry(key);
210 }
211
212 static DictEntry* New(const string& key, const string& value) {
213 return new StrSingleValueDictEntry(key, value);
214 }
215
216 static DictEntry* New(const string& key, const vector<string>& values) {
217 return new StrMultiValueDictEntry(key, values);
218 }
219
220 static DictEntry* New(const DictEntry* entry) {
221 if (entry->NumValues() == 0) {
222 return new NoValueDictEntry(entry->Key());
223 } else if (entry->NumValues() == 1) {
224 const auto svEntry = static_cast<const SingleValueDictEntry*>(entry);
225 return new StrSingleValueDictEntry(svEntry->Key(), svEntry->Value());
226 } else {
227 const auto mvEntry = static_cast<const MultiValueDictEntry*>(entry);
228 return new StrMultiValueDictEntry(mvEntry->Key(), mvEntry->Values());
229 }
230 }
231 };
232
233 }
opencc::MultiValueDictEntry
Definition: DictEntry.hpp:127
opencc::PtrDictEntry
Definition: DictEntry.hpp:180
opencc::DictEntry
Key-values pair entry.
Definition: DictEntry.hpp:30
opencc::StrMultiValueDictEntry
Definition: DictEntry.hpp:142
opencc
Definition: BinaryDict.hpp:24
opencc::DictEntryFactory
Definition: DictEntry.hpp:206
opencc::StrSingleValueDictEntry
Definition: DictEntry.hpp:105
opencc::SingleValueDictEntry
Definition: DictEntry.hpp:88
opencc::NoValueDictEntry
Definition: DictEntry.hpp:60