⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rprofile.c

📁 linux下的配置文件读取原代码,简单易用
💻 C
字号:
/* Magic C++ utilities programme (Mostly) portable public-domain implementation -- Copyright(C) 2003 Magicunix Infomation Technology Limited This file is part of magicd. magicd is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. For details, see the Magic C++ World-Wide-Web page, `http://www.magicunix.com', or send a mail to the Magic C++ developers . */  //#include <linux/config.h> #include "RProfile.h" /*subtract value in shared memory if it's 0return -1 */  /*get a int value in config section - name of section key - name of key value - value of key */ int util_getinfo_int(char *section , char *key , int *nvalue , char * szFileName) { 	char szvalue[100]={0}; 	//memset( szvalue , 0 , sizeof( szvalue)); 	if( util_getinfo_str( section , key , szvalue , szFileName) == -1 ) 	{ 		*nvalue = 0; return -1; 	} 	*nvalue = atoi( szvalue ); 	//printf("nvalue: %d\n",&nvalue)	//if( errno == EINVAL || errno == ERANGE) 	//	return -1; 	return 0; } /*get a section in configuation file*/ int util_getinfo_str(char *section ,char *key , char *value , char * szFileName) { 	FILE *fd; 	char buf[1024]; 	char cfile[100]; 	char line[100]; 	char thekey[100]; 	char thesection[100]; 	char thevalue[100]; 	char cursection[100]; 	char *env; 	memset( thekey , 0 , sizeof( thekey )); 	memset( thesection , 0 , sizeof( thesection )); 	memset( cursection , 0 , sizeof( cursection )); 	memset( thevalue , 0 , sizeof( thevalue )); 	memset(cfile,0,sizeof(cfile)); 	memset(buf, 0 , sizeof( buf )); 	memset(line , 0 , sizeof( line )); /* if( szInstallDir[ 0 ] != '/' ) { fprintf(stderr,"Can not get install dir\n"); return -1; } sprintf( cfile , "%s/etc/magic.ini" , szInstallDir); */ 	if(( fd = fopen(szFileName,"r")) == NULL) 	{ 		printf("open profile error!\n" ); 		return -1; 	} 		while( util_getline( fd , 1023 , line) != -1) 	{ /*it is a section*/ 		memset( thekey , 0 ,sizeof(thekey)); 		memset( thevalue , 0 ,sizeof(thevalue)); 		memset( thesection , 0 , sizeof(thesection )); 		util_trim( line ); 		if( line[0] == '#' ) 			continue; 		if( util_getsection( line , thesection ) == 0 ) 		{ 			memset( cursection , 0 , sizeof( cursection ) ); 			strcpy( cursection , thesection ); 			continue; 		} /*it is a key*/ 		else if( util_getkeyvalue( line , thekey , thevalue ) == 0 ) 		{ /*it's the same with query value*/ 			if( strcasecmp( cursection , section ) == 0 && strcasecmp( thekey , key) == 0 ) 			{ 				strcpy( value , thevalue ); 				fclose(fd ); 				return 0; 			} 		} 		else if ( strlen( line ) != 0 ) 		{ 			printf("an unrecognise format:%s\n" , line ); 			fclose(fd ); 			return -1; 		} 		memset( line , 0 ,sizeof(line)); 	} 	fclose(fd ); 	return -1; } /*pick out section name */ /*return -1 for error format*/ int util_getsection(char *line , char *section) { 	char *p1 , 	*p2; 	p1 = strchr( line , '[' ); 	p2 = strchr( line , ']' ); 	if( p1 == 0 || p2 == 0 ) 	{ return -1; } 	else 	{ 		memset( section , 0 , sizeof( section )); 		strncpy( section , p1 + 1 , p2 - p1 - 1); 		util_trim( section ); 		return 0; 	} } /*pick out section value */ /*return -1 for error format*/ int util_getkeyvalue(char *line ,char *thekey ,char *thevalue ) { 	char *p1; 	p1 = strchr( line , '=' ); 	if( p1 == 0 ) 		return -1; 	strncpy( thekey , line , p1 - line ); 	strcpy( thevalue , p1 + 1 );	 util_trim( thekey ); 	util_trim( thevalue ); /*it's empty value*/ 	if( strlen( thekey ) == 0 || strlen( thevalue ) == 0 ) 		return -1; 	return 0; } /*pick out the key of line string(with out delete blank space) */ /*return -1 for error format*/ /*return 0 for success*/ int util_getkeyvalue_notrim(char *line ,char *thekey ,char *thevalue ) { 	char *p1; p1 = strchr( line , '=' ); if( p1 == 0 ) return -1; strncpy( thekey , line , p1 - line ); strcpy( thevalue , p1 + 1 ); /*empty value*/ if( strlen( thekey ) == 0 || strlen( thevalue ) == 0 ) return -1; return 0; } /*get one line in file /*done not check file format return 1 for end of file */ int util_getline( FILE *fd , int nLineLen , char *line) { 	int c; 	int i = 0; 	memset( line , 0 , sizeof( line) ); 	c = fgetc( fd ); 	if( c == EOF ) 	return -1; 	while( c == '\r' || c == '\n' ) 	{ 		c = fgetc( fd ); 		if( c == EOF ) 			return -1; 	} 	while( c != '\r' && c != '\n' && c != EOF && i < nLineLen ) 	{ 		line[ i++ ] = c; 		c = fgetc(fd ); 	} 	line[ i ] = '\0'; 	return i; } void util_trim(char *buf ) { 	char tmp[1024]; 	char *pbegin = buf; 	int nlen = strlen(buf); 	char *pend = buf + nlen - 1; 	memset( tmp , 0 , sizeof( tmp )); 	while( pbegin < buf + nlen && strchr( "\r\n\t " , *pbegin))	 pbegin ++; 	while( pend >= buf && strchr( "\r\n\t " , *pend))	 pend --; 	if( pend >= pbegin ) 	strncpy( tmp , pbegin , pend - pbegin + 1); 	memset( buf , 0 , sizeof( buf )); 	strcpy( buf , tmp ); } /*get last modified time of a file*/ 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -