373 lines
7.9 KiB
C++
373 lines
7.9 KiB
C++
|
|
#ifndef COMMON__H_
|
|
#define COMMON__H_
|
|
|
|
#ifdef _WIN32
|
|
|
|
#pragma once
|
|
|
|
//#define FD_SETSIZE 1024
|
|
#include <winsock2.h>
|
|
#include <windows.h>
|
|
|
|
#pragma comment(lib, "winmm.lib")
|
|
|
|
#pragma warning(disable:4996)
|
|
|
|
#include <io.h>
|
|
|
|
#else
|
|
|
|
#include <unistd.h> // fork
|
|
#include <errno.h> // errno
|
|
#include <sys/wait.h> // wait
|
|
|
|
#endif
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <time.h>
|
|
#include <sys/timeb.h> // ftime
|
|
#include <fcntl.h>
|
|
#include <sys/stat.h>
|
|
|
|
#ifdef USE_STD_ASSERT
|
|
#include <assert.h>
|
|
#elif USE_TENMIN_ASSERT
|
|
|
|
#define assert(EXP) (void)((!!(EXP)) || (tmassert((#EXP), __FILE__, __LINE__), 0))
|
|
|
|
inline void tmassert(const char * psz, const char * pszFileName, unsigned nLine)
|
|
{
|
|
//ALOG(1, "[FATAL ERROR], Assertion failed : %s, file : %s, line : %d\n", psz, pszFileName, nLine);
|
|
exit(-1);
|
|
}
|
|
|
|
#else
|
|
#if _MSC_VER <= 1200
|
|
#define assert(EXP) ((void)0)
|
|
#else
|
|
#define assert(EXP) __noop
|
|
#endif
|
|
#endif
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#pragma once
|
|
|
|
#else
|
|
|
|
#ifndef NULL
|
|
#define NULL 0
|
|
#endif
|
|
|
|
typedef unsigned int DWORD;
|
|
|
|
#ifndef false
|
|
#define false 0
|
|
#endif
|
|
|
|
#ifndef true
|
|
#define true 1
|
|
#endif
|
|
|
|
#endif // _WIN32
|
|
|
|
|
|
#define TENMIN_FAILED 0
|
|
#define TENMIN_SUCCESS 1
|
|
|
|
|
|
#ifdef _WIN32
|
|
#if _MSC_VER <= 1200
|
|
#ifdef _DEBUG
|
|
#include <crtdbg.h>
|
|
#include <malloc.h>
|
|
#include <list>
|
|
#include <queue>
|
|
#include <set>
|
|
#include <map>
|
|
#include <string>
|
|
// add stl header ...
|
|
|
|
inline void * operator new(size_t nSize, LPCSTR lpszFileName, int nLine)
|
|
{
|
|
return _malloc_dbg(nSize, _NORMAL_BLOCK, lpszFileName, nLine);
|
|
}
|
|
|
|
inline void operator delete(void * p)
|
|
{
|
|
_free_dbg(p, _NORMAL_BLOCK);
|
|
}
|
|
|
|
inline void operator delete(void * p, LPCSTR, int)
|
|
{
|
|
_free_dbg(p, _NORMAL_BLOCK);
|
|
}
|
|
|
|
#define new new(__FILE__, __LINE__)
|
|
|
|
#define malloc(SIZE) _malloc_dbg(SIZE, _NORMAL_BLOCK, __FILE__, __LINE__)
|
|
#define free(PTR) _free_dbg(PTR, _NORMAL_BLOCK)
|
|
|
|
#endif // _DEBUG
|
|
#endif // _MSC_VER <= 1200
|
|
|
|
#ifndef snprintf
|
|
#define snprintf _snprintf
|
|
#endif
|
|
|
|
#ifndef strcasecmp
|
|
#define strcasecmp stricmp
|
|
#endif
|
|
|
|
#define sched_yield() Sleep(0)
|
|
|
|
#endif // _WIN32
|
|
|
|
|
|
unsigned GetRandNum(unsigned nMin, unsigned nMax); // return 0 ~ RAND_MAX
|
|
unsigned int ELFHash(const char * str, unsigned int len);
|
|
|
|
extern inline int GetLastDayOfTheMonth(int nYear, int nMonth)
|
|
{
|
|
struct tm tmTmp = { 0, 0, 0, 1, nMonth, nYear - 1900 };
|
|
time_t timeTmp = mktime(&tmTmp) - 86400;
|
|
|
|
if (timeTmp < 0)
|
|
return -1;
|
|
|
|
struct tm * ptmNewTmp = localtime(&timeTmp);
|
|
|
|
return ptmNewTmp->tm_mday;
|
|
}
|
|
|
|
extern inline unsigned int TimeDiff(unsigned int nTimePrev, unsigned int nTimeCurr)
|
|
{
|
|
if (nTimePrev <= nTimeCurr)
|
|
return nTimeCurr - nTimePrev;
|
|
|
|
return nTimeCurr + (0xFFFFFFFFU - nTimePrev) + 1U;
|
|
}
|
|
|
|
///////////////////////////////////////
|
|
#define TERR_INVALID_PROCESSNAME 2 // Failed to access()
|
|
#define TERR_FAILED_FORK 3 // Failed to fork()
|
|
#define TERR_FAILED_WAIT 4 // Failed to wait()
|
|
|
|
extern inline int ExecuteProcess(const char * pszProcessName,
|
|
const char * pszCommandLine, bool bWaiting = false, int * pnExitCode = NULL)
|
|
{
|
|
#ifdef _WIN32
|
|
STARTUPINFO startUpInfo;
|
|
memset(&startUpInfo, 0, sizeof startUpInfo);
|
|
startUpInfo.cb = sizeof(STARTUPINFO);
|
|
|
|
//char cmdline[] = "dir /w";
|
|
char cmdline[] = "calc";
|
|
|
|
PROCESS_INFORMATION processInfo;
|
|
memset(&processInfo, 0, sizeof(PROCESS_INFORMATION));
|
|
BOOL b = CreateProcess( NULL, cmdline, NULL, NULL, false,
|
|
NORMAL_PRIORITY_CLASS | CREATE_NO_WINDOW, NULL, NULL, &startUpInfo, &processInfo);
|
|
DWORD dwErr = 0;
|
|
if( b ) {
|
|
WaitForSingleObject( processInfo.hProcess, INFINITE );
|
|
GetExitCodeProcess( processInfo.hProcess, &dwErr );
|
|
CloseHandle( processInfo.hProcess );
|
|
printf("\n\n ------- ExecuteProcess error1 --------\n\n");
|
|
} else {
|
|
dwErr = GetLastError();
|
|
printf("\n\n ------- ExecuteProcess error2 --------\n\n");
|
|
}
|
|
if( dwErr ) {
|
|
// deal with error here
|
|
printf("\n\n ------- ExecuteProcess error3 --------\n\n");
|
|
}
|
|
|
|
return TENMIN_SUCCESS;
|
|
#else
|
|
if (pszProcessName == NULL || pszProcessName[0] == '\0' || access(pszProcessName, X_OK) == -1)
|
|
return TERR_INVALID_PROCESSNAME;
|
|
|
|
int nPid = fork();
|
|
if (nPid == -1)
|
|
{
|
|
return TERR_FAILED_FORK;
|
|
}
|
|
else if (nPid == 0)
|
|
{
|
|
//if (execvp(pszProcessName, (char* const*)lpszArgList) == -1)
|
|
{
|
|
if (errno)
|
|
_exit(errno);
|
|
}
|
|
|
|
_exit(255);
|
|
}
|
|
|
|
if (bWaiting)
|
|
{
|
|
int nStatVal;
|
|
if (wait(&nStatVal) == -1)
|
|
return TERR_FAILED_WAIT;
|
|
|
|
if (pnExitCode)
|
|
{
|
|
if (WIFEXITED(nStatVal))
|
|
*pnExitCode = WEXITSTATUS(nStatVal);
|
|
}
|
|
}
|
|
|
|
return TENMIN_SUCCESS;
|
|
#endif
|
|
}
|
|
|
|
#ifndef _WIN32
|
|
|
|
#if defined(__ANDROID__) || defined(ANDROID)
|
|
#ifndef S_IREAD
|
|
#define S_IREAD S_IRUSR
|
|
#endif
|
|
|
|
#ifndef S_IWRITE
|
|
#define S_IWRITE S_IWUSR
|
|
#endif
|
|
|
|
#ifndef S_IEXEC
|
|
#define S_IEXEC S_IXUSR
|
|
#endif
|
|
#endif
|
|
|
|
|
|
extern inline DWORD timeGetTime()
|
|
{
|
|
#if 0
|
|
struct timeval tv;
|
|
gettimeofday(&tv, NULL); // obsolescent at 2008
|
|
|
|
return (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
|
|
#else
|
|
struct timespec ts;
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
|
|
|
return (ts.tv_sec * 1000) + (ts.tv_nsec / 1000000);
|
|
#endif
|
|
}
|
|
#define GetTickCount timeGetTime
|
|
|
|
extern inline void Sleep(DWORD nTime)
|
|
{
|
|
#if 0
|
|
struct timeval tv;
|
|
|
|
tv.tv_sec = nTime / 1000;
|
|
tv.tv_usec = (nTime % 1000) * 1000; // msec
|
|
|
|
select(0, NULL, NULL, NULL, &tv);
|
|
#else
|
|
struct timespec ts;
|
|
|
|
ts.tv_sec = nTime / 1000;
|
|
ts.tv_nsec = (nTime % 1000) * 1000000; // nsec
|
|
|
|
nanosleep(&ts, NULL);
|
|
#endif
|
|
}
|
|
|
|
typedef struct tagSYSTEMTIME
|
|
{
|
|
DWORD wYear;
|
|
DWORD wMonth;
|
|
DWORD wDay;
|
|
DWORD wHour;
|
|
DWORD wMinute;
|
|
DWORD wSecond;
|
|
DWORD wMilliseconds;
|
|
} SYSTEMTIME, *PSYSTEMTIME;
|
|
|
|
extern inline void GetLocalTime(PSYSTEMTIME lpSystemTime)
|
|
{
|
|
struct tm * tmTmp;
|
|
struct timeb tTimeb;
|
|
|
|
ftime(&tTimeb);
|
|
tmTmp = localtime(&tTimeb.time);
|
|
|
|
lpSystemTime->wYear = tmTmp->tm_year + 1900;
|
|
lpSystemTime->wMonth = tmTmp->tm_mon + 1;
|
|
lpSystemTime->wDay = tmTmp->tm_mday;
|
|
lpSystemTime->wHour = tmTmp->tm_hour;
|
|
lpSystemTime->wMinute = tmTmp->tm_min;
|
|
lpSystemTime->wSecond = tmTmp->tm_sec;
|
|
lpSystemTime->wMilliseconds = tTimeb.millitm;
|
|
}
|
|
|
|
#endif // _WIN32
|
|
|
|
extern inline void GetCurrentDatetime(char * pszDst, int nType = 0)
|
|
{
|
|
SYSTEMTIME systemTime;
|
|
GetLocalTime(&systemTime);
|
|
|
|
if (nType == 0) // YYYY-MM-DD hh:mm:ss
|
|
sprintf(pszDst, "%.4ld-%.2ld-%.2ld %.2ld:%.2ld:%.2ld", systemTime.wYear, systemTime.wMonth, systemTime.wDay,
|
|
systemTime.wHour, systemTime.wMinute, systemTime.wSecond);
|
|
else if (nType == 1) // YYYYMMDDhhmmss
|
|
sprintf(pszDst, "%.4ld%.2ld%.2ld%.2ld%.2ld%.2ld", systemTime.wYear, systemTime.wMonth, systemTime.wDay,
|
|
systemTime.wHour, systemTime.wMinute, systemTime.wSecond);
|
|
else if (nType == 2) // YYYYMMDDhhmmss.msec
|
|
sprintf(pszDst, "%.4ld%.2ld%.2ld%.2ld%.2ld%.2ld.%.3ld", systemTime.wYear,
|
|
systemTime.wMonth, systemTime.wDay, systemTime.wHour, systemTime.wMinute, systemTime.wSecond, systemTime.wMilliseconds);
|
|
}
|
|
|
|
extern inline void GetDateTimeNum(char * pszDst)
|
|
{
|
|
SYSTEMTIME systemTime;
|
|
GetLocalTime(&systemTime);
|
|
sprintf(pszDst, "%.4ld%.2ld%.2ld%.2ld%.2ld%.2ld.%.3ld", systemTime.wYear,
|
|
systemTime.wMonth, systemTime.wDay, systemTime.wHour, systemTime.wMinute, systemTime.wSecond, systemTime.wMilliseconds);
|
|
}
|
|
|
|
extern inline void GetDateTimeNum2(char * pszDst)
|
|
{
|
|
SYSTEMTIME systemTime;
|
|
GetLocalTime(&systemTime);
|
|
sprintf(pszDst, "%.4ld%.2ld%.2ld%.2ld%.2ld%.2ld", systemTime.wYear,
|
|
systemTime.wMonth, systemTime.wDay, systemTime.wHour, systemTime.wMinute, systemTime.wSecond);
|
|
}
|
|
|
|
|
|
bool RealignPathDepth(const char * pszSrc, char * pszDst);
|
|
|
|
extern inline bool GetProcessDirectory(char * pszDir, DWORD nBufferLength)
|
|
{
|
|
#ifdef _WIN32
|
|
if (::GetModuleFileName(NULL, pszDir, nBufferLength) == FALSE)
|
|
return false;
|
|
|
|
if (::GetLongPathName(pszDir, pszDir, nBufferLength) == FALSE)
|
|
return false;
|
|
|
|
char * pTemp;
|
|
if ((pTemp = strrchr(pszDir, '\\')) != NULL ||
|
|
(pTemp = strrchr(pszDir, '/')) != NULL)
|
|
{
|
|
size_t nLen = strlen(pszDir) - strlen(pTemp);
|
|
pszDir[nLen + 1] = '\0';
|
|
}
|
|
#else
|
|
//...
|
|
|
|
#endif
|
|
return true;
|
|
}
|
|
|
|
extern inline void Hassert(bool b)
|
|
{
|
|
}
|
|
|
|
#endif // COMMON__H_
|