Chinaunix首页 | 论坛 | 博客
  • 博客访问: 15315364
  • 博文数量: 112
  • 博客积分: 11195
  • 博客等级: 上将
  • 技术积分: 1989
  • 用 户 组: 普通用户
  • 注册时间: 2005-06-20 11:04
文章分类

全部博文(112)

文章存档

2013年(2)

2012年(27)

2011年(6)

2010年(11)

2009年(6)

2007年(7)

2006年(23)

2005年(30)

分类: LINUX

2009-08-21 17:49:16

比较具有收藏价值,用到的时候拿出来直接用了。
配置文件的格式如下:
 

[COMMON]
#common file
port = 100
login = /usr/bin/login

 

函数:

 

int GetProfileString(char * profile, char * AppName, char * KeyName, char * KeyVal )
{
        char appname[20],keyname[20];
        char buf[KEYVALLEN],*c;
        FILE *fp;
        int found=0; /* 1 AppName 2 KeyName */

        if( (fp=fopen( profile,"r" ))==NULL ){
                printf( "openfile [%s] error [%s]\n",
                        profile,strerror(errno) );
                return(-1);
        }
        fseek( fp, 0, SEEK_SET );

        sprintf( appname,"[%s]", AppName );
        memset( keyname, 0, sizeof(keyname) );

        while( !feof(fp) && fgets( buf, KEYVALLEN, fp )!=NULL ){
                if( l_trim( buf )==0 )
                        continue;

                if( found==0 ){
                        if( buf[0]!='[' ) {
                                continue;
                        } else if ( strncmp(buf,appname,strlen(appname))==0 ){
                                found=1;
                                continue;
                        }
                } else if( found==1 ){
                        if( buf[0]=='#' ){
                                continue;
                        } else if ( buf[0]=='[' ) {
                                break;
                        } else {
                                if( (c=(char*)strchr(buf,'='))==NULL )
                                        continue;
                                memset( keyname, 0, sizeof(keyname) );
                                sscanf( buf, "%[^=]", keyname );
                                if( strcmp(keyname, KeyName)==0 ){
                                        sscanf( ++c, "%[^\n]", KeyVal );
                                        found=2;
                                        break;
                                } else {
                                        continue;
                                }
                        }
                }
        }

        fclose( fp );

        if( found==2 )
                return(0);
        else
                return(-1);
}

阅读(102485) | 评论(1) | 转发(8) |
给主人留下些什么吧!~~