Loading...
Searching...
No Matches
parse_delimited.h
1//
2// Created by henri on 03/11/22.
3//
4
5#ifndef IDOL_PARSE_DELIMITED_H
6#define IDOL_PARSE_DELIMITED_H
7
8#include <fstream>
9#include <sstream>
10#include <vector>
11
12namespace idol {
13
14 std::vector<std::vector<std::string>> parse_delimited(const std::string &t_filename, char t_delimiter) {
15 std::vector<std::vector<std::string>> result;
16
17 std::ifstream file(t_filename);
18
19 if (!file.is_open()) {
20 throw std::runtime_error("Could not open file.");
21 }
22
23 while (file) {
24 std::string line;
25 if (!getline(file, line)) { break; }
26
27 std::istringstream line_stream(line);
28
29 std::vector<std::string> line_cells;
30 while (line_stream) {
31 std::string cell_content;
32 if (!getline(line_stream, cell_content, t_delimiter)) break;
33 line_cells.push_back(cell_content);
34 }
35
36 result.emplace_back(std::move(line_cells));
37 }
38
39 return result;
40 }
41
42}
43
44#endif //IDOL_PARSE_DELIMITED_H