//ini文件操作类
//write by Khan
//最开头的注释默认的 section为 "###"
//普通的注释和空行 key为"##"
//所以 大家不要让配置文件的key = "##"
支持的注释格式 "#注释"
//LIniFile.h
#ifndef __LINI_FILE_H__
#define __LINI_FILE_H__
#include <string>
#include <vector>
#include <stdio.h>
#include <utility>
using namespace std;
class CLogFileException{
string m_strError;
public:
inline CLogFileException(string p_strError){ m_strError = m_strError; }
inline string OnMessage(){ return this->m_strError; }
};
class LIniFile
{
public:
LIniFile(const string p_strFileName);
~LIniFile(void);
public:
string GetValue(const string p_strSection, const string p_strKey);
int SetValue(const string p_strSection, const string p_strKey, const string p_strValue);
void Print();
int OpenIni(const string p_strFileName);
int OpenIni();
void CloseIni();
protected:
private:
vector<pair<string, pair<string,string> > > m_szStrItem;
string m_strFileName;
FILE* m_fileHandle;
string TrimL(string p_strValue);
string TrimR(string p_strValue);
string Trim(string p_strValue);
void LIniFile::DeleteEnter(char *p_str, int size);
};
#endif //__LINI_FILE_H__
//LIniFile.cpp
#include "LIniFile.h"
#include <algorithm>
LIniFile::LIniFile(const string p_strFileName)
{
this->m_strFileName = p_strFileName;
this->m_fileHandle = NULL;
//this->OpenIni(p_strFileName);
}
LIniFile::~LIniFile(void){
//this->CloseIni();
}
int LIniFile::OpenIni(){
return OpenIni(m_strFileName);
}
int LIniFile::OpenIni(const string p_strFileName){
this->m_fileHandle = fopen(p_strFileName.c_str(),"rt+");
if (NULL == this->m_fileHandle) {
return 0;
}
fseek(this->m_fileHandle, 0, SEEK_SET); //将文件指针指向文件开头
char cTemp[512];
memset(cTemp, 0, sizeof(cTemp));
vector<string> vecStrItem;
int iLen = 0;
while(NULL != fgets(cTemp, sizeof(cTemp), this->m_fileHandle)){
DeleteEnter(cTemp, sizeof(cTemp));//去结尾回车符
Trim(cTemp); //去前后空格
string str = cTemp;
vecStrItem.push_back(str);
}
this->CloseIni();
pair<string, string> pKVItem;
pair<string, pair<string, string> > pItem;
string strSection = "";
string strCurrentSection = "";
for (unsigned int i = 0; i < vecStrItem.size(); i++) {
string strLine = ((string)vecStrItem[i]);
if( '[' == strLine[0] ){
strSection = strLine;
strSection = strSection.erase(0,1);
if(']' == strSection[strSection.size() -1 ])
strSection = strSection.erase(strSection.size() - 1 );
if (strCurrentSection != strSection)
strCurrentSection = strSection;
}else if( string::npos != strLine.find('=') ){
if ("" != strCurrentSection ) {
string strKey;
string strValue;
strKey = TrimR( strLine.substr(0, strLine.find('=')) );
strValue = TrimL( strLine.substr(strLine.find('=') + 1, strLine.size() - strLine.find('=') ) );
pKVItem.first = strKey;
pKVItem.second = strValue;
pItem.first = strCurrentSection;
pItem.second = pKVItem;
m_szStrItem.push_back(pItem);
}
}else{
if ("" != strCurrentSection ) {
string strKey = "##";
string strValue = TrimL( strLine );
pKVItem.first = strKey;
pKVItem.second = strValue;
pItem.first = strCurrentSection;
pItem.second = pKVItem;
m_szStrItem.push_back(pItem);
}else{
string strKey = "##";
string strValue = TrimL( strLine );
pKVItem.first = strKey;
pKVItem.second = strValue;
pItem.first = "###";
pItem.second = pKVItem;
m_szStrItem.push_back(pItem);
}
}
}
return 1;
}
string LIniFile::GetValue(const string p_strSection, const string p_strKey){
string strValue;
for (unsigned i = 0; i< m_szStrItem.size(); i++) {
if( p_strSection == (string) ( ((pair<string, pair<string, string> >)m_szStrItem[i]).first ) ){
pair<string, string> pKVItem = ((pair<string, pair<string, string> >)m_szStrItem[i]).second;
if ( p_strKey == (string)(pKVItem.first)) {
strValue = (string)(pKVItem.second);
break;
}
}
}
return strValue;
}
void LIniFile::Print(){
string strValue;
string strKey;
string strSection;
for (unsigned i = 0; i< m_szStrItem.size(); i++) {
strSection = (string) ( ((pair<string, pair<string, string> >)m_szStrItem[i]).first );
pair<string, string> pKVItem = ((pair<string, pair<string, string> >)m_szStrItem[i]).second;
strKey = (string)(pKVItem.first);
strValue = (string)(pKVItem.second);
//if((strSection != "###") && (strKey != "##") )
printf("[%s]:%s=%s\n", strSection.c_str(), strKey.c_str(), strValue.c_str());
}
}
void LIniFile::CloseIni(){
if(NULL != this->m_fileHandle){
fclose(this->m_fileHandle);
this->m_fileHandle = NULL;
}
}
string LIniFile::TrimL(string p_strValue){
int i = 0 ;
if ((i=(int)(p_strValue.length())) < 1)
return "";
i = 0;
while(isspace(p_strValue[i])){
i++ ;
}
p_strValue.erase(0,i);
return p_strValue;
}
string LIniFile::TrimR(string p_strValue){
int i = 0;
if ( (i = (int)(p_strValue.length()-1)) < 1 )
return "";
while( ( p_strValue[i] ) == ' ' ){
i--;
}
p_strValue.erase(i + 1);
return p_strValue;
}
string LIniFile::Trim(string p_strValue){
return TrimL(TrimR(p_strValue));
}
int LIniFile::SetValue(const string p_strSection, const string p_strKey, const string p_strValue){
pair<string, string> pKVItem;
pair<string, pair<string, string> > pItem;
pKVItem.first = p_strKey;
pKVItem.second = p_strValue;
pItem.first = p_strSection;
pItem.second = pKVItem;
int bFind = 0;
for(int i = (int)(m_szStrItem.size())-1; i >= 0; i--){ //找到已有的section, 并在末尾添加
string bb = m_szStrItem[i].second.first;
if((m_szStrItem[i].first == p_strSection) && (m_szStrItem[i].second.first != "##")/* && (m_szStrItem[i].second.second != "")*/){
m_szStrItem.insert( m_szStrItem.begin()+ i+1, pItem );
bFind = 1;
break;
}
}
if(bFind != 1) //如果是新增的section
m_szStrItem.push_back(pItem);
this->m_fileHandle = fopen(this->m_strFileName.c_str(),"wt+");
//this->m_fileHandle = fopen("c:\\aaa.ini","wt+");
if (NULL == this->m_fileHandle) {
return 0;
}
string strCurSection = "";
string strTmp = "";
if((int)(m_szStrItem.size()) > 0){
strCurSection = m_szStrItem[0].first;
if(strCurSection != "###"){
strTmp = "[" + strCurSection + "]" + "\n";
fputs(strTmp.c_str(), this->m_fileHandle);
}
for(int i=0; i<(int)(m_szStrItem.size()); i++){
if( strCurSection == m_szStrItem[i].first ){
if((m_szStrItem[i].second.first == "##") && (m_szStrItem[i].second.second ==""))
strTmp = "\n";
else if((m_szStrItem[i].second.first == "##") && (m_szStrItem[i].second.second !=""))
strTmp = m_szStrItem[i].second.second+ "\n";
else
strTmp = m_szStrItem[i].second.first + " = " + m_szStrItem[i].second.second+ "\n";
fputs(strTmp.c_str(), this->m_fileHandle);
}else{
strCurSection = m_szStrItem[i].first;
if(strCurSection != "###"){
string strTmp = "[" + strCurSection + "]"+ "\n";
fputs(strTmp.c_str(), this->m_fileHandle);
}
if((m_szStrItem[i].second.first == "##") && (m_szStrItem[i].second.second ==""))
strTmp = "\n";
else if((m_szStrItem[i].second.first == "##") && (m_szStrItem[i].second.second !=""))
strTmp = m_szStrItem[i].second.second+ "\n";
else
strTmp = m_szStrItem[i].second.first + " = " + m_szStrItem[i].second.second+ "\n";
fputs(strTmp.c_str(), this->m_fileHandle);
}
}
}
this->CloseIni();
return 1;
}
void LIniFile::DeleteEnter(char *p_str, int size){
int i_len = (int)(strlen(p_str));
if(p_str[i_len-1] == 0x0d || p_str[i_len-1] == 0x0a)
p_str[i_len-1] = 0x00;
i_len = (int)(strlen(p_str));
if(p_str[i_len-1] == 0x0d || p_str[i_len-1] == 0x0a)
p_str[i_len-1] = 0x00;
}
调用方式
#include "LIniFile.h"
int main(int argc, char** argv)
{
LIniFile ini("d:\\My Documents\\Visual Studio 2005\\Projects\\inifile\\cbm.ini");
ini.OpenIni();
ini.SetValue("cbm","a", "b");
ini.SetValue("aaa","a", "b");
ini.Print();
ini.CloseIni();
getchar();
return 0;
}
处理前的日志文件内容
1 ## cbm配置文件
2
3 [cbm]
4
5 #sp端口号
6 sp_port = 07554400001
7
8 # 开通的频道列表
9 channels = 2001,2002,2003,2004
10
11 #rsa私钥
12 rsa_key1 = 0004b831414e0b4613922bd35b4b36802bc1e1e81c95a27c958f5382003df646154ca92fc1ce02c3be047a45e9b02a9089b4b90278237c965192a0fcc86bb49bc82ae6
13 rsa_key2 = fdc2de709006b86c7676efdf597626fad633a4f7dc48c445d37eb55fcb3b1abb95baaa826d5390e15fd14ed403fa2d0cb841c650609524ec555e3bc56ca95700000000
14 rsa_key3 = 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
15 rsa_key4 = 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010001
16
17
18 [2001]
19 flag = 1
20 des3_key1 = 3e072bb9698439377cea73f33e0d5c5af1f5ce581f8e585ac4df2e98c8627e84fea8dbfc2ad4e66375187ab021aedc094c37196534c41611262419bfd07822a3ee
21 des3_key2 = a2bd32dd13ffc59535d99e2ae79c866f308fbd1de7466034f4ed3742651731d307e95a1e9e838e5c77f135124c0714efe06cb7c596a6c35e64eb7572b1737b
22
23 [2002]
24 flag = 1
25 des3_key1 = 3e072bb9698439377cea73f33e0d5c5af1f5ce581f8e585ac4df2e98c8627e84fea8dbfc2ad4e66375187ab021aedc094c37196534c41611262419bfd07822a3ee
26 des3_key2 = a2bd32dd13ffc59535d99e2ae79c866f308fbd1de7466034f4ed3742651731d307e95a1e9e838e5c77f135124c0714efe06cb7c596a6c35e64eb7572b1737b
27
28 [2003]
29 flag = 1
30 des3_key1 = 3e072bb9698439377cea73f33e0d5c5af1f5ce581f8e585ac4df2e98c8627e84fea8dbfc2ad4e66375187ab021aedc094c37196534c41611262419bfd07822a3ee
31 des3_key2 = a2bd32dd13ffc59535d99e2ae79c866f308fbd1de7466034f4ed3742651731d307e95a1e9e838e5c77f135124c0714efe06cb7c596a6c35e64eb7572b1737b
32
33 [2004]
34 flag = 1
35 des3_key1 = 3e072bb9698439377cea73f33e0d5c5af1f5ce581f8e585ac4df2e98c8627e84fea8dbfc2ad4e66375187ab021aedc094c37196534c41611262419bfd07822a3ee
36 des3_key2 = a2bd32dd13ffc59535d99e2ae79c866f308fbd1de7466034f4ed3742651731d307e95a1e9e838e5c77f135124c0714efe06cb7c596a6c35e64eb7572b1737b
处理后的ini内容
1 ## cbm配置文件
2
3 [cbm]
4
5 #sp端口号
6 sp_port = 07554400001
7
8 # 开通的频道列表
9 channels = 2001,2002,2003,2004
10
11 #rsa私钥
12 rsa_key1 = 0004b831414e0b4613922bd35b4b36802bc1e1e81c95a27c958f5382003df646154ca92fc1ce02c3be047a45e9b02a9089b4b90278237c965192a0fcc86bb49bc82ae6
13 rsa_key2 = fdc2de709006b86c7676efdf597626fad633a4f7dc48c445d37eb55fcb3b1abb95baaa826d5390e15fd14ed403fa2d0cb841c650609524ec555e3bc56ca95700000000
14 rsa_key3 = 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
15 rsa_key4 = 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010001
16
17
18 [2001]
19 flag = 1
20 des3_key1 = 3e072bb9698439377cea73f33e0d5c5af1f5ce581f8e585ac4df2e98c8627e84fea8dbfc2ad4e66375187ab021aedc094c37196534c41611262419bfd07822a3ee
21 des3_key2 = a2bd32dd13ffc59535d99e2ae79c866f308fbd1de7466034f4ed3742651731d307e95a1e9e838e5c77f135124c0714efe06cb7c596a6c35e64eb7572b1737b
22
23 [2002]
24 flag = 1
25 des3_key1 = 3e072bb9698439377cea73f33e0d5c5af1f5ce581f8e585ac4df2e98c8627e84fea8dbfc2ad4e66375187ab021aedc094c37196534c41611262419bfd07822a3ee
26 des3_key2 = a2bd32dd13ffc59535d99e2ae79c866f308fbd1de7466034f4ed3742651731d307e95a1e9e838e5c77f135124c0714efe06cb7c596a6c35e64eb7572b1737b
27 a = b
28
29 [2003]
30 flag = 1
31 des3_key1 = 3e072bb9698439377cea73f33e0d5c5af1f5ce581f8e585ac4df2e98c8627e84fea8dbfc2ad4e66375187ab021aedc094c37196534c41611262419bfd07822a3ee
32 des3_key2 = a2bd32dd13ffc59535d99e2ae79c866f308fbd1de7466034f4ed3742651731d307e95a1e9e838e5c77f135124c0714efe06cb7c596a6c35e64eb7572b1737b
33
34 [2004]
35 flag = 1
36 des3_key1 = 3e072bb9698439377cea73f33e0d5c5af1f5ce581f8e585ac4df2e98c8627e84fea8dbfc2ad4e66375187ab021aedc094c37196534c41611262419bfd07822a3ee
37 des3_key2 = a2bd32dd13ffc59535d99e2ae79c866f308fbd1de7466034f4ed3742651731d307e95a1e9e838e5c77f135124c0714efe06cb7c596a6c35e64eb7572b1737b
38
39 [aaa]
40 a = b
41