1 | // |
2 | // TextEncoding.cpp |
3 | // |
4 | // Library: Foundation |
5 | // Package: Text |
6 | // Module: TextEncoding |
7 | // |
8 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
9 | // and Contributors. |
10 | // |
11 | // SPDX-License-Identifier: BSL-1.0 |
12 | // |
13 | |
14 | |
15 | #include "Poco/TextEncoding.h" |
16 | #include "Poco/Exception.h" |
17 | #include "Poco/ASCIIEncoding.h" |
18 | #include "Poco/Latin1Encoding.h" |
19 | #include "Poco/Latin2Encoding.h" |
20 | #include "Poco/Latin9Encoding.h" |
21 | #include "Poco/UTF32Encoding.h" |
22 | #include "Poco/UTF16Encoding.h" |
23 | #include "Poco/UTF8Encoding.h" |
24 | #include "Poco/Windows1250Encoding.h" |
25 | #include "Poco/Windows1251Encoding.h" |
26 | #include "Poco/Windows1252Encoding.h" |
27 | #include "Poco/SingletonHolder.h" |
28 | |
29 | |
30 | namespace Poco { |
31 | |
32 | |
33 | // |
34 | // TextEncodingRegistry |
35 | // |
36 | |
37 | |
38 | TextEncodingRegistry::TextEncodingRegistry() |
39 | { |
40 | TextEncoding::Ptr pUtf8Encoding(new UTF8Encoding); |
41 | add(pUtf8Encoding, TextEncoding::GLOBAL); |
42 | |
43 | add(new ASCIIEncoding); |
44 | add(new Latin1Encoding); |
45 | add(new Latin2Encoding); |
46 | add(new Latin9Encoding); |
47 | add(pUtf8Encoding); |
48 | add(new UTF16Encoding); |
49 | add(new UTF32Encoding); |
50 | add(new Windows1250Encoding); |
51 | add(new Windows1251Encoding); |
52 | add(new Windows1252Encoding); |
53 | } |
54 | |
55 | TextEncodingRegistry::~TextEncodingRegistry() |
56 | { |
57 | } |
58 | |
59 | |
60 | bool TextEncodingRegistry::has(const std::string& name) const |
61 | { |
62 | if (_encodings.find(name) != _encodings.end()) |
63 | return true; |
64 | |
65 | for (const auto& enc : _encodings) |
66 | { |
67 | if (enc.second->isA(name)) return true; |
68 | } |
69 | return false; |
70 | } |
71 | |
72 | |
73 | void TextEncodingRegistry::add(TextEncoding::Ptr pEncoding) |
74 | { |
75 | add(pEncoding, pEncoding->canonicalName()); |
76 | } |
77 | |
78 | |
79 | void TextEncodingRegistry::add(TextEncoding::Ptr pEncoding, const std::string& name) |
80 | { |
81 | RWLock::ScopedLock lock(_lock, true); |
82 | |
83 | _encodings[name] = pEncoding; |
84 | } |
85 | |
86 | |
87 | void TextEncodingRegistry::remove(const std::string& name) |
88 | { |
89 | RWLock::ScopedLock lock(_lock, true); |
90 | |
91 | _encodings.erase(name); |
92 | } |
93 | |
94 | |
95 | TextEncoding::Ptr TextEncodingRegistry::find(const std::string& name) const |
96 | { |
97 | RWLock::ScopedLock lock(_lock); |
98 | |
99 | EncodingMap::const_iterator it = _encodings.find(name); |
100 | if (it != _encodings.end()) |
101 | return it->second; |
102 | |
103 | for (it = _encodings.begin(); it != _encodings.end(); ++it) |
104 | { |
105 | if (it->second->isA(name)) |
106 | return it->second; |
107 | } |
108 | return TextEncoding::Ptr(); |
109 | } |
110 | |
111 | |
112 | |
113 | // |
114 | // TextEncoding |
115 | // |
116 | |
117 | |
118 | const std::string TextEncoding::GLOBAL; |
119 | |
120 | |
121 | TextEncoding::~TextEncoding() |
122 | { |
123 | } |
124 | |
125 | |
126 | int TextEncoding::convert(const unsigned char* bytes) const |
127 | { |
128 | return static_cast<int>(*bytes); |
129 | } |
130 | |
131 | |
132 | int TextEncoding::convert(int /*ch*/, unsigned char* /*bytes*/, int /*length*/) const |
133 | { |
134 | return 0; |
135 | } |
136 | |
137 | |
138 | int TextEncoding::queryConvert(const unsigned char* bytes, int /*length*/) const |
139 | { |
140 | return (int) *bytes; |
141 | } |
142 | |
143 | |
144 | int TextEncoding::sequenceLength(const unsigned char* /*bytes*/, int /*length*/) const |
145 | { |
146 | return 1; |
147 | } |
148 | |
149 | |
150 | TextEncoding& TextEncoding::byName(const std::string& encodingName) |
151 | { |
152 | TextEncoding* pEncoding = registry(0)->find(encodingName); |
153 | if (pEncoding) |
154 | return *pEncoding; |
155 | else |
156 | throw NotFoundException(encodingName); |
157 | } |
158 | |
159 | |
160 | TextEncoding::Ptr TextEncoding::find(const std::string& encodingName) |
161 | { |
162 | return registry(0)->find(encodingName); |
163 | } |
164 | |
165 | |
166 | void TextEncoding::add(TextEncoding::Ptr pEncoding) |
167 | { |
168 | registry(0)->add(pEncoding, pEncoding->canonicalName()); |
169 | } |
170 | |
171 | |
172 | void TextEncoding::add(TextEncoding::Ptr pEncoding, const std::string& name) |
173 | { |
174 | registry(0)->add(pEncoding, name); |
175 | } |
176 | |
177 | |
178 | void TextEncoding::remove(const std::string& encodingName) |
179 | { |
180 | registry(0)->remove(encodingName); |
181 | } |
182 | |
183 | |
184 | TextEncoding::Ptr TextEncoding::global(TextEncoding::Ptr encoding) |
185 | { |
186 | TextEncoding::Ptr prev = find(GLOBAL); |
187 | add(encoding, GLOBAL); |
188 | return prev; |
189 | } |
190 | |
191 | |
192 | TextEncoding& TextEncoding::global() |
193 | { |
194 | return byName(GLOBAL); |
195 | } |
196 | |
197 | |
198 | namespace |
199 | { |
200 | TextEncodingRegistry* getRegistry() |
201 | { |
202 | static SingletonHolder<TextEncodingRegistry> sh; |
203 | return sh.get(); |
204 | } |
205 | } |
206 | |
207 | |
208 | const TextEncodingRegistry& TextEncoding::registry() |
209 | { |
210 | return *getRegistry(); |
211 | } |
212 | |
213 | |
214 | TextEncodingRegistry* TextEncoding::registry(int) |
215 | { |
216 | return getRegistry(); |
217 | } |
218 | |
219 | |
220 | } // namespace Poco |
221 | |