mptStringParse.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * mptStringParse.h
  3. * ----------------
  4. * Purpose: Convert strings to other types.
  5. * Notes : (currently none)
  6. * Authors: OpenMPT Devs
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. #pragma once
  10. #include "openmpt/all/BuildSettings.hpp"
  11. OPENMPT_NAMESPACE_BEGIN
  12. bool ConvertStrToBool(const std::string &str);
  13. signed char ConvertStrToSignedChar(const std::string &str);
  14. unsigned char ConvertStrToUnsignedChar(const std::string &str);
  15. signed short ConvertStrToSignedShort(const std::string &str);
  16. unsigned short ConvertStrToUnsignedShort(const std::string &str);
  17. signed int ConvertStrToSignedInt(const std::string &str);
  18. unsigned int ConvertStrToUnsignedInt(const std::string &str);
  19. signed long ConvertStrToSignedLong(const std::string &str);
  20. unsigned long ConvertStrToUnsignedLong(const std::string &str);
  21. signed long long ConvertStrToSignedLongLong(const std::string &str);
  22. unsigned long long ConvertStrToUnsignedLongLong(const std::string &str);
  23. float ConvertStrToFloat(const std::string &str);
  24. double ConvertStrToDouble(const std::string &str);
  25. long double ConvertStrToLongDouble(const std::string &str);
  26. template<typename T> inline T ConvertStrTo(const std::string &str); // not defined, generates compiler error for non-specialized types
  27. template<> inline std::string ConvertStrTo(const std::string &str) { return str; }
  28. template<> inline bool ConvertStrTo(const std::string &str) { return ConvertStrToBool(str); }
  29. template<> inline signed char ConvertStrTo(const std::string &str) { return ConvertStrToSignedChar(str); }
  30. template<> inline unsigned char ConvertStrTo(const std::string &str) { return ConvertStrToUnsignedChar(str); }
  31. template<> inline signed short ConvertStrTo(const std::string &str) { return ConvertStrToSignedShort(str); }
  32. template<> inline unsigned short ConvertStrTo(const std::string &str) { return ConvertStrToUnsignedShort(str); }
  33. template<> inline signed int ConvertStrTo(const std::string &str) { return ConvertStrToSignedInt(str); }
  34. template<> inline unsigned int ConvertStrTo(const std::string &str) { return ConvertStrToUnsignedInt(str); }
  35. template<> inline signed long ConvertStrTo(const std::string &str) { return ConvertStrToSignedLong(str); }
  36. template<> inline unsigned long ConvertStrTo(const std::string &str) { return ConvertStrToUnsignedLong(str); }
  37. template<> inline signed long long ConvertStrTo(const std::string &str) { return ConvertStrToSignedLongLong(str); }
  38. template<> inline unsigned long long ConvertStrTo(const std::string &str) { return ConvertStrToUnsignedLongLong(str); }
  39. template<> inline float ConvertStrTo(const std::string &str) { return ConvertStrToFloat(str); }
  40. template<> inline double ConvertStrTo(const std::string &str) { return ConvertStrToDouble(str); }
  41. template<> inline long double ConvertStrTo(const std::string &str) { return ConvertStrToLongDouble(str); }
  42. #if MPT_WSTRING_FORMAT
  43. bool ConvertStrToBool(const std::wstring &str);
  44. signed char ConvertStrToSignedChar(const std::wstring &str);
  45. unsigned char ConvertStrToUnsignedChar(const std::wstring &str);
  46. signed short ConvertStrToSignedShort(const std::wstring &str);
  47. unsigned short ConvertStrToUnsignedShort(const std::wstring &str);
  48. signed int ConvertStrToSignedInt(const std::wstring &str);
  49. unsigned int ConvertStrToUnsignedInt(const std::wstring &str);
  50. signed long ConvertStrToSignedLong(const std::wstring &str);
  51. unsigned long ConvertStrToUnsignedLong(const std::wstring &str);
  52. signed long long ConvertStrToSignedLongLong(const std::wstring &str);
  53. unsigned long long ConvertStrToUnsignedLongLong(const std::wstring &str);
  54. float ConvertStrToFloat(const std::wstring &str);
  55. double ConvertStrToDouble(const std::wstring &str);
  56. long double ConvertStrToLongDouble(const std::wstring &str);
  57. template<typename T> inline T ConvertStrTo(const std::wstring &str); // not defined, generates compiler error for non-specialized types
  58. template<> inline std::wstring ConvertStrTo(const std::wstring &str) { return str; }
  59. template<> inline bool ConvertStrTo(const std::wstring &str) { return ConvertStrToBool(str); }
  60. template<> inline signed char ConvertStrTo(const std::wstring &str) { return ConvertStrToSignedChar(str); }
  61. template<> inline unsigned char ConvertStrTo(const std::wstring &str) { return ConvertStrToUnsignedChar(str); }
  62. template<> inline signed short ConvertStrTo(const std::wstring &str) { return ConvertStrToSignedShort(str); }
  63. template<> inline unsigned short ConvertStrTo(const std::wstring &str) { return ConvertStrToUnsignedShort(str); }
  64. template<> inline signed int ConvertStrTo(const std::wstring &str) { return ConvertStrToSignedInt(str); }
  65. template<> inline unsigned int ConvertStrTo(const std::wstring &str) { return ConvertStrToUnsignedInt(str); }
  66. template<> inline signed long ConvertStrTo(const std::wstring &str) { return ConvertStrToSignedLong(str); }
  67. template<> inline unsigned long ConvertStrTo(const std::wstring &str) { return ConvertStrToUnsignedLong(str); }
  68. template<> inline signed long long ConvertStrTo(const std::wstring &str) { return ConvertStrToSignedLongLong(str); }
  69. template<> inline unsigned long long ConvertStrTo(const std::wstring &str) { return ConvertStrToUnsignedLongLong(str); }
  70. template<> inline float ConvertStrTo(const std::wstring &str) { return ConvertStrToFloat(str); }
  71. template<> inline double ConvertStrTo(const std::wstring &str) { return ConvertStrToDouble(str); }
  72. template<> inline long double ConvertStrTo(const std::wstring &str) { return ConvertStrToLongDouble(str); }
  73. #endif
  74. #if defined(MPT_WITH_MFC)
  75. template<typename T>
  76. inline T ConvertStrTo(const CString &str)
  77. {
  78. #if defined(UNICODE) && MPT_WSTRING_FORMAT
  79. return ConvertStrTo<T>(mpt::ToWide(str));
  80. #elif defined(UNICODE)
  81. return ConvertStrTo<T>(mpt::ToCharset(mpt::Charset::UTF8, str));
  82. #else // !UNICODE
  83. return ConvertStrTo<T>(mpt::ToCharset(mpt::Charset::Locale, str));
  84. #endif // UNICODE
  85. }
  86. #endif // MPT_WITH_MFC
  87. template<typename T>
  88. inline T ConvertStrTo(const char *str)
  89. {
  90. if(!str)
  91. {
  92. return T();
  93. }
  94. return ConvertStrTo<T>(std::string(str));
  95. }
  96. #if MPT_WSTRING_FORMAT
  97. #if MPT_USTRING_MODE_UTF8
  98. template<> inline mpt::ustring ConvertStrTo(const std::wstring &str) { return mpt::ToUnicode(str); }
  99. #endif
  100. template<typename T>
  101. inline T ConvertStrTo(const wchar_t *str)
  102. {
  103. if(!str)
  104. {
  105. return T();
  106. }
  107. return ConvertStrTo<T>(std::wstring(str));
  108. }
  109. #endif
  110. #if MPT_USTRING_MODE_UTF8
  111. template<typename T>
  112. inline T ConvertStrTo(const mpt::ustring &str)
  113. {
  114. return ConvertStrTo<T>(mpt::ToCharset(mpt::Charset::UTF8, str));
  115. }
  116. template<> inline mpt::ustring ConvertStrTo(const mpt::ustring &str) { return str; }
  117. #if MPT_WSTRING_CONVERT
  118. template<> inline std::wstring ConvertStrTo(const mpt::ustring &str) { return mpt::ToWide(str); }
  119. #endif
  120. #endif
  121. #if defined(MPT_ENABLE_CHARSET_LOCALE)
  122. template<typename T>
  123. inline T ConvertStrTo(const mpt::lstring &str)
  124. {
  125. return ConvertStrTo<T>(mpt::ToCharset(mpt::Charset::Locale, str));
  126. }
  127. template<> inline mpt::lstring ConvertStrTo(const mpt::lstring &str) { return str; }
  128. #endif
  129. namespace mpt
  130. {
  131. namespace String
  132. {
  133. namespace Parse
  134. {
  135. unsigned char HexToUnsignedChar(const std::string &str);
  136. unsigned short HexToUnsignedShort(const std::string &str);
  137. unsigned int HexToUnsignedInt(const std::string &str);
  138. unsigned long HexToUnsignedLong(const std::string &str);
  139. unsigned long long HexToUnsignedLongLong(const std::string &str);
  140. template<typename T> inline T Hex(const std::string &str); // not defined, generates compiler error for non-specialized types
  141. template<> inline unsigned char Hex(const std::string &str) { return HexToUnsignedChar(str); }
  142. template<> inline unsigned short Hex(const std::string &str) { return HexToUnsignedShort(str); }
  143. template<> inline unsigned int Hex(const std::string &str) { return HexToUnsignedInt(str); }
  144. template<> inline unsigned long Hex(const std::string &str) { return HexToUnsignedLong(str); }
  145. template<> inline unsigned long long Hex(const std::string &str) { return HexToUnsignedLongLong(str); }
  146. template<typename T>
  147. inline T Hex(const char *str)
  148. {
  149. if(!str)
  150. {
  151. return T();
  152. }
  153. return Hex<T>(std::string(str));
  154. }
  155. #if MPT_WSTRING_FORMAT
  156. template<typename T>
  157. inline T Hex(const std::wstring &str)
  158. {
  159. return Hex<T>(mpt::ToCharset(mpt::Charset::UTF8, str));
  160. }
  161. template<typename T>
  162. inline T Hex(const wchar_t *str)
  163. {
  164. if(!str)
  165. {
  166. return T();
  167. }
  168. return Hex<T>(std::wstring(str));
  169. }
  170. #endif
  171. #if MPT_USTRING_MODE_UTF8
  172. template<typename T>
  173. inline T Hex(const mpt::ustring &str)
  174. {
  175. return Hex<T>(mpt::ToCharset(mpt::Charset::UTF8, str));
  176. }
  177. #endif
  178. } // namespace Parse
  179. } // namespace String
  180. } // namespace mpt
  181. namespace mpt { namespace String {
  182. // Split the given string at separator positions into individual values returned as a vector.
  183. // An empty string results in an empty vector.
  184. // Leading or trailing separators result in a default-constructed element being inserted before or after the other elements.
  185. template<typename T>
  186. std::vector<T> Split(const mpt::ustring &str, const mpt::ustring &sep=U_(","))
  187. {
  188. std::vector<T> vals;
  189. std::size_t pos = 0;
  190. while(str.find(sep, pos) != std::string::npos)
  191. {
  192. vals.push_back(ConvertStrTo<T>(str.substr(pos, str.find(sep, pos) - pos)));
  193. pos = str.find(sep, pos) + sep.length();
  194. }
  195. if(!vals.empty() || (str.substr(pos).length() > 0))
  196. {
  197. vals.push_back(ConvertStrTo<T>(str.substr(pos)));
  198. }
  199. return vals;
  200. }
  201. template<typename T>
  202. std::vector<T> Split(const std::string &str, const std::string &sep=std::string(","))
  203. {
  204. std::vector<T> vals;
  205. std::size_t pos = 0;
  206. while(str.find(sep, pos) != std::string::npos)
  207. {
  208. vals.push_back(ConvertStrTo<T>(str.substr(pos, str.find(sep, pos) - pos)));
  209. pos = str.find(sep, pos) + sep.length();
  210. }
  211. if(!vals.empty() || (str.substr(pos).length() > 0))
  212. {
  213. vals.push_back(ConvertStrTo<T>(str.substr(pos)));
  214. }
  215. return vals;
  216. }
  217. } } // namespace mpt::String
  218. OPENMPT_NAMESPACE_END