标准C处理类似INI配置文件的键值型文档

      Linux下处理配置文档经常是个让人头痛的问题, 因为它不像Win32平台有INI文件处理的API或类对象, 这样, 我们就只能以字符流的方式来分析处理了, 下面的程序段实现了一个典型的处理配置文档的方法...

/* 
      读取并处理键值型文档
*/

char name[20];
byte age;
char sex[8];

int PropertyConfigurator (char *configFilename)
{         
      FILE* fp;        
      int i;         
      char line[256];         
      char* pstr=(char*)NULL;    
             
      if((fp = fopen(configFilename, "r")) == (FILE*)NULL) // configFileName是欲处理的配置文件名        
      {                 
            printf("Can not open configfile/n");                          // 是配置文档中的注释行                       
            return 0;        
      }       
       
      else         
      {                
            while(fgets(line, 256, fp) !=(char*)NULL)                
            {                                         
                  DeteteWhiteSpace(line);                                   // Delete spaces                                           
                  if(line[0] == '#');                                                    // '#' 是配置文档中的注释行
                        continue;
                  strupr(line);                                                          // Convert character to lowercase.

                  if(pstr = strstr(line, "NAME"))                             // Process key-value                        
                  {                                                             
                        pstr = strstr(line, '=');                               
                        DeteteWhiteSpace(++pstr);                               
                        strcpy(name, pstr);                         
                  }
                                                   
                  if(pstr = strstr(line, "AGE"))                                 // Process key-value                       
                  {                                                              
                        pstr = strstr(line, '=');                               
                        DeteteWhiteSpace(++pstr);                                
                        age = atoi(pstr);
                  }

                  else if((pstr = strstr(line, "SEX")))                     // Process key-value
                  {                                                                                      
                        pstr=strstr(line, '=');                                
                        DeteteWhiteSpace(++pstr);                                 
                        strcpy(sex, pstr);                        
                  }                 
            }        
      }
             
      fclose(fp);        
      return 1;
}

/*
      标准C不提供的两个函数
      itoa 将数字字符串转换成数值类型
      strupr 将字符串中的字符转换成大写
*/
char* itoa(long value, char *string)
{                              // Enough for a 128 bit integer
    if (string)
       sprintf(string, "%d", value);
    return string;
}

char* strupr(char *string)
{
   if (string != NULL)
    {
       char *cp;               // traverses string for C locale conversion */
       for (cp = string; *cp; ++cp)
        {
            if ('a' <= *cp && *cp <= 'z')
                *cp += 'A' - 'a';
        }
        return(string);
    }                         //C locale
   return NULL;                      
}

/*
      附加函数
      删除配置文档中的空行及空格    
*/
char* DeteteWhiteSpace(char* str)                // Delete space
{
        char strTemp[256];
        char strTemp2[256];
        unsigned int i,j;
        memset(strTemp, 0, 256);
        strcpy(strTemp2, str);
        j = 0;
        if (str != NULL)
        {
                for(i=0; i<strlen(str); i++)
                {
                        if (strTemp2[i] != ' ')
                        {
                                strTemp[j++] = strTemp2[i];
                        }
                }
        }
        strcpy(str, strTemp);
        return(str);
}

发布了9 篇原创文章 · 获赞 7 · 访问量 4万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览