TextEncoding.cpp
Go to the documentation of this file.
00001 //
00002 // TextEncoding.cpp
00003 //
00004 // $Id: //poco/1.3/Foundation/src/TextEncoding.cpp#2 $
00005 //
00006 // Library: Foundation
00007 // Package: Text
00008 // Module:  TextEncoding
00009 //
00010 // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
00011 // and Contributors.
00012 //
00013 // Permission is hereby granted, free of charge, to any person or organization
00014 // obtaining a copy of the software and accompanying documentation covered by
00015 // this license (the "Software") to use, reproduce, display, distribute,
00016 // execute, and transmit the Software, and to prepare derivative works of the
00017 // Software, and to permit third-parties to whom the Software is furnished to
00018 // do so, all subject to the following:
00019 // 
00020 // The copyright notices in the Software and this entire statement, including
00021 // the above license grant, this restriction and the following disclaimer,
00022 // must be included in all copies of the Software, in whole or in part, and
00023 // all derivative works of the Software, unless such copies or derivative
00024 // works are solely in the form of machine-executable object code generated by
00025 // a source language processor.
00026 // 
00027 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00028 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00029 // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
00030 // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
00031 // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
00032 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00033 // DEALINGS IN THE SOFTWARE.
00034 //
00035 
00036 
00037 #include "Poco/TextEncoding.h"
00038 #include "Poco/Exception.h"
00039 #include "Poco/String.h"
00040 #include "Poco/ASCIIEncoding.h"
00041 #include "Poco/Latin1Encoding.h"
00042 #include "Poco/Latin9Encoding.h"
00043 #include "Poco/UTF16Encoding.h"
00044 #include "Poco/UTF8Encoding.h"
00045 #include "Poco/Windows1252Encoding.h"
00046 #include "Poco/RWLock.h"
00047 #include "Poco/SingletonHolder.h"
00048 #include <map>
00049 
00050 
00051 namespace Poco {
00052 
00053 
00054 //
00055 // TextEncodingManager
00056 //
00057 
00058 
00059 class TextEncodingManager
00060 {
00061 public:
00062         TextEncodingManager()
00063         {
00064                 TextEncoding::Ptr pUtf8Encoding(new UTF8Encoding);
00065                 add(pUtf8Encoding, TextEncoding::GLOBAL); 
00066 
00067                 add(new ASCIIEncoding);
00068                 add(new Latin1Encoding);
00069                 add(new Latin9Encoding);
00070                 add(pUtf8Encoding);
00071                 add(new UTF16Encoding);
00072                 add(new Windows1252Encoding);
00073         }
00074         
00075         ~TextEncodingManager()
00076         {
00077         }
00078         
00079         void add(TextEncoding::Ptr pEncoding)
00080         {
00081                 add(pEncoding, pEncoding->canonicalName());
00082         }
00083         
00084         void add(TextEncoding::Ptr pEncoding, const std::string& name)
00085         {
00086                 RWLock::ScopedLock lock(_lock, true);
00087         
00088                 _encodings[name] = pEncoding;
00089         }
00090         
00091         void remove(const std::string& name)
00092         {
00093                 RWLock::ScopedLock lock(_lock, true);
00094         
00095                 _encodings.erase(name);
00096         }
00097         
00098         TextEncoding::Ptr find(const std::string& name) const
00099         {
00100                 RWLock::ScopedLock lock(_lock);
00101                 
00102                 EncodingMap::const_iterator it = _encodings.find(name);
00103                 if (it != _encodings.end())
00104                         return it->second;
00105                 
00106                 for (it = _encodings.begin(); it != _encodings.end(); ++it)
00107                 {
00108                         if (it->second->isA(name))
00109                                 return it->second;
00110                 }
00111                 return TextEncoding::Ptr();
00112         }
00113 
00114 private:
00115         TextEncodingManager(const TextEncodingManager&);
00116         TextEncodingManager& operator = (const TextEncodingManager&);
00117         
00118         struct ILT
00119         {
00120                 bool operator() (const std::string& s1, const std::string& s2) const
00121                 {
00122                         return Poco::icompare(s1, s2) < 0;
00123                 }
00124         };
00125         
00126         typedef std::map<std::string, TextEncoding::Ptr, ILT> EncodingMap;
00127         
00128         EncodingMap    _encodings;
00129         mutable RWLock _lock;
00130 };
00131 
00132 
00133 //
00134 // TextEncoding
00135 //
00136 
00137 
00138 const std::string TextEncoding::GLOBAL;
00139 
00140 
00141 TextEncoding::~TextEncoding()
00142 {
00143 }
00144 
00145 
00146 int TextEncoding::convert(const unsigned char* bytes) const
00147 {
00148         return (int) *bytes;
00149 }
00150 
00151 
00152 int TextEncoding::convert(int ch, unsigned char* bytes, int length) const
00153 {
00154         return 0;
00155 }
00156 
00157 
00158 TextEncoding& TextEncoding::byName(const std::string& encodingName)
00159 {
00160         TextEncoding* pEncoding = manager().find(encodingName);
00161         if (pEncoding)
00162                 return *pEncoding;
00163         else
00164                 throw NotFoundException(encodingName);
00165 }
00166 
00167         
00168 TextEncoding::Ptr TextEncoding::find(const std::string& encodingName)
00169 {
00170         return manager().find(encodingName);
00171 }
00172 
00173 
00174 void TextEncoding::add(TextEncoding::Ptr pEncoding)
00175 {
00176         manager().add(pEncoding, pEncoding->canonicalName());
00177 }
00178 
00179 
00180 void TextEncoding::add(TextEncoding::Ptr pEncoding, const std::string& name)
00181 {
00182         manager().add(pEncoding, name);
00183 }
00184 
00185 
00186 void TextEncoding::remove(const std::string& encodingName)
00187 {
00188         manager().remove(encodingName);
00189 }
00190 
00191 
00192 TextEncoding::Ptr TextEncoding::global(TextEncoding::Ptr encoding)
00193 {
00194         TextEncoding::Ptr prev = find(GLOBAL);
00195         add(encoding, GLOBAL);
00196         return prev;
00197 }
00198 
00199 
00200 TextEncoding& TextEncoding::global()
00201 {
00202         return byName(GLOBAL);
00203 }
00204 
00205 
00206 TextEncodingManager& TextEncoding::manager()
00207 {
00208         static SingletonHolder<TextEncodingManager> sh;
00209         return *sh.get();
00210 }
00211 
00212 
00213 } // namespace Poco


pluginlib
Author(s): Tully Foote and Eitan Marder-Eppstein
autogenerated on Sat Dec 28 2013 17:20:19