StringOpt.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #pragma once
  2. #include "stdafx.h"
  3. #include <string>
  4. #include <vector>
  5. namespace CCOS_Kernel
  6. {
  7. class CStringOpt
  8. {
  9. public:
  10. static int GetCharacterSetPageCode(const std::string& strPageCode);
  11. static std::wstring StringFormat(const wchar_t *wszformat, ...);
  12. static std::string Transformwstring2string(const std::wstring& wstr, unsigned int nCodePage = 0);
  13. static std::wstring Transformstring2wstring(const std::string& str, unsigned int nCodePage = 0);
  14. static std::string TrimLeft(const std::string & strSource, const std::string& strTrim = " ");
  15. static std::string TrimRight(const std::string & strSource, const std::string& strTrim = " ");
  16. static std::string Trim(const std::string & strSource, const std::string& strTrim = " ");
  17. static std::wstring TrimLeft(const std::wstring & strSource, const std::wstring& strTrim = L" ");
  18. static std::wstring TrimRight(const std::wstring & strSource, const std::wstring& strTrim = L" ");
  19. static std::wstring Trim(const std::wstring & strSource, const std::wstring& strTrim = L" ");
  20. static std::string Replace(const std::string& org, const std::string& keystr, const std::string& replacestr);
  21. static std::wstring Replace(const std::wstring& org, const std::wstring &keystr, const std::wstring &replacestr);
  22. static bool SplitN(const std::wstring& wstrSource, const std::wstring& wstrSplit, unsigned int nProcessCount, std::vector<std::wstring>& vecParams, std::wstring& wstrLeftSource)
  23. {
  24. vecParams.clear();
  25. if (wstrSource.empty())
  26. {
  27. wstrLeftSource = wstrSource;
  28. if (nProcessCount == 0)
  29. return true;
  30. else
  31. return false;
  32. }
  33. if (nProcessCount == 0)
  34. {
  35. wstrLeftSource = wstrSource;
  36. return true;
  37. }
  38. std::wstring wstrCurrString = wstrSource;
  39. for (unsigned int i = 0; i< nProcessCount; i++)
  40. {
  41. size_t nIndex = wstrCurrString.find(wstrSplit);
  42. if(nIndex == std::string::npos)
  43. break;
  44. vecParams.push_back(wstrCurrString.substr(0, nIndex));
  45. wstrCurrString = wstrCurrString.substr(nIndex + wstrSplit.length());
  46. }
  47. if (vecParams.size() == nProcessCount)
  48. {
  49. wstrLeftSource = wstrCurrString;
  50. return true;
  51. }
  52. else if (vecParams.size() == nProcessCount - 1 && !wstrCurrString.empty())
  53. {
  54. vecParams.push_back(wstrCurrString);
  55. wstrLeftSource = L"";
  56. return true;
  57. }
  58. else
  59. return false;
  60. }
  61. static void Split(const std::wstring& wstrSource, const std::wstring& wstrSplit, std::vector<std::wstring>& vecParams)
  62. {
  63. vecParams.clear();
  64. if (wstrSource.empty())
  65. return;
  66. std::wstring wstrCurrString = wstrSource;
  67. while(!wstrCurrString.empty())
  68. {
  69. size_t nIndex = wstrCurrString.find(wstrSplit);
  70. if (nIndex == std::string::npos)
  71. break;
  72. vecParams.push_back(wstrCurrString.substr(0, nIndex));
  73. wstrCurrString = wstrCurrString.substr(nIndex + wstrSplit.length());
  74. }
  75. if (!wstrCurrString.empty())
  76. vecParams.push_back(wstrCurrString);
  77. }
  78. static bool HexString2Integer(const std::wstring& wstrSource, int& nResult);
  79. static std::string MakeUpper(const std::string & strSource);
  80. static std::wstring MakeUpper(const std::wstring & strSource);
  81. static std::string MakeLower(const std::string & strSource);
  82. static std::wstring MakeLower(const std::wstring & strSource);
  83. static bool Hex2Decimal(const std::string & hex_str, int& nDecimal);
  84. static bool Hex2Decimal(const std::wstring & hex_str, int& nDecimal);
  85. static std::wstring String(unsigned char Value);
  86. static std::wstring String(char Value);
  87. static std::wstring String(unsigned short Value);
  88. static std::wstring String(short Value);
  89. static std::wstring String(int nValue);
  90. static std::wstring String(unsigned int uValue);
  91. static std::wstring String(float fValue);
  92. static std::wstring String(double dfValue);
  93. static std::wstring String(long long llValue);
  94. static std::wstring String(unsigned long long ullValue);
  95. };
  96. #define STRINGOPT CCOS_Kernel::CStringOpt
  97. #define WS2S(wstr, nCodePage) STRINGOPT::Transformwstring2string(wstr, nCodePage)
  98. #define WS2LS(wstr) WS2S(wstr, 0)
  99. #define S2WS(str, nCodePage) STRINGOPT::Transformstring2wstring(str, nCodePage)
  100. #define LS2WS(str) S2WS(str, 0)
  101. #define TOSTRING(value) STRINGOPT::String(value)
  102. }