Linux下讀取Ini檔案類 最近項目上有需要讀取Ini檔案 所謂Ini檔案也就是文字文件 並且以 //注釋1 /*注釋2 [Section] Key1=aaa Key2=bbb 這種形式存在的文檔 自己編寫了一個類 比較使用 簡單 可以跨平台讀寫INI檔案 標頭檔Ini.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
#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) )