From 2a0f9b744709d74e0825fb82167e88be5221ee01 Mon Sep 17 00:00:00 2001 From: Rodrigo Braz Monteiro <zeratul@cellosoft.com> Date: Sat, 22 Nov 2008 00:14:39 +0000 Subject: [PATCH] Fixed file/format refactoring. Originally committed to SVN as r2473. --- athenasub/src/exception.cpp | 5 +++++ athenasub/src/format_manager.cpp | 27 ++++++++++++++++++++++++++- athenasub/src/formats/format_ass.cpp | 2 ++ athenasub/src/reader.cpp | 14 +++++++++++++- athenasub/src/reader.h | 1 + athenasub/src/text_file_writer.cpp | 18 +++++++++++++----- athenasub/src/text_file_writer.h | 1 + athenasub/src/text_writer.h | 1 + athenasub/src/writer.cpp | 7 +++++++ athenasub/src/writer.h | 1 + 10 files changed, 70 insertions(+), 7 deletions(-) diff --git a/athenasub/src/exception.cpp b/athenasub/src/exception.cpp index c8d81906e..216e8c8a5 100644 --- a/athenasub/src/exception.cpp +++ b/athenasub/src/exception.cpp @@ -108,5 +108,10 @@ std::string Exception::GetExceptionMessage(int code,String message,const char *f Stack stack(str); stack.Walk(2); + // Append extra message + if (!message.IsEmpty()) { + str = str + "\nExtra message: " + message; + } + return str; } diff --git a/athenasub/src/format_manager.cpp b/athenasub/src/format_manager.cpp index 1d98b1f7e..7062d35be 100644 --- a/athenasub/src/format_manager.cpp +++ b/athenasub/src/format_manager.cpp @@ -38,6 +38,7 @@ #include "reader.h" #include "text_reader.h" #include <wx/string.h> +#include <algorithm> using namespace Athenasub; @@ -48,8 +49,15 @@ std::vector<Format> FormatManager::formats; //////////////// // Add a format -void FormatManager::AddFormat(const Format format) +void FormatManager::AddFormat(Format format) { + // Abort if there is already a format with this name + String name = format->GetName(); + for (size_t i=0;i<formats.size();i++) { + if (formats[i]->GetName() == name) return; + } + + // Add formats.push_back(format); } @@ -131,8 +139,22 @@ std::vector<Format> FormatManager::GetCompatibleFormatList(Reader &reader) std::vector<std::pair<float,Format> > results; size_t len = formats.size(); for (size_t i=0;i<len;i++) { + // Reset reader reader.Rewind(); + + // Check how certain it is that it can read the format float certainty = formats[i]->CanReadFile(reader); + + // Compare to extension + StringArray exts = formats[i]->GetReadExtensions(); + for (size_t j=0;j<exts.size();j++) { + if (reader.GetFileName().EndsWith(exts[j],false)) { + certainty *= 2.0f; + break; + } + } + + // If it thinks that it can read the format, add to list. if (certainty > 0.0f) { results.push_back(std::pair<float,Format>(certainty,formats[i])); } @@ -152,5 +174,8 @@ std::vector<Format> FormatManager::GetCompatibleFormatList(Reader &reader) for (size_t i=0;i<len;i++) { finalResults.push_back(results[i].second); } + + // Reset reader again and return results + reader.Rewind(); return finalResults; } diff --git a/athenasub/src/formats/format_ass.cpp b/athenasub/src/formats/format_ass.cpp index d8f23b79c..4f0e133ab 100644 --- a/athenasub/src/formats/format_ass.cpp +++ b/athenasub/src/formats/format_ass.cpp @@ -199,6 +199,8 @@ void FormatHandlerASS::Save(const IModel& model,Writer &file) const WriteSection(writer,section); } } + + writer->Flush(); } diff --git a/athenasub/src/reader.cpp b/athenasub/src/reader.cpp index 177799fbb..3f297fcc0 100644 --- a/athenasub/src/reader.cpp +++ b/athenasub/src/reader.cpp @@ -40,7 +40,8 @@ using namespace Athenasub; -Reader::Reader(String filename,String encoding) +Reader::Reader(String fn,String encoding) +: filename(fn) { stream = shared_ptr<wxFFileInputStream>(new wxFFileInputStream(filename.GetWxString())); text = TextReader::GetReader(*stream,encoding); @@ -51,6 +52,17 @@ shared_ptr<TextReader> Athenasub::Reader::GetTextReader() return text; } +Athenasub::String Athenasub::Reader::GetFileName() +{ + return filename; +} + +Athenasub::Reader::~Reader() +{ + text = shared_ptr<TextReader>(); + stream = shared_ptr<wxFFileInputStream>(); +} + void Reader::Rewind() { text->Rewind(); diff --git a/athenasub/src/reader.h b/athenasub/src/reader.h index 168c8151d..0579bd57b 100644 --- a/athenasub/src/reader.h +++ b/athenasub/src/reader.h @@ -52,6 +52,7 @@ namespace Athenasub { public: Reader(String filename,String encoding=""); + ~Reader(); shared_ptr<TextReader> GetTextReader(); String GetFileName(); diff --git a/athenasub/src/text_file_writer.cpp b/athenasub/src/text_file_writer.cpp index 916155ac8..58d2ca6d9 100644 --- a/athenasub/src/text_file_writer.cpp +++ b/athenasub/src/text_file_writer.cpp @@ -58,8 +58,8 @@ TextFileWriter::TextFileWriter(wxOutputStream &stream,String enc) ////////////// // Destructor TextFileWriter::~TextFileWriter() { - // Flush - if (bufferPos) file.Write(&buffer[0],(std::streamsize)bufferPos); + Flush(); + file.Close(); } @@ -96,9 +96,7 @@ void TextFileWriter::WriteLineToFile(String line,bool addLineBreak) { // Resize buffer if it won't fit if (buffer.size() < bufferPos+len) { - // Flush - file.Write(&buffer[0],(std::streamsize)bufferPos); - bufferPos = 0; + Flush(); // Resize if it still doesn't fit if (buffer.size() < len) buffer.resize(len); @@ -143,3 +141,13 @@ void TextFileWriter::SetEncoding(String enc) { } } } + + +///////// +// Flush +void TextFileWriter::Flush() { + if (bufferPos) { + file.Write(&buffer[0],(std::streamsize)bufferPos); + bufferPos = 0; + } +} diff --git a/athenasub/src/text_file_writer.h b/athenasub/src/text_file_writer.h index 30d6650a0..487583ffc 100644 --- a/athenasub/src/text_file_writer.h +++ b/athenasub/src/text_file_writer.h @@ -60,5 +60,6 @@ namespace Athenasub { ~TextFileWriter(); void WriteLineToFile(Athenasub::String line,bool addLineBreak=true); + void Flush(); }; } diff --git a/athenasub/src/text_writer.h b/athenasub/src/text_writer.h index 43dac49dd..5f5a14de1 100644 --- a/athenasub/src/text_writer.h +++ b/athenasub/src/text_writer.h @@ -47,6 +47,7 @@ namespace Athenasub { virtual ~TextWriter() {} virtual void WriteLineToFile(String line,bool addLineBreak=true) = 0; + virtual void Flush() = 0; static shared_ptr<TextWriter> GetWriter(wxOutputStream &stream,String encoding); }; diff --git a/athenasub/src/writer.cpp b/athenasub/src/writer.cpp index e3261ea8a..95dd288ff 100644 --- a/athenasub/src/writer.cpp +++ b/athenasub/src/writer.cpp @@ -47,6 +47,13 @@ Writer::Writer(String filename,String encoding) } +Writer::~Writer() +{ + text = shared_ptr<TextWriter>(); + stream = shared_ptr<wxFFileOutputStream>(); +} + + shared_ptr<TextWriter> Writer::GetTextWriter() { return text; diff --git a/athenasub/src/writer.h b/athenasub/src/writer.h index e54fd0eb2..15194cfd5 100644 --- a/athenasub/src/writer.h +++ b/athenasub/src/writer.h @@ -52,6 +52,7 @@ namespace Athenasub { public: Writer(String filename,String encoding=""); + ~Writer(); shared_ptr<TextWriter> GetTextWriter(); }; -- GitLab