linux 读取Ini文件类

Linux下读取Ini文件类

最近项目上有需要读取Ini文件 所谓Ini文件也就是文本文档 并且以
//注释1
/*注释2
[Section]
Key1=aaa
Key2=bbb
这种形式存在的文档
自己编写了一个类  比较使用 简单 可以跨平台读写INI文件
头文件Ini.h
  1. #include <map>  
  2. #include <string>  
  3. using namespace std;  
  4.   
  5. #define CONFIGLEN           256   
  6.   
  7. enum INI_RES  
  8. {  
  9.     INI_SUCCESS,            //成功  
  10.     INI_ERROR,              //普通错误  
  11.     INI_OPENFILE_ERROR,     //打开文件失败  
  12.     INI_NO_ATTR            //无对应的键值  
  13. };  
  14.   
  15. //              子键索引    子键值   
  16. typedef map<std::string,std::string> KEYMAP;  
  17. //              主键索引 主键值    
  18. typedef map<std::string,KEYMAP> MAINKEYMAP;  
  19. // config 文件的基本操作类  
  20.   
  21. class CIni   
  22. {  
  23. public:  
  24.     // 构造函数  
  25.     CIni();  
  26.   
  27.     // 析够函数  
  28.     virtual ~CIni();  
  29. public:  
  30.     //获取整形的键值  
  31.     int  GetInt(const char* mAttr, const char* cAttr );  
  32.     //获取键值的字符串  
  33.     char *GetStr(const char* mAttr, const char* cAttr );  
  34.     // 打开config 文件  
  35.     INI_RES OpenFile(const char* pathName, const char* type);  
  36.     // 关闭config 文件  
  37.     INI_RES CloseFile();  
  38. protected:  
  39.     // 读取config文件  
  40.     INI_RES GetKey(const char* mAttr, const char* cAttr, char* value);  
  41. protected:  
  42.     // 被打开的文件局柄  
  43.     FILE* m_fp;  
  44.     char  m_szKey[ CONFIGLEN ];  
  45.     MAINKEYMAP m_Map;  
  46. };  
  47.   
  48. #endif // FILLE_H  
#include <map>




#include <string> using namespace std; #define CONFIGLEN 256 enum INI_RES { INI_SUCCESS, //成功 INI_ERROR, //普通错误 INI_OPENFILE_ERROR, //打开文件失败 INI_NO_ATTR //无对应的键值 }; // 子键索引 子键值 typedef map<std::string,std::string> KEYMAP; // 主键索引 主键值 typedef map<std::string,KEYMAP> MAINKEYMAP; // config 文件的基本操作类 class CIni { public: // 构造函数 CIni(); // 析够函数 virtual ~CIni(); public: //获取整形的键值 int GetInt(const char* mAttr, const char* cAttr ); //获取键值的字符串 char *GetStr(const char* mAttr, const char* cAttr ); // 打开config 文件 INI_RES OpenFile(const char* pathName, const char* type); // 关闭config 文件 INI_RES CloseFile(); protected: // 读取config文件 INI_RES GetKey(const char* mAttr, const char* cAttr, char* value); protected: // 被打开的文件局柄 FILE* m_fp; char m_szKey[ CONFIGLEN ]; MAINKEYMAP m_Map; }; #endif // FILLE_H

实现文件Ini.cpp
  1. #include “Ini.h”  
  2. /****************************************************************************** 
  3. * 功  能:构造函数 
  4. * 参  数:无 
  5. * 返回值:无 
  6. * 备  注: 
  7. ******************************************************************************/  
  8. CIni::CIni( )  
  9. {  
  10.  memset( m_szKey,0,sizeof(m_szKey) );  
  11.  m_fp = NULL;  
  12. }  
  13.   
  14. /****************************************************************************** 
  15. * 功  能:析构函数 
  16. * 参  数:无 
  17. * 返回值:无 
  18. * 备  注: 
  19. ******************************************************************************/  
  20.   
  21. CIni::~CIni()  
  22. {  
  23.  m_Map.clear();  
  24. }  
  25.   
  26. /****************************************************************************** 
  27. * 功  能:打开文件函数 
  28. * 参  数:无 
  29. * 返回值: 
  30. * 备  注: 
  31. ******************************************************************************/  
  32. INI_RES CIni::OpenFile(const char* pathName, const char* type)  
  33. {  
  34.  string szLine,szMainKey,szLastMainKey,szSubKey;  
  35.  char strLine[ CONFIGLEN ] = { 0 };  
  36.  KEYMAP mLastMap;  
  37.  int  nIndexPos = -1;  
  38.  int  nLeftPos = -1;  
  39.  int  nRightPos = -1;  
  40.     m_fp = fopen(pathName, type);  
  41.       
  42.     if (m_fp == NULL)  
  43.     {  
  44.   printf( ”open inifile %s error!\n”,pathName );  
  45.         return INI_OPENFILE_ERROR;  
  46.     }  
  47.   
  48.  m_Map.clear();  
  49.   
  50.  while( fgets( strLine, CONFIGLEN,m_fp) )  
  51.  {    
  52.   szLine.assign( strLine );  
  53.   //删除字符串中的非必要字符  
  54.   nLeftPos = szLine.find(”\n” );  
  55.   if( string::npos != nLeftPos )  
  56.   {  
  57.    szLine.erase( nLeftPos,1 );  
  58.   }  
  59.   nLeftPos = szLine.find(”\r” );  
  60.   if( string::npos != nLeftPos )  
  61.   {  
  62.    szLine.erase( nLeftPos,1 );  
  63.   }     
  64.   //判断是否是主键  
  65.   nLeftPos = szLine.find(”[“);  
  66.   nRightPos = szLine.find(”]”);  
  67.   if(  nLeftPos != string::npos && nRightPos != string::npos )  
  68.   {  
  69.    szLine.erase( nLeftPos,1 );  
  70.    nRightPos–;  
  71.    szLine.erase( nRightPos,1 );  
  72.    m_Map[ szLastMainKey ] = mLastMap;  
  73.    mLastMap.clear();  
  74.    szLastMainKey =  szLine ;  
  75.   }  
  76.   else  
  77.   {    
  78.      
  79.       
  80.    //是否是子键  
  81.    if( nIndexPos = szLine.find(“=” ),string::npos != nIndexPos)  
  82.    {  
  83.     string szSubKey,szSubValue;  
  84.     szSubKey = szLine.substr( 0,nIndexPos );  
  85.     szSubValue = szLine.substr( nIndexPos+1,szLine.length()-nIndexPos-1);  
  86.     mLastMap[szSubKey] = szSubValue ;  
  87.    }  
  88.    else  
  89.    {  
  90.     //TODO:不符合ini键值模板的内容 如注释等  
  91.    }  
  92.   }  
  93.   
  94.  }  
  95.  //插入最后一次主键  
  96.  m_Map[ szLastMainKey ] = mLastMap;  
  97.   
  98.     return INI_SUCCESS;  
  99. }  
  100.   
  101. /****************************************************************************** 
  102. * 功  能:关闭文件函数 
  103. * 参  数:无 
  104. * 返回值: 
  105. * 备  注: 
  106. ******************************************************************************/  
  107. INI_RES CIni::CloseFile()  
  108. {  
  109.    
  110.    
  111.     if (m_fp != NULL)  
  112.     {  
  113.         fclose(m_fp);  
  114.   m_fp = NULL;  
  115.     }   
  116.    
  117.     return INI_SUCCESS;  
  118. }  
  119.   
  120. /****************************************************************************** 
  121. * 功  能:获取[SECTION]下的某一个键值的字符串 
  122. * 参  数: 
  123. *  char* mAttr  输入参数    主键 
  124. *  char* cAttr  输入参数 子键 
  125. *  char* value  输出参数 子键键值 
  126. * 返回值: 
  127. * 备  注: 
  128. ******************************************************************************/  
  129. INI_RES CIni::GetKey(const char* mAttr, const char* cAttr, char* pValue)  
  130. {  
  131.    
  132.     KEYMAP mKey = m_Map[ mAttr ];  
  133.   
  134.  string sTemp = mKey[ cAttr ];  
  135.    
  136.  strcpy( pValue,sTemp.c_str() );  
  137.    
  138.     return INI_SUCCESS;  
  139. }  
  140.   
  141. /****************************************************************************** 
  142. * 功  能:获取整形的键值 
  143. * 参  数: 
  144. *       cAttr                     主键 
  145. *      cAttr                     子键 
  146. * 返回值:正常则返回对应的数值 未读取成功则返回0(键值本身为0不冲突) 
  147. * 备  注: 
  148. ******************************************************************************/  
  149. int CIni::GetInt(const char* mAttr, const char* cAttr )  
  150. {  
  151.  int nRes = 0;  
  152.   
  153.  memset( m_szKey,0,sizeof(m_szKey) );  
  154.    
  155.  if( INI_SUCCESS == GetKey( mAttr,cAttr,m_szKey ) )  
  156.  {  
  157.   nRes = atoi( m_szKey );  
  158.  }  
  159.  return nRes;  
  160. }  
  161.   
  162. /****************************************************************************** 
  163. * 功  能:获取键值的字符串 
  164. * 参  数: 
  165. *       cAttr                     主键 
  166. *      cAttr                     子键 
  167. * 返回值:正常则返回读取到的子键字符串 未读取成功则返回”NULL” 
  168. * 备  注: 
  169. ******************************************************************************/  
  170. char *CIni::GetStr(const char* mAttr, const char* cAttr )  
  171. {  
  172.  memset( m_szKey,0,sizeof(m_szKey) );  
  173.    
  174.  if( INI_SUCCESS != GetKey( mAttr,cAttr,m_szKey ) )  
  175.  {  
  176.   strcpy( m_szKey,”NULL” );  
  177.  }  
  178.    
  179.  return m_szKey;  
  180.   
  181. }  
#include "Ini.h"
/******************************************************************************
* 功  能:构造函数
* 参  数:无
* 返回值:无
* 备  注:
******************************************************************************/
CIni::CIni( )
{
 memset( m_szKey,0,sizeof(m_szKey) );
 m_fp = NULL;
}

/******************************************************************************
* 功  能:析构函数
* 参  数:无
* 返回值:无
* 备  注:
******************************************************************************/

CIni::~CIni()
{
 m_Map.clear();
}

/******************************************************************************
* 功  能:打开文件函数
* 参  数:无
* 返回值:
* 备  注:
******************************************************************************/
INI_RES CIni::OpenFile(const char* pathName, const char* type)
{
 string szLine,szMainKey,szLastMainKey,szSubKey;
 char strLine[ CONFIGLEN ] = { 0 };
 KEYMAP mLastMap;
 int  nIndexPos = -1;
 int  nLeftPos = -1;
 int  nRightPos = -1;
    m_fp = fopen(pathName, type);

    if (m_fp == NULL)
    {
  printf( "open inifile %s error!\n",pathName );
        return INI_OPENFILE_ERROR;
    }

 m_Map.clear();

 while( fgets( strLine, CONFIGLEN,m_fp) )
 {  
  szLine.assign( strLine );
  //删除字符串中的非必要字符
  nLeftPos = szLine.find("\n" );
  if( string::npos != nLeftPos )
  {
   szLine.erase( nLeftPos,1 );
  }
  nLeftPos = szLine.find("\r" );
  if( string::npos != nLeftPos )
  {
   szLine.erase( nLeftPos,1 );
  }   
  //判断是否是主键
  nLeftPos = szLine.find("[");
  nRightPos = szLine.find("]");
  if(  nLeftPos != string::npos && nRightPos != string::npos )
  {
   szLine.erase( nLeftPos,1 );
   nRightPos--;
   szLine.erase( nRightPos,1 );
   m_Map[ szLastMainKey ] = mLastMap;
   mLastMap.clear();
   szLastMainKey =  szLine ;
  }
  else
  {  


   //是否是子键
   if( nIndexPos = szLine.find("=" ),string::npos != nIndexPos)
   {
    string szSubKey,szSubValue;
    szSubKey = szLine.substr( 0,nIndexPos );
    szSubValue = szLine.substr( nIndexPos+1,szLine.length()-nIndexPos-1);
    mLastMap[szSubKey] = szSubValue ;
   }
   else
   {
    //TODO:不符合ini键值模板的内容 如注释等
   }
  }

 }
 //插入最后一次主键
 m_Map[ szLastMainKey ] = mLastMap;

    return INI_SUCCESS;
}

/******************************************************************************
* 功  能:关闭文件函数
* 参  数:无
* 返回值:
* 备  注:
******************************************************************************/
INI_RES CIni::CloseFile()
{


    if (m_fp != NULL)
    {
        fclose(m_fp);
  m_fp = NULL;
    } 

    return INI_SUCCESS;
}

/******************************************************************************
* 功  能:获取[SECTION]下的某一个键值的字符串
* 参  数:
*  char* mAttr  输入参数    主键
*  char* cAttr  输入参数 子键
*  char* value  输出参数 子键键值
* 返回值:
* 备  注:
******************************************************************************/
INI_RES CIni::GetKey(const char* mAttr, const char* cAttr, char* pValue)
{

    KEYMAP mKey = m_Map[ mAttr ];

 string sTemp = mKey[ cAttr ];

 strcpy( pValue,sTemp.c_str() );

    return INI_SUCCESS;
}

/******************************************************************************
* 功  能:获取整形的键值
* 参  数:
*       cAttr                     主键
*      cAttr                     子键
* 返回值:正常则返回对应的数值 未读取成功则返回0(键值本身为0不冲突)
* 备  注:
******************************************************************************/
int CIni::GetInt(const char* mAttr, const char* cAttr )
{
 int nRes = 0;

 memset( m_szKey,0,sizeof(m_szKey) );

 if( INI_SUCCESS == GetKey( mAttr,cAttr,m_szKey ) )
 {
  nRes = atoi( m_szKey );
 }
 return nRes;
}

/******************************************************************************
* 功  能:获取键值的字符串
* 参  数:
*       cAttr                     主键
*      cAttr                     子键
* 返回值:正常则返回读取到的子键字符串 未读取成功则返回"NULL"
* 备  注:
******************************************************************************/
char *CIni::GetStr(const char* mAttr, const char* cAttr )
{
 memset( m_szKey,0,sizeof(m_szKey) );

 if( INI_SUCCESS != GetKey( mAttr,cAttr,m_szKey ) )
 {
  strcpy( m_szKey,"NULL" );
 }

 return m_szKey;

}

用法:比如读取
[Section1]
key1=1
key2=abcdw
[Section2]
key1=3
key2=ddba
 
CIni  ini;
ini.OpenFile(“./Test.ini”,”r” );
char *pVal1 = ini.GetStr(“Section1”,”key2”);
int  nKey = ini.GetInt(“Section2”,”key1”);
 
再封装一下
#define INIINT( a ,b )   ini.GetInt(a,b)
#define INISTR(a,b)   ini.GetStr(a,b)
 
读取所有的字段都可以用 以下形式
int a=INIINT(…… )
strcpy( szTemp,INIStr(a,b) )
                </div>

Linux下读取Ini文件类

最近项目上有需要读取Ini文件 所谓Ini文件也就是文本文档 并且以
//注释1
/*注释2
[Section]
Key1=aaa
Key2=bbb
这种形式存在的文档
自己编写了一个类  比较使用 简单 可以跨平台读写INI文件
头文件Ini.h
  1. #include <map>  
  2. #include <string>  
  3. using namespace std;  
  4.   
  5. #define CONFIGLEN           256   
  6.   
  7. enum INI_RES  
  8. {  
  9.     INI_SUCCESS,            //成功  
  10.     INI_ERROR,              //普通错误  
  11.     INI_OPENFILE_ERROR,     //打开文件失败  
  12.     INI_NO_ATTR            //无对应的键值  
  13. };  
  14.   
  15. //              子键索引    子键值   
  16. typedef map<std::string,std::string> KEYMAP;  
  17. //              主键索引 主键值    
  18. typedef map<std::string,KEYMAP> MAINKEYMAP;  
  19. // config 文件的基本操作类  
  20.   
  21. class CIni   
  22. {  
  23. public:  
  24.     // 构造函数  
  25.     CIni();  
  26.   
  27.     // 析够函数  
  28.     virtual ~CIni();  
  29. public:  
  30.     //获取整形的键值  
  31.     int  GetInt(const char* mAttr, const char* cAttr );  
  32.     //获取键值的字符串  
  33.     char *GetStr(const char* mAttr, const char* cAttr );  
  34.     // 打开config 文件  
  35.     INI_RES OpenFile(const char* pathName, const char* type);  
  36.     // 关闭config 文件  
  37.     INI_RES CloseFile();  
  38. protected:  
  39.     // 读取config文件  
  40.     INI_RES GetKey(const char* mAttr, const char* cAttr, char* value);  
  41. protected:  
  42.     // 被打开的文件局柄  
  43.     FILE* m_fp;  
  44.     char  m_szKey[ CONFIGLEN ];  
  45.     MAINKEYMAP m_Map;  
  46. };  
  47.   
  48. #endif // FILLE_H  
#include <map>




猜你喜欢

转载自blog.csdn.net/wushuangge/article/details/78899819