|
一个通用的linux系统中C语言版读取配置文件的函数
[复制链接]
一个通用的linux系统中C语言版读取配置文件的函数。 [size=1em][color=#ffffff !important] ?
[backcolor=rgb(248, 248, 248) !important]1
2
[backcolor=rgb(248, 248, 248) !important]3
4
[backcolor=rgb(248, 248, 248) !important]5
6
[backcolor=rgb(248, 248, 248) !important]7
8
[backcolor=rgb(248, 248, 248) !important]9
10
[backcolor=rgb(248, 248, 248) !important]11
12
[backcolor=rgb(248, 248, 248) !important]13
14
[backcolor=rgb(248, 248, 248) !important]15
16
[backcolor=rgb(248, 248, 248) !important]17
18
[backcolor=rgb(248, 248, 248) !important]19
20
[backcolor=rgb(248, 248, 248) !important]21
22
[backcolor=rgb(248, 248, 248) !important]23
24
[backcolor=rgb(248, 248, 248) !important]25
26
[backcolor=rgb(248, 248, 248) !important]27
28
[backcolor=rgb(248, 248, 248) !important]29
30
[backcolor=rgb(248, 248, 248) !important]31
32
[backcolor=rgb(248, 248, 248) !important]33
34
[backcolor=rgb(248, 248, 248) !important]35
36
[backcolor=rgb(248, 248, 248) !important]37
38
[backcolor=rgb(248, 248, 248) !important]39
40
[backcolor=rgb(248, 248, 248) !important]41
42
[backcolor=rgb(248, 248, 248) !important]43
44
[backcolor=rgb(248, 248, 248) !important]45
46
[backcolor=rgb(248, 248, 248) !important]47
48
[backcolor=rgb(248, 248, 248) !important]49
50
[backcolor=rgb(248, 248, 248) !important]51
52
[backcolor=rgb(248, 248, 248) !important]53
54
[backcolor=rgb(248, 248, 248) !important]55
56
[backcolor=rgb(248, 248, 248) !important]57
58
[backcolor=rgb(248, 248, 248) !important]59
60
[backcolor=rgb(248, 248, 248) !important]61
62
[backcolor=rgb(248, 248, 248) !important]63
64
[backcolor=rgb(248, 248, 248) !important]65
66
[backcolor=rgb(248, 248, 248) !important]67
68
[backcolor=rgb(248, 248, 248) !important]69
70
[backcolor=rgb(248, 248, 248) !important]71
72
[backcolor=rgb(248, 248, 248) !important]73
74
[backcolor=rgb(248, 248, 248) !important]75
76
[backcolor=rgb(248, 248, 248) !important]77
78
[backcolor=rgb(248, 248, 248) !important]79
80
[backcolor=rgb(248, 248, 248) !important]81
82
[backcolor=rgb(248, 248, 248) !important]83
84
[backcolor=rgb(248, 248, 248) !important]85
86
[backcolor=rgb(248, 248, 248) !important]87
88
[backcolor=rgb(248, 248, 248) !important]89
90
[backcolor=rgb(248, 248, 248) !important]91
92
[backcolor=rgb(248, 248, 248) !important]93
94
[backcolor=rgb(248, 248, 248) !important]95
96
[backcolor=rgb(248, 248, 248) !important]97
98
[backcolor=rgb(248, 248, 248) !important]99
100
[backcolor=rgb(248, 248, 248) !important]101
102
[backcolor=rgb(248, 248, 248) !important]103
104
[backcolor=rgb(248, 248, 248) !important]105
106
[backcolor=rgb(248, 248, 248) !important]107
108
[backcolor=rgb(248, 248, 248) !important]109
110
[backcolor=rgb(248, 248, 248) !important]111
112
[backcolor=rgb(248, 248, 248) !important]113
114
[backcolor=rgb(248, 248, 248) !important]115
116
[backcolor=rgb(248, 248, 248) !important]117
118
[backcolor=rgb(248, 248, 248) !important]119
120
[backcolor=rgb(248, 248, 248) !important]121
122
[backcolor=rgb(248, 248, 248) !important]123
| [backcolor=rgb(248, 248, 248) !important]#include
#include
[backcolor=rgb(248, 248, 248) !important]#include
#include
[backcolor=rgb(248, 248, 248) !important]#include
[backcolor=rgb(248, 248, 248) !important]#define KEYVALLEN 100
[backcolor=rgb(248, 248, 248) !important]/* 删除左边的空格 */
char * l_trim(char * szOutput, const char *szInput)
[backcolor=rgb(248, 248, 248) !important]{
assert(szInput != NULL);
[backcolor=rgb(248, 248, 248) !important] assert(szOutput != NULL);
assert(szOutput != szInput);
[backcolor=rgb(248, 248, 248) !important] for (NULL; *szInput != '\0' && isspace(*szInput); ++szInput){
;
[backcolor=rgb(248, 248, 248) !important] }
return strcpy(szOutput, szInput);
[backcolor=rgb(248, 248, 248) !important]}
[backcolor=rgb(248, 248, 248) !important]/* 删除右边的空格 */
char *r_trim(char *szOutput, const char *szInput)
[backcolor=rgb(248, 248, 248) !important]{
char *p = NULL;
[backcolor=rgb(248, 248, 248) !important] assert(szInput != NULL);
assert(szOutput != NULL);
[backcolor=rgb(248, 248, 248) !important] assert(szOutput != szInput);
strcpy(szOutput, szInput);
[backcolor=rgb(248, 248, 248) !important] for(p = szOutput + strlen(szOutput) - 1; p >= szOutput && isspace(*p); --p){
;
[backcolor=rgb(248, 248, 248) !important] }
*(++p) = '\0';
[backcolor=rgb(248, 248, 248) !important] return szOutput;
}
/* 删除两边的空格 */
[backcolor=rgb(248, 248, 248) !important]char * a_trim(char * szOutput, const char * szInput)
{
[backcolor=rgb(248, 248, 248) !important] char *p = NULL;
assert(szInput != NULL);
[backcolor=rgb(248, 248, 248) !important] assert(szOutput != NULL);
l_trim(szOutput, szInput);
[backcolor=rgb(248, 248, 248) !important] for (p = szOutput + strlen(szOutput) - 1;p >= szOutput && isspace(*p); --p){
;
[backcolor=rgb(248, 248, 248) !important] }
*(++p) = '\0';
[backcolor=rgb(248, 248, 248) !important] return szOutput;
}
[backcolor=rgb(248, 248, 248) !important]int GetProfileString(char *profile, char *AppName, char *KeyName, char *KeyVal )
{
[backcolor=rgb(248, 248, 248) !important] char appname[32],keyname[32];
char *buf,*c;
[backcolor=rgb(248, 248, 248) !important] char buf_i[KEYVALLEN], buf_o[KEYVALLEN];
FILE *fp;
[backcolor=rgb(248, 248, 248) !important] int found=0; /* 1 AppName 2 KeyName */
if( (fp=fopen( profile,"r" ))==NULL ){
[backcolor=rgb(248, 248, 248) !important] printf( "openfile [%s] error [%s]\n",profile,strerror(errno) );
return(-1);
[backcolor=rgb(248, 248, 248) !important] }
fseek( fp, 0, SEEK_SET );
[backcolor=rgb(248, 248, 248) !important] memset( appname, 0, sizeof(appname) );
sprintf( appname,"[%s]", AppName );
while( !feof(fp) && fgets( buf_i, KEYVALLEN, fp )!=NULL ){
[backcolor=rgb(248, 248, 248) !important] l_trim(buf_o, buf_i);
if( strlen(buf_o) <= 0 )
[backcolor=rgb(248, 248, 248) !important] continue;
buf = NULL;
[backcolor=rgb(248, 248, 248) !important] buf = buf_o;
[backcolor=rgb(248, 248, 248) !important] if( found == 0 ){
if( buf[0] != '[' ) {
[backcolor=rgb(248, 248, 248) !important] continue;
} else if ( strncmp(buf,appname,strlen(appname))==0 ){
[backcolor=rgb(248, 248, 248) !important] found = 1;
continue;
[backcolor=rgb(248, 248, 248) !important] }
[backcolor=rgb(248, 248, 248) !important] } else if( found == 1 ){
if( buf[0] == '#' ){
[backcolor=rgb(248, 248, 248) !important] continue;
} else if ( buf[0] == '[' ) {
[backcolor=rgb(248, 248, 248) !important] break;
} else {
[backcolor=rgb(248, 248, 248) !important] if( (c = (char*)strchr(buf, '=')) == NULL )
continue;
[backcolor=rgb(248, 248, 248) !important] memset( keyname, 0, sizeof(keyname) );
[backcolor=rgb(248, 248, 248) !important] sscanf( buf, "%[^=|^ |^\t]", keyname );
if( strcmp(keyname, KeyName) == 0 ){
[backcolor=rgb(248, 248, 248) !important] sscanf( ++c, "%[^\n]", KeyVal );
char *KeyVal_o = (char *)malloc(strlen(KeyVal) + 1);
[backcolor=rgb(248, 248, 248) !important] if(KeyVal_o != NULL){
memset(KeyVal_o, 0, sizeof(KeyVal_o));
[backcolor=rgb(248, 248, 248) !important] a_trim(KeyVal_o, KeyVal);
if(KeyVal_o && strlen(KeyVal_o) > 0)
[backcolor=rgb(248, 248, 248) !important] strcpy(KeyVal, KeyVal_o);
free(KeyVal_o);
[backcolor=rgb(248, 248, 248) !important] KeyVal_o = NULL;
}
[backcolor=rgb(248, 248, 248) !important] found = 2;
break;
[backcolor=rgb(248, 248, 248) !important] } else {
continue;
[backcolor=rgb(248, 248, 248) !important] }
}
[backcolor=rgb(248, 248, 248) !important] }
}
[backcolor=rgb(248, 248, 248) !important] fclose( fp );
if( found == 2 )
[backcolor=rgb(248, 248, 248) !important] return(0);
else
[backcolor=rgb(248, 248, 248) !important] return(-1);
}
void main()
[backcolor=rgb(248, 248, 248) !important]{
char ip[16];
[backcolor=rgb(248, 248, 248) !important] GetProfileString("./cls.conf", "cls_server", "ip", ip);
printf("%s\n",ip);
[backcolor=rgb(248, 248, 248) !important]}
|
配置文件例子如下: [size=1em][color=#ffffff !important] ?
[backcolor=rgb(248, 248, 248) !important]1
2
[backcolor=rgb(248, 248, 248) !important]3
| [backcolor=rgb(248, 248, 248) !important][cls_server]
#配置文件等号左右可以有空格也可以没有
[backcolor=rgb(248, 248, 248) !important]ip=192.16.31.2
|
|
|