zoukankan      html  css  js  c++  java
  • ini文件读取

    #define   SUCCESS                0
    #define   NOT_EXIST_FILE        1
    #define   NOT_OPEN_FILE        2
    #define   NOT_EXIST_SECTION   3
    #define   NOT_EXIST_ITEM       4
    #define   NOT_ENOUGH_MEMORY 5       /*item_value需要比
                                           max_item_value_length还要多的值
    */
    #define   NOT_EXIST_VALUE      6
    #define   NOT_MATCH_BRACKET  7
    #define   NOT_EXIST_EQUAL      8

    #define   SPACE         0x20   
    #define   LEFT_SQUARE_BRACKET '['
    #define   RIGHT_SQUARE_BRACKET ']'
    #define   CHINESE_LEFT_SQUARE_BRACKET  '【' 
    #define   CHINESE_RIGHT_SQUARE_BRACKET '】'
    #define   EQUAL_SIGN   '='
    #define   POSTIL        ';'
    #define   MAX_BUFFER_LENGTH    (255+1)
    #define   MAX_ITEM_LENGTH      (128)

    #define   INI  (".ini") 

    #define   NOT_FIND_SECTION 0
    #define   FIND_SECTION      1

    #ifdef _cplusplus
    #define    FALSE        false
    #define   TRUE         true
    #else
     
    #define    FALSE      0
     
    #define   TRUE        1
    #endif 

    #ifndef _cplusplus
     
    #define inline    /*虽然编译器不支持,可以在C中支持inline用法不出现语法错误*/
    #endif



    int GetValue(const char *file_name, const char *section_name, const char *item_name, char *item_value,const unsigned int max_item_value_length);
    /*
    [in] file_name    :提供ini的文件名,可以省略后缀名"ini"
       section_name  :提供ini中的节名,可以省略"[]"
       item_name   :提供ini中的项目名
       max_item_value_length: 提示item_value的最大值
    [out] item_value  :获取ini中的项目的值

    返回值: 
      no_file:        NOT_EXIST_FILE
      not_open_file:  NOT_OPEN_FILE
      no_section:      NOT_EXIST_SECTION
      no_item:        NOT_EXIST_ITEM
      no_more_memory NOT_ENOUGH_MEMORY  // item_value需要比max_item_value_length 
                                        // 还要多的值 
      no_value:       NOT_EXIST_VALUE
      []no_match:     NOT_MATCH_BRACKET
      no_equal:       NOT_EXIST_EQUAL
    */

    {
        FILE 
    *fp;                                                
        
    char  real_file_name[MAX_BUFFER_LENGTH] = {0};    
        
    char  line_buffer[MAX_BUFFER_LENGTH]={0};
        
    bool  section_exist = FALSE;
        
    bool  item_exist   = FALSE;
        
    bool  enough_memory = FALSE;
        
    bool  equal_exist  = FALSE;
           unsigned 
    int equal_position = 0;
                                                                 
       
    /* 文件名设置 */                                                    
       memset( real_file_name, 
    0sizeof(real_file_name) ); 
       strcpy( real_file_name, file_name ); 
        
    /*判断是否有后缀*/                                                     
       
    if(0 != strcmp(&real_file_name[strlen(real_file_name)-4], INI) )
       
    {
             strcat(real_file_name, INI );   
    /* 添加后缀 */              
       }
                                           
       
    /* 打开ini文件 */
       fp 
    = fopen(real_file_name,"r");         
       
    if( NULL == fp)
         
    return NOT_OPEN_FILE;   
                                                                    
      
    while!feof(fp) )
      
    {
         fgets(fp, line_buffer);     
    /* 获取一行的值 */
         DeleteBeginningAndEndSpace(line_buffer); 
    /* 删除首尾空 */
         
         
    if( POSTIL == line_buffer[0] )  /* 注释 */
         
    {
             
    continue;
         }


         
    /* 如果节没有找到,判断是否为节,是,判断是否为所需节 */
         
    if( (section_exist == FALSE) && IsSection(line_buffer) && 
                FindSection(line_buffer,section_name) )    
         

           section_exist 
    = TRUE;
         }


          
    /* 
          找到了所需节,那么进行中内容查找,如果在下一个节之前还没有找到item
          那么表明没有找到item值
          
    */

        
    if(section_exist ) /*节已经找到,那么查找节下面的item */
        
    {
           equal_position 
    = ItemEqualPosition( line_buffer ); //
           if( (equal_position!=0&& /*表明存在等于号*/
              FindItem( line_buffer, equal_position,item_name ) ) 
           
    /* 找到所需要的item了 */
           
    {
              item_exist 
    = TRUE;
              
    if( strlen(line_buffer)-equal_position+1 > max_item_value_length )
              
    {
                enough_memory 
    = FALSE;
                strncpy(value,line_buffer[equal_position
    +1],
                        max_item_value_length);
              }
    //if
              else
              

                 enough_memory 
    = TRUE;
                 strncpy(value,line_buffer[equal_poistion
    +1],
                          strlen(line_buffer)
    -equal_position+1);
              }
    //else
             if( equal_position != 0)
                equal_position 
    = TRUE;
           }
    //if
        }

      }

     
      fclose(fp);

      
    if!section_exist )
         
    return NOT_EXIST_SECTION;
         
      
    if( section_exist && (!item_exist) )
         
    return NOT_EXIST_ITEM;
         
      
    if( item_exit && (!enough_memory) )
         
    return NOT_ENOUGH_MEMORY;
         
      
    if!equal_exist)
       
    return NOT_EXIST_EQUAL;
                                                                                                                                 
    }

    bool FindItem(const char *string, unsigned int equal_position,const char *item_name )
    /*
     如果找到了所需的item,那么返回TRUE,否则返回FALSE
    */

    {
      
    char item[MAX_ITEM_LENGTH] = {0};
      equal_position 
    = ( equal_position>MAX_ITEM_LENGTH ?
    MAX_ITEM_LENGTH : equal_position);
      strncpy(item, 
    string, equal_position);
     item[equal_poistion]
    ='
     
    /*
    *
    * Copyright (c) 2011 Ubunoon.
    * All rights reserved.
    *
    * email: netubu#gmail.com replace '#' to '@'
    * http://www.cnblogs.com/ubunoon
    * 欢迎来邮件定制各类验证码识别,条码识别,图像处理等软件
    * 推荐不错的珍珠饰品,欢迎订购 * 宜臣珍珠(淡水好珍珠) */
  • 相关阅读:
    js命名空间笔记
    css3兼容性问题归纳
    flexbox-CSS3弹性盒模型flexbox完整版教程
    JavaScript 预解析
    消除页面上的链接虚线框
    图片压缩之 PNG
    常见的前端优化技巧有哪些
    for-of循环和for-in循环的区别
    函数式编程初探
    js中同步与异步处理方法
  • 原文地址:https://www.cnblogs.com/ubunoon/p/2058925.html
Copyright © 2011-2022 走看看