123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- #include "stdafx.h"
- #include <algorithm>
- #include <stdlib.h>
- //#include "common.h"
- #include "common_def.h"
- inline std::string format_string(const char* format, va_list args) {
- size_t oldlen = BUFSIZ;
- char buffer[BUFSIZ]; // 默认栈上的缓冲区
- va_list argscopy;
- va_copy(argscopy, args);
- size_t newlen = vsnprintf(&buffer[0], oldlen, format, args) + 1;
- newlen++; // 算上终止符'\0'
- if (newlen > oldlen) { // 默认缓冲区不够大,从堆上分配
- std::vector<char> newbuffer(newlen);
- vsnprintf(newbuffer.data(), newlen, format, argscopy);
- return newbuffer.data();
- }
- return buffer;
- }
- std::string format_string(const char* format, ...) {
- va_list args;
- va_start(args, format);
- string s = format_string(format, args);
- va_end(args);
- return s;
- }
- string INT64TOSTR(INT64 val)
- {
- string total;
- string lowstr, highstr;
- PUINT32 p = (PUINT32)&val;
- lowstr = format_string("%08X", *(p++));
- highstr = format_string("0X%08X", *(p));
- total = highstr + lowstr;
- transform(total.begin(), total.end(), total.begin(), toupper);
- return total;
- }
- INT64 STRTOINT64(string &str)
- {
- INT64 val = 0;
- if(StrToInt64(str.c_str(),&val) == TRUE)
- {
- return val;
- }
- assert(0);
- return 0;
- }
- bool StrToInt64(const char * str,INT64 *result)
- {
- INT64 value = 0;
- INT64 sign = 1;
- INT64 radix;
- 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
- {
- 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;
- }
- BOOL StrToInt32(const char * str,int *result)
- {
- int value = 0;
- int sign = 1;
- int radix;
- 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
- {
- 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;
- }
- wstring GetWcharFromChar(const char *pChar)
- {
- wstring wstr;
- wchar_t *pWcharBuff;
- int uLen = MultiByteToWideChar(CP_UTF8,0,pChar,-1,NULL,0);
- if(uLen > 0)
- {
- pWcharBuff = new wchar_t[uLen + 1];
- memset(pWcharBuff, 0, sizeof(wchar_t)*uLen);
- MultiByteToWideChar(CP_UTF8,0,pChar,-1, (LPWSTR )pWcharBuff,uLen);
- wstr = pWcharBuff;
- delete []pWcharBuff;
- return wstr;
- }
- return wstr;
- }
- string ConvUcharToU8(wstring &uniStr)
- {
- std::string str;
- int uLen = WideCharToMultiByte(CP_UTF8,0,uniStr.c_str(),-1,NULL,0,NULL,NULL);
- if(uLen > 0)
- {
- str.resize(uLen);
- WideCharToMultiByte(CP_UTF8, 0, uniStr.c_str(), -1, (LPSTR)&str[0], uLen, NULL, NULL);
- return str;
- }
- else
- {
- return "";
- }
- return str;
- }
- BOOL GetInt64WithCheck(string &str,INT64 &val)
- {
- INT64 nReturn = 0;
- if (StrToInt64(str.c_str(), &nReturn) == FALSE)
- {
- return FALSE;
- }
- val = nReturn;
- return TRUE;
- }
- BOOL GetIntWithCheck(string &str,int &val)
- {
- int nReturn = 0;
- if (StrToInt32(str.c_str(), &nReturn) == FALSE)
- {
- return FALSE;
- }
- val = nReturn;
- return TRUE;
- }
- int GetInt(string &str)
- {
- int nReturn = 0;
- if (StrToInt32(str.c_str(), &nReturn) == FALSE)
- {
- MessageBox(NULL, "The String Number is Wrong", "ERROR", MB_OK);
- return 0;
- }
- return nReturn;
- }
|