#include<iostream>
#include <fstream>
#include <vector>
#include<string>
using
namespace
std;
class
CConfigOperator
{
public
:
CConfigOperator(
void
);
~CConfigOperator(
void
);
void
SetFilePath(
const
string &sFilePath);
string GetFilePath();
bool
GetConfigValue(
const
string &sName,
const
string &skey,string &sValue,string &sError);
bool
ModefyConfigValue(
const
string &sName,
const
string &skey,
const
string &sValue,string &sError);
private
:
bool
OpenFile();
void
FindName();
bool
FindKey();
bool
WriteFile();
bool
ModefyValue();
void
WriteToFile(vector<string> &vContent);
fstream m_fout;
ifstream m_fin;
string m_sFilePath;
string m_Name;
string m_Key;
string m_value;
string m_sError;
string m_sStr;
bool
m_bFindName;
};
#include "ConfigOperator.h"
CConfigOperator::CConfigOperator(
void
)
{
}
CConfigOperator::~CConfigOperator(
void
)
{
}
void
CConfigOperator::SetFilePath(
const
string &sFilePath)
{
m_sFilePath = sFilePath;
}
string CConfigOperator::GetFilePath()
{
return
this
->m_sFilePath;
}
bool
CConfigOperator:: OpenFile()
{
if
(
true
== m_fin.is_open())
{
m_fin.close();
}
m_fin.open(m_sFilePath.c_str());
if
(NULL == m_fin)
{
m_sError =
"can not open file "
+m_sFilePath;
return
false
;
}
return
true
;
}
void
CConfigOperator::FindName()
{
if
(-1 != m_sStr.find(
'['
))
{
string sTemp = m_sStr.substr(m_sStr.find(
'['
) + 1,m_sStr.find(
']'
) - m_sStr.find(
'['
) - 1);
if
(0 ==
strcmp
(sTemp.c_str(),m_Name.c_str()))
{
m_bFindName =
true
;
m_sError =
"Find Name But Not Find Key"
;
}
else
{
m_bFindName =
false
;
}
}
}
bool
CConfigOperator::FindKey()
{
int
iDelePlace = m_sStr.find(
'//'
);
int
iFindEqual = m_sStr.find(
'='
);
if
( (- 1 != iDelePlace && iDelePlace < iFindEqual) || (- 1 != iDelePlace && -1 == iFindEqual) || -1 == iFindEqual )
{
return
false
;
}
string sKey = m_sStr.substr(0,m_sStr.find(
'='
));
if
(0 ==
strcmp
(sKey.c_str(),m_Key.c_str()))
{
m_value = m_sStr.substr(m_sStr.find(
'='
) + 1 , m_sStr.length() - m_sStr.find(
'='
) - 1);
return
true
;
}
return
false
;
}
bool
CConfigOperator::GetConfigValue(
const
string &sName,
const
string &skey,string &sValue,string &sError)
{
m_sError =
""
;
m_Name = sName;
m_Key = skey;
if
(
false
== OpenFile())
{
sError = m_sError;
return
false
;
}
char
str[1024];
m_bFindName =
false
;
while
(NULL != m_fin.getline(str,
sizeof
(str)))
{
m_sStr = str;
FindName();
if
(
true
== m_bFindName)
{
if
(
true
== FindKey())
{
m_fin.close();
sError =
""
;
sValue = m_value;
return
true
;
}
}
}
sError = m_sError;
m_fin.close();
return
false
;
}
bool
CConfigOperator::WriteFile()
{
m_fout.close();
m_fout.clear();
m_fout.open(m_sFilePath.c_str(),ios::in|ios::out);
if
(NULL == m_fout)
{
m_sError =
"can not open file "
+m_sFilePath;
return
false
;
}
return
true
;
}
bool
CConfigOperator::ModefyValue()
{
int
iDelePlace = m_sStr.find(
'//'
);
int
iFindEqual = m_sStr.find(
'='
);
if
( (- 1 != iDelePlace && iDelePlace < iFindEqual) || (- 1 != iDelePlace && -1 == iFindEqual) || -1 == iFindEqual )
{
return
false
;
}
string sKey = m_sStr.substr(0,m_sStr.find(
'='
));
if
(0 ==
strcmp
(sKey.c_str(),m_Key.c_str()))
{
m_sStr = m_sStr.substr(0,m_sStr.find(
'='
) + 1) + m_value ;
return
true
;
}
return
false
;
}
void
CConfigOperator::WriteToFile(vector<string> &vContent)
{
if
(
false
== WriteFile())
{
m_fout.close();
return
;
}
for
(
size_t
iIndex = 0; iIndex < vContent.size(); iIndex ++)
{
m_fout<<vContent[iIndex]<<endl;
}
m_fout.close();
vector<string>().swap(vContent);
}
bool
CConfigOperator::ModefyConfigValue(
const
string &sName,
const
string &skey,
const
string &sValue,string &sError)
{
m_sError =
""
;
m_Name = sName;
m_Key = skey;
m_value = sValue;
if
(
false
== WriteFile())
{
sError = m_sError;
return
false
;
}
char
str[1024];
m_bFindName =
false
;
vector<string> vContent;
bool
isModefy =
false
;
while
(NULL != m_fout.getline(str,
sizeof
(str)))
{
m_sStr = str;
FindName();
if
(
true
== m_bFindName)
{
if
(
true
== ModefyValue())
{
isModefy =
true
;
}
}
vContent.push_back(m_sStr);
}
sError = m_sError;
WriteToFile(vContent);
m_fout.close();
return
isModefy;
}
int
_tmain(
int
argc, _TCHAR* argv[])
{
CConfigOperator tCConfigOperator;
string sFielPath =
"D:\\OAM\\UniTester_Code\\cfg\\123\\cfg.ini"
;
tCConfigOperator.SetFilePath(sFielPath);
string sName =
"Admin_Group"
;
string sKey =
"163253"
;
string sValue =
""
;
string sEroor =
""
;
tCConfigOperator.GetConfigValue(sName,sKey,sValue,sEroor);
cout<<
"valuse is"
<<sValue<<
" sEroor is"
<<sEroor<<endl;
sValue =
"ssss"
;
tCConfigOperator.ModefyConfigValue(sName,sKey,sValue,sEroor);
tCConfigOperator.GetConfigValue(sName,sKey,sValue,sEroor);
cout<<
"valuse is"
<<sValue<<
" sEroor is"
<<sEroor<<endl;
char
c;
c =
getchar
();
return
0;
}