diff --git a/athenasub/athenasub_2008.vcproj b/athenasub/athenasub_2008.vcproj index e23a03936..9bc5a2546 100644 --- a/athenasub/athenasub_2008.vcproj +++ b/athenasub/athenasub_2008.vcproj @@ -467,6 +467,22 @@ RelativePath=".\src\text_file_writer.h" > + + + + + + + + diff --git a/athenasub/src/formats/format_ass.cpp b/athenasub/src/formats/format_ass.cpp index 8c9f06b70..9a99aecb4 100644 --- a/athenasub/src/formats/format_ass.cpp +++ b/athenasub/src/formats/format_ass.cpp @@ -38,8 +38,8 @@ #include "format_ass.h" #include "format_ass_plain.h" #include "version.h" -#include "../text_file_reader.h" -#include "../text_file_writer.h" +#include "../text_reader.h" +#include "../text_writer.h" #include #include #include @@ -108,7 +108,7 @@ FormatHandlerASS::~FormatHandlerASS() void FormatHandlerASS::Load(IModel &model,wxInputStream &file,const String encoding) { // Make text file reader - TextFileReader reader(file,encoding); + shared_ptr reader = TextReader::GetReader(file,encoding); // Variables int version = 1; @@ -117,9 +117,9 @@ void FormatHandlerASS::Load(IModel &model,wxInputStream &file,const String encod Section section = Section(); // Read file - while (reader.HasMoreLines()) { + while (reader->HasMoreLines()) { // Read a line - String cur = reader.ReadLineFromFile(); + String cur = reader->ReadLineFromFile(); if (cur.IsEmpty()) continue; // Process group @@ -151,7 +151,7 @@ void FormatHandlerASS::Load(IModel &model,wxInputStream &file,const String encod void FormatHandlerASS::Save(const IModel& model,wxOutputStream &file,const String encoding) const { // Make text file writer - TextFileWriter writer(file,encoding); + shared_ptr writer = TextWriter::GetWriter(file,encoding); // Set up list of sections to write StringArray sections; @@ -178,7 +178,7 @@ void FormatHandlerASS::Save(const IModel& model,wxOutputStream &file,const Strin ConstSection section = GetSection(model,sections[i]); if (section) { // Add a spacer - if (i != 0) writer.WriteLineToFile(""); + if (i != 0) writer->WriteLineToFile(""); // Write the section WriteSection(writer,section); @@ -334,11 +334,11 @@ void FormatHandlerASS::ProcessGroup(String cur,String &curGroup,int &version) { /////////////////////////////// // Write a section to the file -void FormatHandlerASS::WriteSection(TextFileWriter &writer,ConstSection section) const +void FormatHandlerASS::WriteSection(shared_ptr writer,ConstSection section) const { // Write name String name = section->GetName(); - writer.WriteLineToFile("[" + name + "]"); + writer->WriteLineToFile("[" + name + "]"); // Write program and library credits if (name == "Script Info") { @@ -346,17 +346,17 @@ void FormatHandlerASS::WriteSection(TextFileWriter &writer,ConstSection section) String programURL = GetHostApplicationURL(); String libVersion = GetLibraryVersionString(); String libURL = GetLibraryURL(); - writer.WriteLineToFile("; Script generated by " + programName); - if (!programURL.IsEmpty()) writer.WriteLineToFile("; " + programURL); - writer.WriteLineToFile("; With " + libVersion); - if (programURL != libURL) writer.WriteLineToFile("; " + libURL); + writer->WriteLineToFile("; Script generated by " + programName); + if (!programURL.IsEmpty()) writer->WriteLineToFile("; " + programURL); + writer->WriteLineToFile("; With " + libVersion); + if (programURL != libURL) writer->WriteLineToFile("; " + libURL); } // Write properties size_t props = section->GetPropertyCount(); for (size_t i=0;iGetPropertyName(i); - writer.WriteLineToFile(propName + ": " + section->GetProperty(propName)); + writer->WriteLineToFile(propName + ": " + section->GetProperty(propName)); } // Write contents @@ -364,7 +364,7 @@ void FormatHandlerASS::WriteSection(TextFileWriter &writer,ConstSection section) for (size_t i=0;iGetEntry(i); shared_ptr serial = dynamic_pointer_cast(entry); - writer.WriteLineToFile(serial->ToText(formatVersion)); + writer->WriteLineToFile(serial->ToText(formatVersion)); } } diff --git a/athenasub/src/formats/format_ass.h b/athenasub/src/formats/format_ass.h index f89be1d38..374a84220 100644 --- a/athenasub/src/formats/format_ass.h +++ b/athenasub/src/formats/format_ass.h @@ -46,7 +46,7 @@ namespace Athenasub { // Prototypes class CModel; - class TextFileWriter; + class TextWriter; // Advanced Substation Alpha format handler class FormatHandlerASS : public CFormatHandler { @@ -55,7 +55,7 @@ namespace Athenasub { Entry MakeEntry(const String &data,Section section,int version); void ProcessGroup(String cur,String &curGroup,int &version); - void WriteSection(TextFileWriter &writer,ConstSection section) const; + void WriteSection(shared_ptr writer,ConstSection section) const; void MakeValid(IModel &model); public: diff --git a/athenasub/src/text_file_reader.cpp b/athenasub/src/text_file_reader.cpp index f5fb59bed..128f9b755 100644 --- a/athenasub/src/text_file_reader.cpp +++ b/athenasub/src/text_file_reader.cpp @@ -49,13 +49,11 @@ using namespace Athenasub; /////////////// // Constructor -TextFileReader::TextFileReader(wxInputStream &stream,String enc,bool _trim,bool prefetch) +TextFileReader::TextFileReader(wxInputStream &stream,String enc,bool _trim) : file(stream) { // Setup trim = _trim; - threaded = prefetch && false; - thread = NULL; // Set encoding encoding = enc.GetWxString(); @@ -68,8 +66,6 @@ TextFileReader::TextFileReader(wxInputStream &stream,String enc,bool _trim,bool // Destructor TextFileReader::~TextFileReader() { - wxCriticalSectionLocker locker(mutex); - if (thread) thread->Delete(); } @@ -191,7 +187,7 @@ void ParseLine(FastBuffer &_buffer,wxInputStream &file,String &stringBuffer,s ////////////////////////// // Reads a line from file -Athenasub::String TextFileReader::ActuallyReadLine() +Athenasub::String TextFileReader::ReadLineFromFile() { String stringBuffer; size_t bufAlloc = 1024; @@ -225,8 +221,6 @@ Athenasub::String TextFileReader::ActuallyReadLine() // Checks if there's more to read bool TextFileReader::HasMoreLines() { - if (cache.size()) return true; - wxCriticalSectionLocker locker(mutex); return (!file.Eof() || buffer1.GetSize() || buffer2.GetSize()); } @@ -252,68 +246,9 @@ String TextFileReader::GetCurrentEncoding() } -/////////////////////////// -// Reads a line from cache -String TextFileReader::ReadLineFromFile() +/////////////////// +// Rewind the file +void TextFileReader::Rewind() { - // Not threaded, just return it - if (!threaded) return ActuallyReadLine(); - // Load into cache if needed - String final; - { - wxCriticalSectionLocker locker(mutex); - if (cache.size() == 0) { - cache.push_back(ActuallyReadLine()); - } - } - - { - // Retrieve from cache - wxCriticalSectionLocker locker(mutex); - if (cache.size()) { - final = cache.front(); - cache.pop_front(); - } - - // Start the thread to prefetch more - if (cache.size() < 3 && thread == NULL) { - thread = new PrefetchThread(this); - thread->Create(); - thread->Run(); - } - } - - return final; -} - - -//////////////// -// Thread entry -wxThread::ExitCode PrefetchThread::Entry() -{ - // Lock - bool run = true; - while (run) { - if (TestDestroy()) { - parent->thread = NULL; - return 0; - } - { - wxCriticalSectionLocker locker(parent->mutex); - if (parent->cache.size() < 6) { - if (!parent->file.Eof()) { - // Get line - parent->cache.push_back(parent->ActuallyReadLine()); - } - else run = false; - } - } - Sleep(50); - } - - // Die - wxCriticalSectionLocker locker(parent->mutex); - parent->thread = NULL; - return 0; } diff --git a/athenasub/src/text_file_reader.h b/athenasub/src/text_file_reader.h index 7fe784d8a..5139c7c8c 100644 --- a/athenasub/src/text_file_reader.h +++ b/athenasub/src/text_file_reader.h @@ -1,4 +1,4 @@ -// Copyright (c) 2005, Rodrigo Braz Monteiro +// Copyright (c) 2008, Rodrigo Braz Monteiro // All rights reserved. // // Redistribution and use in source and binary forms, with or without @@ -39,19 +39,15 @@ // Headers #include "athenasub.h" +#include "text_reader.h" #include "fastbuffer.h" -#include namespace Athenasub { // Text file reader - class TextFileReader { - friend class PrefetchThread; + class TextFileReader : public TextReader { private: - wxCriticalSection mutex; - - std::list cache; FastBuffer buffer1; FastBuffer buffer2; @@ -62,33 +58,21 @@ namespace Athenasub { bool isUtf8; bool swap; bool trim; - bool threaded; - wxThread *thread; void SetEncodingConfiguration(); - String ActuallyReadLine(); public: - TextFileReader(wxInputStream &stream,String encoding="",bool trim=true,bool prefetch=true); + TextFileReader(wxInputStream &stream,String encoding="",bool trim=true); ~TextFileReader(); String ReadLineFromFile(); bool HasMoreLines(); + void Rewind(); static void EnsureValid(const String encoding); String GetCurrentEncoding(); static String GetEncoding(const String filename); }; - // Prefetch thread - class PrefetchThread : public wxThread { - private: - TextFileReader *parent; - - public: - wxThread::ExitCode Entry(); - PrefetchThread(TextFileReader *_parent) : parent(_parent) {} - }; - } diff --git a/athenasub/src/text_file_writer.h b/athenasub/src/text_file_writer.h index 45606f644..30d6650a0 100644 --- a/athenasub/src/text_file_writer.h +++ b/athenasub/src/text_file_writer.h @@ -35,12 +35,13 @@ #pragma once -#include "Athenasub.h" +#include "athenasub.h" +#include "text_writer.h" #include namespace Athenasub { - class TextFileWriter { + class TextFileWriter : public TextWriter { private: wxString encoding; wxOutputStream &file; diff --git a/athenasub/src/text_reader.cpp b/athenasub/src/text_reader.cpp new file mode 100644 index 000000000..c6dc0888f --- /dev/null +++ b/athenasub/src/text_reader.cpp @@ -0,0 +1,45 @@ +// Copyright (c) 2008, Rodrigo Braz Monteiro +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// * Neither the name of the Aegisub Group nor the names of its contributors +// may be used to endorse or promote products derived from this software +// without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. +// +// ----------------------------------------------------------------------------- +// +// AEGISUB +// +// Website: http://aegisub.cellosoft.com +// Contact: mailto:zeratul@cellosoft.com +// + + +// Headers +#include "text_reader.h" +#include "text_file_reader.h" +using namespace Athenasub; + +shared_ptr TextReader::GetReader(wxInputStream &stream,String encoding) +{ + return shared_ptr(new TextFileReader(stream,encoding)); +} \ No newline at end of file diff --git a/athenasub/src/text_reader.h b/athenasub/src/text_reader.h new file mode 100644 index 000000000..952697ea8 --- /dev/null +++ b/athenasub/src/text_reader.h @@ -0,0 +1,55 @@ +// Copyright (c) 2008, Rodrigo Braz Monteiro +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// * Neither the name of the Aegisub Group nor the names of its contributors +// may be used to endorse or promote products derived from this software +// without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. +// +// ----------------------------------------------------------------------------- +// +// AEGISUB +// +// Website: http://aegisub.cellosoft.com +// Contact: mailto:zeratul@cellosoft.com +// + + +#pragma once + + +// Headers +#include "athenasub.h" + + +namespace Athenasub { + class TextReader { + public: + virtual ~TextReader() {} + + virtual String ReadLineFromFile() = 0; + virtual bool HasMoreLines() = 0; + virtual void Rewind() = 0; + + static shared_ptr GetReader(wxInputStream &stream,String encoding); + }; +} diff --git a/athenasub/src/text_writer.cpp b/athenasub/src/text_writer.cpp new file mode 100644 index 000000000..08b1048cc --- /dev/null +++ b/athenasub/src/text_writer.cpp @@ -0,0 +1,45 @@ +// Copyright (c) 2008, Rodrigo Braz Monteiro +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// * Neither the name of the Aegisub Group nor the names of its contributors +// may be used to endorse or promote products derived from this software +// without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. +// +// ----------------------------------------------------------------------------- +// +// AEGISUB +// +// Website: http://aegisub.cellosoft.com +// Contact: mailto:zeratul@cellosoft.com +// + + +// Headers +#include "text_writer.h" +#include "text_file_writer.h" +using namespace Athenasub; + +shared_ptr TextWriter::GetWriter(wxOutputStream &stream,String encoding) +{ + return shared_ptr(new TextFileWriter(stream,encoding)); +} \ No newline at end of file diff --git a/athenasub/src/text_writer.h b/athenasub/src/text_writer.h new file mode 100644 index 000000000..43dac49dd --- /dev/null +++ b/athenasub/src/text_writer.h @@ -0,0 +1,53 @@ +// Copyright (c) 2008, Rodrigo Braz Monteiro +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// * Neither the name of the Aegisub Group nor the names of its contributors +// may be used to endorse or promote products derived from this software +// without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. +// +// ----------------------------------------------------------------------------- +// +// AEGISUB +// +// Website: http://aegisub.cellosoft.com +// Contact: mailto:zeratul@cellosoft.com +// + + +#pragma once + + +// Headers +#include "athenasub.h" + + +namespace Athenasub { + class TextWriter { + public: + virtual ~TextWriter() {} + + virtual void WriteLineToFile(String line,bool addLineBreak=true) = 0; + + static shared_ptr GetWriter(wxOutputStream &stream,String encoding); + }; +}