#include "stdafx.h" #include #include //#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 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; }