source: XMLIO_V2/external/src/POCO/Foundation.save/TextEncoding.cpp @ 80

Last change on this file since 80 was 80, checked in by ymipsl, 14 years ago

ajout lib externe

  • Property svn:eol-style set to native
File size: 4.9 KB
Line 
1//
2// TextEncoding.cpp
3//
4// $Id: //poco/1.3/Foundation/src/TextEncoding.cpp#5 $
5//
6// Library: Foundation
7// Package: Text
8// Module:  TextEncoding
9//
10// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
11// and Contributors.
12//
13// Permission is hereby granted, free of charge, to any person or organization
14// obtaining a copy of the software and accompanying documentation covered by
15// this license (the "Software") to use, reproduce, display, distribute,
16// execute, and transmit the Software, and to prepare derivative works of the
17// Software, and to permit third-parties to whom the Software is furnished to
18// do so, all subject to the following:
19//
20// The copyright notices in the Software and this entire statement, including
21// the above license grant, this restriction and the following disclaimer,
22// must be included in all copies of the Software, in whole or in part, and
23// all derivative works of the Software, unless such copies or derivative
24// works are solely in the form of machine-executable object code generated by
25// a source language processor.
26//
27// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
30// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
31// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
32// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
33// DEALINGS IN THE SOFTWARE.
34//
35
36
37#include "Poco/TextEncoding.h"
38#include "Poco/Exception.h"
39#include "Poco/String.h"
40#include "Poco/ASCIIEncoding.h"
41#include "Poco/Latin1Encoding.h"
42#include "Poco/Latin9Encoding.h"
43#include "Poco/UTF16Encoding.h"
44#include "Poco/UTF8Encoding.h"
45#include "Poco/Windows1252Encoding.h"
46#include "Poco/RWLock.h"
47#include "Poco/SingletonHolder.h"
48#include <map>
49
50
51namespace Poco {
52
53
54//
55// TextEncodingManager
56//
57
58
59class TextEncodingManager
60{
61public:
62        TextEncodingManager()
63        {
64                TextEncoding::Ptr pUtf8Encoding(new UTF8Encoding);
65                add(pUtf8Encoding, TextEncoding::GLOBAL); 
66
67                add(new ASCIIEncoding);
68                add(new Latin1Encoding);
69                add(new Latin9Encoding);
70                add(pUtf8Encoding);
71                add(new UTF16Encoding);
72                add(new Windows1252Encoding);
73        }
74       
75        ~TextEncodingManager()
76        {
77        }
78       
79        void add(TextEncoding::Ptr pEncoding)
80        {
81                add(pEncoding, pEncoding->canonicalName());
82        }
83       
84        void add(TextEncoding::Ptr pEncoding, const std::string& name)
85        {
86                RWLock::ScopedLock lock(_lock, true);
87       
88                _encodings[name] = pEncoding;
89        }
90       
91        void remove(const std::string& name)
92        {
93                RWLock::ScopedLock lock(_lock, true);
94       
95                _encodings.erase(name);
96        }
97       
98        TextEncoding::Ptr find(const std::string& name) const
99        {
100                RWLock::ScopedLock lock(_lock);
101               
102                EncodingMap::const_iterator it = _encodings.find(name);
103                if (it != _encodings.end())
104                        return it->second;
105               
106                for (it = _encodings.begin(); it != _encodings.end(); ++it)
107                {
108                        if (it->second->isA(name))
109                                return it->second;
110                }
111                return TextEncoding::Ptr();
112        }
113
114private:
115        TextEncodingManager(const TextEncodingManager&);
116        TextEncodingManager& operator = (const TextEncodingManager&);
117       
118        struct ILT
119        {
120                bool operator() (const std::string& s1, const std::string& s2) const
121                {
122                        return Poco::icompare(s1, s2) < 0;
123                }
124        };
125       
126        typedef std::map<std::string, TextEncoding::Ptr, ILT> EncodingMap;
127       
128        EncodingMap    _encodings;
129        mutable RWLock _lock;
130};
131
132
133//
134// TextEncoding
135//
136
137
138const std::string TextEncoding::GLOBAL;
139
140
141TextEncoding::~TextEncoding()
142{
143}
144
145
146int TextEncoding::convert(const unsigned char* bytes) const
147{
148        return static_cast<int>(*bytes);
149}
150
151
152int TextEncoding::convert(int ch, unsigned char* bytes, int length) const
153{
154        return 0;
155}
156
157
158int TextEncoding::queryConvert(const unsigned char* bytes, int length) const
159{
160        return (int) *bytes;
161}
162
163
164int TextEncoding::sequenceLength(const unsigned char* bytes, int length) const
165{
166        return 1;
167}
168
169
170TextEncoding& TextEncoding::byName(const std::string& encodingName)
171{
172        TextEncoding* pEncoding = manager().find(encodingName);
173        if (pEncoding)
174                return *pEncoding;
175        else
176                throw NotFoundException(encodingName);
177}
178
179       
180TextEncoding::Ptr TextEncoding::find(const std::string& encodingName)
181{
182        return manager().find(encodingName);
183}
184
185
186void TextEncoding::add(TextEncoding::Ptr pEncoding)
187{
188        manager().add(pEncoding, pEncoding->canonicalName());
189}
190
191
192void TextEncoding::add(TextEncoding::Ptr pEncoding, const std::string& name)
193{
194        manager().add(pEncoding, name);
195}
196
197
198void TextEncoding::remove(const std::string& encodingName)
199{
200        manager().remove(encodingName);
201}
202
203
204TextEncoding::Ptr TextEncoding::global(TextEncoding::Ptr encoding)
205{
206        TextEncoding::Ptr prev = find(GLOBAL);
207        add(encoding, GLOBAL);
208        return prev;
209}
210
211
212TextEncoding& TextEncoding::global()
213{
214        return byName(GLOBAL);
215}
216
217
218TextEncodingManager& TextEncoding::manager()
219{
220        static SingletonHolder<TextEncodingManager> sh;
221        return *sh.get();
222}
223
224
225} // namespace Poco
Note: See TracBrowser for help on using the repository browser.