#ifndef CSVFILE_HPP_
#define CSVFILE_HPP_
// #include <standard C library headers>
// #include <standard C++ library headers>
#include <string>
#include <vector>
#include <deque>
#include <fstream>
#include <stdexcept>
// #include <other library headers>
// #include "customer headers"
#include "string.hpp"
class CsvFile
{
public:
CsvFile(const std::string& path = "")
{ Clear(); if (!path.empty()) { Read(path); } }
~CsvFile() {}
private:
CsvFile(const CsvFile& rhs);
CsvFile& operator=(const CsvFile& rhs);
public:
void Read(const std::string& path);
void Save() { Save(mFilepath); }
void Save(const std::string& path);
void Clear() { mData.clear(); mColCount = 0; mFilepath = ""; }
int GetRowCount() const { return mData.size(); }
int GetColumnCount() const { return mColCount; }
void SetColumnCount(const int col) { mColCount = col; }
const std::string& GetData(int row, int col) { return GetRow(row).at(col); }
const std::vector<std::string>& GetData(int row) { return GetRow(row); }
void SetData(int row, int col, const std::string& data)
{ GetRow(row).at(col) = data; }
void SetData(int row, const std::vector<std::string>& data)
{ GetRow(row) = data; GetRow(row); }
void AddRow(const std::vector<std::string>& data)
{ mData.push_back(data); }
protected:
private:
std::vector<std::string>& GetRow(int row);
private:
std::deque<std::vector<std::string> > mData;
int mColCount;
std::string mFilepath;
};
inline void CsvFile::Read(const std::string& path)
{
std::ifstream file(path.c_str());
if (!file)
{
throw std::domain_error("file not found!");
}
Clear();
std::string line;
while (std::getline(file, line))
{
std::vector<std::string> v = string::Split<char>(line, ",");
mData.push_back(v);
mColCount = (v.size() > mColCount) ? v.size() : mColCount;
}
mFilepath = path;
}
inline void CsvFile::Save(const std::string& path)
{
std::ofstream file(path.c_str());
if (!file)
{
throw std::domain_error("open file failed!");
}
for (unsigned int i = 0; i < mData.size(); ++i)
{
std::vector<std::string>& line = GetRow(i);
std::string str = string::Join<char>(line.begin(), line.end(), ",");
file << str << std::endl;
}
}
inline std::vector<std::string>& CsvFile::GetRow(int row)
{
std::vector<std::string>& line = mData.at(row);
while (line.size() < mColCount)
{
line.push_back("");
}
while (line.size() > mColCount)
{
line.pop_back();
}
assert(line.size() == mColCount);
return line;
}
#endif // CSVFILE_HPP_
CSV文件解析
最新推荐文章于 2022-10-11 22:48:21 发布