Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //csv.cpp
- #include "csv.h"
- #include <stdio.h>
- static void _split(std::string str, std::string d, std::vector<std::string> &dest)
- {
- int cut;
- while((cut = str.find_first_of(d)) != str.npos)
- {
- if(cut > 0)
- dest.push_back(str.substr(0, cut));
- str = str.substr(cut + 1);
- }
- if(str.length() > 0)
- dest.push_back(str);
- }
- void CSV::populate()
- {
- std::ifstream ifs(fname);
- //Get column names.
- std::string line;
- if(ifs.is_open() && ifs.good())
- std::getline(ifs, line);
- //Split it into col_names.
- _split(line, ";", col_names);
- //Read rows and split them.
- int i;
- for(i = 0; ifs.good(); i++)
- {
- getline(ifs, line);
- std::vector<std::string> row;
- _split(line, ";", row);
- rows.push_back(row);
- }
- }
- CSV::CSV(std::string _fname)
- : changed(false)
- {
- fname = _fname;
- //Populate buffer.
- populate();
- }
- CSV::~CSV()
- {
- //Write buffer back.
- writeback();
- }
- void CSV::writeback()
- {
- //Only do a writeback if the data changed.
- if(changed == false)
- return;
- std::ofstream ofs(fname);
- int i, j;
- //Write column names.
- for(i = 0; i < col_names.size(); i++)
- {
- ofs << col_names[i];
- if(i < col_names.size() - 1)
- ofs << ";";
- }
- ofs << std::endl;
- //Write rows.
- for(i = 0; i < rows.size(); i++)
- {
- //Write row.
- for(j = 0; j < rows[i].size(); j++)
- {
- ofs << rows[i][j];
- if(j < rows[i].size() - 1)
- ofs << ";";
- }
- if(i < rows.size() - 1)
- ofs << std::endl;
- }
- }
- std::vector<std::string> &CSV::getColNames()
- {
- return col_names;
- }
- std::vector<std::vector<std::string>> &CSV::getRows()
- {
- return rows;
- }
- bool CSV::setCell(int row, int col, std::string value)
- {
- //Test if row and col are in range.
- if(row > rows.size() - 1)
- return false;
- if(col > rows[row].size() - 1)
- return false;
- //Set value.
- rows[row][col] = value;
- changed = true;
- return true;
- }
- std::string CSV::getCell(int row, int col)
- {
- //Test if row and col are in range.
- if(row > rows.size() - 1)
- return "";
- if(col > rows[row].size() - 1)
- return "";
- return rows[row][col];
- }
- bool CSV::deleteRow(int row)
- {
- //Test if row is in range.
- if(row > rows.size() - 1)
- return false;
- //Erase it.
- rows.erase(rows.begin() + row);
- return true;
- }
- bool CSV::isChanged()
- {
- return changed;
- }
- //csv.h
- #pragma once
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <vector>
- class CSV
- {
- private:
- //Filename.
- std::string fname;
- //Column names.
- std::vector<std::string> col_names;
- //Row buffers.
- std::vector<std::vector<std::string>> rows;
- //If this is true, the data has changed.
- bool changed;
- //Populate buffer.
- void populate();
- public:
- CSV(std::string _fname);
- ~CSV();
- //Write buffer back to file.
- void writeback();
- //Get column names.
- std::vector<std::string> &getColNames();
- //Get rows.
- std::vector<std::vector<std::string>> &getRows();
- //Set cell value.
- bool setCell(int row, int col, std::string value);
- //Get cell value.
- std::string getCell(int row, int col);
- //Delete row.
- bool deleteRow(int row);
- //Get changed status.
- bool isChanged();
- };
Add Comment
Please, Sign In to add comment