Pass the detected or chosen charset to SubtitleFormat::CanReadFile

The MicroDVD format needs it since it also probes the file contents.
This commit is contained in:
Thomas Goyne 2013-10-05 06:58:15 -07:00
parent 73b0f7c88c
commit 6f79d8731f
6 changed files with 9 additions and 13 deletions

View File

@ -583,7 +583,7 @@ void DialogStyleManager::OnCurrentImport() {
AssFile temp; AssFile temp;
try { try {
auto reader = SubtitleFormat::GetReader(filename); auto reader = SubtitleFormat::GetReader(filename, charset);
if (!reader) if (!reader)
wxMessageBox("Unsupported subtitle format", "Error", wxOK | wxICON_ERROR | wxCENTER, this); wxMessageBox("Unsupported subtitle format", "Error", wxOK | wxICON_ERROR | wxCENTER, this);
else else

View File

@ -107,7 +107,7 @@ void SubsController::Load(agi::fs::path const& filename, std::string charset) {
} }
} }
const SubtitleFormat *reader = SubtitleFormat::GetReader(filename); const SubtitleFormat *reader = SubtitleFormat::GetReader(filename, charset);
try { try {
AssFile temp; AssFile temp;

View File

@ -76,7 +76,7 @@ SubtitleFormat::~SubtitleFormat() {
formats.erase(remove(begin(formats), end(formats), this)); formats.erase(remove(begin(formats), end(formats), this));
} }
bool SubtitleFormat::CanReadFile(agi::fs::path const& filename) const { bool SubtitleFormat::CanReadFile(agi::fs::path const& filename, std::string const&) const {
auto wildcards = GetReadWildcards(); auto wildcards = GetReadWildcards();
return any_of(begin(wildcards), end(wildcards), return any_of(begin(wildcards), end(wildcards),
[&](std::string const& ext) { return agi::fs::HasExtension(filename, ext); }); [&](std::string const& ext) { return agi::fs::HasExtension(filename, ext); });
@ -335,9 +335,9 @@ SubtitleFormat *find_or_throw(Cont &container, Pred pred) {
return *it; return *it;
} }
const SubtitleFormat *SubtitleFormat::GetReader(agi::fs::path const& filename) { const SubtitleFormat *SubtitleFormat::GetReader(agi::fs::path const& filename, std::string const& encoding) {
LoadFormats(); LoadFormats();
return find_or_throw(formats, std::bind(&SubtitleFormat::CanReadFile, _1, filename)); return find_or_throw(formats, std::bind(&SubtitleFormat::CanReadFile, _1, filename, encoding));
} }
const SubtitleFormat *SubtitleFormat::GetWriter(agi::fs::path const& filename) { const SubtitleFormat *SubtitleFormat::GetWriter(agi::fs::path const& filename) {

View File

@ -93,7 +93,7 @@ public:
/// ///
/// Default implementation simply checks if the file's extension is in the /// Default implementation simply checks if the file's extension is in the
/// format's wildcard list /// format's wildcard list
virtual bool CanReadFile(agi::fs::path const& filename) const; virtual bool CanReadFile(agi::fs::path const& filename, std::string const& encoding) const;
/// @brief Check if the given file can be written by this format /// @brief Check if the given file can be written by this format
/// ///
@ -124,7 +124,7 @@ public:
static std::string GetWildcards(int mode); static std::string GetWildcards(int mode);
/// Get a subtitle format that can read the given file or nullptr if none can /// Get a subtitle format that can read the given file or nullptr if none can
static const SubtitleFormat *GetReader(agi::fs::path const& filename); static const SubtitleFormat *GetReader(agi::fs::path const& filename, std::string const& encoding);
/// Get a subtitle format that can write the given file or nullptr if none can /// Get a subtitle format that can write the given file or nullptr if none can
static const SubtitleFormat *GetWriter(agi::fs::path const& filename); static const SubtitleFormat *GetWriter(agi::fs::path const& filename);
/// Initialize subtitle formats /// Initialize subtitle formats

View File

@ -39,7 +39,6 @@
#include "ass_dialogue.h" #include "ass_dialogue.h"
#include "ass_file.h" #include "ass_file.h"
#include "ass_time.h" #include "ass_time.h"
#include "charset_detect.h"
#include "text_file_reader.h" #include "text_file_reader.h"
#include "text_file_writer.h" #include "text_file_writer.h"
#include "video_context.h" #include "video_context.h"
@ -70,14 +69,11 @@ std::vector<std::string> MicroDVDSubtitleFormat::GetWriteWildcards() const {
static const boost::regex line_regex("^[\\{\\[]([0-9]+)[\\}\\]][\\{\\[]([0-9]+)[\\}\\]](.*)$"); static const boost::regex line_regex("^[\\{\\[]([0-9]+)[\\}\\]][\\{\\[]([0-9]+)[\\}\\]](.*)$");
bool MicroDVDSubtitleFormat::CanReadFile(agi::fs::path const& filename) const { bool MicroDVDSubtitleFormat::CanReadFile(agi::fs::path const& filename, std::string const& encoding) const {
// Return false immediately if extension is wrong // Return false immediately if extension is wrong
if (!agi::fs::HasExtension(filename, "sub")) return false; 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 // Since there is an infinity of .sub formats, load first line and check if it's valid
auto encoding = CharSetDetect::GetEncoding(filename);
if (encoding == "binary") return false;
TextFileReader file(filename, encoding); TextFileReader file(filename, encoding);
if (file.HasMoreLines()) if (file.HasMoreLines())
return regex_match(file.ReadLineFromFile(), line_regex); return regex_match(file.ReadLineFromFile(), line_regex);

View File

@ -41,7 +41,7 @@ public:
std::vector<std::string> GetReadWildcards() const override; std::vector<std::string> GetReadWildcards() const override;
std::vector<std::string> GetWriteWildcards() const override; std::vector<std::string> GetWriteWildcards() const override;
bool CanReadFile(agi::fs::path const& filename) const override; bool CanReadFile(agi::fs::path const& filename, std::string const& encoding) const override;
void ReadFile(AssFile *target, agi::fs::path const& filename, std::string const& forceEncoding) const override; void ReadFile(AssFile *target, agi::fs::path const& filename, std::string const& forceEncoding) const override;
void WriteFile(const AssFile *src, agi::fs::path const& filename, std::string const& encoding) const override; void WriteFile(const AssFile *src, agi::fs::path const& filename, std::string const& encoding) const override;