ini文件的讀取

ini.h代碼

#ifndef INI_H

#define 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


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;


}


config.ini


[cctv.thrift]

IP=192.168.37.123

Port=7001

username=admin

password=admin


測試


static void ReadIniFile()

{

CIni  ini;

ini.OpenFile("config.ini", "r");

char *pVal1 = ini.GetStr("cctv.thrift", "IP");

int  nKey = ini.GetInt("cctv.thrift", "Port");

}


注意

    GetStr返回的字符串,必須馬上保存到其他的變量中,如果這個時候重新調用GetInt,上面的pVal1的值將會改變


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章