linux inifile.cpp

来源:互联网 发布:安全守护下载软件 编辑:程序博客网 时间:2024/05/08 12:34
#include <iostream>
#include "inifile.h"
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>


CIniFile::CIniFile(const string &strFileName):m_strFileName(strFileName)
{
Load();
}


CIniFile::~CIniFile(void)
{
Save();
}


int CIniFile::Dump()
{
map<string, ConfigType>::iterator tSecIter1 = m_mSec2Config.begin(), tSecIter2 = m_mSec2Config.end();
ConfigType::iterator tConfigTypeIter1, tConfigTypeIter2;


while (tSecIter1 != tSecIter2)
{
cout << "[" << tSecIter1->first << "]" << endl;
tConfigTypeIter1 = tSecIter1->second.begin(), tConfigTypeIter2 = tSecIter1->second.end();
while (tConfigTypeIter1 != tConfigTypeIter2)
{
cout << tConfigTypeIter1->first << "=" << tConfigTypeIter1->second << endl;


++tConfigTypeIter1;
}
cout << endl;
++tSecIter1;
}


return 0;
}


int CIniFile::ReadItem(const string& strSection, const string& strKey, const string& strDefault, string& strValue)
{
strValue = strDefault;


if (!m_mSec2Config.count(strSection))
{
ConfigType tConfigType;
tConfigType[strKey] = strValue;
m_mSec2Config[strSection] = tConfigType;


return -1;
}


ConfigType& tConfigType = m_mSec2Config[strSection];
if(tConfigType.count(strKey))
{
strValue = tConfigType[strKey];
return 0;
}
else
{
tConfigType[strKey] = strValue;
return -2;
}
}


int CIniFile::WriteItem(const string& strSection, const string& strKey, const string& strValue)
{
if (!m_mSec2Config.count(strSection))
{
ConfigType tConfigType;
tConfigType[strKey] = strValue;
m_mSec2Config[strSection] = tConfigType;


return -1;
}


ConfigType& tConfigType = m_mSec2Config[strSection];
tConfigType[strKey] = strValue;
return 0;
}


int CIniFile::Load()
{
FILE* pFile;


if (NULL == (pFile = ::fopen(m_strFileName.c_str(), "a+")))
{
return -1;
}


string strLine, strSection;
string strKey, strValue;
size_t nPos, nEndPos;
ConfigType tConfigType;
while (0 == ReadLine(pFile, strLine))
{
if (string::npos != (nPos = strLine.find_first_of("[")))
{
if (string::npos == (nEndPos = strLine.find_first_of("]")))
{
::fclose(pFile);
return -1;
}


strSection = strLine.substr(nPos + 1, nEndPos - nPos - 1);
if (0 > TrimString(strSection))
{
::fclose(pFile);
return -1;
}
}
else if (string::npos != (nPos = strLine.find_first_of("=")))
{
strKey = strLine.substr(0, nPos);
strValue = strLine.substr(nPos + 1);


if (0 > TrimString(strKey) || 0 > TrimString(strValue) || strSection.empty())
{
::fclose(pFile);
return -1;
}


m_mSec2Config[strSection][strKey] = strValue;
}



return ::fclose(pFile);
}


int CIniFile::Save()
{
FILE* pFile;


if (NULL == (pFile = ::fopen(m_strFileName.c_str(), "w")))
{
return -1;
}


map<string, ConfigType>::iterator tSecIter1 = m_mSec2Config.begin(), tSecIter2 = m_mSec2Config.end();
ConfigType::iterator tConfigTypeIter1, tConfigTypeIter2;


string strSection, strConfig;
while (tSecIter1 != tSecIter2)
{
strSection = string("[") + tSecIter1->first + string("]\n");
::fwrite(strSection.c_str(), sizeof(char), strSection.length(), pFile);
tConfigTypeIter1 = tSecIter1->second.begin(), tConfigTypeIter2 = tSecIter1->second.end();
while (tConfigTypeIter1 != tConfigTypeIter2)
{
strConfig = tConfigTypeIter1->first + string("=") + tConfigTypeIter1->second + string("\n");
::fwrite(strConfig.c_str(), sizeof(char), strConfig.length(), pFile);


++tConfigTypeIter1;
}
::fwrite("\n", sizeof(char), 1, pFile);
++tSecIter1;
}


return ::fclose(pFile);
}


/*读取FILE文件中的内容*/
int CIniFile::ReadLine(FILE* pFile, string& strLine)
{
char szBuff[BUFFER_LEN];
int nLen;
char * pos=NULL;
do 
{
if (NULL == ::fgets(szBuff, BUFFER_LEN, pFile))
{
return -1;
}
if((pos=strstr(szBuff,"\n")))
pos = '\0';
if((pos=strstr(szBuff,"\r")))
pos = '\0';
if (0 < (nLen = (int)::strlen(szBuff)))
{
break;
}


} while (true);


szBuff[nLen - 1] = '\0';


strLine = szBuff;


return 0;
}


int CIniFile::TrimString(string& strToken)
{
if (strToken.empty())
{
return -1;
}


size_t nPos    = strToken.find_first_not_of(" \t");
size_t nEndPos = strToken.find_last_not_of(" \t");


strToken = strToken.substr(nPos, nEndPos - nPos + 1);


return (strToken.empty()) ? -1 : 0;
}
0 0