以前用过tinyxml,ini等进行配置文件的存取,总的来说感觉相对累,而boost::property_tree则可以很方便的来做这些事。
#include "StdAfx.h"
#include
#include
#include
#include "Configure.h"
#include "../utils/Common.h"
CConfigure::CConfigure(void)
{
}
CConfigure::~CConfigure(void)
{
}
unsigned long CConfigure::save( const string szUser,const string szPswd,bool bAutoSync,const string szStartTime,const string szIntevel,int nSyncModel,const string szServer,std::set& lDirs )
{
unsigned long ulRes=0;
using boost::property_tree::ptree;
ptree pt;
std::string filename;
char path[255]={0};
getPath(path);
filename = path;
filename += "\\";
filename += FILENAME;
pt.put(USER_NAME,szUser);
pt.put(PASSWORD,szPswd);
std::string sDirFlag;
sDirFlag = SYNC_DIRS;
sDirFlag += ".";
sDirFlag += SYNC_DIR;
std::set::iterator it=lDirs.begin();
在此处要注意是xml中是ab在这里用add,而不是put
for (;it != lDirs.end();it++)
{
std::string name = *it;
pt.add(sDirFlag,name);
}
//以下的是想把xml.encoding配置为gbk,避免如果是utf-8,则要进行很多的码流转换
boost::property_tree::xml_parser::xml_writer_settings ss(' ',0,"gbk");
write_xml(filename, pt,std::locale(),ss);
return ulRes;
}
unsigned long CConfigure::load( string& szUser, string& szPswd,bool& bAutoSync, string& szStartTime, string& szIntevel,int& nSyncModel, string& szServer,std::set& lDirs )
{
unsigned long ulRes=0;
using boost::property_tree::ptree;
ptree pt;
std::string filename;
char sTmp[255]={0};
getPath(sTmp);
filename = sTmp;
filename += "\\";
filename += FILENAME;
CChineseCodeLib ccl;
read_xml(filename, pt);
try
{
szUser= pt.get(USER_NAME," ");
szPswd= pt.get(PASSWORD,"");
}
catch(...)
{
ulRes = 2;
}
try
{
std::string sDirFlag;
sDirFlag = SYNC_DIRS;
/*sDirFlag += ".";
sDirFlag += SYNC_DIR;*/
ptree & p=pt.get_child(sDirFlag);
在此处要注意是xml中是ab在这里用只要到DIRS就可以进行遍历了
BOOST_FOREACH(const ptree::value_type &v,p )
{
string strTmp=v.second.data();
lDirs.insert(strTmp);
}
}
catch (...)
{
ulRes = 2;
}
return ulRes;
}
unsigned long CConfigure::file_exist()
{
std::string filename;
char path[255]={0};
getPath(path);
filename = path;
filename += "\\";
filename += FILENAME;
namespace fs = boost::filesystem;
fs::path pathFile(filename, fs::native);
if (boost::filesystem::exists(pathFile))
{
return CONFIG_SUCCESS;
}
else
{
return CONFIG_FILE_NOT_EXIST;
}
}
阅读(2944) | 评论(1) | 转发(0) |