convstring.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Copyright 2005 - 2016 Zarafa and its licensors
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Affero General Public License, version 3,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. #include <kopano/platform.h>
  18. #include <kopano/charset/convstring.h>
  19. namespace KC {
  20. /** Create a convstring instance from a SPropValue.
  21. *
  22. * Extarcts the lpszA or lpszW depending on the PROP_TYPE of the provided
  23. * prop value and creates a convstring object representing the data.
  24. *
  25. * @param[in] lpsPropVal
  26. * Pointer to the SPropValue object to extract the data from.
  27. * @return A new convstring object.
  28. */
  29. convstring convstring::from_SPropValue(const SPropValue *lpsPropVal)
  30. {
  31. if (!lpsPropVal)
  32. return convstring();
  33. switch (PROP_TYPE(lpsPropVal->ulPropTag)) {
  34. case PT_STRING8:
  35. return convstring(lpsPropVal->Value.lpszA);
  36. case PT_UNICODE:
  37. return convstring(lpsPropVal->Value.lpszW);
  38. default:
  39. return convstring();
  40. }
  41. }
  42. /** Create a convstring instance from a SPropValue.
  43. *
  44. * Extarcts the lpszA or lpszW depending on the PROP_TYPE of the provided
  45. * prop value and creates a convstring object representing the data.
  46. *
  47. * @param[in] sPropVal
  48. * Reference to the SPropValue object to extract the data from.
  49. * @return A new convstring object.
  50. */
  51. convstring convstring::from_SPropValue(const SPropValue &sPropVal)
  52. {
  53. return from_SPropValue(&sPropVal);
  54. }
  55. /**
  56. * Create a new convstring object based on another convstring object.
  57. *
  58. * @param[in] other
  59. * The convstring object to base the new object on.
  60. */
  61. convstring::convstring(const convstring &other) :
  62. m_ulFlags(other.m_ulFlags), m_str(other.m_str)
  63. {
  64. // We create a new convert_context as the context of other contains
  65. // nothing we really need. If we would copy its map with iconv_context's, we would
  66. // still need to create all the internal iconv objects, which is usseless since we
  67. // might not even use them all.
  68. // Also the lists with stored strings are useless as they're only used as storage.
  69. if (other.m_lpsz)
  70. m_lpsz = m_str.c_str();
  71. }
  72. /** Create a new convstring object based on a raw char pointer.
  73. *
  74. * Creates an object and assumes that the provided string is encoded
  75. * in the current locale.
  76. *
  77. * @param[in] lpsz
  78. * The string to base the new object on. This string
  79. * is expected to be encoded in the current locale.
  80. */
  81. convstring::convstring(const char *lpsz)
  82. : m_lpsz(reinterpret_cast<const TCHAR*>(lpsz))
  83. {
  84. }
  85. /** Create a new convstring object based on a raw char pointer.
  86. *
  87. * Creates an object and assumes that the provided string is encoded
  88. * as a wide character string.
  89. *
  90. * @param[in] lpsz
  91. * The string to base the new object on. This string
  92. * is expected to be encoded as a wide character string.
  93. */
  94. convstring::convstring(const wchar_t *lpsz)
  95. : m_lpsz(reinterpret_cast<const TCHAR*>(lpsz))
  96. , m_ulFlags(MAPI_UNICODE)
  97. {
  98. }
  99. /** Create a new convstring object based on a raw pointer.
  100. *
  101. * Creates an object and assumes that the provided string is encoded in
  102. * the current locale or as a wide character string depending on the
  103. * precense of the MAPI_UNICODE flag in ulFlags.
  104. *
  105. * @param[in] lpsz
  106. * The string to base the new object on.
  107. * @param[in] ulFlags
  108. * Flags changing the behaviour of the convstring.
  109. * If the MAPI_UNICODE flag is specified, the provided
  110. * string is assumed to be encoded as a wide character
  111. * string. Otherwise it is assumed to be encoded in the
  112. * current locale.
  113. */
  114. convstring::convstring(const TCHAR *lpsz, ULONG ulFlags)
  115. : m_lpsz(lpsz)
  116. , m_ulFlags(ulFlags)
  117. {
  118. }
  119. /** Perform a conversion from the internal string to the requested encoding.
  120. *
  121. * @tparam T
  122. * The type to convert to string to. The actual encoding
  123. * is determined implicitly by this type.
  124. * @return An object of type T.
  125. */
  126. template <typename T>
  127. T convstring::convert_to() const {
  128. if (m_lpsz == NULL)
  129. return T();
  130. if (m_ulFlags & MAPI_UNICODE)
  131. return m_converter.convert_to<T>(reinterpret_cast<const wchar_t*>(m_lpsz));
  132. else
  133. return m_converter.convert_to<T>(reinterpret_cast<const char*>(m_lpsz));
  134. }
  135. /** Perform a conversion from the internal string to the requested encoding.
  136. *
  137. * With this version the to charset is explicitly specified.
  138. * @tparam T
  139. * The type to convert to string to. The actual encoding
  140. * is determined implicitly by this type.
  141. * @param[in] tocode
  142. * The charset in which to encode the converted string.
  143. * @return An object of type T.
  144. */
  145. template<typename T>
  146. T convstring::convert_to(const char *tocode) const {
  147. if (m_lpsz == NULL)
  148. return T();
  149. if (m_ulFlags & MAPI_UNICODE) {
  150. auto lpszw = reinterpret_cast<const wchar_t *>(m_lpsz);
  151. return m_converter.convert_to<T>(tocode, lpszw, rawsize(lpszw), CHARSET_WCHAR);
  152. } else {
  153. auto lpsza = reinterpret_cast<const char *>(m_lpsz);
  154. return m_converter.convert_to<T>(tocode, lpsza, rawsize(lpsza), CHARSET_CHAR);
  155. }
  156. }
  157. /** Check if the convstring is initialized with a NULL pointer or with an empty string.
  158. *
  159. * @return A boolean specifying of the internal string is a NULL pointer or an empty string.
  160. * @retval true The internal string is a NULL pointer or an empty string.
  161. * @retval false The internal string is valid and non-empty.
  162. */
  163. bool convstring::null_or_empty() const
  164. {
  165. if (m_lpsz == NULL)
  166. return true;
  167. if (m_ulFlags & MAPI_UNICODE)
  168. return *reinterpret_cast<const wchar_t*>(m_lpsz) == L'\0';
  169. else
  170. return *reinterpret_cast<const char*>(m_lpsz) == '\0';
  171. }
  172. /** Convert this convstring object to an utf8string.
  173. *
  174. * @return An utf8string representing the internal string encoded in UTF-8.
  175. */
  176. convstring::operator utf8string() const
  177. {
  178. return (m_lpsz == NULL ? utf8string::null_string() : convert_to<utf8string>());
  179. }
  180. /** Convert this convstring object to a std::string.
  181. *
  182. * @return A std::string representing the internal string encoded in the current locale.
  183. */
  184. convstring::operator std::string() const
  185. {
  186. return convert_to<std::string>();
  187. }
  188. /** Convert this convstring object to a std::wstring.
  189. *
  190. * @return A std::wstring representing the internal string encoded as a wide character string.
  191. */
  192. convstring::operator std::wstring() const
  193. {
  194. return convert_to<std::wstring>();
  195. }
  196. /**
  197. * Convert this convstring object to a raw char pointer.
  198. *
  199. * @return A character pointer that represents the internal string encoded in the current locale.
  200. *
  201. * @note Don't call this too often as the results are stored internally since storage needs to be
  202. * guaranteed for the caller to be able to use the data.
  203. */
  204. const char* convstring::c_str() const
  205. {
  206. return (m_lpsz ? convert_to<char*>() : NULL);
  207. }
  208. /**
  209. * Convert this convstring object to a raw char pointer encoded in UTF-8.
  210. *
  211. * @return A character pointer that represents the internal string encoded in the current locale.
  212. *
  213. * @note Don't call this too often as the results are stored internally since storage needs to be
  214. * guaranteed for the caller to be able to use the data.
  215. */
  216. const char* convstring::u8_str() const
  217. {
  218. return (m_lpsz ? convert_to<char*>("UTF-8") : NULL);
  219. }
  220. } /* namespace */