diff --git a/aegisub/src/subs_controller.cpp b/aegisub/src/subs_controller.cpp index d4ca914ed..fd35d0070 100644 --- a/aegisub/src/subs_controller.cpp +++ b/aegisub/src/subs_controller.cpp @@ -83,9 +83,6 @@ SubsController::SubsController(agi::Context *context) } void SubsController::Load(agi::fs::path const& filename, std::string charset) { - // TextFileReader does this automatically, but relying on that results in - // the user being prompted twice if it can't be auto-detected, since we - // open the file twice below. try { if (charset.empty()) charset = CharSetDetect::GetEncoding(filename); diff --git a/aegisub/src/subtitle_format_microdvd.cpp b/aegisub/src/subtitle_format_microdvd.cpp index bf2756bfe..6f9c13b46 100644 --- a/aegisub/src/subtitle_format_microdvd.cpp +++ b/aegisub/src/subtitle_format_microdvd.cpp @@ -39,6 +39,7 @@ #include "ass_dialogue.h" #include "ass_file.h" #include "ass_time.h" +#include "charset_detect.h" #include "text_file_reader.h" #include "text_file_writer.h" #include "video_context.h" @@ -74,7 +75,10 @@ bool MicroDVDSubtitleFormat::CanReadFile(agi::fs::path const& filename) const { if (!agi::fs::HasExtension(filename, "sub")) return false; // Since there is an infinity of .sub formats, load first line and check if it's valid - TextFileReader file(filename); + auto encoding = CharSetDetect::GetEncoding(filename); + if (encoding == "binary") return false; + + TextFileReader file(filename, encoding); if (file.HasMoreLines()) return regex_match(file.ReadLineFromFile(), line_regex); diff --git a/aegisub/src/text_file_reader.cpp b/aegisub/src/text_file_reader.cpp index 5b5d61e5c..a70d59e5f 100644 --- a/aegisub/src/text_file_reader.cpp +++ b/aegisub/src/text_file_reader.cpp @@ -23,8 +23,6 @@ #include "text_file_reader.h" -#include "charset_detect.h" - #include #include @@ -32,12 +30,10 @@ #include TextFileReader::TextFileReader(agi::fs::path const& filename, std::string encoding, bool trim) -: trim(trim) +: file(agi::io::Open(filename, true)) +, trim(trim) +, iter(agi::line_iterator(*file, encoding)) { - if (encoding.empty()) - encoding = CharSetDetect::GetEncoding(filename); - file.reset(agi::io::Open(filename, true)); - iter = agi::line_iterator(*file, encoding); } TextFileReader::~TextFileReader() { diff --git a/aegisub/src/text_file_reader.h b/aegisub/src/text_file_reader.h index 9b54cf3cf..2a34c86cb 100644 --- a/aegisub/src/text_file_reader.h +++ b/aegisub/src/text_file_reader.h @@ -43,7 +43,7 @@ public: /// @param filename File to open /// @param enc Encoding to use, or empty to autodetect /// @param trim Whether to trim whitespace from lines read - TextFileReader(agi::fs::path const& filename, std::string encoding="", bool trim=true); + TextFileReader(agi::fs::path const& filename, std::string encoding, bool trim=true); /// @brief Destructor ~TextFileReader();