|
2 | 2 | #include <CLI/CLI.hpp>
|
3 | 3 | #include <iostream>
|
4 | 4 | #include <fstream>
|
5 |
| -#include "csv.hpp" |
6 | 5 | #include <vector>
|
7 | 6 | #include <string>
|
8 | 7 |
|
9 |
| - |
| 8 | +// dumb and simple way to parse CSV |
10 | 9 | std::vector<ChatMessage> parseCSV(const std::string &filename, int timeMultiplier) {
|
11 | 10 | std::vector<ChatMessage> messages;
|
12 |
| - try { |
13 |
| - csv::CSVReader reader(filename); |
14 |
| - for (auto &row: reader) { |
15 |
| - ChatMessage msg; |
16 |
| - msg.time = row["time"].get<int>() * timeMultiplier; |
17 |
| - |
18 |
| - msg.user.name = row["user_name"].get<>(); |
19 |
| - |
20 |
| - std::string col = row["user_color"].get<>(); |
21 |
| - if (col.empty()) { |
22 |
| - msg.user.color = getRandomColor(msg.user.name); |
23 |
| - } else { |
24 |
| - msg.user.color = Color(col); |
25 |
| - } |
26 |
| - |
27 |
| - msg.message = row["message"].get<>(); |
28 |
| - messages.push_back(std::move(msg)); |
29 |
| - } |
30 |
| - } catch (const std::exception &ex) { |
31 |
| - std::cerr << "Error parsing CSV \"" << filename << "\": " |
32 |
| - << ex.what() << "\n"; |
| 11 | + std::ifstream file(filename); |
| 12 | + std::string line; |
| 13 | + |
| 14 | + if (!file.is_open()) { |
| 15 | + std::cerr << "Error: Could not open file " << filename << "\n"; |
33 | 16 | std::exit(-1);
|
34 | 17 | }
|
| 18 | + |
| 19 | + std::getline(file, line); |
| 20 | + if (line != "time,user_name,user_color,message") { |
| 21 | + std::cerr << "Error: Unexpected CSV header format.\n"; |
| 22 | + std::exit(-1); |
| 23 | + } |
| 24 | + |
| 25 | + while (std::getline(file, line)) { |
| 26 | + std::stringstream ss(line); |
| 27 | + std::string field; |
| 28 | + ChatMessage msg; |
| 29 | + |
| 30 | + std::getline(ss, field, ','); |
| 31 | + msg.time = std::stoi(field) * timeMultiplier; |
| 32 | + |
| 33 | + std::getline(ss, msg.user.name, ','); |
| 34 | + |
| 35 | + std::getline(ss, field, ','); |
| 36 | + msg.user.color = field.empty() ? getRandomColor(msg.user.name) : Color(field); |
| 37 | + |
| 38 | + std::getline(ss, msg.message); |
| 39 | + |
| 40 | + if (msg.message.size() >= 2 && |
| 41 | + msg.message.front() == '"' && |
| 42 | + msg.message.back() == '"') { |
| 43 | + msg.message = msg.message.substr(1, msg.message.size() - 2); |
| 44 | + } |
| 45 | + |
| 46 | + messages.emplace_back(std::move(msg)); |
| 47 | + } |
| 48 | + |
35 | 49 | return messages;
|
36 | 50 | }
|
37 | 51 |
|
| 52 | + |
38 | 53 | int main(int argc, char *argv[]) {
|
39 | 54 | CLI::App app{"Chat → YTT/SRV3 subtitle generator"};
|
40 | 55 |
|
|
0 commit comments