Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1390580
  • 博文数量: 430
  • 博客积分: 9995
  • 博客等级: 中将
  • 技术积分: 4388
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-24 18:04
文章存档

2013年(1)

2008年(2)

2007年(14)

2006年(413)

分类:

2006-06-13 18:04:57

/*
 * File: inifile.h
 * Read INI File
*/
#ifndef _INIFILE_H_
#define _INIFILE_H_

#include
#include

/*
 * char* GetInitKey(FileName, Section, Key)
 * Return Key=>Value
 * Ex:
 *
 * + [config]
 * + dbhost=localhost
 *
 * strcpy(dbhost,GetInitKey("config.ini", "config", "dbhost"));
*/
char * GetInitKey(char *filename, char *title,char *key)
{
   FILE * fp;
   char tmpLine[1024];
   int rtnval;
   int i = 0;
   int flag = 0;
   char * tmp;
   static char tmpstr[1024];
   
   if ((fp = fopen( filename, "r")) == NULL )
   {
      return "have no such file";
   }
   while (!feof(fp))
   {
      rtnval = fgetc( fp );
      if ( rtnval == EOF )
      {
         break;
      }
      else
      {
         tmpLine[i++] = rtnval;
      }
      if ( rtnval == '\n' )
      {
         tmpLine[--i]=0;
         i = 0;
         tmp = strchr(tmpLine, '=');
         
         if (( tmp != NULL )&&(flag == 1))
         {
            if (strstr(tmpLine,key)!=NULL)
            {
               strcpy ( tmpstr, tmp + 1 );
               fclose ( fp );
               
               return tmpstr;
            }
         }
         else
         {
            strcpy(tmpstr,"[");
            strcat(tmpstr,title);
            strcat(tmpstr,"]");
            if (strcmp(tmpstr,tmpLine)==0)
            {
               flag = 1;
            }
         }
      
      }
   }
   fclose ( fp );
   return "";
}

#endif //_INIFILE_H_
阅读(1133) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~