2026-05-09 11:23:45 +08:00
|
|
|
#pragma once
|
|
|
|
|
#include <fstream>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <sstream>
|
2026-05-13 15:46:06 +08:00
|
|
|
#include <stdexcept>
|
2026-05-09 11:23:45 +08:00
|
|
|
#include <string>
|
|
|
|
|
#include <type_traits>
|
|
|
|
|
#include <vector>
|
|
|
|
|
namespace ReadCSV {
|
|
|
|
|
using std::string;
|
|
|
|
|
using std::vector;
|
|
|
|
|
const string dir_root = "/users/dsc/code/TestProject/data_files";
|
|
|
|
|
template <typename T>
|
|
|
|
|
struct ReadCSV {
|
|
|
|
|
vector<vector<T>> operator()(string file_name) {
|
|
|
|
|
vector<vector<T>> res;
|
|
|
|
|
string file_dir = dir_root + "/" + file_name;
|
|
|
|
|
std::ifstream infile(file_dir, std::ios::in);
|
|
|
|
|
string line;
|
|
|
|
|
string word;
|
|
|
|
|
using std::is_same_v;
|
|
|
|
|
if (!infile.is_open()) {
|
2026-05-13 15:46:06 +08:00
|
|
|
throw std::runtime_error("Cannot open CSV file: " + file_dir);
|
2026-05-09 11:23:45 +08:00
|
|
|
}
|
|
|
|
|
while (std::getline(infile, line)) {
|
|
|
|
|
std::istringstream sin;
|
|
|
|
|
sin.str(line);
|
|
|
|
|
vector<T> line_data;
|
|
|
|
|
while (std::getline(sin, word, ',')) {
|
|
|
|
|
if constexpr (is_same_v<T, double>) {
|
|
|
|
|
line_data.push_back(std::stod(word));
|
|
|
|
|
} else if constexpr (is_same_v<T, int>) {
|
|
|
|
|
line_data.push_back(std::stoi(word));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
res.push_back(line_data);
|
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
using ReadCSVF = ReadCSV<double>;
|
|
|
|
|
using ReadCSVI = ReadCSV<int>;
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
|
struct FlowData {
|
|
|
|
|
ReadCSVF readf;
|
|
|
|
|
ReadCSVI readi;
|
|
|
|
|
vector<vector<T>> data;
|
|
|
|
|
int length;
|
|
|
|
|
int cols;
|
|
|
|
|
FlowData(string file_name = "D102-1#酸槽数据.csv") {
|
2026-05-13 16:02:27 +08:00
|
|
|
if constexpr (std::is_same_v<T, double>) {
|
2026-05-09 11:23:45 +08:00
|
|
|
data = readf(file_name);
|
2026-05-13 16:02:27 +08:00
|
|
|
} else if constexpr (std::is_same_v<T, int>) {
|
2026-05-09 11:23:45 +08:00
|
|
|
data = readi(file_name);
|
|
|
|
|
}
|
|
|
|
|
length = data.size();
|
|
|
|
|
cols = data[0].size();
|
|
|
|
|
}
|
|
|
|
|
double operator()(int index, int col) {
|
|
|
|
|
return data[index % length][col % cols];
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
using DoubleData = FlowData<double>;
|
|
|
|
|
using IntData = FlowData<int>;
|
|
|
|
|
}; // namespace ReadCSV
|