您的位置:首页 > 其它

VC常用的函数(原创)转载需加地址

2009-04-01 16:06 411 查看
Code

///////////////////////////////////////////////////////////////////////

//

// 函数名 : trim

// 功能描述 : 去空格

// 参数 : char *szStr [in][out]

// 返回值 : 去空格后的字符串

//

///////////////////////////////////////////////////////////////////////

char * trim(char *szStr)

{

char *p = NULL;

p = szStr;

while ( *p == ' '|| *p == '\r' || *p == '\n' )

{

p++;

}

strcpy(szStr,p);

p = szStr+strlen(szStr)-1;

while (*p == ' ' || *p == '\r' || *p == '\n' )

{

*p=0;

p--;

}

return szStr;

}

///////////////////////////////////////////////////////////////////////

//

// 函数名 : isNumber

// 功能描述 : 判断是不是号码

// 参数 : char *pszIn

// 返回值 : bool

//

///////////////////////////////////////////////////////////////////////

bool isNumber(char *pszIn)

{

return strspn(pszIn,"0123456789")==strlen(pszIn)?true:false; //strspn()从参数s 字符串的开头计算连续的字符,而这些字符都完全是accept 所指字符串中的//字符。简单的说,若strspn()返回的数值为n,则代表字符串s 开头连续有n 个字符都是属于字符串accept内的字符

}

void writeToLog(char *pszAppID,int nFlag,char *pszLogPath,char *pszLog,)

{

char szLogFile[256]={0};

char szTokens[] = "/\\";

char *lpToken = NULL;

char sName[256] = { 0 };

char soldName[256] = { 0 };

char szLogPath[256] = { 0 };

char szBuffer[1024*5] = { 0 };

char szLogFileName[128] = { 0 };

va_list args;

try

{

if ( pszLogPath != NULL )

{

strcpy ( szLogPath, pszLogPath );

if ( access ( szLogPath, 0 ) == -1 )

{

strcpy ( soldName, szLogPath );

lpToken = strtok( soldName, szTokens );

while( lpToken != NULL )

{

strcat ( sName, lpToken );

lpToken = strtok ( NULL, szTokens );// Get next token:

if( access ( sName, 0 ) == -1 )

{

mkdir(sName);

}

strcat(sName,"\\");

}

}

}

va_start(args, pszLog);

vsprintf(szBuffer, pszLog, args);

char szTime[128]={0};

time_t tv;

time ( &tv );

struct tm *t;

t = localtime( &tv );

if( nFlag == 0 )

{

sprintf ( szTime, "%s_%.4d.%.2d.%.2d_err.log",pszAppID,t->tm_year+1900,t->tm_mon+1,t->tm_mday);

}

else if(nFlag == 1 )

{

sprintf ( szTime, "%s_%.4d.%.2d.%.2d_report.log",pszAppID,t->tm_year+1900,t->tm_mon+1,t->tm_mday);

}

else

{

sprintf ( szTime, "%s_%.4d.%.2d.%.2d.log",pszAppID,t->tm_year+1900,t->tm_mon+1,t->tm_mday);

}

trim(szBuffer);

strcat(szBuffer,"\n");

sprintf (szLogFileName,"%s%s",szLogPath,szTime);

FILE* fp = fopen ( szLogFileName, "a+" );

if( fp )

{

time ( &tv );

t = localtime( &tv );

memset(szTime,0,128);

sprintf ( szTime, "%.4d-%.2d-%.2d %.2d:%.2d:%.2d\n",t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);

fwrite ( szTime, strlen(szTime), 1, fp );

fwrite ( szBuffer, strlen(szBuffer), 1, fp );

fclose ( fp );

}

va_end ( args );

}

catch(char *)

{

throw "写日志失败";

}

}

// 字符串替换函数.

// 能替换所有的要替换的字符串,被替换的字符串和替换的字符串不一定一样长.

// pInput - 输入字符串.

// pOutput - 输出字符串, 要保证足够的空间可以存储替换后的字符串.

// pSrc - 要被替换的子字符串, 比如%user%

// pDst - 要替换成的字符串, 比如user1

// 注意:以上的字符串均要以'\0'结尾.

void replaceAll(char *pInput, char *pOutput, char *pSrc, char *pDst)

{

char *pi, *po, *p;

int nSrcLen, nDstLen, nLen;

pi = pInput;

po = pOutput;

nSrcLen = strlen(pSrc);

nDstLen = strlen(pDst);

p = strstr(pi, pSrc); //从字符串pi中寻找pSrc第一次出现的位置

if(p)

{

while(p)

{

nLen = (int)(p - pi);

memcpy(po, pi, nLen);

memcpy(po + nLen, pDst, nDstLen);

pi = p + nSrcLen;

po = po + nLen + nDstLen;

p = strstr(pi, pSrc);

}

strcpy(po, pi);

}

else

{

strcpy(po, pi);

}

}

char * get_now_time(char *pbuf)

{

time_t tt;

struct tm *now;

time( &tt);

now = localtime( &tt);

sprintf( pbuf, "%04d-%02d-%02d %02d:%02d:%02d",

now->tm_year+1900,

now->tm_mon + 1,

now->tm_mday,

now->tm_hour,

now->tm_min,

now->tm_sec);

return pbuf;

}

char * get_format_time(char *pbuf,char *pformat)

{

time_t tt;

struct tm *now;

time( &tt);

now = localtime( &tt);

if(!strcmp(pformat,"yyyymmddhhmmss"))

sprintf( pbuf, "%04d%02d%02d%02d%02d%02d",

now->tm_year+1900,

now->tm_mon + 1,

now->tm_mday,

now->tm_hour,

now->tm_min,

now->tm_sec);

else

sprintf( pbuf, "%04d-%02d-%02d %02d:%02d:%02d",

now->tm_year+1900,

now->tm_mon + 1,

now->tm_mday,

now->tm_hour,

now->tm_min,

now->tm_sec);

return pbuf;

}

void split(char *pbuf,char *psplit,char *p[],int &nDisNum)

{

char *pPos = NULL;

char *pSrc = pbuf;

int i=0;

if ( pbuf == NULL || psplit == NULL)

{

return;

}

pPos = strstr(pbuf,psplit);

while ( pPos != NULL )

{

if ( pPos == pbuf )

{

strcpy(pSrc,pbuf+strlen(psplit));

}

else

{

if(p[i] == NULL )

return;

memcpy(p[i],pSrc,pPos-pSrc);

i++;

}

pSrc = pPos+strlen(psplit);

pPos = strstr(pSrc,psplit);

}

strcpy(p[i],pSrc);

nDisNum = i;

}

void debug(int nFlag,char *pbuf)

{

char szBuf[32] = {0};

if(nFlag == 1)

{

printf("%s\n",get_now_time(szBuf));

printf("%s\n",pbuf);

}

}

int get_total_miniute()

{

int nTotalMin;

int vmin,vhour,vday,vmon,vyear;

time_t vNow;

struct tm *vtm;

time( &vNow );

vtm = localtime( &vNow );

vmin = vtm->tm_min;

vhour = vtm->tm_hour;

vday = vtm->tm_mday;

vmon = vtm->tm_mon+1;

vyear = vtm->tm_year+1900-2005;

nTotalMin = ((vyear*365+vday)*24+vhour)*60+vmin;

return nTotalMin;

}

///////////////////////////////////////////////////////////////////////

// 函数 : getElementText

// 功能 : 获取xml文件的第nItemCount对的两个节点之间的值

// 参数 : char *pXmlContent[IN] xml文件内容

// 参数 : char *pBeginElement[IN] 第一个节点的内容

// 参数 : char *pEndElement[IN] 第二个节点的内容

// 参数 : char *pElementValue[OUT] 两个节点之间的值

// 参数 : int nItemCount[IN] 第几对节点之间的值

// 返回 : -1失败 0成功

// 描述 :

///////////////////////////////////////////////////////////////////////

int getElementText( const char *pXmlContent, char *pBeginElement, char *pEndElement, char *pElementValue,int nItemCount)

{

char *pBPos = NULL;

char *pEPos = NULL;

char *pOldBPos = NULL;

char *pOldEPos = NULL;

char szBuffer[4096] = { 0 };

pBPos = strstr( const_cast<char*>(pXmlContent), pBeginElement );

if ( pBPos == NULL )

{

return -1;

}

pEPos = strstr( const_cast<char*>(pXmlContent), pEndElement );

if ( pEPos == NULL )

{

return -1;

}

while ( nItemCount-- > 0 && pBPos != NULL && pEPos != NULL )

{

memset( szBuffer,0,sizeof(szBuffer) );

memcpy( szBuffer, pBPos+strlen(pBeginElement), trim(pEPos) - trim(pBPos)-strlen(pBeginElement) );

pBPos = strstr( pEPos+strlen(pEndElement), pBeginElement );

if ( pBPos == NULL )

{

break;

}

pEPos = strstr( pBPos+strlen(pBeginElement), pEndElement );

}

strcpy( pElementValue, szBuffer);

return 0;

}

///////////////////////////////////////////////////////////////////////

// 函数 : getElementCount

// 功能 : 取得两个节点在xml文件中的总共对数

// 参数 : const char *pXmlContent[IN]

// 参数 : char *pBeginElement[IN]

// 参数 : char *pEndElement[IN]

// 返回 : 总共对数

// 描述 :

///////////////////////////////////////////////////////////////////////

int getElementCount(const char *pXmlContent, char *pBeginElement, char *pEndElement)

{

char *pBPos = NULL;

char *pEPos = NULL;

int nItemCount = 0;

pBPos = strstr( const_cast<char*>(pXmlContent), pBeginElement );

if ( pBPos == NULL )

{

return 0;

}

while ( pBPos != NULL )

{

nItemCount++;

pEPos = strstr( pBPos+strlen(pBeginElement), pEndElement );

pBPos = strstr( pEPos+strlen(pEndElement), pBeginElement );

}

return nItemCount;

}

void ConvertGBKToUtf8(char *pszGBK,char *pszUtf8)

{

int len=MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)pszGBK, -1, NULL,0);

unsigned short * wszUtf8 = new unsigned short[len+1];

memset(wszUtf8, 0, len * 2 + 2);

MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)pszGBK, -1, (LPWSTR)wszUtf8, len);

len = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)wszUtf8, -1, NULL, 0, NULL, NULL);

char *szUtf8=new char[len + 1];

memset(szUtf8, 0, len + 1);

WideCharToMultiByte (CP_UTF8, 0, (LPCWSTR)wszUtf8, -1, szUtf8, len, NULL,NULL);

strcpy( pszUtf8, szUtf8 );

delete[] szUtf8;

delete[] wszUtf8;

szUtf8 = NULL;

wszUtf8 = NULL;

}

void ConvertUtf8ToGBK(char *pszUtf8, char *pszGBK)

{

int len=MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)pszUtf8, -1, NULL,0);

unsigned short * wszGBK = new unsigned short[len+1];

memset(wszGBK, 0, len * 2 + 2);

MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)pszUtf8, -1, (LPWSTR)wszGBK, len);

len = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)wszGBK, -1, NULL, 0, NULL, NULL);

char *szGBK=new char[len + 1];

memset(szGBK, 0, len + 1);

WideCharToMultiByte (CP_ACP, 0, (LPCWSTR)wszGBK, -1, szGBK, len, NULL,NULL);

strcpy( pszGBK, szGBK );

delete[] szGBK;

delete[] wszGBK;

szGBK = NULL;

wszGBK = NULL;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: