From 5e16567e81dd6c05c8f9219700811aec3fd5b44e Mon Sep 17 00:00:00 2001 From: Rodrigo Braz Monteiro Date: Sat, 8 Mar 2008 07:14:14 +0000 Subject: [PATCH] Some more work on the subs library. Originally committed to SVN as r1968. --- aegilib/aegilib.vcproj | 24 +++++ aegilib/include/aegilib/aegilib.h | 2 + aegilib/include/aegilib/aegistring.h | 5 +- aegilib/include/aegilib/format.h | 7 +- aegilib/include/aegilib/format_handler.h | 49 ++++++++++ aegilib/include/aegilib/format_manager.h | 58 ++++++++++++ aegilib/include/aegilib/model.h | 13 +-- aegilib/src/format_manager.cpp | 114 +++++++++++++++++++++++ aegilib/src/formats/format_ass.cpp | 62 ++++++++++++ aegilib/src/formats/format_ass.h | 69 ++++++++++++++ aegilib/src/model.cpp | 22 +++-- aegilib/test/src/main.cpp | 40 +++++--- 12 files changed, 435 insertions(+), 30 deletions(-) create mode 100644 aegilib/include/aegilib/format_handler.h create mode 100644 aegilib/include/aegilib/format_manager.h create mode 100644 aegilib/src/format_manager.cpp create mode 100644 aegilib/src/formats/format_ass.cpp create mode 100644 aegilib/src/formats/format_ass.h diff --git a/aegilib/aegilib.vcproj b/aegilib/aegilib.vcproj index 6949467ae..6211d1fd2 100644 --- a/aegilib/aegilib.vcproj +++ b/aegilib/aegilib.vcproj @@ -183,6 +183,14 @@ RelativePath=".\include\aegilib\format.h" > + + + + @@ -235,6 +243,10 @@ RelativePath=".\src\exception.cpp" > + + @@ -261,6 +273,18 @@ > + + + + + + diff --git a/aegilib/include/aegilib/aegilib.h b/aegilib/include/aegilib/aegilib.h index fbc649be7..b666ef2ac 100644 --- a/aegilib/include/aegilib/aegilib.h +++ b/aegilib/include/aegilib/aegilib.h @@ -40,5 +40,7 @@ #include "notification.h" #include "aegistring.h" #include "format.h" +#include "format_handler.h" +#include "format_manager.h" #include "manipulator.h" #include "exception.h" diff --git a/aegilib/include/aegilib/aegistring.h b/aegilib/include/aegilib/aegistring.h index f1d774872..0e893a5bf 100644 --- a/aegilib/include/aegilib/aegistring.h +++ b/aegilib/include/aegilib/aegistring.h @@ -35,9 +35,12 @@ #pragma once +#include + namespace Aegilib { // Define the string type used throughout this library - typedef std::basic_string String; + //typedef std::basic_string String; + typedef wxString String; }; diff --git a/aegilib/include/aegilib/format.h b/aegilib/include/aegilib/format.h index 1488463f9..1ade11451 100644 --- a/aegilib/include/aegilib/format.h +++ b/aegilib/include/aegilib/format.h @@ -39,15 +39,16 @@ namespace Aegilib { // Prototypes class FormatHandler; + class Model; // Format interface class Format { public: - virtual ~Format(); + virtual ~Format() {} virtual String GetName() const = 0; - virtual String GetExtensionWildcard() const = 0; - virtual const FormatHandler& GetHandler() const = 0; + virtual String GetExtension() const = 0; + virtual FormatHandler* GetHandler(const Model &model) const = 0; virtual bool CanStoreText() const { return false; } virtual bool CanStoreImages() const { return false; } diff --git a/aegilib/include/aegilib/format_handler.h b/aegilib/include/aegilib/format_handler.h new file mode 100644 index 000000000..9c7af0d51 --- /dev/null +++ b/aegilib/include/aegilib/format_handler.h @@ -0,0 +1,49 @@ +// 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/AEGILIB +// +// Website: http://www.aegisub.net +// Contact: mailto:amz@aegisub.net +// + +#pragma once +#include "aegistring.h" + +namespace Aegilib { + + // Format handler interface + class FormatHandler { + public: + virtual ~FormatHandler() {} + + virtual void Load(wxInputStream &file,const String encoding) = 0; + }; + +}; diff --git a/aegilib/include/aegilib/format_manager.h b/aegilib/include/aegilib/format_manager.h new file mode 100644 index 000000000..5893253ec --- /dev/null +++ b/aegilib/include/aegilib/format_manager.h @@ -0,0 +1,58 @@ +// 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/AEGILIB +// +// Website: http://www.aegisub.net +// Contact: mailto:amz@aegisub.net +// + +#pragma once +#include "format.h" + +namespace Aegilib { + + // Format manager class + class FormatManager { + private: + static std::vector formats; + FormatManager() {} + + public: + static void AddFormat(const Format *format); + static void InitializeFormats(); + static void ClearFormats(); + + static int GetFormatCount(); + static const Format* GetFormatByIndex(const int index); + static const Format* GetFormatFromFilename(const String &filename); + static const Format* GetFormatFromName(const String &name); + }; + +}; diff --git a/aegilib/include/aegilib/model.h b/aegilib/include/aegilib/model.h index 37b86b039..b23ea2b92 100644 --- a/aegilib/include/aegilib/model.h +++ b/aegilib/include/aegilib/model.h @@ -35,6 +35,7 @@ #pragma once #include +#include #include "manipulator.h" #include "file.h" @@ -65,13 +66,13 @@ namespace Aegilib { const Format& GetFormat() const; void AddListener(View *listener); - void LoadFile(FileReader &file,Format *format=NULL); - void SaveFile(FileWriter &file,Format *format=NULL); + void LoadFile(wxInputStream &input,const Format *format=NULL,const String encoding=L""); + void SaveFile(wxOutputStream &output,const Format *format=NULL,const String encoding=L"UTF-8"); - bool CanUndo(String owner=L"") const; - bool CanRedo(String owner=L"") const; - bool Undo(String owner=L""); - bool Redo(String owner=L""); + bool CanUndo(const String owner=L"") const; + bool CanRedo(const String owner=L"") const; + bool Undo(const String owner=L""); + bool Redo(const String owner=L""); }; }; diff --git a/aegilib/src/format_manager.cpp b/aegilib/src/format_manager.cpp new file mode 100644 index 000000000..f6a35ab76 --- /dev/null +++ b/aegilib/src/format_manager.cpp @@ -0,0 +1,114 @@ +// 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/AEGILIB +// +// Website: http://www.aegisub.net +// Contact: mailto:amz@aegisub.net +// + +#include "format_manager.h" +#include "formats/format_ass.h" +#include +using namespace Aegilib; + + +//////// +// List +std::vector FormatManager::formats; + + +//////////////// +// Add a format +void FormatManager::AddFormat(const Format *format) +{ + formats.push_back(format); +} + + +/////////////////////////////////// +// Initialzie all built-in formats +void FormatManager::InitializeFormats() +{ + formats.push_back(new FormatASS); +} + + +/////////////////////// +// Removes all formats +void FormatManager::ClearFormats() +{ + formats.clear(); +} + + +///////////////////// +// Number of formats +int FormatManager::GetFormatCount() +{ + return (int) formats.size(); +} + + +//////////// +// By index +const Format* FormatManager::GetFormatByIndex(const int index) +{ + try { + return formats.at(index); + } + catch (...) { + return NULL; + } +} + + +/////////////// +// By filename +const Format* FormatManager::GetFormatFromFilename(const String &filename) +{ + size_t len = formats.size(); + for (size_t i=0;iGetExtension())) return formats[i]; + } + return NULL; +} + + +////////////////// +// By format name +const Format* FormatManager::GetFormatFromName(const String &name) +{ + size_t len = formats.size(); + for (size_t i=0;iGetName()) return formats[i]; + } + return NULL; +} + diff --git a/aegilib/src/formats/format_ass.cpp b/aegilib/src/formats/format_ass.cpp new file mode 100644 index 000000000..7c64a7058 --- /dev/null +++ b/aegilib/src/formats/format_ass.cpp @@ -0,0 +1,62 @@ +// 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/AEGILIB +// +// Website: http://www.aegisub.net +// Contact: mailto:amz@aegisub.net +// + +#include "model.h" +#include "format_ass.h" +using namespace Aegilib; + + +/////////////// +// Constructor +FormatHandlerASS::FormatHandlerASS(const Model &model) +{ + (void) model; +} + + +////////////// +// Destructor +FormatHandlerASS::~FormatHandlerASS() +{ +} + + +/////////////// +// Load a file +void FormatHandlerASS::Load(wxInputStream &file,const String encoding) +{ + (void) file; + (void) encoding; +} diff --git a/aegilib/src/formats/format_ass.h b/aegilib/src/formats/format_ass.h new file mode 100644 index 000000000..de9795253 --- /dev/null +++ b/aegilib/src/formats/format_ass.h @@ -0,0 +1,69 @@ +// 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/AEGILIB +// +// Website: http://www.aegisub.net +// Contact: mailto:amz@aegisub.net +// + +#pragma once +#include "format.h" +#include "format_handler.h" + +namespace Aegilib { + + // Prototypes + class Model; + + // Advanced Substation Alpha format handler + class FormatHandlerASS : public FormatHandler { + public: + FormatHandlerASS(const Model &model); + ~FormatHandlerASS(); + + void Load(wxInputStream &file,const String encoding); + }; + + // Advanced Substation Alpha format + class FormatASS : public Format { + public: + String GetName() const { return L"Advanced Substation Alpha"; } + String GetExtension() const { return L".ass"; } + FormatHandler* GetHandler(const Model &model) const { return new FormatHandlerASS(model); } + + bool CanStoreText() const { return true; } + bool CanUseTime() const { return true; } + + bool HasStyles() const { return true; } + bool HasMargins() const { return true; } + bool HasActors() const { return true; } + }; + +}; diff --git a/aegilib/src/model.cpp b/aegilib/src/model.cpp index da3175c92..df2aaa889 100644 --- a/aegilib/src/model.cpp +++ b/aegilib/src/model.cpp @@ -86,26 +86,34 @@ Manipulator Model::CreateAntiManipulator(const Manipulator &src) /////////////// // Load a file -void Model::LoadFile(FileReader &file,Format *format) +void Model::LoadFile(wxInputStream &input,const Format *format,const String encoding) { - // Detect format + // Autodetect format if (format == NULL) { // TODO + + // No format found + throw Exception(Exception::No_Format_Handler); } - // No format found - throw Exception(Exception::No_Format_Handler); + // Get handler + FormatHandler *handler = format->GetHandler(*this); + if (!handler) throw Exception(Exception::No_Format_Handler); // Load - (void) file; + handler->Load(input,encoding); + + // Clean up + delete handler; } ////////////////// // Save to a file -void Model::SaveFile(FileWriter &file,Format *format) +void Model::SaveFile(wxOutputStream &output,const Format *format,const String encoding) { - (void) file; + (void) output; (void) format; + (void) encoding; // TODO } diff --git a/aegilib/test/src/main.cpp b/aegilib/test/src/main.cpp index f8a11a718..54e946c7d 100644 --- a/aegilib/test/src/main.cpp +++ b/aegilib/test/src/main.cpp @@ -34,6 +34,7 @@ // #include +#include #include #include "text_file_reader.h" #include "text_file_writer.h" @@ -44,20 +45,33 @@ int main () { cout << "Aegilib test program by amz.\n\n"; - // Subtitles model - Model subs; + try { + // Set up the lib + FormatManager::InitializeFormats(); - // Load subtitles - cout << "Loading file... "; - subs.LoadFile(TextFileReader(L"subs_in.ass")); - cout << "Done.\n"; + // Subtitles model + Model subs; - // Modify subtitles - cout << "Modifying file..."; - cout << "Done.\n"; + // Load subtitles + cout << "Loading file... "; + String filename = L"subs_in.ass"; + const Format *handler = FormatManager::GetFormatFromFilename(filename); + subs.LoadFile(wxFileInputStream(filename),handler); + cout << "Done.\n"; - // Save subtitles - cout << "Saving file... "; - subs.SaveFile(TextFileWriter(L"subs_out.ass")); - cout << "Done.\n"; + // Modify subtitles + cout << "Modifying file..."; + cout << "Done.\n"; + + // Save subtitles + cout << "Saving file... "; + filename = L"subs_out.ass"; + handler = FormatManager::GetFormatFromFilename(filename); + subs.SaveFile(wxFileOutputStream(filename),handler); + cout << "Done.\n"; + } + + catch (Exception &e) { + cout << "\n\nException: " << e.GetMessage().mb_str(wxConvUTF8) << endl << endl; + } }