mptStringParse.cpp 5.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * mptStringParse.cpp
  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. #include "stdafx.h"
  10. #include "mptStringParse.h"
  11. #include "mpt/parse/parse.hpp"
  12. OPENMPT_NAMESPACE_BEGIN
  13. template<typename T>
  14. inline T ConvertStrToHelper(const std::string &str)
  15. {
  16. return mpt::ConvertStringTo<T>(str);
  17. }
  18. template<> inline bool ConvertStrToHelper(const std::string &str) { return ConvertStrToHelper<int>(str)?true:false; }
  19. template<> inline signed char ConvertStrToHelper(const std::string &str) { return static_cast<signed char>(ConvertStrToHelper<signed int>(str)); }
  20. template<> inline unsigned char ConvertStrToHelper(const std::string &str) { return static_cast<unsigned char>(ConvertStrToHelper<unsigned int>(str)); }
  21. #if MPT_WSTRING_FORMAT
  22. template<typename T>
  23. inline T ConvertStrToHelper(const std::wstring &str)
  24. {
  25. return mpt::ConvertStringTo<T>(str);
  26. }
  27. template<> inline bool ConvertStrToHelper(const std::wstring &str) { return ConvertStrToHelper<int>(str)?true:false; }
  28. template<> inline signed char ConvertStrToHelper(const std::wstring &str) { return static_cast<signed char>(ConvertStrToHelper<signed int>(str)); }
  29. template<> inline unsigned char ConvertStrToHelper(const std::wstring &str) { return static_cast<unsigned char>(ConvertStrToHelper<unsigned int>(str)); }
  30. #endif
  31. bool ConvertStrToBool(const std::string &str) { return ConvertStrToHelper<bool>(str); }
  32. signed char ConvertStrToSignedChar(const std::string &str) { return ConvertStrToHelper<signed char>(str); }
  33. unsigned char ConvertStrToUnsignedChar(const std::string &str) { return ConvertStrToHelper<unsigned char>(str); }
  34. signed short ConvertStrToSignedShort(const std::string &str) { return ConvertStrToHelper<signed short>(str); }
  35. unsigned short ConvertStrToUnsignedShort(const std::string &str) { return ConvertStrToHelper<unsigned short>(str); }
  36. signed int ConvertStrToSignedInt(const std::string &str) { return ConvertStrToHelper<signed int>(str); }
  37. unsigned int ConvertStrToUnsignedInt(const std::string &str) { return ConvertStrToHelper<unsigned int>(str); }
  38. signed long ConvertStrToSignedLong(const std::string &str) { return ConvertStrToHelper<signed long>(str); }
  39. unsigned long ConvertStrToUnsignedLong(const std::string &str) { return ConvertStrToHelper<unsigned long>(str); }
  40. signed long long ConvertStrToSignedLongLong(const std::string &str) { return ConvertStrToHelper<signed long long>(str); }
  41. unsigned long long ConvertStrToUnsignedLongLong(const std::string &str) { return ConvertStrToHelper<unsigned long long>(str); }
  42. float ConvertStrToFloat(const std::string &str) { return ConvertStrToHelper<float>(str); }
  43. double ConvertStrToDouble(const std::string &str) { return ConvertStrToHelper<double>(str); }
  44. long double ConvertStrToLongDouble(const std::string &str) { return ConvertStrToHelper<long double>(str); }
  45. #if MPT_WSTRING_FORMAT
  46. bool ConvertStrToBool(const std::wstring &str) { return ConvertStrToHelper<bool>(str); }
  47. signed char ConvertStrToSignedChar(const std::wstring &str) { return ConvertStrToHelper<signed char>(str); }
  48. unsigned char ConvertStrToUnsignedChar(const std::wstring &str) { return ConvertStrToHelper<unsigned char>(str); }
  49. signed short ConvertStrToSignedShort(const std::wstring &str) { return ConvertStrToHelper<signed short>(str); }
  50. unsigned short ConvertStrToUnsignedShort(const std::wstring &str) { return ConvertStrToHelper<unsigned short>(str); }
  51. signed int ConvertStrToSignedInt(const std::wstring &str) { return ConvertStrToHelper<signed int>(str); }
  52. unsigned int ConvertStrToUnsignedInt(const std::wstring &str) { return ConvertStrToHelper<unsigned int>(str); }
  53. signed long ConvertStrToSignedLong(const std::wstring &str) { return ConvertStrToHelper<signed long>(str); }
  54. unsigned long ConvertStrToUnsignedLong(const std::wstring &str) { return ConvertStrToHelper<unsigned long>(str); }
  55. signed long long ConvertStrToSignedLongLong(const std::wstring &str) { return ConvertStrToHelper<signed long long>(str); }
  56. unsigned long long ConvertStrToUnsignedLongLong(const std::wstring &str) { return ConvertStrToHelper<unsigned long long>(str); }
  57. float ConvertStrToFloat(const std::wstring &str) { return ConvertStrToHelper<float>(str); }
  58. double ConvertStrToDouble(const std::wstring &str) { return ConvertStrToHelper<double>(str); }
  59. long double ConvertStrToLongDouble(const std::wstring &str) { return ConvertStrToHelper<long double>(str); }
  60. #endif
  61. namespace mpt
  62. {
  63. namespace String
  64. {
  65. namespace Parse
  66. {
  67. template<typename T>
  68. T HexToHelper(const std::string &str)
  69. {
  70. return mpt::ConvertHexStringTo<T>(str);
  71. }
  72. template<> unsigned char HexToHelper(const std::string &str) { return static_cast<unsigned char>(HexToHelper<unsigned int>(str)); }
  73. unsigned char HexToUnsignedChar(const std::string &str) { return HexToHelper<unsigned char>(str); }
  74. unsigned short HexToUnsignedShort(const std::string &str) { return HexToHelper<unsigned short>(str); }
  75. unsigned int HexToUnsignedInt(const std::string &str) { return HexToHelper<unsigned int>(str); }
  76. unsigned long HexToUnsignedLong(const std::string &str) { return HexToHelper<unsigned long>(str); }
  77. unsigned long long HexToUnsignedLongLong(const std::string &str) { return HexToHelper<unsigned long long>(str); }
  78. } // namespace Parse
  79. } // namespace String
  80. } // namespace mpt
  81. OPENMPT_NAMESPACE_END