?? basefun.cc
字號(hào):
/*
* Copyright 2002 LinkAge Co.,Ltd. Inc. All Rights Reversed
*/
/*********************************************************************************/
/*** Name : BASEFUN.CC ***/
/*** ***/
/*** Description : 基本功能函數(shù) ***/
/*** ***/
/*** Author : 郭亮 ***/
/*** ***/
/*** Begin Time : 2002/05/26 ***/
/*** ***/
/*** Last Change Time : 2002/05/30 ***/
/*** ***/
/*********************************************************************************/
#include <stdlib.h>
#include <time.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include "basefun.h"
extern "C"
{
int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result);
}
bool _GetSubRange(const char **Src, char *Range1, char *Range2)
{
const char *pSrc = *Src;
const char *p1 = Range1;
const char *p2 = Range2;
//解析--subrange1:subrange2
//前一個(gè)range1
while (*pSrc == ' ' || *pSrc == '\t')
++pSrc;
while ( isalnum(*pSrc) )
*Range1++ = *pSrc++;
*Range1 = 0;
while (*pSrc == ' ' || *pSrc == '\t')
++pSrc;
if (*pSrc != ':')
return false;
else
++pSrc;
//后一個(gè)range2
while (*pSrc == ' ' || *pSrc == '\t')
++pSrc;
while ( isalnum(*pSrc) )
*Range2++ = *pSrc++;
*Range2 = 0;
while (*pSrc == ' ' || *pSrc == '\t')
++pSrc;
if (*p1 == 0 || *p2 == 0)
return false;
*Src += pSrc - *Src;
return true;
}
/*
* Function Name: Compare
* Description: 比較兩個(gè)字符串是否匹配(相等)
* Input Param:
* lv_chCompareString -------> 被比較的字符串
* lv_chCompareMod -------> 匹配的字符串,支持*,?,[]等通配符
* Returns:
* 如果兩個(gè)字符串匹配,返回true
* 如果兩個(gè)字符串不匹配,返回false
* complete: 2001/12/13
*/
bool Compare(const char *lv_chCompareString,const char *lv_chCompareMod)
{
while(1)
{
switch(*lv_chCompareMod)
{
case '\0':
if (*lv_chCompareString == '\0')
{
return true;
}
else
{
return false;
}
case '%': //掩碼:mask_type[mask_value] %R表示范圍(range),%R[sub1:sub2, sub3:sub4]
++lv_chCompareMod;
if (strncmp(lv_chCompareMod, "R[", 2) == 0)
{
int iLen = 0;
char *p;
char SubRange1[30];
char SubRange2[30];
lv_chCompareMod += 2; //跳過(guò)R[
while (1)
{
if (!_GetSubRange(&lv_chCompareMod, SubRange1, SubRange2))
return false;
iLen = strlen(SubRange1);
if (strlen(lv_chCompareString) < (size_t)iLen)
return false;
if ( (strncmp(lv_chCompareString, SubRange1, iLen) >= 0) &&
(strncmp(lv_chCompareString, SubRange2, iLen) <= 0 ) )
{ //匹配
p = strchr(lv_chCompareMod, ']');
if (p)
{
//跳過(guò)整個(gè)模式以及相應(yīng)長(zhǎng)度的字符串
lv_chCompareMod = p + 1;
lv_chCompareString += iLen;
break;
}
else
{
return false;
}
} //測(cè)試下一個(gè)
else if (*lv_chCompareMod == ',')
{
++ lv_chCompareMod;
continue;
}
else
{
return false;
}
}
continue; //外層while
}
return false;
case '!':
if (Compare(lv_chCompareString,lv_chCompareMod + 1) == true)
{
return false;
}
else
{
return true;
}
case '?' :
if(*lv_chCompareString == '\0')
{
return false;
}
return Compare(lv_chCompareString + 1,lv_chCompareMod + 1);
case '*' :
if(*(lv_chCompareMod+1) == '\0')
{
return true;
}
do
{
if(Compare(lv_chCompareString,lv_chCompareMod+1)==true)
{
return true;
}
}while(*(lv_chCompareString++));
return false;
case '[' :
lv_chCompareMod++;
do
{
if(*lv_chCompareMod == *lv_chCompareString)
{
while(*lv_chCompareMod != '\0' && *lv_chCompareMod != ']')
{
lv_chCompareMod++;
}
if(*lv_chCompareMod == ']')
{
lv_chCompareMod++;
}
return Compare(lv_chCompareString+1,lv_chCompareMod);
}
lv_chCompareMod++;
if((*lv_chCompareMod == ':') && (*(lv_chCompareMod+1) != ']'))
{
if((*lv_chCompareString >= *(lv_chCompareMod - 1)) && (*lv_chCompareString <= *(lv_chCompareMod + 1)))
{
while(*lv_chCompareMod != '\0' && *lv_chCompareMod != ']')
{
lv_chCompareMod++;
}
if(*lv_chCompareMod == ']')
{
lv_chCompareMod++;
}
return Compare(lv_chCompareString+1,lv_chCompareMod);
}
lv_chCompareMod++;
lv_chCompareMod++;
}
}while(*lv_chCompareMod != '\0' && *lv_chCompareMod != ']');
return false;
default :
if(*lv_chCompareString == *lv_chCompareMod)
{
++lv_chCompareString;
++lv_chCompareMod;
continue;
//return Compare(lv_chCompareString+1,lv_chCompareMod+1);
}
else
{
return false;
}
}//switch
}//while(1)
}
/*
* Function Name: GetSystemDateTime
* Description: 獲得當(dāng)前系統(tǒng)日期時(shí)間
* Input Param:
* CurrentDateTime -------> 當(dāng)前系統(tǒng)日期(格式14位YYYYMMDDHHMMSS)
* Returns: 無(wú)
* complete: 2003/06/30
*/
void GetSystemDateTime(char *CurrentDateTime)
{
time_t timer;
struct tm *today;
timer = time(NULL);
today = localtime(&timer);
sprintf(CurrentDateTime,"%04d%02d%02d%02d%02d%02d",today->tm_year + 1900,
today->tm_mon+1,today->tm_mday,
today->tm_hour,today->tm_min,today->tm_sec);
}
/*
* Function Name: GetShortSystemDateTime
* Description: 獲得當(dāng)前系統(tǒng)日期時(shí)間
* Input Param:
* CurrentDateTime -------> 當(dāng)前系統(tǒng)日期(格式12位YYMMDDHHMMSS)
* Returns: 無(wú)
* complete: 2003/06/30
*/
void GetShortSystemDateTime(char *CurrentDateTime)
{
time_t timer;
struct tm *today;
timer = time(NULL);
today = localtime(&timer);
sprintf(CurrentDateTime,"%02d%02d%02d%02d%02d%02d",today->tm_year,
today->tm_mon+1,today->tm_mday,
today->tm_hour,today->tm_min,today->tm_sec);
}
/*
* Function Name: GetSystemDate
* Description: 獲得當(dāng)前系統(tǒng)日期
* Input Param:
* CurrentDate -------> 當(dāng)前系統(tǒng)日期(格式y(tǒng)yyymmdd)
* Returns: 無(wú)
* complete: 2001/12/07
*/
void GetSystemDate(char *CurrentDate)
{
time_t timer;
struct tm *today;
timer = time(NULL);
today = localtime(&timer);
sprintf(CurrentDate,"%04d%02d%02d",today->tm_year + 1900,today->tm_mon+1,today->tm_mday);
}
/*
* Function Name: GetSystemTime
* Description: 獲得當(dāng)前系統(tǒng)時(shí)間
* Input Param:
* CurrentTime -------> 當(dāng)前系統(tǒng)時(shí)間(格式y(tǒng)ymmdd)
* Returns: 無(wú)
* complete: 2001/12/07
*/
void GetSystemTime(char *CurrentTime)
{
time_t timer;
struct tm *timenow;
timer = time(NULL);
timenow = localtime(&timer);
sprintf(CurrentTime,"%02d%02d%02d",timenow->tm_hour,timenow->tm_min,timenow->tm_sec);
}
/*
* Function Name: GetLocalIp
* Description: 得到本機(jī)的IP地址
* Input Param:
* LocalIp -------> 得到的本機(jī)IP地址
* Returns: 無(wú)
* complete: 2002/03/13
*/
bool GetLocalIp(char *LocalIp)
{
char chHostName[LENGTH];
int i;
struct in_addr addr;
/*取得機(jī)器名稱*/
if(gethostname(chHostName,sizeof(chHostName)) !=-1)
{
/*取得給定機(jī)器的信息*/
struct hostent* phost=gethostbyname(chHostName);
/*到每一個(gè)地址結(jié)尾*/
for(i=0;phost!= NULL&&phost->h_addr_list[i]!=NULL;i++)
{
memcpy(&addr, phost->h_addr_list[i], sizeof(struct in_addr));
}
strcpy(LocalIp,inet_ntoa(addr));
return true;
}
else
{
return false;
}
}
/*
* Function Name: FullPath
* Description: 給出完整的路徑名
* Input Param:
* chPath -------> 路徑名
* Returns: 如果路徑存在,則補(bǔ)齊,并返回0;不存在則返回-1
* complete: 2002/03/13
*/
int FullPath(char *chPath)
{
int len;
if(chPath[0] == '\0')
{
return -1;
}
len = strlen(chPath);
if(chPath[len - 1] != '/')
{
chPath[len] = '/';
chPath[len + 1] = '\0';
}
return 0;
}
/*
* Function Name : IsDirectory
* Description : 判斷是否路徑存在
* Input Param :
* dirname -------> 需要進(jìn)行判斷的路徑名
* Returns : 如果是路徑名,則返回true
* 如果不是路徑名,則返回false
*/
bool IsDirectory(const char *dirname)
{
struct stat sDir;
if (stat(dirname,&sDir) < 0)
{
return false;
}
if (S_IFDIR == (sDir.st_mode & S_IFMT))
{
return true;
}
else
{
return false;
}
}
/*
* Function Name: GetFileSize
* Description: 獲取文件的大小
* Input Param:
* chFileName -------> 文件的名字
* Returns: 無(wú)
* complete: 2002/03/13
*/
long GetFileSize(const char *chFileName)
{
struct stat buf;
if(stat(chFileName, &buf) < 0)
{
return -1;
}
return buf.st_size;
}
/*
* Function Name: Trim
* Description: 去掉字符串左右的空格
* Input Param:
* String -------> 需要去掉空格的字符串
* Returns: 無(wú)
* complete: 2002/03/13
*/
void Trim(char * String)
{
char *Position = String;
/*找到第一個(gè)不是空格的位置*/
while(isspace(*Position))
{
Position++;
}
/*如果為一空串則退出*/
if (*Position == '\0')
{
*String = '\0';
return;
}
/*除去前面的空格*/
while(*Position)
{
*String = *Position;
String++;
Position++;
}
/*除去后面的空格*/
do
{
*String = '\0';
String--;
}while(isspace(*String));
}
/*
* Function Name : GetLine
* Description : 在文件中獲得一行的內(nèi)容
* Input lv_chParameters :
* Line -----------> 這一行的內(nèi)容(字符串)
* File -----------> 打開(kāi)的文件指針
* Returns :得到的字節(jié)數(shù).
* complete :2001/11/09
*/
int GetLine(char *Line,FILE *File)
{
int iByteRead = 0;
for(;;)
{
if (feof(File))
{
*Line='\0';
break;
}
*Line=fgetc(File);
iByteRead ++;
if (*Line == '\n')
{
*Line='\0';
break;
}
Line++;
}
return iByteRead;
}
/*
* Function Name: MakeUpper
* Description: 將字符串全部改為大寫(xiě)
* Input Param:
* pchString -------> 輸入字符串
* Returns: 無(wú)
* complete: 2002/03/13
*/
void MakeUpper(char * pchString)
{
int iLen = strlen(pchString);
for(int i=0; i<iLen; i++)
{
pchString[i] = toupper(pchString[i]);
}
}
/*
* Function Name: MakeLower
* Description: 將字符串全部改為小寫(xiě)
* Input Param:
* pchString -------> 輸入字符串
* Returns: 無(wú)
* complete: 2002/03/13
*/
void MakeLower(char * pchString)
{
int iLen = strlen(pchString);
for(int i=0; i<iLen; i++)
{
pchString[i] = tolower(pchString[i]);
}
}
/*
* Function Name: CheckDate
* Description: 校驗(yàn)日期正確性
* Input Param:
* pchString -------> 輸入字符串(格式y(tǒng)yyymmdd或者yymmdd)
* Returns:
* 成功,返回true
* 失敗,返回false
* complete: 2002/03/13
*/
bool CheckDate(const char *pchString)
{
int num = 0,iLen;
char tmp[LENGTH];
int iYear = 0,iMonth = 0,i;
iLen = strlen(pchString);
if ((iLen != 6) && (iLen != 8))
{
return false;
}
for (i=0; i<iLen; i++)
{
if ((pchString[i] < '0') || (pchString[i] > '9'))
{
return false;
}
}
if (iLen == 6)
{
//校驗(yàn)?zāi)攴? strncpy(tmp, pchString, 2);
tmp[2] = 0;
num = atoi(tmp);
iYear = num + 2000;
if ((num < 0) || (num > 99))
{
return false;
}
//校驗(yàn)月份
strncpy(tmp, pchString + 2, 2);
tmp[2] = 0;
num = atoi(tmp);
iMonth = num;
if ((num < 1) || (num > 12))
{
return false;
}
//校驗(yàn)天
strncpy(tmp, pchString + 4, 2);
tmp[2] = 0;
num = atoi(tmp);
if ((num < 1) || (num > 31))
{
return false;
}
}
else if (iLen == 8)
{
strncpy(tmp, pchString, 4);
tmp[4] = '\0';
num = atoi(tmp);
iYear = num;
num = num - 2000;
if ((num <0) || (num > 99))
{
return false;
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -