From 3b7dce9c8e9db124bc8f3009d6d9a34a15d154d3 Mon Sep 17 00:00:00 2001 From: Rodrigo Braz Monteiro Date: Wed, 12 Mar 2008 19:28:24 +0000 Subject: [PATCH] Some code simplification on the subs lib. Originally committed to SVN as r2028. --- aegilib/include/aegilib/model.h | 8 ++- aegilib/include/aegilib/tokenizer.h | 2 +- aegilib/src/formats/format_ass.h | 2 + aegilib/src/formats/format_ass_dialogue.cpp | 24 +++---- aegilib/src/formats/format_ass_style.cpp | 77 +++++++++++---------- aegilib/src/model.cpp | 30 ++++++-- aegilib/src/tokenizer.cpp | 8 ++- aegilib/test/src/main.cpp | 8 +-- aegilib/test/test.vcproj | 4 +- 9 files changed, 92 insertions(+), 71 deletions(-) diff --git a/aegilib/include/aegilib/model.h b/aegilib/include/aegilib/model.h index efbfaf579..6ec69a1bc 100644 --- a/aegilib/include/aegilib/model.h +++ b/aegilib/include/aegilib/model.h @@ -35,7 +35,7 @@ #pragma once #include -#include +#include #include "manipulator.h" #include "section.h" @@ -68,8 +68,10 @@ namespace Aegilib { const Format& GetFormat() const; void AddListener(View *listener); - 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"); + void Load(wxInputStream &input,const Format *format=NULL,const String encoding=L""); + void Save(wxOutputStream &output,const Format *format=NULL,const String encoding=L"UTF-8"); + void LoadFile(const String filename,const String encoding=L""); + void SaveFile(const String filename,const String encoding=L"UTF-8"); Section* GetSection(String name) const; void AddSection(String name); diff --git a/aegilib/include/aegilib/tokenizer.h b/aegilib/include/aegilib/tokenizer.h index 7bcb4ebdc..13f31b1bf 100644 --- a/aegilib/include/aegilib/tokenizer.h +++ b/aegilib/include/aegilib/tokenizer.h @@ -53,7 +53,7 @@ namespace Aegilib { bool HasMore(); int GetPosition(); - String GetString(); + String GetString(bool trim=false); int GetInt(); long GetLong(); float GetFloat(); diff --git a/aegilib/src/formats/format_ass.h b/aegilib/src/formats/format_ass.h index ffeb24a07..a27ad0939 100644 --- a/aegilib/src/formats/format_ass.h +++ b/aegilib/src/formats/format_ass.h @@ -150,6 +150,8 @@ namespace Aegilib { float shadow_w; bool Parse(String data,int version); + int AlignSSAtoASS(int ssaAlignment); + int AlignASStoSSA(int assAlignment); public: // Constructors diff --git a/aegilib/src/formats/format_ass_dialogue.cpp b/aegilib/src/formats/format_ass_dialogue.cpp index 904798d1e..c07f80321 100644 --- a/aegilib/src/formats/format_ass_dialogue.cpp +++ b/aegilib/src/formats/format_ass_dialogue.cpp @@ -81,13 +81,15 @@ bool DialogueASS::Parse(wxString rawData, int version) Tokenizer tkn(rawData.Mid(pos),_T(",")); // Get first token and see if it has "Marked=" in it - temp = tkn.GetString().Trim(false).Trim(true); - if (temp.Lower().StartsWith(_T("marked="))) version = 0; - else if (version == 0) version = 1; + temp = tkn.GetString(true); + if (temp.Lower().StartsWith(_T("marked="))) { + version = 0; + layer = 0; + } - // Get layer number - if (version == 0) layer = 0; + // Not SSA, so read layer number else { + if (version == 0) version = 1; // Only do it for SSA, not ASS2 long templ; temp.ToLong(&templ); layer = templ; @@ -97,15 +99,9 @@ bool DialogueASS::Parse(wxString rawData, int version) start.Parse(tkn.GetString()); end.Parse(tkn.GetString()); - // Get style - style = tkn.GetString(); - style.Trim(true); - style.Trim(false); - - // Get actor - actor = tkn.GetString(); - actor.Trim(true); - actor.Trim(false); + // Get style and actor + style = tkn.GetString(true); + actor = tkn.GetString(true); // Get margins for (int i=0;i<3;i++) margin[i] = tkn.GetInt(); diff --git a/aegilib/src/formats/format_ass_style.cpp b/aegilib/src/formats/format_ass_style.cpp index e50393838..50ac442e4 100644 --- a/aegilib/src/formats/format_ass_style.cpp +++ b/aegilib/src/formats/format_ass_style.cpp @@ -65,23 +65,15 @@ bool StyleASS::Parse(String data,int version) wxString temp; Tokenizer tkn(data.Trim(false).Mid(6),_T(",")); - // Read name - name = tkn.GetString(); - name.Trim(true); - name.Trim(false); - - // Read font name and size - font = tkn.GetString(); - font.Trim(true); - font.Trim(false); + // Read name, font name and size + name = tkn.GetString(true); + font = tkn.GetString(true); fontSize = tkn.GetFloat(); // Read colours for (int i=0;i<5;i++) { if ((i == 4 && version == 0) || (i == 2 && version != 0)) colour[i] = colour[i-1]; - else { - colour[i].Parse(tkn.GetString(),true); - } + else colour[i].Parse(tkn.GetString(),true); } // Read bold and italics @@ -115,43 +107,24 @@ bool StyleASS::Parse(String data,int version) shadow_w = tkn.GetFloat(); // Read alignment - int align = tkn.GetInt(); - if (version == 0) { - switch(align) { - case 1: alignment = 1; break; - case 2: alignment = 2; break; - case 3: alignment = 3; break; - case 5: alignment = 7; break; - case 6: alignment = 8; break; - case 7: alignment = 9; break; - case 9: alignment = 4; break; - case 10: alignment = 5; break; - case 11: alignment = 6; break; - default: alignment = 2; break; - } - } - else alignment = align; + alignment = tkn.GetInt(); + if (version == 0) alignment = AlignSSAtoASS(alignment); // Read margins for (int i=0;i<4;i++) { if (i == 3 && version < 2) margin[i] = margin[i-1]; - else { - margin[i] = tkn.GetInt(); - } + else margin[i] = tkn.GetInt(); } - // Read and discard alpha level - if (version == 0) { - tkn.GetString(); - } + // Read and discard alpha level on SSA + // TODO: do something with this? + if (version == 0) tkn.GetString(); // Read encoding encoding = tkn.GetInt(); // Read relative to - if (version == 2) { - relativeTo = tkn.GetInt(); - } + if (version == 2) relativeTo = tkn.GetInt(); // End if (tkn.HasMore()) return false; @@ -162,3 +135,31 @@ bool StyleASS::Parse(String data,int version) return false; } } + + +//////////////////////////////// +// Convert SSA alignment to ASS +int StyleASS::AlignSSAtoASS(int align) +{ + switch(align) { + case 1: return 1; + case 2: return 2; + case 3: return 3; + case 5: return 7; + case 6: return 8; + case 7: return 9; + case 9: return 4; + case 10: return 5; + case 11: return 6; + default: return 2; + } +} + + +//////////////////////////////// +// Convert ASS alignment to SSA +int StyleASS::AlignASStoSSA(int assAlignment) +{ + // TODO + return assAlignment; +} diff --git a/aegilib/src/model.cpp b/aegilib/src/model.cpp index 4b5f8685f..f5a83885e 100644 --- a/aegilib/src/model.cpp +++ b/aegilib/src/model.cpp @@ -84,9 +84,9 @@ Manipulator Model::CreateAntiManipulator(const Manipulator &src) } -/////////////// -// Load a file -void Model::LoadFile(wxInputStream &input,const Format *format,const String encoding) +////////////////// +// Load subtitles +void Model::Load(wxInputStream &input,const Format *format,const String encoding) { // Autodetect format if (format == NULL) { @@ -109,8 +109,8 @@ void Model::LoadFile(wxInputStream &input,const Format *format,const String enco ////////////////// -// Save to a file -void Model::SaveFile(wxOutputStream &output,const Format *format,const String encoding) +// Save subtitles +void Model::Save(wxOutputStream &output,const Format *format,const String encoding) { (void) output; (void) format; @@ -119,6 +119,26 @@ void Model::SaveFile(wxOutputStream &output,const Format *format,const String en } +/////////////// +// Load a file +void Model::LoadFile(const String filename,const String encoding) +{ + const Format *handler = FormatManager::GetFormatFromFilename(filename,true); + wxFileInputStream stream(filename); + Load(stream,handler,encoding); +} + + +/////////////// +// Save a file +void Model::SaveFile(const String filename,const String encoding) +{ + const Format *handler = FormatManager::GetFormatFromFilename(filename,true); + wxFileOutputStream stream(filename); + Save(stream,handler,encoding); +} + + ////////////////// // Gets a section Section* Model::GetSection(String name) const diff --git a/aegilib/src/tokenizer.cpp b/aegilib/src/tokenizer.cpp index 4983cb602..37f848c97 100644 --- a/aegilib/src/tokenizer.cpp +++ b/aegilib/src/tokenizer.cpp @@ -68,9 +68,13 @@ int Tokenizer::GetPosition() ///////////// // Get token -String Tokenizer::GetString() +String Tokenizer::GetString(bool trim) { - return tkn->GetNextToken(); + wxString str = tkn->GetNextToken(); + if (trim) { + str.Trim(true).Trim(false); + } + return str; } int Tokenizer::GetInt() { diff --git a/aegilib/test/src/main.cpp b/aegilib/test/src/main.cpp index 8dfdbde5d..54bae5213 100644 --- a/aegilib/test/src/main.cpp +++ b/aegilib/test/src/main.cpp @@ -54,9 +54,7 @@ int main () { // Load subtitles cout << "Loading file... "; - String filename = L"subs_in.ass"; - const Format *handler = FormatManager::GetFormatFromFilename(filename,true); - subs.LoadFile(wxFileInputStream(filename),handler,L"UTF-8"); + subs.LoadFile(L"subs_in.ass",L"UTF-8"); cout << "Done.\n"; // Modify subtitles @@ -65,9 +63,7 @@ int main () { // Save subtitles cout << "Saving file... "; - filename = L"subs_out.ass"; - handler = FormatManager::GetFormatFromFilename(filename,false); - subs.SaveFile(wxFileOutputStream(filename),handler); + subs.SaveFile(L"subs_out.ass",L"UTF-8"); cout << "Done.\n"; } diff --git a/aegilib/test/test.vcproj b/aegilib/test/test.vcproj index 76dadaee7..c6b533809 100644 --- a/aegilib/test/test.vcproj +++ b/aegilib/test/test.vcproj @@ -46,7 +46,7 @@ BasicRuntimeChecks="3" RuntimeLibrary="3" UsePrecompiledHeader="0" - WarningLevel="3" + WarningLevel="4" Detect64BitPortabilityProblems="true" DebugInformationFormat="4" DisableSpecificWarnings="4996" @@ -121,7 +121,7 @@ PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE" RuntimeLibrary="2" UsePrecompiledHeader="0" - WarningLevel="3" + WarningLevel="4" Detect64BitPortabilityProblems="true" DebugInformationFormat="3" DisableSpecificWarnings="4996"