📄 basefunc.cpp
字号:
//////////////////////////////////////////////////////////////////////
// Purpose: base function for game
//
// WriteBy: blacknull 10-16-2002
//
// ModifyBy: blacknull 10-16-2002
//
// CopyRight: Acme Entertrainment, copyright 2002
//////////////////////////////////////////////////////////////////////
#include "basefunc.H"
#include <map>
#include <string>
#pragma comment(lib, "winmm.lib" )
// global...
HWND g_hGameWnd = NULL;
#ifdef _ANALYSIS_ON
DWORD g_dwIniGetAmount = 0;
DWORD g_dwIniSearchLineAmount = 0;
DWORD g_dwIniGetAmountFrame = 0;
DWORD g_dwIniSearchLineAmountFrame = 0;
#endif
//////////////////////////////////////////////////////////////////////
int Double2Int(double dValue)
{
if((int)(dValue+0.5) >(int)dValue)
return int(dValue)+1;
else
return int(dValue);
}
//////////////////////////////////////////////////////////////////////
BOOL IniStrGet(const char *pszFileName, const char *pszTitle, const char *pszSubTitle, char *pszStr, BOOL bCritical/*=false*/)
{
if (!pszFileName || !pszTitle || !pszSubTitle || !pszStr)
return false;
FILE* fp = fopen(pszFileName, "r");
if (!fp)
return false;
char szSection[256] = "";
sprintf(szSection, "[%s]", pszTitle);
bool bSucFound =false;
while(true)
{
char szLine[1024] ="";
if (EOF == fscanf(fp, "%s\n", szLine))
break;
if (0 != stricmp(szSection, szLine))
continue;
// section found
char szFormat[256] = "";
sprintf(szFormat, "%s=%s\n", pszSubTitle, "%s");
while (true)
{
char szLine[1024] ="";
if (EOF == fscanf(fp, "%s\n", szLine))
goto out;
if (strrchr(szLine, '[') && strrchr(szLine, ']'))
goto out;
if (sscanf(szLine, szFormat, pszStr))
{
bSucFound = true;
goto out;
}
}
}
out:
fclose(fp);
if (!bSucFound)
{
if (bCritical)
{
char szMsg[1024] = "";
sprintf(szMsg, "Error: [%s] %s not found in %s.", pszTitle, pszSubTitle, pszFileName);
::MessageBox(NULL, szMsg, "Error", MB_OK|MB_ICONERROR);
::LogMsg(szMsg);
}
else
::DebugMsg("Error: [%s] %s not found in %s.", pszTitle, pszSubTitle, pszFileName);
}
return bSucFound;
}
//////////////////////////////////////////////////////////////////////
BOOL IniDataGet(const char *pszFileName, const char *pszTitle, const char *pszSubTitle, int &iData, BOOL bCritical/*=false*/)
{
if (!pszFileName || !pszTitle || !pszSubTitle)
return false;
FILE* fp = fopen(pszFileName, "r");
if (!fp)
return false;
char szSection[256] = "";
sprintf(szSection, "[%s]", pszTitle);
bool bSucFound =false;
while(true)
{
char szLine[1024] ="";
if (EOF == fscanf(fp, "%s\n", szLine))
break;
if (0 != stricmp(szSection, szLine))
continue;
// section found
char szFormat[256] = "";
sprintf(szFormat, "%s=%s\n", pszSubTitle, "%d");
while (true)
{
char szLine[1024] ="";
if (EOF == fscanf(fp, "%s\n", szLine))
goto out;
if (strrchr(szLine, '[') && strrchr(szLine, ']'))
goto out;
if (sscanf(szLine, szFormat, &iData))
{
bSucFound = true;
goto out;
}
}
}
out:
fclose(fp);
if (!bSucFound)
{
if (bCritical)
{
char szMsg[1024] = "";
sprintf(szMsg, "Error: [%s] %s not found in %s.", pszTitle, pszSubTitle, pszFileName);
::MessageBox(NULL, szMsg, "Error", MB_OK|MB_ICONERROR);
::LogMsg(szMsg);
}
else
::DebugMsg("Error: [%s] %s not found in %s.", pszTitle, pszSubTitle, pszFileName);
}
return bSucFound;
}
//////////////////////////////////////////////////////////////////////
// 从文本内存缓冲区取得一行TXT, 回车符号0x0a或缓冲区尾部为结束, 返回 FALSE 表示读完缓冲区或者出错.
BOOL MemTxtLineGet(const char* pBuf, DWORD dwBufSize, DWORD& dwOffset, char* szLine, DWORD dwLineSize)
{
if (!pBuf || !szLine)
return FALSE;
if (dwOffset >= dwBufSize)
return FALSE;
for (DWORD i = 0; i < dwLineSize; i++)
{
szLine[i] = pBuf[dwOffset++];
if (0x0a == szLine[i] || dwOffset >= dwBufSize)
{
szLine[i] = '\0';
if (i > 0 && 0x0d == szLine[i-1])
szLine[i-1] = '\0';
break;
}
}
return (i < dwLineSize); // 文本行缓冲如果小了也会返回 FALSE
}
//////////////////////////////////////////////////////////////////////
BOOL MemIniStrGet(const char *pBuf, DWORD dwSize, const char *pszTitle, const char *pszSubTitle, char *pszStr, BOOL bCritical/*=false*/)
{
if (!pBuf || !pszTitle || !pszSubTitle || !pszStr)
return FALSE;
char szSection[256] = "";
sprintf(szSection, "[%s]", pszTitle);
DWORD dwOffset = 0;
bool bSucFound =false;
while(true)
{
// 取出一行Txt
char szLine[1024] = "";
if (!MemTxtLineGet(pBuf, dwSize, dwOffset, szLine, sizeof(szLine)))
goto out;
// 解析此行, 先寻找SECTION
if (0 != stricmp(szSection, szLine))
continue;
// section found
char szFormat[256] = "";
sprintf(szFormat, "%s=%s", pszSubTitle, "%s");
while (true)
{
char szLine[1024] ="";
if (!MemTxtLineGet(pBuf, dwSize, dwOffset, szLine, sizeof(szLine)))
goto out;
if (strrchr(szLine, '[') && strrchr(szLine, ']'))
goto out;
if (sscanf(szLine, szFormat, pszStr))
{
bSucFound = true;
goto out;
}
}
}
out:
if (!bSucFound)
{
if (bCritical)
{
char szMsg[1024] = "";
sprintf(szMsg, "Error: [%s] %s not found in MemIniStrGet.", pszTitle, pszSubTitle);
::MessageBox(NULL, szMsg, "Error", MB_OK|MB_ICONERROR);
::LogMsg(szMsg);
}
else
::DebugMsg("Error: [%s] %s not found in MemIniStrGet.", pszTitle, pszSubTitle);
}
return bSucFound;
}
//////////////////////////////////////////////////////////////////////
BOOL MemIniDataGet(const char *pBuf, DWORD dwSize, const char *pszTitle, const char *pszSubTitle, int &iData, BOOL bCritical/*=false*/)
{
if (!pBuf || !pszTitle || !pszSubTitle)
return FALSE;
char szSection[256] = "";
sprintf(szSection, "[%s]", pszTitle);
DWORD dwOffset = 0;
bool bSucFound =false;
while(true)
{
// 取出一行Txt
char szLine[1024] = "";
if (!MemTxtLineGet(pBuf, dwSize, dwOffset, szLine, sizeof(szLine)))
goto out;
// 解析此行, 先寻找SECTION
if (0 != stricmp(szSection, szLine))
continue;
// section found
char szFormat[256] = "";
sprintf(szFormat, "%s=%s", pszSubTitle, "%d");
while (true)
{
char szLine[1024] ="";
if (!MemTxtLineGet(pBuf, dwSize, dwOffset, szLine, sizeof(szLine)))
goto out;
if (strrchr(szLine, '[') && strrchr(szLine, ']'))
goto out;
if (sscanf(szLine, szFormat, &iData))
{
bSucFound = true;
goto out;
}
}
}
out:
if (!bSucFound)
{
if (bCritical)
{
char szMsg[1024] = "";
sprintf(szMsg, "Error: [%s] %s not found in MemIniDataGet.", pszTitle, pszSubTitle);
::MessageBox(NULL, szMsg, "Error", MB_OK|MB_ICONERROR);
::LogMsg(szMsg);
}
else
::DebugMsg("Error: [%s] %s not found in MemIniDataGet.", pszTitle, pszSubTitle);
}
return bSucFound;
}
//////////////////////////////////////////////////////////////////////
BOOL TxtStrGet(const char *pszFileName, const char *pszTitle, char *pszStr, BOOL bCritical/*=false*/)
{
if (!pszFileName || !pszTitle || !pszStr)
return false;
FILE* fp = fopen(pszFileName, "r");
if (!fp)
return false;
char szFormat[256] ="";
sprintf(szFormat, "%s=%s", pszTitle, "%s");
bool bSucFound =false;
while(true)
{
char szLine[1024] ="";
if (EOF == fscanf(fp, "%s\n", szLine))
break;
if (sscanf(szLine, szFormat, pszStr))
{
bSucFound =true;
break;
}
}
fclose(fp);
if (!bSucFound)
{
if (bCritical)
::LogMsg("Error: %s not found in %s.", pszTitle, pszFileName);
else
::DebugMsg("Error: %s not found in %s.", pszTitle, pszFileName);
}
return bSucFound;
}
DWORD AddHour(DWORD dwYYMMDDHH, int nAddHour)
{
// CHECKF(nAddHour > 0);
int nYear = dwYYMMDDHH / 1000000;
int nMonth = (dwYYMMDDHH % 1000000) / 10000;
int nDay = (dwYYMMDDHH % 10000) / 100;
int nHour = dwYYMMDDHH % 100;
// 闰年 (nYear % 400 == 0) || (nYear % 100 != 0 && nYear % 4 == 0)
bool bLeap = false;
if (nYear % 4 == 0) // 2位 YY
{
bLeap = true;
}
int arrLeapDays[] = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, };
int arrDays[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, };
int* _arrDays = arrDays;
if (bLeap)
{
_arrDays = arrLeapDays;
}
// CHECKF(nMonth >= 1 && nMonth <= 12 && nDay <= _arrDays[nMonth] && nHour < 24);
nHour += nAddHour;
int nAddDay = nHour / 24;
nHour %= 24;
nDay += nAddDay;
while (nDay > _arrDays[nMonth])
{
nDay -= _arrDays[nMonth];
nMonth++;
if (nMonth > 12)
{
nMonth = 0;
nYear++;
}
}
dwYYMMDDHH = nYear*1000000 + nMonth*10000 + nDay*100 + nHour;
return dwYYMMDDHH;
}
//////////////////////////////////////////////////////////////////////
BOOL TxtDataGet(const char *pszFileName, const char *pszTitle, int& nData, BOOL bCritical/*=false*/)
{
if (!pszFileName || !pszTitle)
return false;
FILE* fp = fopen(pszFileName, "r");
if (!fp)
return false;
char szFormat[256] ="";
sprintf(szFormat, "%s=%s\n", pszTitle, "%d");
bool bSucFound =false;
while(true)
{
char szLine[1024] = "";
if (EOF == fscanf(fp, "%s\n", szLine))
break;
if (sscanf(szLine, szFormat, &nData))
{
bSucFound =true;
break;
}
}
fclose(fp);
if (bCritical && !bSucFound)
::LogMsg("Error: %s not found in %s.", pszTitle, pszFileName);
return bSucFound;
}
//////////////////////////////////////////////////////////////////////
DWORD TimeGet(TIME_TYPE type)
{
DWORD dwTime = 0;
switch(type)
{
case TIME_SECOND:
dwTime = ::time(NULL);
break;
case TIME_MINUTE:
{
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%100*100000000 +
(pTime->tm_mon+1)*1000000 +
pTime->tm_mday*10000 +
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -