|
@@ -0,0 +1,1861 @@
|
|
|
+/*************************************************************************
|
|
|
+ > File Name: CILIX_AUX_UTILS.h
|
|
|
+ > Author: 陈必灏
|
|
|
+ > Mail: chenbh@nantian.com.cn
|
|
|
+ > Created Time: 2017年09月21日 星期四 09时43分04秒
|
|
|
+ ************************************************************************/
|
|
|
+
|
|
|
+#ifndef _CILIX_AUX_UTILS_H
|
|
|
+#define _CILIX_AUX_UTILS_H
|
|
|
+
|
|
|
+
|
|
|
+#include<stdio.h>
|
|
|
+#include<stdlib.h>
|
|
|
+#include<unistd.h>
|
|
|
+#include<sys/types.h>
|
|
|
+#include<sys/stat.h>
|
|
|
+#include<fcntl.h>
|
|
|
+#include<termios.h>
|
|
|
+#include<errno.h>
|
|
|
+#include<dirent.h>
|
|
|
+#include<string>
|
|
|
+#include<string.h>
|
|
|
+#include<sys/time.h>
|
|
|
+#include<time.h>
|
|
|
+#include<stdarg.h>
|
|
|
+#include<map>
|
|
|
+#include<vector>
|
|
|
+#include<iostream>
|
|
|
+#include<list>
|
|
|
+#include<memory.h>
|
|
|
+#include<malloc.h>
|
|
|
+#include<wchar.h>
|
|
|
+#include<dlfcn.h>
|
|
|
+#include <ostream>
|
|
|
+#include <fstream>
|
|
|
+
|
|
|
+using namespace std;
|
|
|
+
|
|
|
+typedef char* LPSTR;
|
|
|
+typedef unsigned long ULONG;
|
|
|
+typedef int BOOL;
|
|
|
+#define TRUE 1
|
|
|
+#define FALSE 0
|
|
|
+
|
|
|
+typedef vector<string> STRVECTOR;
|
|
|
+typedef string::size_type STRPOS;
|
|
|
+
|
|
|
+typedef struct _cilix_sys_time_ {
|
|
|
+ int Year;
|
|
|
+ int Month;
|
|
|
+ int Day;
|
|
|
+ int DayOfWeek;
|
|
|
+ int DayOfYear;
|
|
|
+ int Hour;
|
|
|
+ int Minute;
|
|
|
+ int Second;
|
|
|
+ int MilliSecond;
|
|
|
+} CILIX_SYS_TIME, *LP_CILIX_SYS_TIME;
|
|
|
+
|
|
|
+class CILIX_AUX_X
|
|
|
+{
|
|
|
+
|
|
|
+ public:
|
|
|
+
|
|
|
+ static int CILIX_strlen_00(char* pSrc)
|
|
|
+ {
|
|
|
+ if (!pSrc) return 0;
|
|
|
+
|
|
|
+ int x=0;
|
|
|
+ for (; pSrc[x]||pSrc[x+1]; x++);
|
|
|
+
|
|
|
+ return (x+2);
|
|
|
+ }
|
|
|
+ static int CILIX_strlen_00_w(wchar_t* pSrc)
|
|
|
+ {
|
|
|
+ if (!pSrc) return 0;
|
|
|
+
|
|
|
+ int x=0;
|
|
|
+ for (; pSrc[x]||pSrc[x+1]; x++);
|
|
|
+ return (x+2);
|
|
|
+ }
|
|
|
+ static void GetLocalTime(CILIX_SYS_TIME *pt)
|
|
|
+ {
|
|
|
+ struct timeval ttv;
|
|
|
+ gettimeofday(&ttv,NULL);
|
|
|
+ struct tm* ttm = localtime(&ttv.tv_sec);
|
|
|
+
|
|
|
+ pt->Year = ttm->tm_year+1900;
|
|
|
+ pt->Month = ttm->tm_mon+1;
|
|
|
+ pt->Day = ttm->tm_mday;
|
|
|
+ pt->DayOfWeek = ttm->tm_wday;
|
|
|
+ pt->DayOfYear = ttm->tm_yday+1;
|
|
|
+ pt->Hour = ttm->tm_hour;
|
|
|
+ pt->Minute = ttm->tm_min;
|
|
|
+ pt->Second = ttm->tm_sec;
|
|
|
+ pt->MilliSecond = (int)(ttv.tv_usec/1000);
|
|
|
+ }
|
|
|
+ static void Sleep(int ms)
|
|
|
+ {
|
|
|
+ usleep(ms*1000);
|
|
|
+ }
|
|
|
+ static int Tickcount()
|
|
|
+ {
|
|
|
+ struct timeval tv;
|
|
|
+ gettimeofday(&tv,NULL);
|
|
|
+ return (tv.tv_sec*1000 + tv.tv_usec/1000);
|
|
|
+ }
|
|
|
+ static void CILIX_uti_unpack(unsigned char *pSrc,unsigned char* pDest,int len)
|
|
|
+ {
|
|
|
+ unsigned char ch1, ch2;
|
|
|
+ int i ;
|
|
|
+ for( i = 0; i < len; i++ )
|
|
|
+ {
|
|
|
+ ch1 = (pSrc[i] & 0xF0) >> 4;
|
|
|
+ ch2 = pSrc[i] & 0x0F;
|
|
|
+ ch1 += ( (ch1 > 9 ) ? 0x37 : 0x30 );
|
|
|
+ ch2 += ( (ch2 > 9 ) ? 0x37 : 0x30 );
|
|
|
+ pDest[i*2] = ch1;
|
|
|
+ pDest[i*2 + 1] = ch2;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ static void CILIX_uti_pack(unsigned char* pSrc,unsigned char* pDest,int len)
|
|
|
+ {
|
|
|
+ char ch1, ch2;
|
|
|
+ int i;
|
|
|
+ for( i = 0; i < (len / 2); i++ )
|
|
|
+ {
|
|
|
+ ch1 = pSrc[i*2];
|
|
|
+ ch2 = pSrc[i*2 + 1];
|
|
|
+ (ch1 >= 'a' && ch1 <= 'z') ? (ch1 -= 32) : (ch1);
|
|
|
+ (ch2 >= 'a' && ch2 <= 'z') ? (ch2 -= 32) : (ch2);
|
|
|
+ ch1 -= ( (ch1 > '9' ) ? 0x37 : 0x30 );
|
|
|
+ ch2 -= ( (ch2 > '9' ) ? 0x37 : 0x30 );
|
|
|
+ pDest[i] = ( ch1 << 4 ) | ch2;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ static char* CILIX_GetModuleCurPath(char* sCurPath)
|
|
|
+ {
|
|
|
+ std::string wdir;
|
|
|
+ Dl_info dl_info;
|
|
|
+ dladdr((void*)CILIX_GetModuleCurPath, &dl_info);
|
|
|
+ std::string path(dl_info.dli_fname);
|
|
|
+ wdir = path.substr(0, path.find_last_of('/') + 1);
|
|
|
+
|
|
|
+ strcpy(sCurPath, wdir.c_str());
|
|
|
+ return sCurPath;
|
|
|
+ }
|
|
|
+ static bool CreateDirectory(char* path)
|
|
|
+ {
|
|
|
+ int i = 0;
|
|
|
+ char FilePath[100] = "";
|
|
|
+ FilePath[0] = path[0];
|
|
|
+ for(i= 1; i<=strlen(path); i++)
|
|
|
+ {
|
|
|
+ FilePath[i] = path[i];
|
|
|
+ if(path[i] == '/')
|
|
|
+ {
|
|
|
+ char tempBuff[1024]= "";
|
|
|
+ memcpy(tempBuff,FilePath,i);
|
|
|
+
|
|
|
+ int iRet = access(tempBuff,F_OK);
|
|
|
+
|
|
|
+ if(access(tempBuff, F_OK) != 0)
|
|
|
+ {
|
|
|
+ mkdir(FilePath,S_IRUSR | S_IWUSR | S_IXUSR | S_IRWXG | S_IRWXO);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ static int CILIX_split_str(string strSrc,STRVECTOR& vecDest,char cSep)
|
|
|
+ {
|
|
|
+ STRPOS pos = 0;
|
|
|
+ STRPOS prev_pos = 0;
|
|
|
+ while((pos = strSrc.find_first_of(cSep,pos))!= string::npos)
|
|
|
+ {
|
|
|
+ string strTemp = strSrc.substr(prev_pos,pos-prev_pos);
|
|
|
+ vecDest.push_back(strTemp);
|
|
|
+ prev_pos = ++ pos;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(!strSrc.empty())
|
|
|
+ vecDest.push_back(&strSrc[prev_pos]);
|
|
|
+
|
|
|
+ return vecDest.size();
|
|
|
+ }
|
|
|
+ static int CILIX_split_str_00(char* sSrc,STRVECTOR& vecDest)
|
|
|
+ {
|
|
|
+ if(!sSrc || (!sSrc[0]&& !sSrc[1]))return 0;
|
|
|
+
|
|
|
+ do
|
|
|
+ {
|
|
|
+ vecDest.push_back(sSrc);
|
|
|
+ sSrc += strlen(sSrc)+1;
|
|
|
+ }while(sSrc[0]);
|
|
|
+ return vecDest.size();
|
|
|
+ }
|
|
|
+
|
|
|
+ static bool CILIX_X_SetINIKeyString(char* fileName,char* title,char* key , char* KeyValue)
|
|
|
+ {
|
|
|
+ if((fileName == NULL)||(title == NULL)||
|
|
|
+ (key == NULL)||(KeyValue == NULL)) return FALSE;
|
|
|
+
|
|
|
+ FILE *frp = NULL;
|
|
|
+ char szLine[1024*10],szRcvData[1024*10];
|
|
|
+ memset(szLine,0,sizeof(szLine));
|
|
|
+ memset(szRcvData,0,sizeof(szRcvData));
|
|
|
+
|
|
|
+ int rtnval = 0,iDataLen = 0,iRcvDataLen = 0;
|
|
|
+ frp = fopen(fileName,"r");
|
|
|
+ if(frp == NULL)return FALSE;
|
|
|
+
|
|
|
+ while(!feof(frp))
|
|
|
+ {
|
|
|
+ rtnval = fgetc(frp);
|
|
|
+ szLine[iDataLen++] = rtnval;
|
|
|
+
|
|
|
+ if(rtnval == EOF) break;
|
|
|
+ }
|
|
|
+
|
|
|
+ fclose(frp);
|
|
|
+ // printf("...%d...",iDataLen);
|
|
|
+ // printf("%s",szLine);
|
|
|
+ //iDataLen = data + enter + \n + EOF
|
|
|
+ char sztitle[1024];
|
|
|
+ memset(sztitle,0,sizeof(sztitle));
|
|
|
+ strcpy(sztitle,"[");
|
|
|
+ strcat(sztitle,title);
|
|
|
+ strcat(sztitle,"]");
|
|
|
+ // printf("%s",sztitle);
|
|
|
+ char szkey[1024];
|
|
|
+ memset(szkey,0,sizeof(szkey));
|
|
|
+ strcpy(szkey,key);
|
|
|
+ strcat(szkey,"=");
|
|
|
+
|
|
|
+ char* titlePostion = NULL;
|
|
|
+ if((titlePostion = strstr(szLine,sztitle))== NULL) return FALSE;
|
|
|
+
|
|
|
+ int ititlePostion = titlePostion - szLine;
|
|
|
+ // printf("...%d...",ititlePostion);
|
|
|
+ char* keyPostion = NULL;
|
|
|
+ if((keyPostion = strstr(szLine+ititlePostion,szkey)) == NULL) return FALSE;
|
|
|
+ int ikeyPostion = keyPostion - szLine;
|
|
|
+ // printf("***%d***",ikeyPostion);
|
|
|
+ int iStarPostion = ikeyPostion+strlen(szkey);
|
|
|
+
|
|
|
+ char* endPostion = NULL;
|
|
|
+ if((endPostion = strchr(szLine+iStarPostion,'\r'))!= NULL);
|
|
|
+ else if((endPostion = strchr(szLine+iStarPostion,'\n'))!= NULL);
|
|
|
+ else return FALSE;
|
|
|
+ int iendPostion = endPostion - szLine;
|
|
|
+ // printf("+++%d+++",iendPostion);
|
|
|
+
|
|
|
+ memcpy(szRcvData,szLine,iStarPostion);
|
|
|
+
|
|
|
+ iRcvDataLen += iStarPostion;
|
|
|
+
|
|
|
+ // char keyData[100]= "384000";
|
|
|
+
|
|
|
+ memcpy(szRcvData+iStarPostion,KeyValue,strlen(KeyValue));
|
|
|
+ iRcvDataLen += strlen(KeyValue);
|
|
|
+
|
|
|
+ memcpy(szRcvData+iStarPostion+strlen(KeyValue),szLine+iendPostion,iDataLen-iendPostion);
|
|
|
+ iRcvDataLen += (iDataLen-iendPostion);
|
|
|
+ // printf("%s",szRcvData);
|
|
|
+
|
|
|
+
|
|
|
+ // printf("...%d...",iRcvDataLen);
|
|
|
+ FILE *fwp = NULL;
|
|
|
+ fwp = fopen(fileName,"w+");
|
|
|
+ if(fwp == NULL)
|
|
|
+ {
|
|
|
+ // printf("Open error\n");
|
|
|
+ return FALSE;
|
|
|
+ }
|
|
|
+
|
|
|
+ int iWriteNum = fwrite(szRcvData,sizeof(char),iRcvDataLen-1,fwp);
|
|
|
+ fclose(fwp);
|
|
|
+ if(iWriteNum == 0)
|
|
|
+ {
|
|
|
+ // printf("Write error \n");
|
|
|
+ return FALSE;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // printf("...%d...",iWriteNum);
|
|
|
+
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ static bool CILIX_X_INIKeyString(char* fileName , char* title,char* key,char* KeyValue,char* Default =NULL )
|
|
|
+ {
|
|
|
+ if((fileName == NULL)||(title == NULL)||
|
|
|
+ (key == NULL)||(KeyValue == NULL))
|
|
|
+ return FALSE;
|
|
|
+
|
|
|
+ FILE *fp = NULL ;
|
|
|
+ char szLine[1024],tmpstr[1024];
|
|
|
+ char *tmp = NULL;
|
|
|
+
|
|
|
+ memset(szLine,0,sizeof(szLine));
|
|
|
+ memset(tmpstr,0,sizeof(tmpstr));
|
|
|
+ int rtnval, i = 0,flag = 0;
|
|
|
+
|
|
|
+ fp = fopen(fileName,"r");
|
|
|
+ if(fp == NULL) return FALSE;
|
|
|
+
|
|
|
+ while(!feof(fp))
|
|
|
+ {
|
|
|
+ rtnval = fgetc(fp);
|
|
|
+ if(rtnval == EOF)break;
|
|
|
+ else szLine[i++] = rtnval;
|
|
|
+
|
|
|
+ if(rtnval == '\n')
|
|
|
+ {
|
|
|
+ szLine[--i] = '\0';
|
|
|
+ i = 0;
|
|
|
+ tmp = strchr(szLine,'=');
|
|
|
+ if((tmp != NULL)&&(flag == 1))
|
|
|
+ {
|
|
|
+ if (strstr(szLine,key)!= NULL)
|
|
|
+ {
|
|
|
+ if('#'== szLine[0]);
|
|
|
+ else if('\/'== szLine[0]&& '\/' == szLine[1]);
|
|
|
+ else
|
|
|
+ {
|
|
|
+ strcpy(tmpstr,tmp+1);
|
|
|
+ fclose(fp);
|
|
|
+
|
|
|
+ char* FindPos = NULL;
|
|
|
+ if((FindPos = strchr(tmpstr,'\n'))!= NULL)
|
|
|
+ {
|
|
|
+ memcpy(KeyValue,tmpstr,FindPos-tmpstr);
|
|
|
+ }
|
|
|
+ else if((FindPos =strchr(tmpstr,'\r'))!=NULL)
|
|
|
+ memcpy(KeyValue,tmpstr,FindPos-tmpstr);
|
|
|
+ else strcpy(KeyValue,tmpstr);
|
|
|
+
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ strcpy(tmpstr,"[");
|
|
|
+ strcat(tmpstr,title);
|
|
|
+ strcat(tmpstr,"]");
|
|
|
+ if(strncmp(tmpstr,szLine,strlen(tmpstr)) == 0)
|
|
|
+ flag = 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ fclose(fp);
|
|
|
+ if(Default == NULL) return FALSE;
|
|
|
+ strcpy(KeyValue,Default);
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+
|
|
|
+ static char* strupr(char *str)
|
|
|
+ {
|
|
|
+ char *ptr = str;
|
|
|
+ while (*ptr != '\0') {
|
|
|
+ if (islower(*ptr))
|
|
|
+ *ptr = toupper(*ptr);
|
|
|
+ ptr++;
|
|
|
+ }
|
|
|
+ return str;
|
|
|
+ }
|
|
|
+ static char* strlwr(char* str)
|
|
|
+ {
|
|
|
+ char* ptr = str;
|
|
|
+ while (*ptr != '\0') {
|
|
|
+ if (isupper(*ptr))
|
|
|
+ *ptr = tolower(*ptr);
|
|
|
+ ptr++;
|
|
|
+ }
|
|
|
+ return str;
|
|
|
+ }
|
|
|
+ static void TraceVAS(int hService, ULONG id, const char* f, char* p_szTraceData, ...)
|
|
|
+ {
|
|
|
+
|
|
|
+ char f_trace_data[1024 * 10 + 1] = "";
|
|
|
+ if (p_szTraceData != NULL)
|
|
|
+ {
|
|
|
+ va_list f_va_list;
|
|
|
+ va_start(f_va_list, p_szTraceData);
|
|
|
+ vsprintf(f_trace_data + strlen(f_trace_data), p_szTraceData, f_va_list);
|
|
|
+ va_end(f_va_list);
|
|
|
+ }
|
|
|
+ fprintf(stderr, "$$$$$$$$$$$$$$ %s H:<%d> ID:<%d> [%s] -- %s\n", __TIME__, hService, id, f, f_trace_data);
|
|
|
+ }
|
|
|
+
|
|
|
+};
|
|
|
+//?ַ???\C0\A9չ??
|
|
|
+class CAU_unpacker
|
|
|
+{
|
|
|
+ public:
|
|
|
+
|
|
|
+ CAU_unpacker(const char* s) : str_(0)
|
|
|
+ {
|
|
|
+ unpack(s, ( s ? strlen(s) : 0 ));
|
|
|
+ }
|
|
|
+ CAU_unpacker(const char* s, int l) : str_(0)
|
|
|
+ {
|
|
|
+ unpack(s, l);
|
|
|
+ }
|
|
|
+
|
|
|
+ ~CAU_unpacker()
|
|
|
+ {
|
|
|
+ free(this->str_);
|
|
|
+ }
|
|
|
+
|
|
|
+ char* str()
|
|
|
+ {
|
|
|
+ return this->str_;
|
|
|
+ }
|
|
|
+ int len()
|
|
|
+ {
|
|
|
+ return this->len_;
|
|
|
+ }
|
|
|
+
|
|
|
+ private:
|
|
|
+ void unpack(const char* s, int l)
|
|
|
+ {
|
|
|
+ free(this->str_);
|
|
|
+ this->str_ = NULL;
|
|
|
+ //
|
|
|
+ unsigned char* s_unpack_ = (unsigned char*)malloc(l*2 + 10);
|
|
|
+ memset(s_unpack_, 0, l*2 + 10);
|
|
|
+ CILIX_AUX_X::CILIX_uti_unpack((unsigned char*)s, s_unpack_, l);
|
|
|
+ //
|
|
|
+ this->str_ = (char*)s_unpack_;
|
|
|
+ this->len_ = l*2;
|
|
|
+ }
|
|
|
+
|
|
|
+ private:
|
|
|
+ char* str_;
|
|
|
+ int len_;
|
|
|
+};
|
|
|
+//???ַ????Զ?\C0\A9չ??
|
|
|
+class CAU_unpacker_w
|
|
|
+{
|
|
|
+ public:
|
|
|
+
|
|
|
+ CAU_unpacker_w(const wchar_t* s) : str_(0)
|
|
|
+ {
|
|
|
+ unpack(s, ( s ? wcslen(s) : 0 ));
|
|
|
+ }
|
|
|
+ CAU_unpacker_w(const wchar_t* s, int l) : str_(0)
|
|
|
+ {
|
|
|
+ unpack(s, l);
|
|
|
+ }
|
|
|
+
|
|
|
+ ~CAU_unpacker_w()
|
|
|
+ {
|
|
|
+ free(this->str_);
|
|
|
+ }
|
|
|
+
|
|
|
+ char* str()
|
|
|
+ {
|
|
|
+ return this->str_;
|
|
|
+ }
|
|
|
+ int len()
|
|
|
+ {
|
|
|
+ return this->len_;
|
|
|
+ }
|
|
|
+
|
|
|
+ private:
|
|
|
+ void unpack(const wchar_t* s, int l)
|
|
|
+ {
|
|
|
+ free(this->str_);
|
|
|
+ this->str_ = NULL;
|
|
|
+ //
|
|
|
+ unsigned char* s_unpack_ = (unsigned char*)malloc(l*sizeof(wchar_t)*2 + 10);
|
|
|
+ memset(s_unpack_, 0, l*sizeof(wchar_t)*2 + 10);
|
|
|
+ CILIX_AUX_X::CILIX_uti_unpack((unsigned char*)s, s_unpack_, l*sizeof(wchar_t));
|
|
|
+ //
|
|
|
+ this->str_ = (char*)s_unpack_;
|
|
|
+ this->len_ = l*sizeof(wchar_t)*2;
|
|
|
+ }
|
|
|
+
|
|
|
+ private:
|
|
|
+ char* str_;
|
|
|
+ int len_;
|
|
|
+};
|
|
|
+//?ַ????Զ?ѹ????
|
|
|
+class CAU_packer
|
|
|
+{
|
|
|
+ public:
|
|
|
+
|
|
|
+ CAU_packer(const char* s) : str_(0)
|
|
|
+ {
|
|
|
+ pack(s, ( s ? strlen(s) : 0 ));
|
|
|
+ }
|
|
|
+ CAU_packer(const char* s, int l) : str_(0)
|
|
|
+ {
|
|
|
+ pack(s, l);
|
|
|
+ }
|
|
|
+
|
|
|
+ ~CAU_packer()
|
|
|
+ {
|
|
|
+ free(this->str_);
|
|
|
+ }
|
|
|
+
|
|
|
+ char* str()
|
|
|
+ {
|
|
|
+ return this->str_;
|
|
|
+ }
|
|
|
+ int len()
|
|
|
+ {
|
|
|
+ return this->len_;
|
|
|
+ }
|
|
|
+
|
|
|
+ private:
|
|
|
+ void pack(const char* s, int l)
|
|
|
+ {
|
|
|
+ free(this->str_);
|
|
|
+ this->str_ = NULL;
|
|
|
+ //
|
|
|
+ unsigned char* s_pack_ = (unsigned char*)malloc(l/2 + 10);
|
|
|
+ memset(s_pack_, 0, l/2 + 10);
|
|
|
+ CILIX_AUX_X::CILIX_uti_pack((unsigned char*)s, s_pack_, l);
|
|
|
+ //
|
|
|
+ this->str_ = (char*)s_pack_;
|
|
|
+ this->len_ = l/2;
|
|
|
+ }
|
|
|
+
|
|
|
+ private:
|
|
|
+ char* str_;
|
|
|
+ int len_;
|
|
|
+};
|
|
|
+typedef int (*CILIXTTY_READINISH)(char*,int);
|
|
|
+class CILIX_TTY_Op
|
|
|
+{
|
|
|
+ public:
|
|
|
+ CILIX_TTY_Op()
|
|
|
+ {
|
|
|
+ this->fd = 0;
|
|
|
+ }
|
|
|
+ ~CILIX_TTY_Op()
|
|
|
+ {
|
|
|
+ this->Close();
|
|
|
+ }
|
|
|
+
|
|
|
+ virtual int Open(char* szPort,int nBaudRate)
|
|
|
+ {
|
|
|
+ this->speed = this->GetBaudRate(nBaudRate);
|
|
|
+ if(this->speed == -1) return -1;
|
|
|
+
|
|
|
+ this->fd = open( szPort, O_RDWR|O_NONBLOCK);
|
|
|
+ if(this->fd == FALSE) return -1;
|
|
|
+
|
|
|
+ struct termios options;
|
|
|
+ if(tcgetattr(this->fd,&options))
|
|
|
+ {
|
|
|
+ close(this->fd);
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ options.c_cflag &= ~CSIZE;
|
|
|
+ options.c_cflag |= CS8;
|
|
|
+ options.c_cflag &= ~CSTOPB;
|
|
|
+
|
|
|
+ cfmakeraw(&options);
|
|
|
+ cfsetispeed(&options,this->speed);
|
|
|
+ cfsetospeed(&options,this->speed);
|
|
|
+
|
|
|
+ if(tcsetattr(this->fd,TCSANOW,&options)!= 0)
|
|
|
+ {
|
|
|
+ close(this->fd);
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ virtual int Open(char* szPort,int nBaudRate, int nEvent,int cflag )
|
|
|
+ {
|
|
|
+ this->speed = this->GetBaudRate(nBaudRate);
|
|
|
+ if(this->speed == -1) return -1;
|
|
|
+
|
|
|
+ this->fd = open( szPort, O_RDWR|O_NONBLOCK);
|
|
|
+ if(this->fd == FALSE) return -1;
|
|
|
+
|
|
|
+ struct termios options;
|
|
|
+ if(tcgetattr(this->fd,&options))
|
|
|
+ {
|
|
|
+ close(this->fd);
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ cfmakeraw(&options);
|
|
|
+
|
|
|
+ options.c_cflag &= ~CSIZE;
|
|
|
+ options.c_cflag |= CS8;
|
|
|
+ options.c_cflag &= ~CSTOPB;
|
|
|
+
|
|
|
+ //options.c_cflag |= CLOCAL|CREAD;
|
|
|
+ //options.c_oflag &= ~OPOST;
|
|
|
+ //options.c_cc[VTIME] = 0;
|
|
|
+ //options.c_cc[VMIN] = 0;
|
|
|
+ //tcflush(this->fd,TCOFLUSH);
|
|
|
+ //if(cflag == 0 ) options.c_cflag &= ~CRTSCTS;
|
|
|
+ //else options.c_cflag |= CRTSCTS;
|
|
|
+
|
|
|
+ switch (nEvent)
|
|
|
+ {
|
|
|
+ case 'n':
|
|
|
+ case 'N':
|
|
|
+ options.c_cflag &= ~PARENB; /* Clear parity enable */
|
|
|
+ options.c_iflag &= ~INPCK; /* Enable parity checking */
|
|
|
+ break;
|
|
|
+ case 'o':
|
|
|
+ case 'O':
|
|
|
+ options.c_cflag |= (PARODD | PARENB); /* 设置为奇效验*/
|
|
|
+ options.c_iflag |= INPCK; /* Disnable parity checking */
|
|
|
+ break;
|
|
|
+ case 'e':
|
|
|
+ case 'E':
|
|
|
+ options.c_cflag |= PARENB; /* Enable parity */
|
|
|
+ options.c_cflag &= ~PARODD; /* 转换为偶效验*/
|
|
|
+ options.c_iflag |= INPCK; /* Disnable parity checking */
|
|
|
+ break;
|
|
|
+ case 'S':
|
|
|
+ case 's': /*as no parity*/
|
|
|
+ options.c_cflag &= ~PARENB;
|
|
|
+ options.c_cflag &= ~CSTOPB;
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ fprintf(stderr,"Unsupported parity\n");
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ cfsetispeed(&options,this->speed);
|
|
|
+ cfsetospeed(&options,this->speed);
|
|
|
+
|
|
|
+ if(tcsetattr(this->fd,TCSANOW,&options)!= 0)
|
|
|
+ {
|
|
|
+ close(this->fd);
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ tcflush(this->fd,TCIOFLUSH);
|
|
|
+
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ virtual int Close()
|
|
|
+ {
|
|
|
+ if(this->fd > 0)
|
|
|
+ close(this->fd);
|
|
|
+
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ virtual int Write(char* szSend,int nSendLen,int InnerTime)
|
|
|
+ {
|
|
|
+ if(this->fd == -1) return -1;
|
|
|
+
|
|
|
+ int iLen = 0;
|
|
|
+
|
|
|
+ tcflush(this->fd,TCIFLUSH);
|
|
|
+
|
|
|
+ iLen = write(this->fd,szSend,nSendLen);
|
|
|
+ if(iLen < nSendLen)
|
|
|
+ {
|
|
|
+ //clear buff
|
|
|
+ tcflush(this->fd,TCOFLUSH);
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ return iLen;
|
|
|
+ }
|
|
|
+ virtual int Read(char* szRecvData,int nRcvlen,int inner,int total,int isleep,CILIXTTY_READINISH srf)
|
|
|
+ {
|
|
|
+ int ret_ = 0,count_ = 0 ;
|
|
|
+ char buff_[2];
|
|
|
+
|
|
|
+ int time_old = CILIX_AUX_X::Tickcount(),time_last = 0;
|
|
|
+
|
|
|
+ while((CILIX_AUX_X::Tickcount() -time_old ) < total)
|
|
|
+ {
|
|
|
+ memset(buff_,0,sizeof(buff_));
|
|
|
+ ret_ = read(this->fd,buff_,1);
|
|
|
+
|
|
|
+ if(ret_ != 1)
|
|
|
+ {
|
|
|
+ if(count_ > 0)
|
|
|
+ {
|
|
|
+ if((CILIX_AUX_X::Tickcount() - time_last) >= inner) break;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(isleep > 0) CILIX_AUX_X::Sleep(isleep);
|
|
|
+
|
|
|
+ }
|
|
|
+ else if(ret_ == 1)
|
|
|
+ {
|
|
|
+ memcpy(szRecvData+count_,buff_,1);
|
|
|
+ count_++;
|
|
|
+
|
|
|
+ if(count_ >= nRcvlen) break;
|
|
|
+
|
|
|
+ //get data full
|
|
|
+ if(srf != NULL && srf(szRecvData,count_)) break;
|
|
|
+
|
|
|
+ time_last = CILIX_AUX_X::Tickcount();
|
|
|
+ }
|
|
|
+ // else return ret_;
|
|
|
+ }
|
|
|
+ /*if(count_ == 0) printf("Read Count 0 :%s\n",strerror(errno));*/
|
|
|
+ return count_;
|
|
|
+ }
|
|
|
+ virtual int Read(char* szRecvData ,int nRcvlen,int inner ,int total,int isleep)
|
|
|
+ {
|
|
|
+ int iRet = 0,ret_ = 0;
|
|
|
+ struct timeval time_old,time_last,time_now;
|
|
|
+
|
|
|
+ gettimeofday(&time_old,NULL);
|
|
|
+
|
|
|
+ time_last.tv_sec = 0;
|
|
|
+ time_last.tv_usec = 0;
|
|
|
+ time_now.tv_sec = 0;
|
|
|
+ time_now.tv_usec = 0;
|
|
|
+
|
|
|
+ long _old = time_old.tv_sec * 1000 + time_old.tv_usec /1000;
|
|
|
+ long _time = 0, _now = 0;
|
|
|
+
|
|
|
+ while((_time - _old) < total)
|
|
|
+ {
|
|
|
+ unsigned char buf_;
|
|
|
+ ret_ = read(this->fd,&buf_,1);
|
|
|
+
|
|
|
+ if(ret_ != 1)
|
|
|
+ {
|
|
|
+ if(iRet > 0)
|
|
|
+ {
|
|
|
+ gettimeofday(&time_now,NULL);
|
|
|
+ _now = time_now.tv_sec * 1000 + time_now.tv_usec /1000;
|
|
|
+ if((_now - _time) >= inner ) break;
|
|
|
+ }
|
|
|
+ if(isleep > 0)usleep(isleep*1000);
|
|
|
+
|
|
|
+ }
|
|
|
+ if(ret_ == 1)
|
|
|
+ {
|
|
|
+ szRecvData[iRet]= buf_;
|
|
|
+ iRet ++ ;
|
|
|
+
|
|
|
+ if(iRet >= nRcvlen) break;
|
|
|
+
|
|
|
+ gettimeofday(&time_last,NULL);
|
|
|
+ _time = time_last.tv_sec * 1000 + time_last.tv_usec/1000;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ return iRet;
|
|
|
+ }
|
|
|
+ private:
|
|
|
+ int fd;
|
|
|
+ speed_t speed;
|
|
|
+ static speed_t GetBaudRate(int iBaudRate)
|
|
|
+ {
|
|
|
+ switch(iBaudRate)
|
|
|
+ {
|
|
|
+ case 0: return B0;
|
|
|
+ case 9600: return B9600;
|
|
|
+ case 19200: return B19200;
|
|
|
+ case 38400: return B38400;
|
|
|
+ case 115200:return B115200;
|
|
|
+ case 57600: return B57600;
|
|
|
+ default: return -1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+//日志级别
|
|
|
+#define CILIXTRACE_L_DEBUG 1
|
|
|
+#define CILIXTRACE_L_INFO 2
|
|
|
+#define CILIXTRACE_L_WARN 3
|
|
|
+#define CILIXTRACE_L_ERROR 4
|
|
|
+#define CILIXTRACE_L_FATAL 5
|
|
|
+#define CILIXTRACE_L_SYSTEM 9
|
|
|
+
|
|
|
+class CILIXTrace
|
|
|
+{
|
|
|
+ public:
|
|
|
+ CILIXTrace(char* p_szDir,char* p_szMethodName,char* p_szFilePrefix):day_(0),month_(0),year_(0)
|
|
|
+ {
|
|
|
+ char szPath[1024],iniPath[1024];
|
|
|
+ memset(szPath,0,sizeof(szPath));
|
|
|
+
|
|
|
+ CILIX_AUX_X::CILIX_GetModuleCurPath(szPath);
|
|
|
+ memset(iniPath,0,sizeof(iniPath));
|
|
|
+ strcat(iniPath,szPath);
|
|
|
+ strcat(iniPath,"CILIX_CONFIG.ini");
|
|
|
+
|
|
|
+ //是否记录日志
|
|
|
+ char dtFlag[200] = "";
|
|
|
+ CILIX_AUX_X::CILIX_X_INIKeyString(iniPath,"DEVTRACE","Switch",dtFlag,"1");
|
|
|
+ atoi(dtFlag) ? m_trace_flag = TRUE:m_trace_flag = FALSE;
|
|
|
+
|
|
|
+ //是否日志加密
|
|
|
+ char dtcrypt[200] = "";
|
|
|
+ CILIX_AUX_X::CILIX_X_INIKeyString(iniPath,"DEVTRACE","Encrypt",dtcrypt,"0");
|
|
|
+ atoi(dtcrypt) ? m_trace_crypt = TRUE :m_trace_crypt = FALSE;
|
|
|
+
|
|
|
+ //文件保留天数
|
|
|
+ char dtRD[200] = "";
|
|
|
+ CILIX_AUX_X::CILIX_X_INIKeyString(iniPath,"DEVTRACE","FileDay",dtRD,"30");
|
|
|
+ m_file_reserve_days = atoi(dtRD);
|
|
|
+
|
|
|
+ // m_file_open_mode="a+";
|
|
|
+ strcpy(m_file_open_mode,"a+");
|
|
|
+
|
|
|
+ memset(m_file_prefix_,0,sizeof(m_file_prefix_));
|
|
|
+ strcpy(m_file_prefix_ ,p_szFilePrefix);
|
|
|
+
|
|
|
+ //日志级别
|
|
|
+ char dtLevel[200]= "";
|
|
|
+ CILIX_AUX_X::CILIX_X_INIKeyString(iniPath,"DEVTRACE","Level",dtLevel,"INFO");
|
|
|
+ m_trace_level = this->l_a2i(dtLevel);
|
|
|
+
|
|
|
+ if(m_trace_flag == FALSE) return ;
|
|
|
+
|
|
|
+ //日志路径
|
|
|
+ char dtTracePath[1024];
|
|
|
+ memset(dtTracePath,0,sizeof(dtTracePath));
|
|
|
+ CILIX_AUX_X::CILIX_X_INIKeyString(iniPath,"DEVTRACE","Path",dtTracePath,"/etc/UFS/CILIXTrace");
|
|
|
+
|
|
|
+ //创建日文件夹
|
|
|
+ //CILIX_AUX_X::CreateDirectory(dtTracePath);
|
|
|
+
|
|
|
+ //创建日志文件夹
|
|
|
+ mkdir(dtTracePath,S_IRUSR | S_IWUSR | S_IXUSR | S_IRWXG | S_IRWXO);
|
|
|
+
|
|
|
+ memset(m_dir_,0,sizeof(m_dir_));
|
|
|
+ strcpy(m_dir_, dtTracePath);
|
|
|
+
|
|
|
+ //文件名
|
|
|
+ memset(m_ModuleName,0,sizeof(m_ModuleName));
|
|
|
+ strcpy(m_ModuleName, p_szMethodName);
|
|
|
+
|
|
|
+ CILIX_SYS_TIME p;
|
|
|
+ CILIX_AUX_X::GetLocalTime(&p);
|
|
|
+
|
|
|
+ this->WriteTrace(CILIXTRACE_L_SYSTEM,"===","|*********************************************|");
|
|
|
+ this->WriteTrace(CILIXTRACE_L_SYSTEM,"==="," START %04d-%02d-%2d %02d-%02d-%02d ",
|
|
|
+ p.Year,p.Month,p.Day,p.Hour,p.Minute,p.Second);
|
|
|
+
|
|
|
+ }
|
|
|
+ virtual ~CILIXTrace()
|
|
|
+ {
|
|
|
+
|
|
|
+ CILIX_SYS_TIME p;
|
|
|
+ CILIX_AUX_X::GetLocalTime(&p);
|
|
|
+
|
|
|
+ this->WriteTrace(CILIXTRACE_L_SYSTEM,"==="," END %04d-%02d-%2d %02d-%02d-%02d ",
|
|
|
+ p.Year,p.Month,p.Day,p.Hour,p.Minute,p.Second);
|
|
|
+ this->WriteTrace(CILIXTRACE_L_SYSTEM,"===","|*********************************************|");
|
|
|
+ }
|
|
|
+
|
|
|
+ void WriteTrace(const char* p_szMethodName,const char* p_szTraceData,...)
|
|
|
+ {
|
|
|
+ if(m_trace_flag == FALSE ) return;
|
|
|
+
|
|
|
+ char f_trace_data[1024 * 10+1] = "";
|
|
|
+
|
|
|
+ if(p_szTraceData != NULL)
|
|
|
+ {
|
|
|
+ va_list f_va_list;
|
|
|
+ va_start (f_va_list,p_szTraceData);
|
|
|
+ vsprintf(f_trace_data+strlen(f_trace_data),p_szTraceData,f_va_list);
|
|
|
+ va_end(f_va_list);
|
|
|
+ }
|
|
|
+ this->write_trace(CILIXTRACE_L_INFO,p_szMethodName,f_trace_data);
|
|
|
+ }
|
|
|
+ void WriteTrace(int p_iLevel, const char* p_szMethodName,const char* p_szTraceData,...)
|
|
|
+ {
|
|
|
+ if(m_trace_flag == FALSE ) return;
|
|
|
+
|
|
|
+ //判断日志级别
|
|
|
+ if(p_iLevel < m_trace_level) return ;
|
|
|
+
|
|
|
+ char f_trace_data[1024 * 10+1] = "";
|
|
|
+ if(p_szTraceData != NULL)
|
|
|
+ {
|
|
|
+ va_list f_va_list;
|
|
|
+ va_start (f_va_list,p_szTraceData);
|
|
|
+ vsprintf(f_trace_data+strlen(f_trace_data),p_szTraceData,f_va_list);
|
|
|
+ va_end(f_va_list);
|
|
|
+ }
|
|
|
+ this->write_trace(p_iLevel,p_szMethodName,f_trace_data);
|
|
|
+ }
|
|
|
+
|
|
|
+ private:
|
|
|
+ //Write file Mode
|
|
|
+ void write_trace(int p_iLevel, const char* p_szMethodName ,const char* p_szTraceData)
|
|
|
+ {
|
|
|
+ char f_trace_data[1024*10 +1] = "";
|
|
|
+
|
|
|
+ //trace level
|
|
|
+ char f_sl[200] = "";
|
|
|
+ this->l_i2a(p_iLevel, f_sl);
|
|
|
+
|
|
|
+ //time
|
|
|
+ /*
|
|
|
+ time_t timep = time(NULL);
|
|
|
+ struct tm* p = localtime(&timep);*/
|
|
|
+ CILIX_SYS_TIME p;
|
|
|
+ CILIX_AUX_X::GetLocalTime(&p);
|
|
|
+
|
|
|
+ //生成日期信息
|
|
|
+ sprintf(f_trace_data,"%04d-%02d-%02d %02d:%02d:%02d:%03d [%s] <%s> - ",
|
|
|
+ p.Year,p.Month,p.Day,
|
|
|
+ p.Hour,p.Minute,p.Second,p.MilliSecond,f_sl,(p_szMethodName == NULL ?"":p_szMethodName));
|
|
|
+
|
|
|
+ if(year_ != p.Year || month_ != p.Month || day_ != p.Day)
|
|
|
+ {
|
|
|
+ char sPath[1025] = "\0";
|
|
|
+ //创建日期目录
|
|
|
+ sprintf(sPath,"%s/%04d%02d%02d",m_dir_,p.Year,p.Month,p.Day);
|
|
|
+ mkdir(sPath,S_IRUSR | S_IWUSR | S_IXUSR | S_IRWXG | S_IRWXO);
|
|
|
+
|
|
|
+ sprintf(m_dir_,"%s/%s",sPath,m_ModuleName);
|
|
|
+ mkdir(m_dir_,S_IRUSR | S_IWUSR | S_IXUSR | S_IRWXG | S_IRWXO);
|
|
|
+ }
|
|
|
+
|
|
|
+ //trace info
|
|
|
+ if(p_szTraceData != NULL)
|
|
|
+ strcat(f_trace_data,p_szTraceData);
|
|
|
+
|
|
|
+ strcat(f_trace_data,"\r\n");
|
|
|
+
|
|
|
+ int f_i_trace_data_len = strlen(f_trace_data);
|
|
|
+
|
|
|
+ if(m_trace_crypt == TRUE)
|
|
|
+ {
|
|
|
+ int i ;
|
|
|
+ for(i = 0; i< f_i_trace_data_len; f_trace_data[i++]^=0x55);
|
|
|
+ }
|
|
|
+
|
|
|
+ char f_szFileName[1030] = "";
|
|
|
+ sprintf(f_szFileName,"%s/",m_dir_);
|
|
|
+
|
|
|
+ sprintf(f_szFileName+strlen(f_szFileName), "%s_%d%d%d.txt",
|
|
|
+ m_file_prefix_,
|
|
|
+ p.Year,
|
|
|
+ p.Month,
|
|
|
+ p.Day);
|
|
|
+
|
|
|
+ //open file Write
|
|
|
+ FILE *f_pFile = NULL;
|
|
|
+ if((f_pFile = fopen(f_szFileName,m_file_open_mode))!= NULL)
|
|
|
+ {
|
|
|
+ fseek(f_pFile,0L,SEEK_END);
|
|
|
+ fwrite(f_trace_data,sizeof(char),f_i_trace_data_len,f_pFile);
|
|
|
+ fclose(f_pFile);
|
|
|
+ }
|
|
|
+ //判断是否产生新的一天
|
|
|
+ if(year_ != p.Year || month_ != p.Month || day_ != p.Day)
|
|
|
+ {
|
|
|
+ this->delete_redun_files();
|
|
|
+ this->year_ = p.Year;
|
|
|
+ this->month_ = p.Month;
|
|
|
+ this->day_ = p.Day;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //
|
|
|
+ int l_a2i(char* sl)
|
|
|
+ {
|
|
|
+ int il = CILIXTRACE_L_INFO;
|
|
|
+ {
|
|
|
+ if (!strcmp(sl, "DEBUG")) il = CILIXTRACE_L_DEBUG;
|
|
|
+ if (!strcmp(sl, "INFO")) il = CILIXTRACE_L_INFO;
|
|
|
+ if (!strcmp(sl, "WARN")) il = CILIXTRACE_L_WARN;
|
|
|
+ if (!strcmp(sl, "ERROR")) il = CILIXTRACE_L_ERROR;
|
|
|
+ if (!strcmp(sl, "FATAL")) il = CILIXTRACE_L_FATAL;
|
|
|
+ if (!strcmp(sl, "SYSTEM")) il = CILIXTRACE_L_SYSTEM;
|
|
|
+ }
|
|
|
+ return il;
|
|
|
+ }
|
|
|
+ void l_i2a(int il, char* sl)
|
|
|
+ {
|
|
|
+ strcpy(sl, "UNKNOWN");
|
|
|
+ if (il == CILIXTRACE_L_DEBUG) strcpy(sl, "DEBUG");
|
|
|
+ if (il == CILIXTRACE_L_INFO) strcpy(sl, "INFO");
|
|
|
+ if (il == CILIXTRACE_L_WARN) strcpy(sl, "WARN");
|
|
|
+ if (il == CILIXTRACE_L_ERROR) strcpy(sl, "ERROR");
|
|
|
+ if (il == CILIXTRACE_L_FATAL) strcpy(sl, "FATAL");
|
|
|
+ if (il == CILIXTRACE_L_SYSTEM) strcpy(sl, "SYSTEM");
|
|
|
+ }
|
|
|
+ private:
|
|
|
+
|
|
|
+ //根据文件保留天数,删除过期文件
|
|
|
+ void delete_redun_files()
|
|
|
+ {
|
|
|
+ //如果不记录日志则返回
|
|
|
+ if(!m_trace_flag)
|
|
|
+ return ;
|
|
|
+
|
|
|
+ char szPath[1025] = "\0";
|
|
|
+ char iniPath[1025] = "\0";
|
|
|
+ CILIX_AUX_X::CILIX_GetModuleCurPath(szPath);
|
|
|
+ memset(iniPath,0,sizeof(iniPath));
|
|
|
+ strcat(iniPath,szPath);
|
|
|
+ strcat(iniPath,"CILIX_CONFIG.ini");
|
|
|
+
|
|
|
+ char dtTracePath[1024];
|
|
|
+ memset(dtTracePath,0,sizeof(dtTracePath));
|
|
|
+ CILIX_AUX_X::CILIX_X_INIKeyString(iniPath,"DEVTRACE","Path",dtTracePath,"");
|
|
|
+
|
|
|
+ //fprintf(stderr,"m_dir_%s\n ",dtTracePath);
|
|
|
+ REMOVE_FILES(dtTracePath);
|
|
|
+ }
|
|
|
+ void DeleteFolder(string dirIn)
|
|
|
+ {
|
|
|
+ if(dirIn.empty())return ;
|
|
|
+
|
|
|
+ struct stat s;
|
|
|
+ stat(dirIn.c_str(),&s);
|
|
|
+ if(!S_ISDIR(s.st_mode))return ;
|
|
|
+
|
|
|
+ DIR* open_dir = opendir(dirIn.c_str());
|
|
|
+ if(!open_dir) return ;
|
|
|
+
|
|
|
+ dirent* pDren = NULL;
|
|
|
+ while((pDren = readdir(open_dir)) != NULL)
|
|
|
+ {
|
|
|
+ string fileName = pDren->d_name;
|
|
|
+
|
|
|
+ if(fileName == "."||fileName == "..")
|
|
|
+ continue;
|
|
|
+
|
|
|
+ struct stat st;
|
|
|
+ string name = dirIn+string("/")+fileName;
|
|
|
+
|
|
|
+ stat(name.c_str(),&st);
|
|
|
+
|
|
|
+ if(S_ISDIR(st.st_mode))
|
|
|
+ {
|
|
|
+ DeleteFolder(name);
|
|
|
+
|
|
|
+ remove(name.c_str());
|
|
|
+ }
|
|
|
+ else if(S_ISREG(st.st_mode))
|
|
|
+ remove(name.c_str());
|
|
|
+
|
|
|
+ }
|
|
|
+ closedir(open_dir);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ BOOL REMOVE_FILES(string dirIn)
|
|
|
+ {
|
|
|
+ //搜索到的文件夹个数
|
|
|
+ int f_iFileNumber = 0;
|
|
|
+ //搜索到的文件名列表,最多1024 个文件
|
|
|
+ char *f_szFileNames[1025] = {0};
|
|
|
+
|
|
|
+ if(dirIn.empty())return FALSE;
|
|
|
+
|
|
|
+ struct stat s;
|
|
|
+ stat(dirIn.c_str(),&s);
|
|
|
+ if(!S_ISDIR(s.st_mode))return FALSE;
|
|
|
+
|
|
|
+ DIR* open_dir = opendir(dirIn.c_str());
|
|
|
+ if(!open_dir) return FALSE;
|
|
|
+
|
|
|
+ dirent* pDren = NULL;
|
|
|
+ while((pDren = readdir(open_dir)) != NULL)
|
|
|
+ {
|
|
|
+ string fileName = pDren->d_name;
|
|
|
+
|
|
|
+ if(fileName == "."||fileName == "..")
|
|
|
+ continue;
|
|
|
+
|
|
|
+ struct stat st;
|
|
|
+ string name = dirIn+string("/")+fileName;
|
|
|
+
|
|
|
+ stat(name.c_str(),&st);
|
|
|
+
|
|
|
+ if(S_ISDIR(st.st_mode))
|
|
|
+ {
|
|
|
+
|
|
|
+ f_szFileNames[f_iFileNumber] = (char*)malloc(strlen(name.c_str())+1);
|
|
|
+ strcpy(f_szFileNames[f_iFileNumber],name.c_str());
|
|
|
+
|
|
|
+ f_iFileNumber++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ closedir(open_dir);
|
|
|
+
|
|
|
+ qsort(f_szFileNames,f_iFileNumber,sizeof(char*),cmpfile_names);
|
|
|
+
|
|
|
+ for(int f_iFNTemp = f_iFileNumber;m_file_reserve_days>0 && f_iFNTemp >m_file_reserve_days;f_iFNTemp--)
|
|
|
+ {
|
|
|
+ // cout<<f_szFileNames[f_iFNTemp-1]<<endl;
|
|
|
+ DeleteFolder(f_szFileNames[f_iFNTemp-1]);
|
|
|
+ remove(f_szFileNames[f_iFNTemp-1]);
|
|
|
+ }
|
|
|
+
|
|
|
+ for(int i = 0;i<f_iFileNumber;i++)
|
|
|
+ free(f_szFileNames[i]);
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ static int cmpfile_names(const void* p_pFileName1,const void* p_pFileName2)
|
|
|
+ {
|
|
|
+ return strcmp(*(char**)p_pFileName2,*(char**)p_pFileName1);
|
|
|
+ }
|
|
|
+
|
|
|
+ private:
|
|
|
+ //trace dir
|
|
|
+ char m_dir_[1030];
|
|
|
+ //trace prefix
|
|
|
+ char m_file_prefix_[200];
|
|
|
+ //trace Name
|
|
|
+ char m_ModuleName[100] ;
|
|
|
+ //open mode
|
|
|
+ char m_file_open_mode[100];
|
|
|
+ //data
|
|
|
+ int m_file_reserve_days;
|
|
|
+ //trace flag
|
|
|
+ bool m_trace_flag;
|
|
|
+ //trace crypt
|
|
|
+ bool m_trace_crypt;
|
|
|
+ //level
|
|
|
+ int m_trace_level;
|
|
|
+
|
|
|
+ private:
|
|
|
+ int day_;
|
|
|
+ int month_;
|
|
|
+ int year_;
|
|
|
+};
|
|
|
+//Auto
|
|
|
+class CILIXAutoTrace
|
|
|
+{
|
|
|
+ public:
|
|
|
+ CILIXAutoTrace(CILIXTrace* pt,char *method ,int* iret):m_pt(pt),m_iret(iret)
|
|
|
+ {
|
|
|
+ memset(m_method,0,sizeof(m_method));
|
|
|
+ strcpy(m_method,method);
|
|
|
+ m_pt->WriteTrace(m_method,"Entry");
|
|
|
+ }
|
|
|
+ ~CILIXAutoTrace()
|
|
|
+ {
|
|
|
+ m_pt->WriteTrace(m_method,"Exit - [iret = %d]",*m_iret);
|
|
|
+ }
|
|
|
+ private:
|
|
|
+ CILIXTrace *m_pt;
|
|
|
+ char m_method[1025];
|
|
|
+ int* m_iret;
|
|
|
+};
|
|
|
+class CILIX_X_Config
|
|
|
+{
|
|
|
+ public:
|
|
|
+ private:
|
|
|
+ char sCfgPath[1025];
|
|
|
+};
|
|
|
+
|
|
|
+typedef list<void*> VPLIST,*LPVPLIST;
|
|
|
+typedef VPLIST::iterator VPLISTIT;
|
|
|
+
|
|
|
+typedef map<void*,LPVPLIST> VPLIST_MAP;
|
|
|
+typedef VPLIST_MAP::iterator VPLIST_MAP_IT;
|
|
|
+
|
|
|
+class CILIX_Allocator
|
|
|
+{
|
|
|
+ public:
|
|
|
+ CILIX_Allocator()
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
+ virtual ~CILIX_Allocator()
|
|
|
+ {
|
|
|
+ FreeBuffer();
|
|
|
+ }
|
|
|
+ public:
|
|
|
+ int AllocateBuffer(unsigned long iSize,void** pData)
|
|
|
+ {
|
|
|
+ if(iSize == 0)
|
|
|
+ {
|
|
|
+ *pData = NULL;
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ void *mem = malloc(iSize);
|
|
|
+ if(mem == NULL) return -1;
|
|
|
+
|
|
|
+ memset(mem,0,iSize);
|
|
|
+ LPVPLIST vl = new VPLIST;
|
|
|
+ vl->insert(vl->end(),mem);
|
|
|
+ VM.insert(VPLIST_MAP::value_type(mem,vl));
|
|
|
+ *pData = mem;
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ int AllocateMore(unsigned long iSize,void* pOriginal,void ** pData)
|
|
|
+ {
|
|
|
+ if(iSize == 0)
|
|
|
+ {
|
|
|
+ *pData = NULL;
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ VPLIST_MAP_IT it_ = VM.find(pOriginal);
|
|
|
+ if(it_ == VM.end()) return -1;
|
|
|
+
|
|
|
+ void *mem = malloc(iSize);
|
|
|
+ if(mem == NULL) return -1;
|
|
|
+
|
|
|
+ memset(mem,0,iSize);
|
|
|
+ (it_->second)->insert((it_->second)->end(),mem);
|
|
|
+ *pData = mem;
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ void FreeBuffer(void* pOriginal)
|
|
|
+ {
|
|
|
+ if(pOriginal == NULL) return ;
|
|
|
+
|
|
|
+ VPLIST_MAP_IT it_ = VM.find(pOriginal);
|
|
|
+ if(it_ != VM.end())
|
|
|
+ {
|
|
|
+ LPVPLIST vl = it_->second;
|
|
|
+
|
|
|
+ VPLISTIT t= vl->begin();
|
|
|
+ for(t;t!=vl->end();)
|
|
|
+ {
|
|
|
+ free(*t);
|
|
|
+ vl->erase(t++);
|
|
|
+ }
|
|
|
+ delete vl;
|
|
|
+ VM.erase(it_);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ private:
|
|
|
+ void FreeBuffer()
|
|
|
+ {
|
|
|
+ //遍历VM中的元素
|
|
|
+ while(!VM.empty())
|
|
|
+ {
|
|
|
+ VPLIST_MAP_IT it_ = VM.begin();
|
|
|
+ LPVPLIST vl = it_->second;
|
|
|
+
|
|
|
+ //遍列地址列表
|
|
|
+ VPLISTIT t = vl->begin();
|
|
|
+ for(t;t!=vl->end();)
|
|
|
+ {
|
|
|
+ free(*t);
|
|
|
+ vl->erase(t++);
|
|
|
+ }
|
|
|
+ delete vl;
|
|
|
+ VM.erase(it_);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ private:
|
|
|
+ //存放各个LPVPLIST对象的MAP
|
|
|
+ VPLIST_MAP VM;
|
|
|
+
|
|
|
+};
|
|
|
+
|
|
|
+
|
|
|
+///////////////////////////////////#####未使用STL的字符串处理集合#####///////////////////////////////////
|
|
|
+
|
|
|
+/***********************************************
|
|
|
+ 说明:
|
|
|
+ 空白字符包括:
|
|
|
+ 空格: 0x20
|
|
|
+ 换页符 (\f): 0x0c
|
|
|
+ 换行符 (\n): 0x0a
|
|
|
+ 回车符 (\r): 0x0d
|
|
|
+ 制表符 (\t): 0x09
|
|
|
+ 垂直制表符 (\v): 0x0b
|
|
|
+ ************************************************/
|
|
|
+
|
|
|
+class CPlusStringProcess
|
|
|
+{
|
|
|
+ public:
|
|
|
+ //构造器
|
|
|
+ CPlusStringProcess()
|
|
|
+ {
|
|
|
+ //空白字符合集
|
|
|
+ memset(m_pWhiteChar, 0, sizeof(m_pWhiteChar));
|
|
|
+ strcpy(m_pWhiteChar, "\x20\x0c\x0a\x0d\x09\x0b");
|
|
|
+ }
|
|
|
+
|
|
|
+ public:
|
|
|
+ //功能:去除左边的空白符
|
|
|
+ //参数:p_pStrOld : 要操作的字符串
|
|
|
+ // p_pStrNew : 如果为null则修改作用于p_pStrOld,否则通过p_pStrNew返回
|
|
|
+ //备注:如果p_pStrOld为空指针或空字符串,则不对原字符串做任何修改,同时如果p_pStrNew
|
|
|
+ // 也不为空,则将p_pStrNew置为一个空字符串
|
|
|
+ void TrimLeft(char* p_pStrOld, char* p_pStrNew = NULL)
|
|
|
+ {
|
|
|
+ //如果操作字符串为空则直接返回
|
|
|
+ if (p_pStrOld == NULL || strlen(p_pStrOld) <= 0)
|
|
|
+ {
|
|
|
+ if (p_pStrNew != NULL)
|
|
|
+ p_pStrNew[0] = '\0';
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //临时存储字符串
|
|
|
+ char* f_pTempStr = (char*)malloc(sizeof(char)*(strlen(p_pStrOld)+1));
|
|
|
+ memset(f_pTempStr, 0, sizeof(char)*(strlen(p_pStrOld)+1));
|
|
|
+ //查找左边第一个非空白符
|
|
|
+ char* f_pStrIdx = p_pStrOld;
|
|
|
+ for (unsigned int i=0; i<strlen(p_pStrOld)&&is_white_char_(*f_pStrIdx); f_pStrIdx++,i++);
|
|
|
+ //拷贝不包括左边空白字符的字符串到f_pTempStr
|
|
|
+ strcpy(f_pTempStr, f_pStrIdx);
|
|
|
+ //产生输出
|
|
|
+ if (p_pStrNew == NULL)
|
|
|
+ strcpy(p_pStrOld, f_pTempStr);
|
|
|
+ else
|
|
|
+ strcpy(p_pStrNew, f_pTempStr);
|
|
|
+ //释放临时字符串空间
|
|
|
+ free(f_pTempStr);
|
|
|
+ }
|
|
|
+
|
|
|
+ //功能:去除右边的空白符
|
|
|
+ //参数:p_pStrOld : 要操作的字符串
|
|
|
+ // p_pStrNew : 如果为null则修改作用于p_pStrOld,否则通过p_pStrNew返回
|
|
|
+ //备注:如果p_pStrOld为空指针或空字符串,则不对原字符串做任何修改,同时如果p_pStrNew
|
|
|
+ // 也不为空,则将p_pStrNew置为一个空字符串
|
|
|
+ void TrimRight(char* p_pStrOld, char* p_pStrNew = NULL)
|
|
|
+ {
|
|
|
+ //如果操作字符串为空则直接返回
|
|
|
+ if (p_pStrOld == NULL || strlen(p_pStrOld) <= 0)
|
|
|
+ {
|
|
|
+ if (p_pStrNew != NULL)
|
|
|
+ p_pStrNew[0] = '\0';
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //临时存储字符串
|
|
|
+ char* f_pTempStr = (char*)malloc(sizeof(char)*(strlen(p_pStrOld)+1));
|
|
|
+ memset(f_pTempStr, 0, sizeof(char)*(strlen(p_pStrOld)+1));
|
|
|
+ //拷贝原操作字符串
|
|
|
+ strcpy(f_pTempStr, p_pStrOld);
|
|
|
+ //循环去除右边的空白符
|
|
|
+ for (int i=strlen(f_pTempStr)-1; i>=0&&is_white_char_(f_pTempStr[i]); f_pTempStr[i--]='\0');
|
|
|
+ //产生输出
|
|
|
+ if (p_pStrNew == NULL)
|
|
|
+ strcpy(p_pStrOld, f_pTempStr);
|
|
|
+ else
|
|
|
+ strcpy(p_pStrNew, f_pTempStr);
|
|
|
+ //释放临时字符串空间
|
|
|
+ free(f_pTempStr);
|
|
|
+ }
|
|
|
+
|
|
|
+ //功能:去除左右两边的空白符
|
|
|
+ //参数:p_pStrOld : 要操作的字符串
|
|
|
+ // p_pStrNew : 如果为null则修改作用于p_pStrOld,否则通过p_pStrNew返回
|
|
|
+ //备注:如果p_pStrOld为空指针或空字符串,则不对原字符串做任何修改,同时如果p_pStrNew
|
|
|
+ // 也不为空,则将p_pStrNew置为一个空字符串
|
|
|
+ void TrimLR(char* p_pStrOld, char* p_pStrNew = NULL)
|
|
|
+ {
|
|
|
+ //如果操作字符串为空则直接返回
|
|
|
+ if (p_pStrOld == NULL || strlen(p_pStrOld) <= 0)
|
|
|
+ {
|
|
|
+ if (p_pStrNew != NULL)
|
|
|
+ p_pStrNew[0] = '\0';
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //临时存储字符串
|
|
|
+ char* f_pTempStr = (char*)malloc(sizeof(char)*(strlen(p_pStrOld)+1));
|
|
|
+ memset(f_pTempStr, 0, sizeof(char)*(strlen(p_pStrOld)+1));
|
|
|
+ //拷贝原操作字符串
|
|
|
+ strcpy(f_pTempStr, p_pStrOld);
|
|
|
+ //去除左边空格
|
|
|
+ TrimLeft(f_pTempStr);
|
|
|
+ //去除右边空格
|
|
|
+ TrimRight(f_pTempStr);
|
|
|
+ //产生输出
|
|
|
+ if (p_pStrNew == NULL)
|
|
|
+ strcpy(p_pStrOld, f_pTempStr);
|
|
|
+ else
|
|
|
+ strcpy(p_pStrNew, f_pTempStr);
|
|
|
+ //释放临时字符串空间
|
|
|
+ free(f_pTempStr);
|
|
|
+ }
|
|
|
+
|
|
|
+ //功能:去除字符串里的所有空白符
|
|
|
+ //参数:p_pStrOld : 要操作的字符串
|
|
|
+ // p_pStrNew : 如果为null则修改作用于p_pStrOld,否则通过p_pStrNew返回
|
|
|
+ //备注:如果p_pStrOld为空指针或空字符串,则不对原字符串做任何修改,同时如果p_pStrNew
|
|
|
+ // 也不为空,则将p_pStrNew置为一个空字符串
|
|
|
+ void TrimAll(char* p_pStrOld, char* p_pStrNew = NULL)
|
|
|
+ {
|
|
|
+ //如果操作字符串为空则直接返回
|
|
|
+ if (p_pStrOld == NULL || strlen(p_pStrOld) <= 0)
|
|
|
+ {
|
|
|
+ if (p_pStrNew != NULL)
|
|
|
+ p_pStrNew[0] = '\0';
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //临时存储字符串
|
|
|
+ char* f_pTempStr = (char*)malloc(sizeof(char)*(strlen(p_pStrOld)+1));
|
|
|
+ memset(f_pTempStr, 0, sizeof(char)*(strlen(p_pStrOld)+1));
|
|
|
+ //拷贝非空白字符
|
|
|
+ int f_iIdx = 0;
|
|
|
+ for (unsigned int i=0; i<strlen(p_pStrOld); i++)
|
|
|
+ if (!is_white_char_(p_pStrOld[i]))
|
|
|
+ f_pTempStr[f_iIdx++] = p_pStrOld[i];
|
|
|
+ //产生输出
|
|
|
+ if (p_pStrNew == NULL)
|
|
|
+ strcpy(p_pStrOld, f_pTempStr);
|
|
|
+ else
|
|
|
+ strcpy(p_pStrNew, f_pTempStr);
|
|
|
+ //释放临时字符串空间
|
|
|
+ free(f_pTempStr);
|
|
|
+ }
|
|
|
+
|
|
|
+ //功能:分隔字符串
|
|
|
+ //参数:p_pStrOld : 要操作的字符串
|
|
|
+ // p_pSpliter : 分隔字符串
|
|
|
+ // p_ppResult : 指针数组,保存分割后的各个字符串地址
|
|
|
+ //备注:1. 如果p_pStrOld为空指针或空字符串,则不会产生任何输出
|
|
|
+ // 2. 如果p_pSpliter为空指针或空字符串,则不会产生任何输出
|
|
|
+ // 3. 指针数组p_ppResult指向分割后的各字符串指针,以NULL结束,
|
|
|
+ // 因此如果分割得到n个字符串则该数组大小应为n+1
|
|
|
+ // 4. 指针数组p_ppResult应由外部保证传入时已将所有元素初始化为NULL
|
|
|
+ // 5. p_ppResult各元素所指向的内存数据需要调用FreeSplitOut方法来释放,
|
|
|
+ // 将p_ppResult[0]指针值传给FreeSplitOut函数即可完成释放
|
|
|
+ // 6. 假设用"A"来分割字符串"12A34A",则分割后应得到"12"/"34"/"",
|
|
|
+ // 但因为最后一个字符串是空字符串,所以最终返回的只有"12"/"34",
|
|
|
+ // 有如"12AA",最后得到的字符串也只有"12"
|
|
|
+ void Split(char* p_pStrOld, char* p_pSpliter, char* p_ppResult[])
|
|
|
+ {
|
|
|
+ //如果p_pStrOld为空指针或空字符串则直接返回
|
|
|
+ if (p_pStrOld == NULL || strlen(p_pStrOld) <= 0)
|
|
|
+ return;
|
|
|
+ //如果p_pSpliter为空指针或空字符串则直接返回
|
|
|
+ if (p_pSpliter == NULL || strlen(p_pSpliter) <= 0)
|
|
|
+ return;
|
|
|
+ //操作字符串长度
|
|
|
+ unsigned int f_uiStrOldLen = strlen(p_pStrOld);
|
|
|
+ //分隔字符串长度
|
|
|
+ unsigned int f_uiSpliterLen = strlen(p_pSpliter);
|
|
|
+ //输出字符串,分隔开的各字符串统一存储在该连续的空间上,而由不同的字符串指针来标识
|
|
|
+ char* f_pOutStr = (char*)malloc(sizeof(char)*(f_uiStrOldLen*2));
|
|
|
+ memset(f_pOutStr, 0, sizeof(char)*(f_uiStrOldLen*2));
|
|
|
+ //当前已填充到f_pOutStr的索引值
|
|
|
+ int f_iOutStrIdx = 0;
|
|
|
+ //已分隔开的字符串个数
|
|
|
+ int f_iResultNum = 0;
|
|
|
+ //已查找过的字符串下标索引值
|
|
|
+ int f_iStrOldIdx = 0;
|
|
|
+ //循环查找p_pSpliter出现位置
|
|
|
+ for (char* p=NULL; ((unsigned int)f_iStrOldIdx)<f_uiStrOldLen;)
|
|
|
+ {
|
|
|
+ //新分隔开的字符串长度
|
|
|
+ unsigned int f_uiNewStrLen = 0;
|
|
|
+ //查找下一个分隔字符串
|
|
|
+ p = strstr(p_pStrOld+f_iStrOldIdx, p_pSpliter);
|
|
|
+ //如果没找到分隔字符串说明剩余的全部都需要拷贝
|
|
|
+ if (p == NULL)
|
|
|
+ f_uiNewStrLen = strlen(p_pStrOld+f_iStrOldIdx);
|
|
|
+ //如果找到分隔字符串说明只拷贝间隔部分
|
|
|
+ else
|
|
|
+ f_uiNewStrLen = p - (p_pStrOld+f_iStrOldIdx);
|
|
|
+ //拷贝的前提是不为空字符串
|
|
|
+ if (f_uiNewStrLen > 0)
|
|
|
+ {
|
|
|
+ //记录第n个分隔开的字符串指针地址
|
|
|
+ p_ppResult[f_iResultNum++] = f_pOutStr+f_iOutStrIdx;
|
|
|
+ //拷贝第n个分隔开的字符串
|
|
|
+ memcpy(f_pOutStr+f_iOutStrIdx, p_pStrOld+f_iStrOldIdx, f_uiNewStrLen);
|
|
|
+ //更新输出字符串连续地址的索引值,注意需要多向后移一位以保证有'\0'存在
|
|
|
+ f_iOutStrIdx += (f_uiNewStrLen+1);
|
|
|
+ }
|
|
|
+ //更新下一次查找的起始位置
|
|
|
+ f_iStrOldIdx += (f_uiNewStrLen+(p==NULL?0:f_uiSpliterLen));
|
|
|
+ }
|
|
|
+ //如果没有产生输出,则要将f_pOutStr释放,否则由外部来进行释放
|
|
|
+ if (p_ppResult[0] == NULL)
|
|
|
+ free(f_pOutStr);
|
|
|
+ }
|
|
|
+
|
|
|
+ //功能:分隔字符串,该字符串以0x00分隔以两个0x00结束
|
|
|
+ //参数:p_pStrOld : 要操作的字符串
|
|
|
+ // p_ppResult : 指针数组,保存分割后的各个字符串地址
|
|
|
+ // p_iStrOldMaxLen : 要操作的字符串最大可能长度,默认为10k
|
|
|
+ //备注:1. 指针数组p_ppResult指向分割后的各字符串指针,以NULL结束,
|
|
|
+ // 因此如果分割得到n个字符串则该数组大小应为n+1
|
|
|
+ // 2. 指针数组p_ppResult应由外部保证传入时已将所有元素初始化为NULL
|
|
|
+ // 3. p_ppResult各元素所指向的内存数据需要调用FreeSplitOut方法来释放,
|
|
|
+ // 将p_ppResult[0]指针值传给FreeSplitOut函数即可完成释放
|
|
|
+ //返回:分隔后的串数
|
|
|
+ int Split_00(char* p_pStrOld, char* p_ppResult[], int p_iStrOldMaxLen = 1024*10)
|
|
|
+ {
|
|
|
+ //输出字符串,分隔开的各字符串统一存储在该连续的空间上,而由不同的字符串指针来标识
|
|
|
+ char* f_pOutStr = (char*)malloc(sizeof(char)*(p_iStrOldMaxLen*2));
|
|
|
+ memset(f_pOutStr, 0, sizeof(char)*(p_iStrOldMaxLen*2));
|
|
|
+ //已查找过的字符串下标索引值
|
|
|
+ int f_iStrOldIdx = 0;
|
|
|
+ //当前已填充到f_pOutStr的索引值
|
|
|
+ int f_iOutStrIdx = 0;
|
|
|
+ //新分隔开的字符串长度
|
|
|
+ unsigned int f_uiNewStrLen = 0;
|
|
|
+ //已分隔开的字符串个数
|
|
|
+ int f_iResultNum = 0;
|
|
|
+ //循环获取各字符串
|
|
|
+ for ( ;
|
|
|
+ ( p_pStrOld != NULL ) &&
|
|
|
+ ( f_iStrOldIdx == 0
|
|
|
+ ? !( *(p_pStrOld+f_iStrOldIdx) == NULL && *(p_pStrOld+f_iStrOldIdx+1) == NULL )
|
|
|
+ : 1 ) &&
|
|
|
+ ( f_iStrOldIdx != 0
|
|
|
+ ? !( *(p_pStrOld+f_iStrOldIdx) == NULL )
|
|
|
+ : 1 ) &&
|
|
|
+ ( f_uiNewStrLen = strlen(p_pStrOld+f_iStrOldIdx), 1 )
|
|
|
+ ;
|
|
|
+ ( f_uiNewStrLen > 0
|
|
|
+ ? ( p_ppResult[f_iResultNum++] = f_pOutStr+f_iOutStrIdx ,
|
|
|
+ memcpy(f_pOutStr+f_iOutStrIdx, p_pStrOld+f_iStrOldIdx, f_uiNewStrLen) ,
|
|
|
+ f_iOutStrIdx += (f_uiNewStrLen+1) ,
|
|
|
+ f_iStrOldIdx += (f_uiNewStrLen+1) )
|
|
|
+ : ( f_iStrOldIdx += (f_uiNewStrLen+1) )
|
|
|
+ )
|
|
|
+ );
|
|
|
+ //如果没有产生输出,则要将f_pOutStr释放,否则由外部来进行释放
|
|
|
+ if (p_ppResult == NULL || p_ppResult[0] == NULL)
|
|
|
+ free(f_pOutStr);
|
|
|
+ //返回
|
|
|
+ return f_iResultNum;
|
|
|
+ }
|
|
|
+
|
|
|
+ //功能:分隔Unicode字符串,该字符串以0x0000分隔以两个0x0000结束
|
|
|
+ //参数:p_pStrOld : 要操作的Unicode字符串
|
|
|
+ // p_ppResult : 指针数组,保存分割后的各个Unicode字符串地址
|
|
|
+ // p_iStrOldMaxLen : 要操作的Unicode字符串最大可能长度,默认为10k
|
|
|
+ //备注:1. 指针数组p_ppResult指向分割后的Unicode各字符串指针,以NULL结束,
|
|
|
+ // 因此如果分割得到n个Unicode字符串则该数组大小应为n+1
|
|
|
+ // 2. 指针数组p_ppResult应由外部保证传入时已将所有元素初始化为NULL
|
|
|
+ // 3. p_ppResult各元素所指向的内存数据需要调用FreeSplitOut方法来释放,
|
|
|
+ // 将p_ppResult[0]指针值传给FreeSplitOut函数即可完成释放
|
|
|
+ //返回:分隔后的串数
|
|
|
+ int SplitW_00(wchar_t* p_pStrOld, wchar_t* p_ppResult[], int p_iStrOldMaxLen = 1024*10)
|
|
|
+ {
|
|
|
+ //输出字符串,分隔开的各字符串统一存储在该连续的空间上,而由不同的字符串指针来标识
|
|
|
+ wchar_t* f_pOutStr = (wchar_t*)malloc(sizeof(wchar_t)*(p_iStrOldMaxLen*2));
|
|
|
+ memset(f_pOutStr, 0, sizeof(wchar_t)*(p_iStrOldMaxLen*2));
|
|
|
+ //已查找过的字符串下标索引值
|
|
|
+ int f_iStrOldIdx = 0;
|
|
|
+ //当前已填充到f_pOutStr的索引值
|
|
|
+ int f_iOutStrIdx = 0;
|
|
|
+ //新分隔开的字符串长度
|
|
|
+ unsigned int f_uiNewStrLen = 0;
|
|
|
+ //已分隔开的字符串个数
|
|
|
+ int f_iResultNum = 0;
|
|
|
+ //循环获取各字符串
|
|
|
+ for ( ;
|
|
|
+ ( p_pStrOld != NULL ) &&
|
|
|
+ ( f_iStrOldIdx == 0
|
|
|
+ ? !( *(p_pStrOld+f_iStrOldIdx) == NULL && *(p_pStrOld+f_iStrOldIdx+1) == NULL )
|
|
|
+ : 1 ) &&
|
|
|
+ ( f_iStrOldIdx != 0
|
|
|
+ ? !( *(p_pStrOld+f_iStrOldIdx) == NULL )
|
|
|
+ : 1 ) &&
|
|
|
+ ( f_uiNewStrLen = wcslen(p_pStrOld+f_iStrOldIdx), 1 )
|
|
|
+ ;
|
|
|
+ ( f_uiNewStrLen > 0
|
|
|
+ ? ( p_ppResult[f_iResultNum++] = f_pOutStr+f_iOutStrIdx ,
|
|
|
+ wmemcpy(f_pOutStr+f_iOutStrIdx, p_pStrOld+f_iStrOldIdx, f_uiNewStrLen) ,
|
|
|
+ f_iOutStrIdx += (f_uiNewStrLen+1) ,
|
|
|
+ f_iStrOldIdx += (f_uiNewStrLen+1) )
|
|
|
+ : ( f_iStrOldIdx += (f_uiNewStrLen+1) )
|
|
|
+ )
|
|
|
+ );
|
|
|
+ //如果没有产生输出,则要将f_pOutStr释放,否则由外部来进行释放
|
|
|
+ if (p_ppResult == NULL || p_ppResult[0] == NULL)
|
|
|
+ free(f_pOutStr);
|
|
|
+ //返回
|
|
|
+ return f_iResultNum;
|
|
|
+ }
|
|
|
+
|
|
|
+ //功能:释放Split/Split_00/SplitW_00方法产生的p_ppResult输出
|
|
|
+ //参数:p_pMem : 要释放的地址值
|
|
|
+ //备注:调用Split后输出参数为分隔开的各字符串地址,要释放这些字符串
|
|
|
+ // 的内存只需将p_ppResult[0]的值作为参数传入即可完成全部内存的释放,
|
|
|
+ // 另外,释放一个NULL地址值不会出错
|
|
|
+ void FreeSplitOut(void* p_pMem)
|
|
|
+ {
|
|
|
+ free(p_pMem);
|
|
|
+ }
|
|
|
+
|
|
|
+ private:
|
|
|
+ //判断一个字符是否是空白符,为真则返回1,为假则返回0
|
|
|
+ int is_white_char_(char p_cChar)
|
|
|
+ {
|
|
|
+ for (unsigned int i=0; i<strlen(m_pWhiteChar); i++)
|
|
|
+ if (p_cChar == m_pWhiteChar[i])
|
|
|
+ return 1;
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ private:
|
|
|
+ //空白字符合集
|
|
|
+ char m_pWhiteChar[10];
|
|
|
+};
|
|
|
+
|
|
|
+
|
|
|
+typedef struct __ini_item__
|
|
|
+{
|
|
|
+ string Key;
|
|
|
+ string Value;
|
|
|
+ string Note;
|
|
|
+}INIITEM,*LPINIITEM;
|
|
|
+
|
|
|
+typedef struct __ini_grop__
|
|
|
+{
|
|
|
+ string name;
|
|
|
+ string Note;
|
|
|
+ list<INIITEM> Items;
|
|
|
+}INIGROP,*LPINIGROP;
|
|
|
+
|
|
|
+//INI文件读写
|
|
|
+class UFS_Stream_INI
|
|
|
+{
|
|
|
+ public:
|
|
|
+ //**********
|
|
|
+ //该构造器将读取参数 iniPath 传入的文件信息,来获取INI配置信息,
|
|
|
+ //INI文件编写方式如下
|
|
|
+ // [NTFKV2CF]
|
|
|
+ // DeviceDll=libCardReader.so;注释so库名称
|
|
|
+ // StatusTime=2000;状态轮询时间
|
|
|
+ // OpenReset=0
|
|
|
+ UFS_Stream_INI(const char* iniPath)
|
|
|
+ {
|
|
|
+ list_name.clear();
|
|
|
+
|
|
|
+ if(!iniPath) return ;
|
|
|
+
|
|
|
+ ifstream fin;
|
|
|
+ fin.open(iniPath);
|
|
|
+ if(fin.fail())return;
|
|
|
+
|
|
|
+ string strBuff = "";
|
|
|
+ string desBuff = "";
|
|
|
+ char tmpChar ;
|
|
|
+ while(!fin.eof())
|
|
|
+ {
|
|
|
+ // 逐行读取并解析
|
|
|
+ strBuff = "";
|
|
|
+ while(!fin.eof())
|
|
|
+ {
|
|
|
+ tmpChar = static_cast<char>(fin.get());
|
|
|
+ if(fin.eof())
|
|
|
+ break;
|
|
|
+ // 如果是换行或者回车则一整行读取完毕
|
|
|
+ if(tmpChar == static_cast<char>(10) ||
|
|
|
+ tmpChar == static_cast<char>(13) ||
|
|
|
+ tmpChar == static_cast<char>(EOF))
|
|
|
+ break;
|
|
|
+ strBuff += tmpChar;
|
|
|
+ }
|
|
|
+ if(strBuff.length() < 1 || strBuff[0] == note)
|
|
|
+ continue;
|
|
|
+
|
|
|
+ //剔除字符串前后的空格
|
|
|
+ TrimItem(strBuff,desBuff);
|
|
|
+ //cout<<desBuff<<endl;
|
|
|
+ INIGROP ini_grop;
|
|
|
+ ini_grop.name = "";
|
|
|
+ ini_grop.Note = "";
|
|
|
+ ini_grop.Items.clear();
|
|
|
+ //解析键名
|
|
|
+ if(ParseGroup(desBuff,&ini_grop))
|
|
|
+ {
|
|
|
+ list_name.push_back(ini_grop);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ INIITEM ini_item;
|
|
|
+ ini_item.Key = "";
|
|
|
+ ini_item.Value = "";
|
|
|
+ ini_item.Note = "";
|
|
|
+
|
|
|
+ //解析键值
|
|
|
+ if(ParseItem(desBuff,&ini_item))
|
|
|
+ {
|
|
|
+ list_name.back().Items.push_back(ini_item);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ fin.close();
|
|
|
+
|
|
|
+ bFlag = true;
|
|
|
+ }
|
|
|
+ virtual ~UFS_Stream_INI()
|
|
|
+ {
|
|
|
+
|
|
|
+ list_name.clear();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //功能:数据读取
|
|
|
+ //参数:lpAppName : 要操作的节名
|
|
|
+ // lpKeyName : 要操作的键名
|
|
|
+ // lpDefault :如果lpRetrunString为空,则把个变量赋给lpRetrunString
|
|
|
+ // lpRetrunString :存放键值的指针变量,用于接收INI文件中键值(数据)的接收缓冲区
|
|
|
+ //返回值 :
|
|
|
+ // lpRetrunString数据长度
|
|
|
+ int GetPrivateProfileString(LPSTR lpAppName,LPSTR lpKeyName,LPSTR lpDefault,LPSTR lpRetrunString)
|
|
|
+ {
|
|
|
+ if(!bFlag) return -1;
|
|
|
+ bool bFindFlag = false;
|
|
|
+ //查找组名
|
|
|
+ for(list_name_it = list_name.begin();list_name_it != list_name.end();++list_name_it)
|
|
|
+ {
|
|
|
+ if(!strcmp((char*)lpAppName,(*list_name_it).name.c_str())){
|
|
|
+ bFindFlag = true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(!bFindFlag) return -1;
|
|
|
+
|
|
|
+ //cout<<(*list_name_it).name.c_str()<<endl;
|
|
|
+
|
|
|
+ bFindFlag = false;
|
|
|
+ //获取键名键值
|
|
|
+ for(list_item_it = (*list_name_it).Items.begin();
|
|
|
+ list_item_it != (*list_name_it).Items.end();
|
|
|
+ ++list_item_it)
|
|
|
+ {
|
|
|
+ //cout<<(*list_item_it).Key<<"++"<<(*list_item_it).Value<<"--"<<(*list_item_it).Note<<endl;
|
|
|
+ if(!strcmp((char*)lpKeyName,(*list_item_it).Key.c_str()))
|
|
|
+ {
|
|
|
+ //cout<<(*list_item_it).Key<<endl;
|
|
|
+ bFindFlag =true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //std::cout<<"name"<<(*list_item_it).Key<<"==>"<<(*list_item_it).Value<<endl;
|
|
|
+ if(bFindFlag)
|
|
|
+ memcpy(lpRetrunString,(*list_item_it).Value.c_str(),(*list_item_it).Value.length());
|
|
|
+ else
|
|
|
+ memcpy(lpRetrunString,lpDefault,strlen((char*)lpDefault));
|
|
|
+ return strlen((char*)lpRetrunString);
|
|
|
+ }
|
|
|
+
|
|
|
+ //写文件方式待续。。。
|
|
|
+
|
|
|
+ private:
|
|
|
+
|
|
|
+ list<INIGROP> list_name;
|
|
|
+ list<INIGROP>::iterator list_name_it;
|
|
|
+ list<INIITEM>::iterator list_item_it;
|
|
|
+ //注释标志
|
|
|
+ char note = ';';
|
|
|
+ bool bFlag = false;
|
|
|
+ //剔除字符串前后的空格
|
|
|
+ static void TrimItem(const string& str,string& dst)
|
|
|
+ {
|
|
|
+ int ilen = str.length();
|
|
|
+ if(ilen<1)return ;
|
|
|
+
|
|
|
+ //剔除前端的空格
|
|
|
+ int iStart = 0;
|
|
|
+ while(str[iStart] == ' ')
|
|
|
+ ++iStart;
|
|
|
+ if(iStart>str.length())
|
|
|
+ {
|
|
|
+ dst = "";
|
|
|
+ return ;
|
|
|
+ }
|
|
|
+
|
|
|
+ int iEnd = ilen;
|
|
|
+ if(iEnd >= str.length())
|
|
|
+ iEnd = str.length();
|
|
|
+
|
|
|
+ //剔除后端的空格
|
|
|
+ while(str[iEnd - 1] == ' ' && iEnd > iStart)
|
|
|
+ --iEnd;
|
|
|
+ // 如果字符串包含在 "" 中则提取出来
|
|
|
+ if(str[iStart] == '\"' && str[iEnd - 1] == '\"')
|
|
|
+ {
|
|
|
+ ++iStart;
|
|
|
+ --iEnd;
|
|
|
+ }
|
|
|
+ dst = str.substr(iStart, iEnd - iStart);
|
|
|
+ }
|
|
|
+
|
|
|
+ //解析分组
|
|
|
+ static bool ParseGroup(const string& str ,LPINIGROP lpIniGrop,char note = ';' )
|
|
|
+ {
|
|
|
+ if(str.empty()) return false;
|
|
|
+
|
|
|
+ string::size_type left = str.find('[');
|
|
|
+ // 没有找到[号
|
|
|
+ if(left == string::npos)
|
|
|
+ return false;
|
|
|
+
|
|
|
+ string::size_type right = str.rfind(']');
|
|
|
+
|
|
|
+ if(right == string::npos)return false;
|
|
|
+ if(right < left)return false;
|
|
|
+
|
|
|
+ left++;
|
|
|
+ lpIniGrop->name = str.substr(left, right - left);
|
|
|
+
|
|
|
+ string::size_type tag = str.find(note);
|
|
|
+ if(tag != string::npos && tag > right)
|
|
|
+ {
|
|
|
+ ++tag;
|
|
|
+ lpIniGrop->Note = str.substr(tag, str.length() - tag);
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ //解析元素
|
|
|
+ static bool ParseItem(const string& str ,LPINIITEM lpIniItem,char note = ';' )
|
|
|
+ {
|
|
|
+ if(str.empty()) return false;
|
|
|
+
|
|
|
+ string tempKey = "";
|
|
|
+ string tempValue = "";
|
|
|
+ string tempNote = "";
|
|
|
+ string::size_type pos = str.find('=');//查找 = 位置
|
|
|
+ string::size_type right = str.length();//获取数据长度
|
|
|
+ string::size_type cnote = str.find(note);
|
|
|
+ //未找到 “=”
|
|
|
+ if((pos == string::npos)||(pos == 0))return false;
|
|
|
+ if((cnote!=string::npos)&&(cnote<=pos))return false;
|
|
|
+
|
|
|
+ tempKey= str.substr(0,pos);
|
|
|
+ ++pos;
|
|
|
+
|
|
|
+ if(cnote != string::npos)
|
|
|
+ {
|
|
|
+ tempValue = str.substr(pos,cnote-pos);
|
|
|
+ ++cnote;
|
|
|
+ tempNote = str.substr(cnote,right - cnote);
|
|
|
+
|
|
|
+ }
|
|
|
+ else
|
|
|
+ tempValue = str.substr(pos,right - pos);
|
|
|
+
|
|
|
+ //cout<<tempKey<<"--"<<tempValue<<"++"<<tempNote<<endl;
|
|
|
+ int i_not_pos_start = 0;int i_not_pos_end = 0;
|
|
|
+
|
|
|
+ i_not_pos_start = tempKey.find_first_not_of(" ");
|
|
|
+ i_not_pos_end = tempKey.find_last_not_of(" ");
|
|
|
+ if((i_not_pos_start >= i_not_pos_end))lpIniItem->Key = "";
|
|
|
+ else lpIniItem->Key = str.substr(i_not_pos_start,i_not_pos_end - i_not_pos_start +1);
|
|
|
+
|
|
|
+ i_not_pos_start = tempValue.find_first_not_of(" ");
|
|
|
+ i_not_pos_end = tempValue.find_last_not_of(" ");
|
|
|
+ if((i_not_pos_start >= i_not_pos_end))
|
|
|
+ lpIniItem->Value = "";
|
|
|
+ else lpIniItem->Value = tempValue.substr(i_not_pos_start,i_not_pos_end - i_not_pos_start +1);
|
|
|
+
|
|
|
+ i_not_pos_start = tempNote.find_first_not_of(" ");
|
|
|
+ i_not_pos_end = tempNote.find_last_not_of(" ");
|
|
|
+ if((i_not_pos_start >= i_not_pos_end))
|
|
|
+ lpIniItem->Note = "";
|
|
|
+ else lpIniItem->Note = tempNote.substr(i_not_pos_start,i_not_pos_end - i_not_pos_start +1);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+};
|
|
|
+#endif
|