📄 basefun.cc
字号:
/*
* Copyright 2002 LinkAge Co.,Ltd. Inc. All Rights Reversed
*/
/*********************************************************************************/
/*** Name : BASEFUN.CC ***/
/*** ***/
/*** Description : 基本功能函数 ***/
/*** ***/
/*** 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
//前一个range1
while (*pSrc == ' ' || *pSrc == '\t')
++pSrc;
while ( isalnum(*pSrc) )
*Range1++ = *pSrc++;
*Range1 = 0;
while (*pSrc == ' ' || *pSrc == '\t')
++pSrc;
if (*pSrc != ':')
return false;
else
++pSrc;
//后一个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: 比较两个字符串是否匹配(相等)
* Input Param:
* lv_chCompareString -------> 被比较的字符串
* lv_chCompareMod -------> 匹配的字符串,支持*,?,[]等通配符
* Returns:
* 如果两个字符串匹配,返回true
* 如果两个字符串不匹配,返回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; //跳过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)
{
//跳过整个模式以及相应长度的字符串
lv_chCompareMod = p + 1;
lv_chCompareString += iLen;
break;
}
else
{
return false;
}
} //测试下一个
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: 获得当前系统日期时间
* Input Param:
* CurrentDateTime -------> 当前系统日期(格式14位YYYYMMDDHHMMSS)
* Returns: 无
* 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: 获得当前系统日期时间
* Input Param:
* CurrentDateTime -------> 当前系统日期(格式12位YYMMDDHHMMSS)
* Returns: 无
* 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: 获得当前系统日期
* Input Param:
* CurrentDate -------> 当前系统日期(格式yyyymmdd)
* Returns: 无
* 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: 获得当前系统时间
* Input Param:
* CurrentTime -------> 当前系统时间(格式yymmdd)
* Returns: 无
* 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: 得到本机的IP地址
* Input Param:
* LocalIp -------> 得到的本机IP地址
* Returns: 无
* complete: 2002/03/13
*/
bool GetLocalIp(char *LocalIp)
{
char chHostName[LENGTH];
int i;
struct in_addr addr;
/*取得机器名称*/
if(gethostname(chHostName,sizeof(chHostName)) !=-1)
{
/*取得给定机器的信息*/
struct hostent* phost=gethostbyname(chHostName);
/*到每一个地址结尾*/
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: 如果路径存在,则补齐,并返回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 -------> 需要进行判断的路径名
* 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: 无
* 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: 无
* complete: 2002/03/13
*/
void Trim(char * String)
{
char *Position = String;
/*找到第一个不是空格的位置*/
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 : 在文件中获得一行的内容
* Input lv_chParameters :
* Line -----------> 这一行的内容(字符串)
* File -----------> 打开的文件指针
* Returns :得到的字节数.
* 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: 将字符串全部改为大写
* Input Param:
* pchString -------> 输入字符串
* Returns: 无
* 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: 将字符串全部改为小写
* Input Param:
* pchString -------> 输入字符串
* Returns: 无
* 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: 校验日期正确性
* Input Param:
* pchString -------> 输入字符串(格式yyyymmdd或者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)
{
//校验年份
strncpy(tmp, pchString, 2);
tmp[2] = 0;
num = atoi(tmp);
iYear = num + 2000;
if ((num < 0) || (num > 99))
{
return false;
}
//校验月份
strncpy(tmp, pchString + 2, 2);
tmp[2] = 0;
num = atoi(tmp);
iMonth = num;
if ((num < 1) || (num > 12))
{
return false;
}
//校验天
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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -