00001
00016 #pragma once
00017
00018 #include <iomanip>
00019 #include <iostream>
00020 #include <string>
00021 #include <sstream>
00022
00023 #ifdef _WIN32
00024
00025 #include <atlconv.h>
00026 #include <strsafe.h>
00027
00028 #ifdef _DEBUG
00029 # pragma comment(lib, "atls.lib")
00030 #else
00031 # pragma comment(lib, "atls.lib")
00032 #endif
00033 #endif
00034
00035 class StringUtil
00036 {
00037 public:
00038 #ifdef _WIN32
00039 static std::string wideToStl(const WCHAR* wszString)
00040 {
00041
00042 USES_CONVERSION;
00043 char *szTemp = W2A(wszString);
00044 return std::string(szTemp);
00045 }
00046
00047 static WCHAR* stlToWide(std::string sString)
00048 {
00049 return(asciiToWide(sString.c_str()));
00050 }
00051
00052 static WCHAR* asciiToWide(const char* szString)
00053 {
00054
00055 size_t nLen = strlen(szString);
00056 WCHAR* wszBuffer = new WCHAR[nLen + 1];
00057 USES_CONVERSION;
00058 WCHAR* wszValue = A2W(szString);
00059 StringCchCopyW(wszBuffer, nLen + 1, wszValue);
00060 return wszBuffer;
00061 }
00062 #endif
00063
00064 static std::string itos(int nNumber)
00065 {
00066
00067 char szBuffer[10];
00068 sprintf( szBuffer, "%d", nNumber );
00069 return std::string(szBuffer);
00070 }
00071
00072
00073 static bool stringToBool(const std::string& sValue)
00074 {
00075 if (sValue == "1" || sValue == "TRUE" || sValue == "true")
00076 {
00077 return true;
00078 }
00079 else if (sValue == "0" || sValue == "FALSE" || sValue == "false")
00080 {
00081 return false;
00082 }
00083 else
00084 {
00085
00086 return true;
00087 }
00088 }
00089
00090 static std::string boolToString(bool bValue)
00091 {
00092 std::string sResult = bValue?"1":"0";
00093 return sResult;
00094 }
00095
00096 static std::string doubleToString(double d, int nPrecision = 2)
00097 {
00098 std::ostringstream oss;
00099
00100 oss << std::setprecision(nPrecision)
00101 << std::fixed
00102 << d;
00103
00104 return oss.str();
00105 }
00106
00107 static double stringToDouble(std::string sDouble)
00108 {
00109 std::istringstream i(sDouble);
00110 double x;
00111 if (!(i >> x))
00112 {
00113 return 0.0;
00114 }
00115 return x;
00116 }
00117 };