Q3D-Calibration/include/csvReader.h

75 lines
1.6 KiB
C
Raw Normal View History

#pragma once
#ifndef csv_reader_h
#define csv_reader_h
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using std::string;
using std::vector;
class csvReader {
public:
csvReader(string);
~csvReader();
public:
string file;
vector<vector<string>> strArray;
public:
int rows();
void readData();
string& operator()(int i, int j) const;
};
csvReader::csvReader(string file_) { file = file_; }
csvReader::~csvReader() {}
int csvReader::rows() { return strArray.size(); }
void csvReader::readData() {
std::ifstream inFile(file);
string lineStr;
while (std::getline(inFile, lineStr)) {
std::stringstream ss(lineStr);
string str;
vector<string> lineArray;
while (std::getline(ss, str, ',')) lineArray.push_back(str);
strArray.push_back(lineArray);
}
}
string& csvReader::operator()(int i, int j) const {
vector<string> lineArray;
int n = strArray.size(), m;
if (i < 0 || i > n - 1) std::cout << "Row Index Out Of Bounds " << std::endl;
lineArray = strArray.at(i);
m = lineArray.size();
if (j < 0 || j > m - 1) std::cout << "Column Index Out Of Bounds " << std::endl;
return lineArray.at(j);
}
std::ostream& operator<<(std::ostream& cout, csvReader& cR) {
vector<string> lineArray;
int n = cR.strArray.size(), m;
for (int i = 0; i < n; i++) {
lineArray = cR.strArray.at(i);
m = lineArray.size();
for (int j = 0; j < m; j++) cout << lineArray.at(j) << " ";
if (i < n - 1) cout << std::endl;
}
return cout;
}
#endif