1
This commit is contained in:
parent
bcca9bb6eb
commit
1083101984
@ -1,92 +0,0 @@
|
|||||||
#include <assert.h>
|
|
||||||
|
|
||||||
#include <a8/a8.h>
|
|
||||||
#include <a8/stringlist.h>
|
|
||||||
#include <a8/csvreader.h>
|
|
||||||
|
|
||||||
namespace a8
|
|
||||||
{
|
|
||||||
|
|
||||||
CsvReader::CsvReader()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CsvReader::Load(const std::string& filename)
|
|
||||||
{
|
|
||||||
columns_.clear();
|
|
||||||
values_.clear();
|
|
||||||
currline_ = 0;
|
|
||||||
bool ret = stringlist_.LoadFromFile(filename.c_str());
|
|
||||||
if(ret){
|
|
||||||
if(NextLine()){
|
|
||||||
for(unsigned int i = 0;i < values_.size(); i++){
|
|
||||||
columns_.insert(std::pair<std::string, int>(values_[i], i));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CsvReader::NextLine()
|
|
||||||
{
|
|
||||||
if(currline_ >= stringlist_.Count()){
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
values_.clear();
|
|
||||||
std::string tmp_str = stringlist_.String(currline_);
|
|
||||||
const char *p = tmp_str.c_str();
|
|
||||||
const char *pv = p;
|
|
||||||
// printf("%s\n", p);
|
|
||||||
while(*p && p){
|
|
||||||
if(*p == ','){
|
|
||||||
if(p > pv){
|
|
||||||
values_.push_back(std::string(pv, p - pv));
|
|
||||||
// printf("%s\n", std::string(pv, p - pv).c_str());
|
|
||||||
}else{
|
|
||||||
values_.push_back("");
|
|
||||||
}
|
|
||||||
pv = p + 1;
|
|
||||||
}
|
|
||||||
p++;
|
|
||||||
}
|
|
||||||
if(p && *p == ','){
|
|
||||||
values_.push_back("");
|
|
||||||
}else if(p > pv){
|
|
||||||
values_.push_back(std::string(pv, p - pv));
|
|
||||||
}
|
|
||||||
if(columns_.size() - values_.size() == 1){
|
|
||||||
values_.push_back("");
|
|
||||||
}
|
|
||||||
if(columns_.size() > 0){
|
|
||||||
assert(values_.size() >= columns_.size());
|
|
||||||
}
|
|
||||||
currline_++;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
a8::XValue CsvReader::GetValue(const char* name)
|
|
||||||
{
|
|
||||||
std::map<std::string, int>::iterator itr = columns_.find(name);
|
|
||||||
if(itr != columns_.end()){
|
|
||||||
std::string decoestring = values_[itr->second];
|
|
||||||
a8::ReplaceString(decoestring, "\\n", "\n");
|
|
||||||
a8::ReplaceString(decoestring, ",", ",");
|
|
||||||
return a8::XValue(decoestring);
|
|
||||||
}else{
|
|
||||||
assert(false);
|
|
||||||
return a8::XValue("");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
a8::XValue CsvReader::GetValue(const std::string& name)
|
|
||||||
{
|
|
||||||
return GetValue(name.c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CsvReader::KeyExists(const std::string& name)
|
|
||||||
{
|
|
||||||
auto itr = columns_.find(name);
|
|
||||||
return itr != columns_.end();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,23 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
namespace a8
|
|
||||||
{
|
|
||||||
class CsvReader
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
CsvReader();
|
|
||||||
|
|
||||||
bool Load(const std::string& filename);
|
|
||||||
bool NextLine();
|
|
||||||
a8::XValue GetValue(const char* name);
|
|
||||||
a8::XValue GetValue(const std::string& name);
|
|
||||||
bool KeyExists(const std::string& name);
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::map<std::string, int> columns_;
|
|
||||||
std::vector<std::string> values_;
|
|
||||||
|
|
||||||
int currline_ = 0;
|
|
||||||
a8::StringList stringlist_;
|
|
||||||
};
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user