From a705b0223664f915d8bb00d4f89e05838da3e4bc Mon Sep 17 00:00:00 2001 From: Spartan322 Date: Sun, 3 Sep 2023 17:54:18 -0400 Subject: Add Windows-1252 CSV Parser Fix CsvGrammar counting behavior Add csv parse argument to headless/main.cpp Change LineObject contained value type to pair Add ostream print to LineObject Add vscode tasks.json Add vscode launch.json Move csv::Parser template class specialization to end of Parser.cpp Add detail/ClassExport.hpp to assist compiler export --- src/headless/main.cpp | 84 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 80 insertions(+), 4 deletions(-) (limited to 'src/headless') diff --git a/src/headless/main.cpp b/src/headless/main.cpp index 94d6b8d..c3ca8c2 100644 --- a/src/headless/main.cpp +++ b/src/headless/main.cpp @@ -1,17 +1,71 @@ +#include +#include #include +#include #include +#include #include +#include #include +#include +#include +#include +#include #include -int main(int argc, char** argv) { - if (argc < 2) { - std::fprintf(stderr, "usage: %s ", argv[0]); +std::string_view trim(std::string_view str) { + std::string_view::iterator begin = str.begin(); + std::string_view::iterator end = str.end(); + for (;; begin++) { + if (begin == end) return std::string_view(); + if (!std::isspace(*begin)) break; + } + end--; + for (;; end--) { + if (end == begin) return std::string_view(); + if (!std::isspace(*end)) break; + } + return std::string_view(&*begin, std::distance(begin, end)); +} + +bool insenitive_trim_eq(std::string_view lhs, std::string_view rhs) { + lhs = trim(lhs); + rhs = trim(rhs); + return std::equal( + lhs.begin(), lhs.end(), + rhs.begin(), rhs.end(), + [](char a, char b) { return std::tolower(a) == std::tolower(b); }); +} + +template +int print_csv(const std::string_view path) { + auto parser = ovdl::csv::Parser::from_file(path); + if (parser.has_error()) { return 1; } - auto parser = ovdl::v2script::Parser::from_file(argv[1]); + parser.parse_csv(); + if (parser.has_error()) { + return 2; + } + + if (parser.has_warning()) { + for (auto& warning : parser.get_warnings()) { + std::cerr << "Warning: " << warning.message << std::endl; + } + } + + std::cout << "lines:\t\t" << parser.get_lines().size() << std::endl; + for (const auto& line : parser.get_lines()) { + std::cout << "line size:\t" << line.value_count() << std::endl; + std::cout << "values:\t\t" << line << std::endl; + } + return EXIT_SUCCESS; +} + +int print_v2script_simple(const std::string_view path) { + auto parser = ovdl::v2script::Parser::from_file(path); if (parser.has_error()) { return 1; } @@ -28,6 +82,28 @@ int main(int argc, char** argv) { } std::cout << parser.get_file_node() << std::endl; + return EXIT_SUCCESS; +} + +int main(int argc, char** argv) { + switch (argc) { + case 2: + return print_v2script_simple(argv[1]); + case 4: + if (insenitive_trim_eq(argv[1], "csv") && insenitive_trim_eq(argv[2], "utf")) + return print_csv(argv[3]); + goto default_jump; + case 3: + if (insenitive_trim_eq(argv[1], "csv")) + return print_csv(argv[2]); + [[fallthrough]]; + default: + default_jump: + std::fprintf(stderr, "usage: %s \n", argv[0]); + std::fprintf(stderr, "usage: %s csv \n", argv[0]); + std::fprintf(stderr, "usage: %s csv utf ", argv[0]); + return EXIT_FAILURE; + } return 0; } \ No newline at end of file -- cgit v1.2.3-56-ga3b1