📄 basefunc.cpp
字号:
pTime->tm_hour*100 +
pTime->tm_min;
}
break;
case TIME_DAY:
{
time_t long_time;
time( &long_time ); /* Get time as long integer. */
struct tm *pTime;
pTime = localtime( &long_time ); /* Convert to local time. */
dwTime = pTime->tm_year*10000 +
(pTime->tm_mon+1)*100 +
pTime->tm_mday;
}
break;
case TIME_DAYTIME:
{
time_t long_time;
time( &long_time ); /* Get time as long integer. */
struct tm *pTime;
pTime = localtime( &long_time ); /* Convert to local time. */
dwTime = pTime->tm_hour*10000 +
pTime->tm_min *100 +
pTime->tm_sec;
}
break;
case TIME_STAMP:
{
time_t long_time;
time( &long_time ); /* Get time as long integer. */
struct tm *pTime;
pTime = localtime( &long_time ); /* Convert to local time. */
dwTime = (pTime->tm_mon+1)*100000000 +
pTime->tm_mday*1000000 +
pTime->tm_hour*10000 +
pTime->tm_min *100 +
pTime->tm_sec;
}
break;
default:
dwTime = ::timeGetTime();
break;
}
return dwTime;
}
DWORD SysTimeGetEx(void)
{
time_t t;
time(&t);
return t;
}
//////////////////////////////////////////////////////////////////////
DWORD SysTimeGet(void)
{
time_t long_time;
time( &long_time ); /* Get time as long integer. */
struct tm *pTime;
pTime = localtime( &long_time ); /* Convert to local time. */
DWORD dwTime =pTime->tm_hour*100 + pTime->tm_min;
return dwTime;
}
/////////////////////////////////////////////////////////////////////////////
void LogMsg (const char* fmt, ...)
{
if (!fmt)
return;
char szFileName[] ="sys";
time_t ltime;
time( <ime );
struct tm *pTime;
pTime = localtime( <ime ); /* Convert to local time. */
char buffer[1024];
vsprintf( buffer, fmt, (char*) ((&fmt)+1) );
char szLogName[256];
sprintf(szLogName, "%s %d-%d.log", szFileName, (pTime->tm_mon+1)%12, pTime->tm_mday);
FILE* fp =fopen(szLogName, "a+");
if(!fp)
return;
fprintf(fp, "%s -- %s", buffer, ctime(<ime));
fclose(fp);
}
/////////////////////////////////////////////////////////////////////////////
void Log(const char* szFile, const char* fmt, ...)
{
if (!fmt || !szFile)
return;
time_t ltime;
time( <ime );
struct tm *pTime;
pTime = localtime( <ime ); /* Convert to local time. */
char buffer[1024];
vsprintf( buffer, fmt, (char*) ((&fmt)+1) );
char szLogName[256];
sprintf(szLogName, "%s %d-%d.log", szFile, (pTime->tm_mon+1)%12, pTime->tm_mday);
FILE* fp =fopen(szLogName, "a+");
if(!fp)
return;
fprintf(fp, "%s -- %s \r\n", ctime(<ime), buffer);
fclose(fp);
}
/////////////////////////////////////////////////////////////////////////////
void DebugMsg (const char* fmt, ...)
{
char szMsg[1024];
vsprintf( szMsg, fmt, (char*) ((&fmt)+1) );
strcat(szMsg, "\n");
::OutputDebugString(szMsg);
}
/////////////////////////////////////////////////////////////////////////////
void ErrorOut (const char* fmt, ...)
{
static BOOL bShowing =false;
if (bShowing)
return;
char szMsg[1024];
vsprintf( szMsg, fmt, (char*) ((&fmt)+1) );
bShowing =true;
::MessageBox(NULL, szMsg, "Error", MB_OK|MB_ICONERROR|MB_SYSTEMMODAL);
bShowing =false;
exit(-1);
}
//////////////////////////////////////////////////////////////////////
void ErrorMsg(const char* fmt, ...)
{
static BOOL bShowing =false;
if (bShowing)
{
::abort();
return;
}
char szMsg[1024];
vsprintf( szMsg, fmt, (char*) ((&fmt)+1) );
bShowing =true;
::MessageBox(NULL, szMsg, "Error", MB_OK|MB_ICONERROR);
bShowing =false;
}
//////////////////////////////////////////////////////////////////////
int RandGet(int nMax, BOOL bRealRand)
{
if(nMax == 0)
return 0;
if (bRealRand)
::srand( (unsigned)TimeGet() );
return ::rand()%nMax;
}
//////////////////////////////////////////////////////////////////////
DWORD Str2ID (const char* str)
{
DWORD dwHash = 0;
while (*str)
dwHash = (dwHash << 5) + dwHash + *str++;
return dwHash;
}
//////////////////////////////////////////////////////////////////////
__int64 DataReplace (__int64 iData, UINT uLowDigit, UINT uHighDigit, DWORD dwReplaceNum)
{
if (uLowDigit > uHighDigit)
{
UINT temp = uHighDigit;
uHighDigit = uLowDigit;
uLowDigit = temp;
}
__int64 iLowBase = __int64(pow(10, uLowDigit));
__int64 iHighBase = __int64(pow(10, uHighDigit + 1));
__int64 iLowData = uLowDigit == 0 ? 0 : iData % iLowBase;
__int64 iHighData = iData / iHighBase * iHighBase;
__int64 iReplaceData = dwReplaceNum * iLowBase;
return iHighData + iReplaceData + iLowData;
}
//////////////////////////////////////////////////////////////////////
__int64 DataTake (__int64 iData, UINT uLowDigit, UINT uHighDigit)
{
if (uLowDigit > uHighDigit)
{
UINT temp = uHighDigit;
uHighDigit = uLowDigit;
uLowDigit = temp;
}
__int64 iLowBase = __int64(pow(10, uLowDigit));
__int64 iTakeMask = __int64(pow(10, uHighDigit - uLowDigit + 1));
return (iData / iLowBase) % iTakeMask;
}
//////////////////////////////////////////////////////////////////////
void RepairString (unsigned char* pszString)
{
if (true)
return;
const unsigned char VALID_CHAR = '?';
if (!pszString)
return;
int nLen =strlen((const char*)pszString);
for (int i=0; i < nLen; i++)
{
unsigned char c =(unsigned char)pszString[i];
if (c >= 0x81 && c <= 0xfe)
{
// the last char is cut improperly
if (i+1 >= nLen)
{
pszString[i] = VALID_CHAR;
continue;
}
// special case
{
if (0xa1 == pszString[i] && 0xa1 == pszString[i+1])
{
pszString[i] = VALID_CHAR;
pszString[i+1] = VALID_CHAR;
i++;
continue;
}
/*
if (pszString[i] >= 0xa4 && pszString[i] <= 0xa9)
{
pszString[i] = VALID_CHAR;
pszString[i+1] = VALID_CHAR;
i++;
continue;
}
*/
}
// the second char is outof range
unsigned char c2 =(unsigned char)pszString[i+1];
if (c2 < 0x40 && c2 > 0x7e && c2 < 0x80 && c2 > 0xfe)
{
pszString[i] = VALID_CHAR;
pszString[i+1] = VALID_CHAR;
i++;
continue;
}
else
i++;
}
else
{
if (c == 0x80)
{
pszString[i] = VALID_CHAR;
}
}
}
}
//////////////////////////////////////////////////////////////////////
BOOL StringCheck(char* pszString)
{
if (!pszString)
return false;
int nLen =strlen(pszString);
for (int i=0; i < nLen; i++)
{
unsigned char c =(unsigned char)pszString[i];
if (c >= 0x81 && c <= 0xfe)
{
if (i+1 >= nLen)
return false;
unsigned char c2 =(unsigned char)pszString[i+1];
if (c2 < 0x40 || (c2 > 0x7e && c2 < 0x80) || c2 > 0xfe) // gb-2312
return false;
else
i++;
}
else
{
if (c==0x80)
return false;
}
}
return true;
}
//////////////////////////////////////////////////////////////////////
// 探测字符串中第idx个字符是否是DBCS的LEAD BYTE
BOOL IsDBCSLeadByte (const char* pszString, int idx)
{
if (!pszString || idx >= strlen(pszString))
return FALSE;
BOOL bLeadByte = FALSE;
for (int i = 0; i < idx; i++)
{
if (bLeadByte)
{
bLeadByte = FALSE;
continue;
}
bLeadByte = ::IsDBCSLeadByte(pszString[i]);
}
return bLeadByte ? FALSE : IsDBCSLeadByte(pszString[idx]);
}
//////////////////////////////////////////////////////////////////////
void ReplaceString(char* pszString, char cFind, char cReplace)
{
if(!pszString)
return;
DWORD dwLength = strlen(pszString);
if (dwLength > 1024)
return;
for(DWORD i = 0; i < dwLength; i ++)
{
if(pszString[i] == cFind)
pszString[i] = cReplace;
}
}
//////////////////////////////////////////////////////////////////////
DWORD GetDate()
{
time_t ltime;
time(<ime);
struct tm *pTime;
pTime = localtime( <ime );
return pTime->tm_mday;
}
//////////////////////////////////////////////////////////////////////
void SafeStrcpy (char* pszTarget, const char* pszSource, int nBufSize)
{
if (!pszTarget || !pszSource || nBufSize <= 0)
return;
if ((int)strlen(pszSource) >= nBufSize)
{
strncpy(pszTarget, pszSource, nBufSize-1);
pszTarget[nBufSize-1] = 0;
}
else
strcpy(pszTarget, pszSource);
}
//////////////////////////////////////////////////////////////////////
// mouse functions
//////////////////////////////////////////////////////////////////////
static CMouseInfo infoMouse;
void MouseInit(void)
{
infoMouse.iEvent =_MOUSE_NONE;
}
void MouseSet(int x, int y, int event)
{
infoMouse.iEvent =event;
infoMouse.iPosX =x;
infoMouse.iPosY =y;
}
int MouseCheck(int& iMouseX, int& iMouseY)
{
iMouseX =infoMouse.iPosX;
iMouseY =infoMouse.iPosY;
return infoMouse.iEvent;
}
void MouseProcess(void)
{
infoMouse.iEvent =_MOUSE_NONE;
}
#ifdef WM_MY_MESSAGE
void PostCmd(DWORD dwCommand, int nData)
{
if(g_hGameWnd)
::PostMessage(g_hGameWnd, WM_MY_MESSAGE, dwCommand, nData);
}
#endif
void SocketClose(SOCKET& sock)
{
if (INVALID_SOCKET != sock) {
closesocket(sock);
sock = INVALID_SOCKET;
}
}
void SimpleEncrypt(void* pBuf, ULONG ulSize)
{
if (!pBuf || ulSize <= 0)
return;
ULONG* buf = (ULONG*)pBuf;
for (ULONG i = 0; ((i + 1) * sizeof(ULONG)) < ulSize; i++)
{
buf[i] = buf[i]^0xde12af3d;
}
}
unsigned char RC5PASSWORD_KEY[] = {0x3C, 0xDC, 0xFE, 0xE8, 0xC4, 0x54, 0xD6, 0x7E,0x16, 0xA6, 0xF8, 0x1A, 0xE8, 0xD0, 0x38, 0xBE};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -