C++__Read Section FiIe


#ifndef __SEC_FILE_H__
#define __SEC_FILE_H__

#include <string>
#include <vector>
#include <map>

using std::string;
using std::map;
using std::vector;

class SecFile
{
private:
string path;
static const char secComment = '#';
vector<string> vFile;

public:
int load(string path);
string getPath();

int getSec(string tag, vector<string>& vSec);
int getMaps(string tag, map<string, string>& mKVs);
int getKets(string tag, vector<string>& vKets);

string getValue(string tag, string ket);
string secToLine(string tag);

};

#endif //__SEC_FILE_H__



#include "secfile.h"
#include "stringutil.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int SecFile::load(string path)
{
this->path = path;
ifstream infile(path.c_str());
if ( ! infile)
{
cout << "not this file" + path << endl;
return -1;
}

int rowCount = 0;
string line;
while (getline(infile, line))
{
line = StringUtil::Trim(line);
if (this->secComment == line[0] || line.length() == 0)
continue;
this->vFile.push_back(line);
++rowCount;
}
infile.close();
return rowCount;
}

string SecFile::getPath()
{
return this->path;
}

int SecFile::getSec(string tag, vector<string>& vSec)
{
const string sBeg("<"+tag+">");
const string sEnd("</"+tag+">");

vector<string>::const_iterator ix = this->vFile.begin();
for ( ; this->vFile.end() != ix; ++ix )
{
if ( 0 == sBeg.compare((*ix).substr(0,sBeg.size())) )
{
++ix;
break;
}
}

if (this->vFile.end() == ix) return -1;
for (int rowCount = 0 ;ix != vFile.end(); ++ix )
{
if (sEnd == *ix) return rowCount;
vSec.push_back(*ix);
++rowCount;
}

return -1;
}

int SecFile::getKets(string tag, vector<string>& vKets)
{
string ket;
int ketCount = 0;
vector<string> vSec;
this->getSec(tag,vSec);

vector<string>::const_iterator ix = vSec.begin();
for ( ; ix != vSec.end(); ++ix )
{
ket = (*ix).substr(0, (*ix).find('='));
ket = StringUtil::Trim(ket);
vKets.push_back(ket);
++ketCount;
}
return ketCount;
}

string SecFile::getValue(string tag, string ket)
{
vector<string> vSec;
this->getSec(tag,vSec);

vector<string>::const_iterator ix = vSec.begin();
for ( ; ix != vSec.end(); ++ix )
{
if ((*ix).substr(0, ket.length()) == ket)
{
string value = (*ix).substr((*ix).find('=') + 1 );
value = StringUtil::Trim(value);
return value;
}
}
return "";
}

int SecFile::getMaps(string tag, map<string, string>& mKVs)
{
vector<string> vSec;
this->getSec(tag,vSec);

string ket;
string value;
int iCount = 0;
vector<string>::const_iterator ix = vSec.begin();

for ( ; ix != vSec.end(); ++ix )
{
ket = (*ix).substr(0, (*ix).find('='));
ket = StringUtil::Trim(ket);

value = (*ix).substr((*ix).find('=') + 1 );
value = StringUtil::Trim(value);

mKVs.insert(make_pair(ket,value));
iCount++;
}

return iCount;
}

string SecFile::secToLine(string tag)
{
vector<string> vSec;
if (this->getSec(tag, vSec) == -1) return "";

vector<string>::const_iterator ix = vSec.begin();
string sLine = *(ix++);

for ( ;ix != vSec.end(); ++ix )
{
sLine = sLine + " " + *ix;
}
return sLine;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值