/************************************************************************/
/* PocGateWay: PocConfig.h */
/************************************************************************/
#if !defined(AFX_POCCONFIG_H__A5C2097D_D8B6_4479_BF25_99548AAD35AF__INCLUDED_)
#define AFX_POCCONFIG_H__A5C2097D_D8B6_4479_BF25_99548AAD35AF__INCLUDED_
#pragma once
#include <string>
#include <vector>
#include "PLog.h"
/************************************************************************/
class PocConfig
{
public:
PocConfig();
PocConfig(string strPath);
virtual ~PocConfig();
unsigned int getIntData( const string& strSection, const string& strKey, const unsigned int uiDefault = 0 );
string getStringData( const string& strSection, const string& strKey, const string& strDefault = string("") );
bool getHexIntData( const string& strSection, const string& strKey, unsigned char* pbHex, unsigned int& uiLen );
size_t getKeyNames( const string& strSection, vector<string>& vcKeys );
size_t getSectionNames( vector<string>& vcNames );
bool writeStringData( const string& strSection, const string& strKey, const string& strVal );
bool writeIntData( const string& strSection, const string& strKey, const unsigned int uiVal );
bool writeHexIntData( const string& strSection, const string& strKey, unsigned char* pbHex, unsigned int uiLen );
private:
string getCurrentPathFileName();
bool StrToHex( const string& strHex, unsigned char* pbHex, unsigned int& uiHexBuf );
string m_strIniPathFileName;
};
#endif // !defined(AFX_POCCONFIG_H__A5C2097D_D8B6_4479_BF25_99548AAD35AF__INCLUDED_)
/************************************************************************/
/* PocGateWay: PocConfig.cpp */
/************************************************************************/
#include "PLog.h"
#include "PocConfig.h"
#include <io.h>
#pragma warning( disable:4996 )
/************************************************************************/
#define BUFF_MAX_LEN (1024)
//const int BUFF_MAX_LEN = 1024;
//
// Construction/Destruction
//
PocConfig::PocConfig()
{
m_strIniPathFileName = string("");
string strCurPath = getCurrentPathFileName();
if ( !strCurPath.empty() )
{
int nSize = strCurPath.find_last_of( '\\' );
strCurPath.erase( nSize+1, strCurPath.length()-nSize-1 );
strCurPath += ("ADKCore.ini");
m_strIniPathFileName = strCurPath;
}
}
PocConfig::PocConfig(string strPath)
{
m_strIniPathFileName = strPath;
}
PocConfig::~PocConfig()
{
}
string PocConfig::getCurrentPathFileName()
{
CHAR pszFilePath[MAX_PATH*2] = {0};
DWORD dwLen = GetModuleFileName( NULL, pszFilePath, MAX_PATH*2 );
if ( 0 == dwLen )
{
LPVOID lpMsgBuf;
FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL );
LocalFree( lpMsgBuf );
return string("");
}
if ( -1 == _access( pszFilePath, 0 ) )
{
HANDLE hFile = CreateFile( pszFilePath, 0, 0, NULL, 0, 0, 0 );
if ( hFile != INVALID_HANDLE_VALUE )
{
CloseHandle( hFile );
}
}
return string( pszFilePath );
}
string PocConfig::getStringData( const string& strSection, const string& strKey, const string& strDefault )
{
if ( m_strIniPathFileName.empty() )
{
return string("");
}
CHAR pszReturn[1024] = {0};
DWORD dwGetLen = GetPrivateProfileString( strSection.c_str(), strKey.c_str(), strDefault.c_str(), pszReturn,
1024, m_strIniPathFileName.c_str() );
return string(pszReturn);
}
unsigned int PocConfig::getIntData( const string& strSection, const string& strKey, const unsigned int nDefault )
{
if ( m_strIniPathFileName.empty() )
{
return 0;
}
return GetPrivateProfileInt( strSection.c_str(), strKey.c_str(), nDefault, m_strIniPathFileName.c_str() );
}
bool PocConfig::writeIntData( const string& strSection, const string& strKey, const unsigned int uiVal )
{
if ( m_strIniPathFileName.empty() )
{
return false;
}
CHAR pszChar[32] = {0};
sprintf_s( pszChar, 32, ("%d"), uiVal );
return writeStringData(strSection, strKey, string(pszChar));
}
size_t PocConfig::getSectionNames( vector<string>& vcNames )
{
CHAR pszBuffer[BUFF_MAX_LEN] = {0};
DWORD dwLen = GetPrivateProfileSectionNames(pszBuffer, BUFF_MAX_LEN, m_strIniPathFileName.c_str());
if ( dwLen == 0 )
{
return 0;
}
DWORD nPos = 0;
while ( nPos < dwLen )
{
string strName = string(pszBuffer+nPos);
vcNames.push_back( strName );
nPos += strName.size()+1;
}
return vcNames.size();
}
bool PocConfig::StrToHex( const string& strHex, unsigned char* pbHex, unsigned int& uiHexBuf )
{
if ( uiHexBuf < strHex.length()/2 )
return false;
for ( int i=0; i<(int)strHex.length(); i+=2 )
{
TCHAR p = strHex[i];
if ( p >= '0' && p <= '9')
{
pbHex[i/2] = (p-'0')<<4;
}
else if ( p>='a' && p<='f')
{
pbHex[i/2] = ((p-'a')+10)<<4;
}
else if( p>='A' && p<='F' )
{
pbHex[i/2] = ((p-'A')+10)<<4;
}
else
{
return false;
}
p = strHex[i+1];
if ( p >= '0' && p <= '9')
{
pbHex[i/2] += (p-'0');
}
else if ( p>='a' && p<='f')
{
pbHex[i/2] += ((p-'a')+10);
}
else if( p>='A' && p<='F' )
{
pbHex[i/2] += ((p-'A')+10);
}
else
{
return false;
}
}
uiHexBuf = strHex.size()/2;
return true;
}
bool PocConfig::getHexIntData( const string& strSection, const string& strKey, unsigned char* pbHex, unsigned int& uiLen )
{
if ( pbHex == NULL || uiLen < 1 )
return false;
string strVal = getStringData( strSection, strKey );
if ( strVal.empty() )
return false;
if ( strVal.size()%2 != 0 )
return false;
return StrToHex( strVal.c_str(), pbHex, uiLen );
}
size_t PocConfig::getKeyNames( const string& strSection, vector<string>& vcKeys )
{
CHAR pszBuffer[BUFF_MAX_LEN] = {0};
DWORD dwLen = GetPrivateProfileString( strSection.c_str(), NULL, NULL, pszBuffer, BUFF_MAX_LEN, m_strIniPathFileName.c_str() );
if ( dwLen == 0 )
{
return 0;
}
DWORD nPos = 0;
while ( nPos < dwLen )
{
string strKey = string(pszBuffer+nPos);
vcKeys.push_back( strKey );
nPos += strKey.size()+1;
}
return vcKeys.size();
}
bool PocConfig::writeHexIntData( const string& strSection, const string& strKey, unsigned char* pbHex, unsigned int uiLen )
{
if ( pbHex == NULL || uiLen < 1 )
return false;
CHAR *pszBuffer = new CHAR[(uiLen+1)*2];
for ( unsigned int i=0; i<uiLen; i++ )
{
sprintf_s( pszBuffer+i, 1, ("%01X" ), pbHex[i] );
}
string str(pszBuffer);
delete [] pszBuffer;
return writeStringData( strSection, strKey, str );
}
bool PocConfig::writeStringData( const string& strSection, const string& strKey, const string& strVal )
{
if ( !WritePrivateProfileString( strSection.c_str(), strKey.c_str(), strVal.c_str(), m_strIniPathFileName.c_str() ) )
return false;
else
return true;
}
/************************************************************************/
/************************************************************************/
/* PocGateWay: PLog.h */
/************************************************************************/
#pragma once
#include <io.h>
#include <new.h>
#include <time.h>
#include <fcntl.h>
#include <stdio.h>
#include <direct.h>
#include <stdlib.h>
#include <windows.h>
#include <process.h>
#include <map>
#include <set>
#include <queue>
#include <vector>
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
using namespace std;
/************************************************************************/
//精简版:正常日志info打印
#define LOG_INFO_S(format, ...) \
{ \
time_t t = time(0); \
struct tm ttt; \
localtime_s(&ttt,&t); \
fprintf(stdout, "[INFO] [Tid:%5d] [%02d:%02d:%02d] [L:%3d] " format "\n", \
GetCurrentThreadId(), ttt.tm_hour,ttt.tm_min, ttt.tm_sec, __LINE__, ##__VA_ARGS__); \
}
//精简版:错误日志info打印
#define LOG_ERR_S(format, ...) \
{ \
time_t t = time(0); \
struct tm ttt; \
localtime_s(&ttt,&t); \
fprintf(stderr, "[ERROR] [Tid:%5d] [%02d:%02d:%02d] [L:%3d] " format "\n", \
GetCurrentThreadId(), ttt.tm_hour,ttt.tm_min, ttt.tm_sec, __LINE__, ##__VA_ARGS__); \
}
//正常日志info打印
#define LOG_INFO(format, ...) \
{ \
time_t t = time(0); \
struct tm ttt; \
localtime_s(&ttt,&t); \
fprintf(stdout, "[INFO] [%5d %4d-%02d-%02d %02d:%02d:%02d] [%s:%d] " format "\n", \
GetCurrentThreadId(), ttt.tm_year + 1900, ttt.tm_mon + 1, ttt.tm_mday, ttt.tm_hour, \
ttt.tm_min, ttt.tm_sec, __FUNCTION__ , __LINE__, ##__VA_ARGS__); \
}
//错误日志error打印
#define LOG_ERR(format, ...) \
{ \
time_t t = time(0); \
struct tm ttt; \
localtime_s(&ttt,&t); \
fprintf(stderr, "[ERROR] [%5d %4d-%02d-%02d %02d:%02d:%02d] [%s:%d] " format "\n", \
GetCurrentThreadId(), ttt.tm_year + 1900, ttt.tm_mon + 1, ttt.tm_mday, ttt.tm_hour, \
ttt.tm_min, ttt.tm_sec, __FUNCTION__ , __LINE__, ##__VA_ARGS__); \
}
/************************************************************************/
enum TIMEFORMAT
{
NETLOG = 0, // [yyyy\mm\dd hh.MM.ss]
LOGINLOG=1, // mm-dd hh:MM:ss
};
/************************************************************************/
// wchar_t to string
void Wchar_tToString(std::string& szDst, wchar_t *wchar)
{
wchar_t * wText = wchar;
DWORD dwNum = WideCharToMultiByte(CP_OEMCP,NULL,wText,-1,NULL,0,NULL,FALSE);// WideCharToMultiByte的运用
char *psText; // psText为char*的临时数组,作为赋值给std::string的中间变量
psText = new char[dwNum];
WideCharToMultiByte (CP_OEMCP,NULL,wText,-1,psText,dwNum,NULL,FALSE);// WideCharToMultiByte的再次运用
szDst.append(psText);// std::string赋值
delete []psText;// psText的清除
}
//string Format input1
template<typename T>
void StrFormat(std::string& str, T Input,int hex=0)
{
char buf[256] = {0};
stringstream ss0;
if(16 == hex)
{
sprintf_s(buf,"%x",Input);
ss0 << buf;
}
else if(10 == hex)
{
sprintf_s(buf,"%d",Input);
ss0 << buf;
}
else{
ss0 <<Input;
}
str.append(ss0.str());
}
//string Format input1 and input2
//template<typename T>
//void StrFormat(std::string& str, T Input1,T Input2,int hex=0);
void GetTimeNow(string& str)
{
char buf[256] = {0};
time_t t = time(0);
struct tm ttt;
localtime_s(&ttt,&t);
sprintf_s(buf, "%d-%d-%d %d:%d:%d", ttt.tm_year+1900,ttt.tm_mon,ttt.tm_mday, ttt.tm_hour,ttt.tm_min, ttt.tm_sec);
str.append(buf);
}
/************************************************************************/