Locale.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. 
  2. #pragma once
  3. #include <locale>
  4. #include "String.DString.hpp"
  5. #include "String.WString.hpp"
  6. using namespace eSTR::Literals;
  7. #include "DDateTime.hpp"
  8. #ifndef LOCALE_EXPORTS
  9. #define _Localet_API _declspec(dllimport)
  10. #else
  11. #define _Localet_API _declspec(dllexport)
  12. #endif
  13. #ifndef LOCALE_EXPORTS
  14. #ifdef _WIN64
  15. #ifdef _DEBUG
  16. #pragma comment (lib, "ECOM.Utility.Locale64D.lib")
  17. #else
  18. #pragma comment (lib, "ECOM.Utility.Locale64.lib")
  19. #endif
  20. #else // X86
  21. #ifdef _DEBUG
  22. #pragma comment (lib, "ECOM.Utility.LocaleD.lib")
  23. #else
  24. #pragma comment (lib, "ECOM.Utility.Locale.lib")
  25. #endif
  26. #endif
  27. #endif // LOCALE_EXPORTS
  28. namespace ECOM
  29. {
  30. namespace Utility
  31. {
  32. class LocaleFormat : public ECOM::Utility::String::_string_format_detail <LocaleFormat>
  33. {
  34. _locale_t m_Locale;
  35. public:
  36. inline LocaleFormat (char * buf, int NbOfChar, _locale_t loc) : _string_format_detail (buf, NbOfChar)
  37. {
  38. m_Locale = loc;
  39. }
  40. public:
  41. template <typename arg>
  42. inline int _my_sprintf (char * buf, int NbOfChar, const char * fmt, arg a)
  43. {
  44. return _sprintf_s_l (buf, NbOfChar, fmt, m_Locale, a);
  45. }
  46. };
  47. //-----------------------------------------------------------------------------
  48. // Locale
  49. //-----------------------------------------------------------------------------
  50. class _Localet_API Locale
  51. {
  52. template <typename> static constexpr bool dependent_false = false;
  53. public:
  54. static Locale FromOS ();
  55. public:
  56. Locale (CV_String & Name);
  57. Locale (const eSTR::DString & Name);
  58. ~Locale ();
  59. public:
  60. Locale (const Locale & from) = delete;
  61. Locale (Locale && from);
  62. Locale & operator = (const Locale & from) = delete;
  63. Locale & operator = (Locale && from);
  64. public:
  65. template <typename... Args>
  66. inline eSTR::DString ToString (const char * szFormat, Args... args) const
  67. {
  68. constexpr int MaxLen = 8192;
  69. eSTR::DString rc;
  70. auto pc = rc.GetBufferSetLength (MaxLen);
  71. auto len = ECOM::Utility::LocaleFormat (pc, MaxLen, m_Locale).Format (szFormat, args...);
  72. rc.ReleaseBuffer (len);
  73. return std::move (rc);
  74. }
  75. public:
  76. enum enDateTime
  77. {
  78. enShortDate = 1,
  79. enLongDate = 2,
  80. enShortTime = 3,
  81. enLongTime = 4
  82. };
  83. public:
  84. eSTR::WString ToString (const DDateTime & dt, enDateTime type) const;
  85. eSTR::WString ToString (const DDateTime & dt, CV_String & fmt) const;
  86. public:
  87. template <typename T>
  88. static T GetFormatOf (enDateTime type)
  89. {
  90. static_assert (dependent_false <T>);
  91. }
  92. template <> static eSTR::WStringView GetFormatOf (enDateTime type);
  93. template <> static eSTR::DStringView GetFormatOf (enDateTime type);
  94. public:
  95. eSTR::DString GetName () const;
  96. _locale_t GetLocale () const;
  97. private:
  98. eSTR::DString m_LocName;
  99. _locale_t m_Locale;
  100. };
  101. }
  102. }