Q3D-Calibration/include/csvReader.h

73 lines
1.6 KiB
C++

#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_; }
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