|
1 | 1 | #include <absl/flags/flag.h> |
2 | 2 | #include <absl/flags/parse.h> |
| 3 | +#include <absl/flags/usage.h> |
| 4 | +#include <csv/csv.h> |
| 5 | +#include <bruh/bruh.h> |
3 | 6 |
|
| 7 | +#include <fstream> |
4 | 8 | #include <iostream> |
5 | 9 |
|
6 | | -// ABSL_FLAG(int, x, 1, "x"); |
7 | | -// ABSL_FLAG(int, y, 2, "y"); |
| 10 | +ABSL_FLAG(std::string, mode, "", "Conversion mode: csv2bruh or bruh2csv"); |
| 11 | +ABSL_FLAG(std::string, schema, "", "Path to .csv schema file"); |
| 12 | +ABSL_FLAG(std::string, input, "", "Input file path"); |
| 13 | +ABSL_FLAG(std::string, output, "", "Output file path"); |
| 14 | + |
| 15 | +using namespace columnar; // NOLINT |
| 16 | + |
| 17 | +std::ifstream OpenInput(const std::string& path) { |
| 18 | + std::ifstream in(path, std::ios::binary); |
| 19 | + if (!in || !in.good()) { |
| 20 | + throw std::runtime_error("Cannot open file " + path); |
| 21 | + } |
| 22 | + return in; |
| 23 | +} |
| 24 | + |
| 25 | +std::ofstream OpenOutput(const std::string& path) { |
| 26 | + std::ofstream out(path, std::ios::binary); |
| 27 | + if (!out || !out.good()) { |
| 28 | + throw std::runtime_error("Cannot open file " + path); |
| 29 | + } |
| 30 | + return out; |
| 31 | +} |
| 32 | + |
| 33 | +template <typename Reader, typename Writer> |
| 34 | +void Convert(Reader& reader, Writer& writer) { |
| 35 | + while (auto batch = reader.ReadNext()) { |
| 36 | + writer.Write(*batch); |
| 37 | + } |
| 38 | + writer.Flush(); |
| 39 | +} |
8 | 40 |
|
9 | 41 | int main(int argc, char** argv) { |
| 42 | + absl::SetProgramUsageMessage( |
| 43 | + "Amazing converter between CSV and BruhDB formats.\n\n" |
| 44 | + "Usage:\n" |
| 45 | + " converter --mode=csv2bruh --schema=schema.csv --input=data.csv --output=data.bruhdb\n" |
| 46 | + " converter --mode=bruh2csv --input=data.bruhdb --output=data.csv [--schema=schema.csv]"); |
10 | 47 | absl::ParseCommandLine(argc, argv); |
11 | | - // int x = absl::GetFlag(FLAGS_x); |
12 | | - // int y = absl::GetFlag(FLAGS_y); |
13 | | - std::cout << "Hello" << "\n"; |
14 | | - return 0; |
| 48 | + |
| 49 | + auto mode = absl::GetFlag(FLAGS_mode); |
| 50 | + auto input = absl::GetFlag(FLAGS_input); |
| 51 | + auto output = absl::GetFlag(FLAGS_output); |
| 52 | + auto schema_path = absl::GetFlag(FLAGS_schema); |
| 53 | + if (mode.empty() || input.empty() || output.empty()) { |
| 54 | + std::cerr << absl::ProgramUsageMessage(); |
| 55 | + return 1; |
| 56 | + } |
| 57 | + |
| 58 | + try { |
| 59 | + if (mode == "csv2bruh") { |
| 60 | + if (schema_path.empty()) { |
| 61 | + throw std::runtime_error("Schema required"); |
| 62 | + } |
| 63 | + auto schema = csv::SchemaManager::ReadFromFile(schema_path); |
| 64 | + auto in = OpenInput(input); |
| 65 | + auto out = OpenOutput(output); |
| 66 | + csv::CSVBatchReader reader(in, schema, {}); |
| 67 | + bruh::BruhBatchWriter writer(out, schema); |
| 68 | + Convert(reader, writer); |
| 69 | + } else if (mode == "bruh2csv") { |
| 70 | + auto in = OpenInput(input); |
| 71 | + bruh::BruhBatchReader reader(in); |
| 72 | + if (!schema_path.empty()) { |
| 73 | + csv::SchemaManager::WriteToFile(schema_path, reader.GetSchema()); |
| 74 | + } |
| 75 | + auto out = OpenOutput(output); |
| 76 | + csv::CSVBatchWriter writer(out, {}); |
| 77 | + Convert(reader, writer); |
| 78 | + } else { |
| 79 | + throw std::runtime_error("Unknown mode"); |
| 80 | + } |
| 81 | + std::cout << "Done.\n"; |
| 82 | + return 0; |
| 83 | + } catch (const std::exception& e) { |
| 84 | + std::cerr << "Error: " << e.what() << "\n"; |
| 85 | + return 1; |
| 86 | + } catch (...) { |
| 87 | + std::cerr << "Unknown error\n"; |
| 88 | + return 1; |
| 89 | + } |
15 | 90 | } |
0 commit comments