From 23e31749e9c6b71effefd7577213a81b78a45364 Mon Sep 17 00:00:00 2001 From: Martin Michelsen Date: Sun, 30 Nov 2025 08:50:48 -0800 Subject: [PATCH] add transcode-text action --- src/Main.cc | 49 ++++++++++++++++++++++++++++++++------- tests/custom-sjis.test.sh | 4 ++-- 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/src/Main.cc b/src/Main.cc index c1f1a487..4dae81b8 100644 --- a/src/Main.cc +++ b/src/Main.cc @@ -1884,16 +1884,47 @@ Action a_extract_ppk("extract-ppk", "\ a_extract_archive_fn); Action a_encode_sjis( - "encode-sjis", nullptr, +[](phosg::Arguments& args) { + "transcode-text", nullptr, +[](phosg::Arguments& args) { + TextTranscoder* tt_from = nullptr; + { + std::string from_name = args.get("from"); + if (from_name == "8859" || from_name == "iso8859") { + tt_from = &tt_8859_to_utf8; + } else if (from_name == "sjis") { + tt_from = &tt_sega_sjis_to_utf8; + } else if (from_name == "utf16") { + tt_from = &tt_utf16_to_utf8; + } else if (from_name == "ascii") { + tt_from = &tt_ascii_to_utf8; + } else if (from_name != "utf8") { + throw std::invalid_argument("invalid value for --from: " + from_name); + } + } + + TextTranscoder* tt_to = nullptr; + { + std::string to_name = args.get("to"); + if (to_name == "8859" || to_name == "iso8859") { + tt_to = &tt_utf8_to_8859; + } else if (to_name == "sjis") { + tt_to = &tt_utf8_to_sega_sjis; + } else if (to_name == "utf16") { + tt_to = &tt_utf8_to_utf16; + } else if (to_name == "ascii") { + tt_to = &tt_utf8_to_ascii; + } else if (to_name != "utf8") { + throw std::invalid_argument("invalid value for --to: " + to_name); + } + } + string data = read_input_data(args); - string result = tt_utf8_to_sega_sjis(data); - write_output_data(args, result.data(), result.size(), "txt"); - }); -Action a_decode_sjis( - "decode-sjis", nullptr, +[](phosg::Arguments& args) { - string data = read_input_data(args); - string result = tt_sega_sjis_to_utf8(data); - write_output_data(args, result.data(), result.size(), "txt"); + if (tt_from) { + data = (*tt_from)(data); + } + if (tt_to) { + data = (*tt_to)(data); + } + write_output_data(args, data.data(), data.size(), "txt"); }); Action a_decode_text_archive( diff --git a/tests/custom-sjis.test.sh b/tests/custom-sjis.test.sh index 87c5cac5..4cc409da 100755 --- a/tests/custom-sjis.test.sh +++ b/tests/custom-sjis.test.sh @@ -8,9 +8,9 @@ if [ -z "$EXECUTABLE" ]; then fi echo "... decode-sjis" -$EXECUTABLE decode-sjis tests/custom-sjis.txt tests/custom-sjis.utf8.txt +$EXECUTABLE transcode-text --from=sjis --to=utf8 tests/custom-sjis.txt tests/custom-sjis.utf8.txt echo "... encode-sjis" -$EXECUTABLE encode-sjis tests/custom-sjis.utf8.txt tests/custom-sjis.recoded.txt +$EXECUTABLE transcode-text --from=utf8 --to=sjis tests/custom-sjis.utf8.txt tests/custom-sjis.recoded.txt diff tests/custom-sjis.txt tests/custom-sjis.recoded.txt