Select converter format through type factory

This commit is contained in:
deeaitch committed 2026-08-28 00:25:41 -04:00
1 parent 0f4da8e695
commit 5474ee1a31
3 files changed
+41 -8

No files matched your search

+1 -1
View File
@@ -48,7 +48,7 @@ static std::string latex_escape (const std::string &s) {
return out; return out;
} }
bool ConvertLatex::save (const std::string &output_file_name) { bool ConverterLatex::save (const std::string &output_file_name) {
if (!parse()) if (!parse())
return false; return false;
+1 -1
View File
@@ -3,7 +3,7 @@
#include "converter.h" #include "converter.h"
class ConvertLatex final : public Converter { class ConverterLatex final : public Converter {
public: public:
using Converter::Converter; using Converter::Converter;
+39 -6
View File
@@ -1,29 +1,52 @@
#include <array>
#include <getopt.h> #include <getopt.h>
#include <iostream> #include <iostream>
#include <string> #include <string>
#include "convertlatex.h" #include "convertlatex.h"
#include "typefactory.h"
using ConverterFactory = TypeFactory<std::string, Converter, std::string>;
static constexpr std::array<const char *, 1> kSupportedTypes = {
"latex"
};
struct CliArgs { struct CliArgs {
std::string input; std::string input;
std::string output; std::string output;
std::string type;
}; };
static void print_usage (const char *argv0) { static void print_usage (const char *argv0) {
std::cerr << "Usage: " << argv0 std::cerr << "Usage: " << argv0
<< " --input <file> --output <file>\n"; << " --input <file> --output <file> --type <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<ConverterLatex> ("latex");
return factory;
} }
static bool parse_args (int argc, char **argv, CliArgs &out) { static bool parse_args (int argc, char **argv, CliArgs &out) {
static option long_opts[] = { static option long_opts[] = {
{"input", required_argument, nullptr, 'i'}, {"input", required_argument, nullptr, 'i'},
{"output", required_argument, nullptr, 'o'}, {"output", required_argument, nullptr, 'o'},
{"type", required_argument, nullptr, 't'},
{"help", no_argument, nullptr, 'h'}, {"help", no_argument, nullptr, 'h'},
{nullptr, 0, nullptr, 0 } {nullptr, 0, nullptr, 0 }
}; };
int c = 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) { switch (c) {
case 'i': case 'i':
out.input = optarg; out.input = optarg;
@@ -31,6 +54,9 @@ static bool parse_args (int argc, char **argv, CliArgs &out) {
case 'o': case 'o':
out.output = optarg; out.output = optarg;
break; break;
case 't':
out.type = optarg;
break;
case 'h': case 'h':
print_usage (argv[0]); print_usage (argv[0]);
return false; 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]); print_usage (argv[0]);
return false; return false;
} }
@@ -53,9 +79,16 @@ int main (int argc, char **argv) {
if (!parse_args (argc, argv, args)) if (!parse_args (argc, argv, args))
return 1; return 1;
ConvertLatex converter (args.input); const ConverterFactory factory = build_converter_factory();
if (!converter.save (args.output)) if (!factory.contains (args.type)) {
return converter.error_code(); 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; return 0;
} }