www.pudn.com > antispam-addin.rar > configcontrol.cxx


#include "unihead.hxx" 
#include "configcontrol.hxx" 
 
 
ConfigControl::ConfigControl() 
{ 
	errorcode = 0; 
	fNextPos = 0; 
	bLineEnd = false; 
} 
 
ConfigControl::~ConfigControl() 
{ 
} 
 
 
///////////////////////////////////////////////////////////////////////////// 
//函数名   :OpenConfig  
//功能描述 :打开指定的config文件 
//参数     :filename             [in]文件名 
//参数     :stream               [in]数据流  
//返回值   :0  成功 
//			 0  失败 
///////////////////////////////////////////////////////////////////////////// 
int ConfigControl::OpenConfig(const char *filename) 
{ 
   if( (stream_read  = fopen( filename, "r" )) != NULL ) 
      return 1; 
   else 
	  errorcode=CONFIG_FILE_OPEN_ERROR; 
       
   return 0; 
} 
 
 
///////////////////////////////////////////////////////////////////////////// 
//函数名   GetConfigValue  
//功能描述 :读取一条信息 
//参数     :group                [in]指定组 
//参数     :item                 [in]指定条目 
//参数     :value                [in]指定存储信息的地址 
//参数     :valuelen               [in]获得值的长度 
//返回值   :>1  成功返回value的长度 
//			 0   失败或长度为0,即该项配置空缺 
//			 <0  返回value地址空间欠缺的长度 
///////////////////////////////////////////////////////////////////////////// 
int ConfigControl::GetConfigValue(const char *group, const char *item,char *value,int valuelen) 
{ 
	char *uppergroup,*normalgroup; 
	char *upperitem,*normalitem; 
	char upperline[1024],normalline[1024]; 
	fpos_t pos=0; 
	int ret; 
	uppergroup=(char*)malloc((strlen(group)+1)*sizeof(char)); 
	normalgroup=(char*)malloc((strlen(group)+1)*sizeof(char)); 
	upperitem=(char*)malloc((strlen(item)+1)*sizeof(char)); 
	normalitem=(char*)malloc((strlen(item)+1)*sizeof(char)); 
	memset(uppergroup,'\0', (strlen(group)+1)*sizeof(char)); 
	memset(normalgroup,'\0', (strlen(group)+1)*sizeof(char)); 
	memset(upperitem,'\0', (strlen(item)+1)*sizeof(char)); 
	memset(normalitem,'\0', (strlen(item)+1)*sizeof(char)); 
	strcpy(uppergroup,group); 
	strcpy(normalgroup,group); 
	strcpy(upperitem,item); 
	strcpy(normalitem,item); 
	memset(upperline,'\0', 1024); 
	memset(normalline,'\0', 1024); 
	if(strlen(trim(uppergroup))<1){ 
		errorcode=CONFIG_GROUP_EMPTY; 
		free(uppergroup); 
		free(normalgroup); 
		free(upperitem); 
		free(normalitem); 
		return 0;//出错返回 
	} 
	if(strlen(upperitem)<1){ 
		errorcode=CONFIG_ITEM_EMPTY; 
		free(uppergroup); 
		free(normalgroup); 
		free(upperitem); 
		free(normalitem); 
		return 0;//出错返回 
	} 
	UpperStr(uppergroup);//判断时采取大小写不敏感                                                                                                                                                                                                                
	UpperStr(upperitem);// 
	//将文件指针移到文件头    
	if( fsetpos( stream_read, &pos ) == 0 ) 
	{ 
		while(GetLine(normalline)>=0) 
		{ 
			if(strlen(normalline)>0) 
			{ 
				strcpy(upperline,normalline); 
				UpperStr(upperline); 
				if(GetGroup(uppergroup,upperline)==1) 
				{ 
					while((GetLine(normalline) >=0)&&(IsGroup(normalline)==0)) 
					{ 
						if(strlen(normalline)>0) 
						{ 
							strcpy(upperline,normalline); 
							UpperStr(upperline); 
							if(GetItem(upperitem,upperline)==1) 
							{ 
								ret=GetValue(value,normalline,valuelen); 
								free(uppergroup); 
								free(normalgroup); 
								free(upperitem); 
								free(normalitem); 
								return ret; 
							} 
						}//if(strlen(normalline)>=0){ 
					} 
 
					free(uppergroup); 
					free(normalgroup); 
					free(upperitem); 
					free(normalitem); 
					errorcode=CONFIG_ITEM_NOTFOUND; 
					 
					return 0; 
				} 
			}////if(strlen(line)>0){ 
		} 
 
		free(uppergroup); 
		free(normalgroup); 
		free(upperitem); 
		free(normalitem); 
		errorcode=CONFIG_GROUP_NOTFOUND; 
		return 0; 
	} 
	 
	free(uppergroup); 
	free(normalgroup); 
	free(upperitem); 
	free(normalitem); 
	errorcode=CONFIG_FILE_OPERATE_ERROR;//文件操作失败 
 
	return 0;	 
} 
 
 
///////////////////////////////////////////////////////////////////////////// 
//函数名   GetConfigValueEx  
//功能描述 :读取一条信息 
//参数     :filename             [in]指定配置文件名 
//参数     :group                [in]指定组 
//参数     :item                 [in]指定条目 
//参数     :value                [in]指定存储信息的地址 
//参数     :valuelen             [in]获得值的长度 
//返回值   :>1  成功返回value的长度 
//			 0   失败 
//			 <0  返回value地址空间欠缺的长度 
///////////////////////////////////////////////////////////////////////////// 
int ConfigControl::GetConfigValueEx(const char *filename,const char *group, const char *item,char *value,int valuelen){ 
	int ret; 
	if(OpenConfig(filename)==0){ 
		return 0;//打开文件失败 
	} 
	ret=GetConfigValue(group,item,value,valuelen); 
		if(CloseConfig()==1){ 
			return ret;///成功获得值返回 
		}else{ 
			return 0;//关闭文件失败 
		} 
} 
 
 
 
///////////////////////////////////////////////////////////////////////////// 
//函数名           GetConfigValue  
//功能描述 :      关闭config文件 
//返回值   :      1    成功 
//			       0  失败 
///////////////////////////////////////////////////////////////////////////// 
int ConfigControl::CloseConfig() 
{ 
	if(fclose(stream_read)==0) 
	{ 
	return 1; 
	}else{ 
		errorcode=CONFIG_FILE_CLOSE_ERROR; 
		return 0; 
	} 
} 
 
 
 
//获得最后出错标记 
int ConfigControl::GetErrorCode() 
{ 
	return errorcode; 
} 
 
 
 
///////////////////////////////////////////////////////////////////////////// 
//函数名           UperStr  
//参数     :string                [in]原字符串的起始地址 
//功能描述 :      将字符串全部转换为大写,转换后的字符串覆盖原字符串。 
///////////////////////////////////////////////////////////////////////////// 
void ConfigControl::UpperStr(char *string) 
{ 
	int i=0; 
	char a_Z[55]="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
	while(string[i] !='\0') 
	{ 
		int j; 
		for(j=0;j<26;j++) 
		{ 
			if(string[i]==a_Z[j]) 
			{ 
				string[i]=a_Z[j+26]; 
				break; 
			} 
		} 
		i++; 
	} 
} 
 
 
bool ConfigControl::NextLine() 
{ 
	fsetpos(stream_read,&fNextPos); 
 
	if(bLineEnd == true) 
	{ 
		return false; 
	} 
 
	return true; 
} 
 
 
///////////////////////////////////////////////////////////////////////////// 
//函数名           GetLine  
//功能描述 :      顺序读取文本文件中的一行 
//参数     :line                [in]指定存储信息的地址 
//参数     :stream              [in]文件流 
//返回值   :      1    成功 
//			       0    得到空行 
//                 -1   最后一行为空 
///////////////////////////////////////////////////////////////////////////// 
int ConfigControl::GetLine(char *line) 
{ 
	bLineEnd = false;//added by Young Nier 
 
	char singlechar[1]; 
	int i=0,ret=1; 
	memset(line,'\0', LINE_LEN_MAX); 
 
	while(fread(singlechar,sizeof(char),1,stream_read)) 
	{//读到一个字符 
		fNextPos++;//added by Young Nier 
 
		switch(singlechar[0]) 
		{ 
		case '\n': 
			//fNextPos++; 
 
			line[i]='\0'; 
			if(i>0) 
				return 1;///成功返回,获得一行 
			else 
				return 0;//得到空行 
		default: 
			line[i]=singlechar[0]; 
			i++; 
			line[i]='\0'; 
		} 
	} 
 
	///////只有读到最后一行才执行此代码 
	if(strlen(line) > 0) 
		return 1; 
	else 
	{ 
		bLineEnd = true; 
		return -1;///最后一行为空 
	} 
} 
 
 
 
///////////////////////////////////////////////////////////////////////////// 
//函数名           GetGroup  
//功能描述 :      从一行中得到一个组group 
//参数     :group                [in]指定组的存储地址 
//参数     :line                 [in]指定行主字符串 
//返回值   :      1    成功 
//                 -1   该行中不包含指定串 
///////////////////////////////////////////////////////////////////////////// 
int ConfigControl::GetGroup(const char *group,const char *line){ 
////子串匹配,并且子串前面只有一个'['和空格, 
////后面有空格一个']'-空格及'#'才符合条件 
	char *pdest; 
	int  dest,i=0,only=0,remark=0; 
	if((pdest=strstr(line,group))==NULL) 
		return -1;//不包含子串	 
	dest = pdest - line;//目标串出现的偏移量 
	while(i1) return -1;//不允许有多个‘]‘存在 
			break; 
		case '#': 
			remark=1;//有注解 
			break; 
		default: 
			return -1;///包含其他字符,不符合条件 
		} 
		if(remark==1)break;//有注释结束操作 
		pdest++; 
	}///////右边符合条件 
	return 1;///符合条件 
} 
 
 
 
 
///////////////////////////////////////////////////////////////////////////// 
//函数名           GetItem  
//功能描述 :      从一行中得到一个项Item 
//参数     :item                 [in]指定条目的存储起始地址 
//参数     :line                 [in]指定行主串 
//返回值   :      1    成功 
//                 -1   该行中不包含指定串 
///////////////////////////////////////////////////////////////////////////// 
int ConfigControl::GetItem(const char *item,const char *line){ 
////子串匹配,并且子串前面只有空格, 
////后面有空格一个'='-空格及'#'才符合条件 
	char *pdest; 
	int  dest,i=0,only=0,remark=0; 
	if((pdest=strstr(line,item))==NULL) 
		return -1; 
	dest = pdest - line;//目标串出现的偏移量 
	while((i0)){ 
		switch(line[i]){ 
		case ' ': 
		case '\t': 
			break; 
		default: 
			return -1;///包含其他字符,不符合条件 
		} 
		i++; 
	}///////左边符合条件 
	pdest=pdest+strlen(item);//指向目标子串后边的字符 
	while(*pdest !='\0'){///一直查到串结尾处 
		switch(*pdest){ 
		case ' ': 
		case '\t': 
			break; 
		case '=': 
			only=1; 
			break; 
		default: 
			return -1;///等号前包含其他字符,不符合条件 
		} 
		if(only==1)break; 
		pdest++; 
	}///////右边符合条件 
	return 1;///符合条件 
} 
 
 
 
///////////////////////////////////////////////////////////////////////////// 
//函数名           GetValue  
//功能描述 :      从一行中得到等号后边的值 
//参数     :strdest              [in]指定存储信息的地址 
//参数     :line                 [in]指定组 
//返回值   :      >0   成功返回返回串的长度 
//                 0   只能获得一个用空格和制表符组成的字串 
//                 <0  strdest的长度无法装下该值,返回欠缺长度 
///////////////////////////////////////////////////////////////////////////// 
int ConfigControl::GetValue( char *strdest,const char *line,int valuelen){ 
////子串和等号前边的忽略, 
////后面的#注释符号前边去掉两端空格的才有所要的值 
	char *pdest; 
	int  dest,i=0; 
	int ret=0;//默认返回空值 
	char *tempdest;//用于临时存放获得值 
	pdest=strstr(line,"=");//去掉等号前边的符号 
	tempdest=(char*)malloc((strlen(pdest)+1)*sizeof(char));//开辟一个足够大的缓冲区 
	memset(tempdest,'\0', (strlen(pdest)+1)*sizeof(char)); 
	pdest++;//指向下一个字符 
	dest = pdest - line;//等号后字符的偏移量 
	while(*pdest !='\0'){///一直查到串结尾处 
		switch(*pdest){ 
		case ' ': 
		case '\t': 
			if(i>0){///如果前边有字符则纪录该空格 
				tempdest[i]=' ';	 
				i++; 
			} 
			break;//忽略 
		case '#': 
			righttrim(tempdest); 
			strcpy(strdest,tempdest); 
			ret=strlen(strdest); 
			if(ret==0) 
				errorcode=CONFIG_VALUE_EMPTY; 
			return ret; 
		default: 
			//找到一个值 
			tempdest[i]=*pdest; 
			i++; 
		} 
		pdest++; 
	} 
	/////////////已经获得一个前边没有空格的字串 
	if(i==0){ 
		////i等于0,说明该行为空值 
		errorcode=CONFIG_VALUE_EMPTY; 
		return 0;//  
	} 
	righttrim(tempdest); 
	strcpy(strdest,tempdest); 
	ret=strlen(strdest); 
	if(ret==0) 
		errorcode=CONFIG_VALUE_EMPTY; 
	return ret; 
} 
 
 
 
///////////////////////////////////////////////////////////////////////////// 
//函数名           IsGroup  
//功能描述 :      判断一行是不是一个Group 
//参数     :line                 [in]指定组 
//返回值   :      1   可能是一个 
//                 0   不可能是 
///////////////////////////////////////////////////////////////////////////// 
int ConfigControl::IsGroup(char *line){ 
	 char *ptemp; 
     ptemp=line; 
	 while((*ptemp==' ')||(*ptemp=='\t')) 
		 ptemp++; 
	 if(*ptemp=='[') 
		 return 1; 
	 else 
		 return 0; 
} 
 
 
char* ConfigControl::righttrim( char *szSource )////清空字符串右边的空格 
{ 
    char *pszEOS; 
 
    // Set pointer to end of string to point to the character just 
    //  before the 0 at the end of the string. 
    pszEOS = szSource + strlen( szSource ) - 1; 
 
    while( pszEOS >= szSource && *pszEOS == ' ' ) 
        *pszEOS-- = '\0'; 
 
    return szSource; 
} 
 
 
 
char* ConfigControl::lefttrim( char *szSource )////清空字符串右边的空格 
{ 
    char *pszEOS,*psztemp; 
	pszEOS=(char*)malloc((strlen(szSource)+1)*sizeof(char)); 
	psztemp=szSource; 
	while(*psztemp==' ') 
        psztemp++; 
	strcpy(pszEOS,psztemp); 
	strcpy(szSource,psztemp); 
	free(pszEOS);     
    return szSource; 
} 
 
 
char* ConfigControl::trim( char *szSource )////清空字符串两边的空格 
{ 
    return lefttrim(righttrim(szSource)); 
}