Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
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
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
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 }