123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- #pragma once
- #include <string>
- using namespace std;
- #pragma warning(disable:4996)
- std::string format_string(const char* format, ...);
- string INT64TOSTR(INT64 val);
- INT64 STRTOINT64(string &str);
- BOOL GetIntWithCheck(string &str, int &val);
- BOOL GetInt64WithCheck(string &str, INT64 &val);
- int GetInt(string &str);
- BOOL StrToInt32(const char * str,int *result);
- bool StrToInt64(const char * str,INT64 *result);
- string ConvUcharToU8(wstring &uniStr);
- wstring GetWcharFromChar(const char *pChar);
- template <class Type> bool StrToIntT(const char * str,Type *result)
- {
- Type value = 0;
- Type sign = 1;
- Type radix;
- if (str == NULL)
- {
- return false;
- }
- if (strlen(str) == 0)
- {
- return false;
- }
- if (*str == '-')
- {
- sign = -1;
- str++;
- }
- if (*str == '0' && (*(str + 1) == 'x' || *(str + 1) == 'X'))
- {
- radix = 16;
- str += 2;
- }
- //else if(*str == '0')
- //{
- // radix = 8;
- // str++;
- //}
- else
- {
- bool HitF = false;
- size_t Len = strlen(str);
- for (size_t i = 0; i < Len; i++)
- {
- if (str[i] >= '0' && str[i] <= '9')
- {
- continue;
- }
- HitF = true;
- break;
- }
- if (HitF)
- {
- radix = 16;
- }
- else
- {
- radix = 10;
- }
- }
- while (*str)
- {
- if (radix == 16)
- {
- if (*str >= '0' && *str <= '9')
- {
- value = value * radix + *str - '0';
- }
- else
- {
- if ((*str | 0x20) >= 'a' && (*str | 0x20) <= 'f')
- {
- value = value * radix + (*str | 0x20) - 'a' + 10;
- }
- else
- {
- return false;
- }
- }
- }
- else
- {
- value = value * radix + *str - '0';
- }
- str++;
- }
- *result = sign*value;
- return true;
- };
- template <class Type> bool GetIntWithCheckT(wstring &str, Type &val ) {
- BYTE *szSrc = new BYTE[str.size() * sizeof(WCHAR) + sizeof(WCHAR)];
- wcscpy((wchar_t *)szSrc, str.c_str());
- char *szBuf = NULL;
- DWORD dwNum = WideCharToMultiByte(CP_OEMCP, NULL, (LPCWSTR)szSrc, -1,
- NULL,0, NULL, FALSE);
- szBuf = new char[dwNum];
- WideCharToMultiByte(CP_OEMCP, NULL, (LPCWSTR)szSrc, -1,
- szBuf, dwNum, NULL, FALSE);
- Type nReturn = 0;
- if(StrToIntT(szBuf,&nReturn) == FALSE)
- {
- return false;
- }
- if (szSrc != NULL)
- {
- delete [] szSrc;
- szSrc = NULL;
- }
- if (szBuf != NULL)
- {
- delete [] szBuf;
- szBuf = NULL;
- }
- val = nReturn;
- return true;
- };
- template <class Type> bool GetIntWithCheckT1(string &str, Type &val) {
- Type nReturn = 0;
- if (StrToIntT(str.c_str(), &nReturn) == FALSE)
- {
- return false;
- }
- val = nReturn;
- return true;
- };
|