From 5474ee1a3143f2a194b55a37f14214456ca4fdc4 Mon Sep 17 00:00:00 2001 From: deeaitch Date: Fri, 28 Aug 2026 00:25:41 -0400 Subject: [PATCH] Select converter format through type factory --- typefactory/convertlatex.cpp | 2 +- typefactory/convertlatex.h | 2 +- typefactory/main.cpp | 45 +++++++++++++++++++++++++++++++----- 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/typefactory/convertlatex.cpp b/typefactory/convertlatex.cpp index 70e1571..bcab56e 100644 --- a/typefactory/convertlatex.cpp +++ b/typefactory/convertlatex.cpp @@ -48,7 +48,7 @@ static std::string latex_escape (const std::string &s) { return out; } -bool ConvertLatex::save (const std::string &output_file_name) { +bool ConverterLatex::save (const std::string &output_file_name) { if (!parse()) return false; diff --git a/typefactory/convertlatex.h b/typefactory/convertlatex.h index 0086da7..0b53ed7 100644 --- a/typefactory/convertlatex.h +++ b/typefactory/convertlatex.h @@ -3,7 +3,7 @@ #include "converter.h" -class ConvertLatex final : public Converter { +class ConverterLatex final : public Converter { public: using Converter::Converter; diff --git a/typefactory/main.cpp b/typefactory/main.cpp index aed6dc0..1fa140f 100644 --- a/typefactory/main.cpp +++ b/typefactory/main.cpp @@ -1,29 +1,52 @@ +#include #include #include #include #include "convertlatex.h" +#include "typefactory.h" + +using ConverterFactory = TypeFactory; + +static constexpr std::array kSupportedTypes = { + "latex" +}; struct CliArgs { std::string input; std::string output; + std::string type; }; static void print_usage (const char *argv0) { std::cerr << "Usage: " << argv0 - << " --input --output \n"; + << " --input --output --type \n"; +} + +static void print_supported_types() { + std::cerr << "Supported types:"; + for (const char *type : kSupportedTypes) + std::cerr << ' ' << type; + std::cerr << '\n'; +} + +static ConverterFactory build_converter_factory() { + ConverterFactory factory; + factory.registerType ("latex"); + return factory; } static bool parse_args (int argc, char **argv, CliArgs &out) { static option long_opts[] = { {"input", required_argument, nullptr, 'i'}, {"output", required_argument, nullptr, 'o'}, + {"type", required_argument, nullptr, 't'}, {"help", no_argument, nullptr, 'h'}, {nullptr, 0, nullptr, 0 } }; int c = 0; - while ((c = ::getopt_long (argc, argv, "i:o:h", long_opts, nullptr)) != -1) { + while ((c = ::getopt_long (argc, argv, "i:o:t:h", long_opts, nullptr)) != -1) { switch (c) { case 'i': out.input = optarg; @@ -31,6 +54,9 @@ static bool parse_args (int argc, char **argv, CliArgs &out) { case 'o': out.output = optarg; break; + case 't': + out.type = optarg; + break; case 'h': print_usage (argv[0]); return false; @@ -40,7 +66,7 @@ static bool parse_args (int argc, char **argv, CliArgs &out) { } } - if (out.input.empty() || out.output.empty()) { + if (out.input.empty() || out.output.empty() || out.type.empty()) { print_usage (argv[0]); return false; } @@ -53,9 +79,16 @@ int main (int argc, char **argv) { if (!parse_args (argc, argv, args)) return 1; - ConvertLatex converter (args.input); - if (!converter.save (args.output)) - return converter.error_code(); + const ConverterFactory factory = build_converter_factory(); + if (!factory.contains (args.type)) { + std::cerr << "Unsupported output type: " << args.type << '\n'; + print_supported_types(); + return 1; + } + + const auto converter = factory.create (args.type, args.input); + if (!converter->save (args.output)) + return converter->error_code(); return 0; }