You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

943 lines
15 KiB
C++

2 years ago
//#ifdef OS_WINDOWS
#ifdef _WIN32
2 years ago
#include <windows.h>
#include <direct.h>
#include <process.h>
#else
#include <ctype.h>
#include <unistd.h>
#include <sys/time.h>
#endif
2 years ago
/*#else
2 years ago
#endif*/
#include <stdio.h>
#include <string.h>
2 years ago
#include <time.h>
#include <fcntl.h>
#include "basefunc.h"
#include <stdlib.h>
2 years ago
extern char IniFilePath[256];
void DebugPrint(char *szDebug)
{
//#ifdef OS_WINDOWS
#ifdef _WIN32
2 years ago
OutputDebugString((WCHAR*)szDebug);
OutputDebugString((WCHAR*)"\n");
#else
printf("%s\n", szDebug);
#endif
2 years ago
}
int CmpString(const u_char *str1, const u_char *str2)
{
size_t i, len;
len = strlen((const char*)str1);
if(len != strlen((const char*)str2))
2 years ago
{
return 0;
}
for (i = 0; i < len; i++)
{
if(toupper(str1[i]) != toupper(str2[i]))
{
return 0;
}
}
return 1;
}
int CmpString2(const char *str1, const char *str2)
{
size_t i, len;
len = strlen(str1);
if(len != strlen(str2))
{
return 0;
}
for (i = 0; i < len; i++)
{
if(toupper(str1[i]) != toupper(str2[i]))
{
return 0;
}
}
return 1;
}
#ifdef OS_UNIX
char *strupr(char *str)
{
char *ptr = str;
while (*ptr != '\0') {
if (islower(*ptr))
*ptr = toupper(*ptr);
ptr++;
}
return str;
}
#endif
2 years ago
BOOL IsRemarkLine(const char *str)
{
int i, iLen;
BOOL bRetVal;
bRetVal = FALSE;
iLen = strlen(str);
for(i=0; i<iLen-1; i++)
{
if(str[i] == 0x20)
{
continue;
}
if(str[i] == '#')
{
bRetVal = TRUE;
}
else if(str[i] == ';')
{
bRetVal = TRUE;
}
//else if((str[i] == '\/') && (str[i+1] == '\/'))
else if((str[i] == '/') && (str[i+1] == '/'))
{
bRetVal = TRUE;
}
break;
}
return bRetVal;
}
void SwapByteSequence(char *pData, int ilen)
{
int i, count;
char tmp;
count = (ilen+1)/2;
for(i=0; i<count; i++)
{
tmp = pData[ilen-i-1];
pData[ilen-i-1] = pData[i];
pData[i] = tmp;
}
}
BOOL IsNetSequence()
{
char *ptr;
short iValue;
ptr = (char *)&iValue;
iValue = 0x55AA;
if(ptr[0] == 0x55)
return TRUE;
else
return FALSE;
}
BOOL SequenceHostToRtu(char *pData, int ilen)
{
int i;
char buf[32];
if(ilen > sizeof(buf))
return FALSE;
if( IsNetSequence() )
{
for( i=0; i<ilen; i++ )
{
buf[i] = pData[ilen-i-1];
}
memcpy(pData, buf, ilen);
}
return TRUE;
}
BOOL SequenceRtuToHost(char *pData, int ilen)
{
return SequenceHostToRtu(pData, ilen);
}
BOOL SequenceHostToNet(char *pData, int ilen)
{
int i;
char buf[32];
if(ilen > sizeof(buf))
{
return FALSE;
}
if(IsNetSequence() == FALSE)
{
for( i=0; i<ilen; i++ )
{
buf[i] = pData[ilen-i-1];
}
memcpy(pData, buf, ilen);
}
return TRUE;
}
BOOL SequenceNetToHost(char *pData, int ilen)
{
return SequenceHostToNet(pData, ilen);
}
WORD SequenceRtuToHostWord(WORD wValue)
{
WORD retval;
retval = wValue;
SequenceRtuToHost((char *)&retval, sizeof(WORD));
return retval;
}
WORD SequenceHostToRtuWord(WORD wValue)
{
WORD retval;
retval = wValue;
SequenceHostToRtu((char *)&retval, sizeof(WORD));
return retval;
}
DWORD SequenceRtuToHostDWord(DWORD dwValue)
{
DWORD retval;
retval = dwValue;
SequenceRtuToHost((char *)&retval, sizeof(DWORD));
return retval;
}
DWORD SequenceHostToRtuDWord(DWORD dwValue)
{
DWORD retval;
retval = dwValue;
SequenceHostToRtu((char *)&retval, sizeof(DWORD));
return retval;
}
int SequenceRtuToHostint(int iValue)
{
int retval;
retval = iValue;
SequenceRtuToHost((char *)&retval, sizeof(int));
return retval;
}
int SequenceHostToRtuint(int iValue)
{
int retval;
retval = iValue;
SequenceHostToRtu((char *)&retval, sizeof(int));
return retval;
}
long SequenceRtuToHostlong(long lValue)
{
long retval;
retval = lValue;
SequenceRtuToHost((char *)&retval, sizeof(long));
return retval;
}
long SequenceHostToRtulong(long lValue)
{
long retval;
retval = lValue;
SequenceHostToRtu((char *)&retval, sizeof(long));
return retval;
}
float SequenceRtuToHostfloat(float fValue)
{
float retval;
retval = fValue;
SequenceRtuToHost((char *)&retval, sizeof(float));
return retval;
}
float SequenceHostToRtufloat(float fValue)
{
float retval;
retval = fValue;
SequenceHostToRtu((char *)&retval, sizeof(float));
return retval;
}
double SequenceRtuToHostdouble(double dbValue)
{
double retval;
retval = dbValue;
SequenceRtuToHost((char *)&retval, sizeof(double));
return retval;
}
double SequenceHostToRtudouble(double dbValue)
{
double retval;
retval = dbValue;
SequenceHostToRtu((char *)&retval, sizeof(double));
return retval;
}
//********************************************************************
//* <09><>ȡϵͳʱ<CDB3><EFBFBD><E4BAAF> *
//*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> DAY_TIME : ϵͳʱ<CDB3><EFBFBD><E4BBBA><EFBFBD><EFBFBD> *
//*<2A><><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>BOOL retval : <20>Ƿ<EFBFBD><C7B7>ɹ<EFBFBD> *
//********************************************************************
BOOL GetLocalTimeEx(DAY_TIME *ptrDateTime)
{
int msec;
struct tm *tmptr=NULL;
time_t tm_t;
#ifdef _WIN32
SYSTEMTIME sm;
GetLocalTime(&sm);
msec = sm.wMilliseconds;
#else
2 years ago
struct timeval timeval;
gettimeofday(&timeval, NULL);
msec = timeval.tv_usec/1000;
#endif
2 years ago
tm_t = time(NULL);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
//tmptr = gmtime(&tm_t);
// <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
tmptr = localtime(&tm_t);
2 years ago
if(!tmptr)
{
return FALSE;
}
ptrDateTime->Year = tmptr->tm_year + 1900;
ptrDateTime->Month = tmptr->tm_mon + 1;
ptrDateTime->Day = tmptr->tm_mday;
ptrDateTime->Hour = tmptr->tm_hour;
ptrDateTime->Min = tmptr->tm_min;
ptrDateTime->Sec = tmptr->tm_sec;
ptrDateTime->mSec = msec;
ptrDateTime->WeekDay = tmptr->tm_wday;
ptrDateTime->MonthDay = tmptr->tm_mday;
return TRUE;
}
//********************************************************************
//* <09><><EFBFBD><EFBFBD>ϵͳʱ<CDB3><EFBFBD><E4BAAF> *
//*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> DAY_TIME : ʱ<><EFBFBD><E4BBBA><EFBFBD><EFBFBD> *
//*<2A><><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>BOOL retval : <20>Ƿ<EFBFBD><C7B7>ɹ<EFBFBD> *
//********************************************************************
//BOOL SetLocalTimeEx(DAY_TIME *ptrDateTime)
void SetLocalTimeEx(DAY_TIME *ptrDateTime)
{
#ifdef _WIN32
SYSTEMTIME systime;
BOOL bRetVal;
systime.wYear = ptrDateTime->Year;
systime.wMonth = ptrDateTime->Month;
//systime.wDayOfWeek = ptrDateTime->wDayOfWeek;
systime.wDay = ptrDateTime->Day;
systime.wHour = ptrDateTime->Hour;
systime.wMinute = ptrDateTime->Min;
systime.wSecond = ptrDateTime->Sec;
systime.wMilliseconds = ptrDateTime->mSec;
bRetVal = SetLocalTime(&systime);
//if(!bRetVal) return FALSE;
//else return TRUE;
#else
2 years ago
//int ret;
//pid_t pid;
//char szCmdLine[256];
//sprintf(szCmdLine, "date %02d%02d%02d%02d%04d.%02d",
// ptrDateTime->Month,
// ptrDateTime->Day,
// ptrDateTime->Hour,
// ptrDateTime->Min,
// ptrDateTime->Year,
// ptrDateTime->Sec);
//pid = vfork();
//if(pid == 0)
//{
// ret = system (szCmdLine); //ִ<><D6B4>date<74><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
// exit(0);
// //return exit(0);
// //if(ret == 0)
// //return TRUE;
// //else
// // return FALSE;
//}
////else return TRUE;
struct tm tm_st;
time_t tm_t;
tm_st.tm_year = ptrDateTime->Year - 1900;
tm_st.tm_mon = ptrDateTime->Month - 1;
tm_st.tm_mday = ptrDateTime->Day;
tm_st.tm_hour = ptrDateTime->Hour;
tm_st.tm_min = ptrDateTime->Min;
tm_st.tm_sec = ptrDateTime->Sec;
//printf("TIP_(%04d): %02d/%02d/%04d_%02d:%02d:%02d.\n",
// getpid(), tm_st.tm_mon+1, tm_st.tm_mday, tm_st.tm_year+1900,
// tm_st.tm_hour, tm_st.tm_min, tm_st.tm_sec);
tm_t = mktime(&tm_st);
if(tm_t > 0)
{
stime(&tm_t);
//printf("TIP_(%04d): set time is ok.\n", getpid());
}
#endif
2 years ago
}
_int64 SystemTimeToMillseconds(DAY_TIME *lpTime, int i8HourOffset)
{
_int64 iMilliseconds;
time_t iTime;
struct tm changetm;
//struct tm *testtm;
iMilliseconds=0;
changetm.tm_hour = lpTime->Hour;
changetm.tm_mday = lpTime->Day;
changetm.tm_min = lpTime->Min;
changetm.tm_mon = lpTime->Month - 1;
changetm.tm_sec = lpTime->Sec;
changetm.tm_year = lpTime->Year - 1900;
changetm.tm_isdst = -1;
iTime = mktime(&changetm);
//testtm = localtime( &iTime );
if(iTime == (time_t)-1)
return 0;
iMilliseconds = iTime - i8HourOffset * 3600 * 8;
iMilliseconds = iMilliseconds*1000 + lpTime->mSec;
return iMilliseconds;
}
void LogRunRecord(char *pstrLogMsg, char *pstrfilename)
{
int ilen;
DAY_TIME sCurtime;
char szFileName[256];
#ifdef _WIN32
2 years ago
FILE *fp;
GetLocalTimeEx(&sCurtime);
//getcwd(szFileName, sizeof(szFileName)-16);
if(pstrfilename == NULL)
{
sprintf(szFileName, "%s/%04d%02d%02d.log", IniFilePath, sCurtime.Year, sCurtime.Month, sCurtime.Day);
}
else
{
ilen = strlen(pstrfilename);
if((ilen < 3) || (ilen >= (sizeof(szFileName)-1)))
{
sprintf(szFileName, "%s/%04d%02d%02d.log", IniFilePath,
sCurtime.Year, sCurtime.Month, sCurtime.Day);
}
else
{
strcpy(szFileName, pstrfilename);
}
}
fp = fopen(szFileName, "ab");
if(fp != NULL)
{
fwrite(pstrLogMsg, sizeof(char), strlen(pstrLogMsg)+1, fp);
fclose(fp);
}
#else
2 years ago
int fp;
GetLocalTimeEx(&sCurtime);
if(pstrfilename == NULL)
{
sprintf(szFileName, "/log/%04d%02d%02d.log", IniFilePath,
sCurtime.Year, sCurtime.Month, sCurtime.Day);
}
else
{
ilen = strlen(pstrfilename);
if((ilen < 3) || (ilen >= (sizeof(szFileName)-1)))
{
sprintf(szFileName, "/log/%04d%02d%02d.log", IniFilePath,
sCurtime.Year, sCurtime.Month, sCurtime.Day);
}
else
{
strcpy(szFileName, pstrfilename);
}
}
fp = open(szFileName, O_CREAT|O_RDWR|O_APPEND);
if(fp < 0)
{
printf("TIP_(%04d): open %s is Failed.\n", getpid(), szFileName);
}
else
{
ilen = strlen(pstrLogMsg);
write(fp, pstrLogMsg, ilen);
close(fp);
}
#endif
2 years ago
}
BOOL CheckTcpIpAddr(char *addr)
{
int d;
char *ptr;
if(strlen(addr) < 7)
return FALSE;
if(strlen(addr) > 20)
return FALSE;
d = atoi(addr);
if((d < 0) || (d > 255))
{
return FALSE;
}
ptr = strstr(addr, ".");
if(ptr == NULL)
{
return FALSE;
}
ptr++;
d = atoi(ptr);
if((d < 0) || (d > 255))
{
return FALSE;
}
ptr = strstr(ptr, ".");
if(ptr == NULL)
{
return FALSE;
}
ptr++;
d = atoi(ptr);
if((d < 0) || (d > 255))
{
return FALSE;
}
ptr = strstr(ptr, ".");
if(ptr == NULL)
{
return FALSE;
}
ptr++;
d = atoi(ptr);
if((d < 0) || (d > 255))
{
return FALSE;
}
return TRUE;
}
WORD CalCheckout(u_char *buf, int len)
{
int i;
WORD sum;
sum = 0;
for(i = 0; i < len; i++)
{
sum += buf[i];
}
return sum;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// wen 2005.10.19 <20><><EFBFBD>ӻ<EFBFBD><D3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int IsHexChar(u_char c)
{
if((c >= '0') && (c <= '9'))
return 1;
if((c >= 'A') && (c <= 'F'))
return 1;
if((c >= 'a') && (c <= 'f'))
return 1;
return 0;
}
int HexToDec(u_char c)
{
if((c >= '0') && (c <= '9'))
return (c - '0');
if((c >= 'A') && (c <= 'F'))
return (c - 'A' + 10);
if((c >= 'a') && (c <= 'f'))
return (c - 'a' + 10);
return 0;
}
u_long atoul(char *buf)
{
int j, len;
u_long val;
char szbuf[256];
if(!buf)
return 0;
if(strlen(buf) == 0)
return 0;
if(buf[0] != '-')
strcpy(szbuf, buf);
else
strcpy(szbuf, &buf[1]);
val = 0;
len = strlen(szbuf);
if((szbuf[0] == '0') && ((szbuf[1] == 'x')||(szbuf[1]=='X')))
{
for(j = 2; j < len; j++)
{
if(IsHexChar(szbuf[j]) == 0)
break;
val = val * 16 + HexToDec(szbuf[j]);
}
}
else
{
for(j = 0; j < len; j++)
{
if((szbuf[j] < '0') || (szbuf[j] > '9'))
break;
val = val*10 + (szbuf[j] - '0');
}
}
return val;
}
/* !
\brief ɾ<EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>еĿո<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Լ<EFBFBD>ע<EFBFBD>ͷ<EFBFBD>
\param str[in/out] -- Ҫɾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>
\<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ retval-- <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
int StringCutSpace(char *str)
{
int i, j, flag, len;
char *ptr;
if(!str)
return 0;
len = (int)strlen(str);
if(len == 0)
return 0;
j = 0;
flag = 0;
for(i = 0; i < len; i++)
{
// <20><>˫<EFBFBD><CBAB><EFBFBD><EFBFBD>" "<22>е<EFBFBD><D0B5>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȥ<EFBFBD>ո<EFBFBD><D5B8><EFBFBD><EFBFBD>Ʊ<EFBFBD><C6B1><EFBFBD><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD>
if(str[i] == '\"')
{
flag = flag ? 0 : 1;
continue;
}
if(flag == 0)
{
if((str[i] == ' ') || (str[i] == '\t'))
continue;
}
str[j++] = str[i];
}
str[j] = 0;
ptr = strstr(str, "//");
if(ptr != NULL)
*ptr = 0x00;
ptr = strstr(str, "/*");
if(ptr != NULL)
*ptr = 0x00;
ptr = strstr(str, "\n");
if(ptr != NULL)
*ptr = 0x00;
ptr = strstr(str, "\r");
if(ptr != NULL)
*ptr = 0x00;
ptr = strstr(str, "#");
if(ptr != NULL)
*ptr = 0x00;
return strlen(str);
}
void StringToUpper(char *buf)
{
int i, len;
if(!buf)
return;
len = strlen(buf);
for(i = 0; i < len; i++)
{
//buf[i] = toupper(buf[i]);
if((buf[i] >= 'a') && (buf[i] <= 'z'))
{
buf[i] -= 32;
}
}
//printf("wen: Upperchar=%s\n", buf);
}
void StringToLower(char *buf)
{
int i, len;
if(!buf)
return;
len = strlen(buf);
for(i = 0; i < len; i++)
{
//buf[i] = tolower(buf[i]);
if((buf[i] >= 'A') && (buf[i] <= 'Z'))
{
buf[i] += 32;
}
}
//printf("wen: Lowerchar=%s\n", buf);
}
void PrintFormatMessage(int iMsgClass, char *ptrMsg)
{
/*
int ilen;
char szMsg[256];
if(ptrMsg == NULL)
{
return;
}
ilen = strlen(ptrMsg);
if(ilen > 200)
{
ptrMsg[200] = 0;
}
switch(iMsgClass)
{
// !<21><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
case MSG_CLASS_WARN:
sprintf(szMsg, "WARN(%04d): %s\n", getpid(), ptrMsg);
break;
/* !<21><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
case MSG_CLASS_ERROR:
sprintf(szMsg, "ERR_(%04d): %s\n", getpid(), ptrMsg);
break;
/* !<21><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>ʾ<EFBFBD><CABE>Ϣ
case MSG_CLASS_TIP:
default:
sprintf(szMsg, "TIP_(%04d): %s\n", getpid(), ptrMsg);
break;
}
#ifdef OS_WINDOWS
OutputDebugString(szMsg);
#else
printf("%s", szMsg);
#endif*/
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// <20><>ʱ<EFBFBD>ж<EFBFBD>
BOOL JudgeTimeOut(DAY_TIME *pStartTime, int iTimeOutSeconds)
{
int iDays, iVal1, iVal2;
int iAllSeconds;
DAY_TIME sCurTime;
GetLocalTimeEx(&sCurTime);
iDays = sCurTime.Day - pStartTime->Day;
if((iDays < 0) || abs(iDays) > 1)
{
return TRUE;
}
iVal1 = sCurTime.Hour*3600 + sCurTime.Min*60 + sCurTime.Sec;
iVal2 = pStartTime->Hour*3600 + pStartTime->Min*60 + pStartTime->Sec;
iAllSeconds = iVal1 - iVal2;
if(iAllSeconds < 0)
{
iAllSeconds += 24*3600;
}
if(iAllSeconds < iTimeOutSeconds)
{
return FALSE;
}
return TRUE;
}
// CRCУ<43><D0A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E3BAAF>
void CalcCRCTableEx(WORD *pwCrcTable, WORD wDivisor)
{
int i, j, k;
WORD ench;
for ( i = 0; i < 256; i++ )
{
ench = 0;
for(k=0; k<8; k++)
{
if(i & (0x01<<k))
ench |= (0x8000>>k);
}
for ( j = 0; j < 8; j++ )
{
if ( (ench & 0x8000) != 0 )
{
ench = ench << 1;
ench = ench ^ wDivisor;
}
else
{
ench = ench << 1;
}
}
pwCrcTable[i]=0;
for(k=0; k<16; k++)
{
if(ench & (0x0001<<k))
pwCrcTable[i] |= (0x8000>>k);
}
}
}
// <20><><EFBFBD><EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD>
BYTE CalcAllDataLpc(BYTE *pData, int iLen)
{
int i;
BYTE retval;
retval = pData[0];
for(i=1; i<iLen; i++)
{
retval += pData[i];
}
return retval;
}
WORD CalcAllDataLpcW(BYTE *pData, int iLen)
{
int i;
WORD retval;
retval = pData[0];
for(i=1; i<iLen; i++)
{
retval += pData[i];
}
return retval;
}