diff --git a/athenasub/src/exception.cpp b/athenasub/src/exception.cpp index c8d81906ee4d25953657e79681d5d7e744bbb484..216e8c8a53a8051688821c0568c774ed5b2a3c53 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 1d98b1f7e857b6204c7c13d01f6bb720cfdda87b..7062d35bef2d26d1f48aa4f770539317bde6a890 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 d8f23b79c880d6703a3fb803555bf663effbf310..4f0e133ab7b41dcbcbc5f8ac16a37c027cb52b5c 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 177799fbb5cd09545de41136b619336405cb7c21..3f297fcc082dbe0debccce38955f64b99a3511f0 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 168c8151df14c8d216483eb48df4b28256e0e6ff..0579bd57b9f75d1cc4a022d6871a782da6926d57 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 916155ac89e599d36c0920588f05e3f8c56bbb2d..58d2ca6d9ba7826afc74e12ea0051873b55b5717 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 30d6650a0fccbe0c5d272d671d18860ec84bd556..487583ffca98020fd568ad37f51c15aab98f14ad 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 43dac49dd06f9863adb66db160a3b25927c86757..5f5a14de17d8ffe26f4b9a5d73cb18af1c09f015 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 e3261ea8a4264672ea938fc3b2cb0350200d127c..95dd288ffaa8a1b78aeb1461b46f14370e7b1e72 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 e54fd0eb299ad8b40b165d04f73141ba795b6087..15194cfd547755a4e73498435748eddcfc934c57 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(); };