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) )