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/String.h"
18#include "Poco/ASCIIEncoding.h"
19#include "Poco/Latin1Encoding.h"
20#include "Poco/Latin2Encoding.h"
21#include "Poco/Latin9Encoding.h"
22#include "Poco/UTF32Encoding.h"
23#include "Poco/UTF16Encoding.h"
24#include "Poco/UTF8Encoding.h"
25#include "Poco/Windows1250Encoding.h"
26#include "Poco/Windows1251Encoding.h"
27#include "Poco/Windows1252Encoding.h"
28#include "Poco/RWLock.h"
29#include "Poco/SingletonHolder.h"
30#include <map>
31
32
33namespace Poco {
34
35
36//
37// TextEncodingManager
38//
39
40
41class TextEncodingManager
42{
43public:
44 TextEncodingManager()
45 {
46 TextEncoding::Ptr pUtf8Encoding(new UTF8Encoding);
47 add(pUtf8Encoding, TextEncoding::GLOBAL);
48
49 add(new ASCIIEncoding);
50 add(new Latin1Encoding);
51 add(new Latin2Encoding);
52 add(new Latin9Encoding);
53 add(pUtf8Encoding);
54 add(new UTF16Encoding);
55 add(new UTF32Encoding);
56 add(new Windows1250Encoding);
57 add(new Windows1251Encoding);
58 add(new Windows1252Encoding);
59 }
60
61 ~TextEncodingManager()
62 {
63 }
64
65 void add(TextEncoding::Ptr pEncoding)
66 {
67 add(pEncoding, pEncoding->canonicalName());
68 }
69
70 void add(TextEncoding::Ptr pEncoding, const std::string& name)
71 {
72 RWLock::ScopedLock lock(_lock, true);
73
74 _encodings[name] = pEncoding;
75 }
76
77 void remove(const std::string& name)
78 {
79 RWLock::ScopedLock lock(_lock, true);
80
81 _encodings.erase(name);
82 }
83
84 TextEncoding::Ptr find(const std::string& name) const
85 {
86 RWLock::ScopedLock lock(_lock);
87
88 EncodingMap::const_iterator it = _encodings.find(name);
89 if (it != _encodings.end())
90 return it->second;
91
92 for (it = _encodings.begin(); it != _encodings.end(); ++it)
93 {
94 if (it->second->isA(name))
95 return it->second;
96 }
97 return TextEncoding::Ptr();
98 }
99
100private:
101 TextEncodingManager(const TextEncodingManager&);
102 TextEncodingManager& operator = (const TextEncodingManager&);
103
104 typedef std::map<std::string, TextEncoding::Ptr, CILess> EncodingMap;
105
106 EncodingMap _encodings;
107 mutable RWLock _lock;
108};
109
110
111//
112// TextEncoding
113//
114
115
116const std::string TextEncoding::GLOBAL;
117
118
119TextEncoding::~TextEncoding()
120{
121}
122
123
124int TextEncoding::convert(const unsigned char* bytes) const
125{
126 return static_cast<int>(*bytes);
127}
128
129
130int TextEncoding::convert(int ch, unsigned char* bytes, int length) const
131{
132 return 0;
133}
134
135
136int TextEncoding::queryConvert(const unsigned char* bytes, int length) const
137{
138 return (int) *bytes;
139}
140
141
142int TextEncoding::sequenceLength(const unsigned char* bytes, int length) const
143{
144 return 1;
145}
146
147
148TextEncoding& TextEncoding::byName(const std::string& encodingName)
149{
150 TextEncoding* pEncoding = manager().find(encodingName);
151 if (pEncoding)
152 return *pEncoding;
153 else
154 throw NotFoundException(encodingName);
155}
156
157
158TextEncoding::Ptr TextEncoding::find(const std::string& encodingName)
159{
160 return manager().find(encodingName);
161}
162
163
164void TextEncoding::add(TextEncoding::Ptr pEncoding)
165{
166 manager().add(pEncoding, pEncoding->canonicalName());
167}
168
169
170void TextEncoding::add(TextEncoding::Ptr pEncoding, const std::string& name)
171{
172 manager().add(pEncoding, name);
173}
174
175
176void TextEncoding::remove(const std::string& encodingName)
177{
178 manager().remove(encodingName);
179}
180
181
182TextEncoding::Ptr TextEncoding::global(TextEncoding::Ptr encoding)
183{
184 TextEncoding::Ptr prev = find(GLOBAL);
185 add(encoding, GLOBAL);
186 return prev;
187}
188
189
190TextEncoding& TextEncoding::global()
191{
192 return byName(GLOBAL);
193}
194
195
196namespace
197{
198 static SingletonHolder<TextEncodingManager> sh;
199}
200
201
202TextEncodingManager& TextEncoding::manager()
203{
204 return *sh.get();
205}
206
207
208} // namespace Poco
209