TextEncoding.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. //
  2. // TextEncoding.cpp
  3. //
  4. // $Id: //poco/Main/Foundation/src/TextEncoding.cpp#13 $
  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. #include "Poco/TextEncoding.h"
  36. #include "Poco/Exception.h"
  37. #include "Poco/String.h"
  38. #include "Poco/ASCIIEncoding.h"
  39. #include "Poco/Latin1Encoding.h"
  40. #include "Poco/Latin9Encoding.h"
  41. #include "Poco/UTF16Encoding.h"
  42. #include "Poco/UTF8Encoding.h"
  43. #include "Poco/Windows1252Encoding.h"
  44. #include "Poco/RWLock.h"
  45. #include "Poco/SingletonHolder.h"
  46. #include <map>
  47. namespace Poco {
  48. //
  49. // TextEncodingManager
  50. //
  51. class TextEncodingManager
  52. {
  53. public:
  54. TextEncodingManager()
  55. {
  56. TextEncoding::Ptr pUtf8Encoding(new UTF8Encoding);
  57. add(pUtf8Encoding, TextEncoding::GLOBAL);
  58. add(new ASCIIEncoding);
  59. add(new Latin1Encoding);
  60. add(new Latin9Encoding);
  61. add(pUtf8Encoding);
  62. add(new UTF16Encoding);
  63. add(new Windows1252Encoding);
  64. }
  65. ~TextEncodingManager()
  66. {
  67. }
  68. void add(TextEncoding::Ptr pEncoding)
  69. {
  70. add(pEncoding, pEncoding->canonicalName());
  71. }
  72. void add(TextEncoding::Ptr pEncoding, const std::string& name)
  73. {
  74. RWLock::ScopedLock lock(_lock, true);
  75. _encodings[name] = pEncoding;
  76. }
  77. void remove(const std::string& name)
  78. {
  79. RWLock::ScopedLock lock(_lock, true);
  80. _encodings.erase(name);
  81. }
  82. TextEncoding::Ptr find(const std::string& name) const
  83. {
  84. RWLock::ScopedLock lock(_lock);
  85. EncodingMap::const_iterator it = _encodings.find(name);
  86. if (it != _encodings.end())
  87. return it->second;
  88. for (it = _encodings.begin(); it != _encodings.end(); ++it)
  89. {
  90. if (it->second->isA(name))
  91. return it->second;
  92. }
  93. return TextEncoding::Ptr();
  94. }
  95. private:
  96. TextEncodingManager(const TextEncodingManager&);
  97. TextEncodingManager& operator = (const TextEncodingManager&);
  98. struct ILT
  99. {
  100. bool operator() (const std::string& s1, const std::string& s2) const
  101. {
  102. return Poco::icompare(s1, s2) < 0;
  103. }
  104. };
  105. typedef std::map<std::string, TextEncoding::Ptr, ILT> EncodingMap;
  106. EncodingMap _encodings;
  107. mutable RWLock _lock;
  108. };
  109. //
  110. // TextEncoding
  111. //
  112. const std::string TextEncoding::GLOBAL;
  113. TextEncoding::~TextEncoding()
  114. {
  115. }
  116. int TextEncoding::convert(const unsigned char* bytes) const
  117. {
  118. return (int) *bytes;
  119. }
  120. int TextEncoding::convert(int ch, unsigned char* bytes, int length) const
  121. {
  122. return 0;
  123. }
  124. TextEncoding& TextEncoding::byName(const std::string& encodingName)
  125. {
  126. TextEncoding* pEncoding = manager().find(encodingName);
  127. if (pEncoding)
  128. return *pEncoding;
  129. else
  130. throw NotFoundException(encodingName);
  131. }
  132. TextEncoding::Ptr TextEncoding::find(const std::string& encodingName)
  133. {
  134. return manager().find(encodingName);
  135. }
  136. void TextEncoding::add(TextEncoding::Ptr pEncoding)
  137. {
  138. manager().add(pEncoding, pEncoding->canonicalName());
  139. }
  140. void TextEncoding::add(TextEncoding::Ptr pEncoding, const std::string& name)
  141. {
  142. manager().add(pEncoding, name);
  143. }
  144. void TextEncoding::remove(const std::string& encodingName)
  145. {
  146. manager().remove(encodingName);
  147. }
  148. TextEncoding::Ptr TextEncoding::global(TextEncoding::Ptr encoding)
  149. {
  150. TextEncoding::Ptr prev = find(GLOBAL);
  151. add(encoding, GLOBAL);
  152. return prev;
  153. }
  154. TextEncoding& TextEncoding::global()
  155. {
  156. return byName(GLOBAL);
  157. }
  158. TextEncodingManager& TextEncoding::manager()
  159. {
  160. static SingletonHolder<TextEncodingManager> sh;
  161. return *sh.get();
  162. }
  163. } // namespace Poco