Some code simplification on the subs lib.

Originally committed to SVN as r2028.
This commit is contained in:
Rodrigo Braz Monteiro 2008-03-12 19:28:24 +00:00
parent 0e3333e4f8
commit 3b7dce9c8e
9 changed files with 92 additions and 71 deletions

View file

@ -35,7 +35,7 @@
#pragma once #pragma once
#include <list> #include <list>
#include <wx/stream.h> #include <wx/wfstream.h>
#include "manipulator.h" #include "manipulator.h"
#include "section.h" #include "section.h"
@ -68,8 +68,10 @@ namespace Aegilib {
const Format& GetFormat() const; const Format& GetFormat() const;
void AddListener(View *listener); void AddListener(View *listener);
void LoadFile(wxInputStream &input,const Format *format=NULL,const String encoding=L""); void Load(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 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; Section* GetSection(String name) const;
void AddSection(String name); void AddSection(String name);

View file

@ -53,7 +53,7 @@ namespace Aegilib {
bool HasMore(); bool HasMore();
int GetPosition(); int GetPosition();
String GetString(); String GetString(bool trim=false);
int GetInt(); int GetInt();
long GetLong(); long GetLong();
float GetFloat(); float GetFloat();

View file

@ -150,6 +150,8 @@ namespace Aegilib {
float shadow_w; float shadow_w;
bool Parse(String data,int version); bool Parse(String data,int version);
int AlignSSAtoASS(int ssaAlignment);
int AlignASStoSSA(int assAlignment);
public: public:
// Constructors // Constructors

View file

@ -81,13 +81,15 @@ bool DialogueASS::Parse(wxString rawData, int version)
Tokenizer tkn(rawData.Mid(pos),_T(",")); Tokenizer tkn(rawData.Mid(pos),_T(","));
// Get first token and see if it has "Marked=" in it // Get first token and see if it has "Marked=" in it
temp = tkn.GetString().Trim(false).Trim(true); temp = tkn.GetString(true);
if (temp.Lower().StartsWith(_T("marked="))) version = 0; if (temp.Lower().StartsWith(_T("marked="))) {
else if (version == 0) version = 1; version = 0;
layer = 0;
}
// Get layer number // Not SSA, so read layer number
if (version == 0) layer = 0;
else { else {
if (version == 0) version = 1; // Only do it for SSA, not ASS2
long templ; long templ;
temp.ToLong(&templ); temp.ToLong(&templ);
layer = templ; layer = templ;
@ -97,15 +99,9 @@ bool DialogueASS::Parse(wxString rawData, int version)
start.Parse(tkn.GetString()); start.Parse(tkn.GetString());
end.Parse(tkn.GetString()); end.Parse(tkn.GetString());
// Get style // Get style and actor
style = tkn.GetString(); style = tkn.GetString(true);
style.Trim(true); actor = tkn.GetString(true);
style.Trim(false);
// Get actor
actor = tkn.GetString();
actor.Trim(true);
actor.Trim(false);
// Get margins // Get margins
for (int i=0;i<3;i++) margin[i] = tkn.GetInt(); for (int i=0;i<3;i++) margin[i] = tkn.GetInt();

View file

@ -65,23 +65,15 @@ bool StyleASS::Parse(String data,int version)
wxString temp; wxString temp;
Tokenizer tkn(data.Trim(false).Mid(6),_T(",")); Tokenizer tkn(data.Trim(false).Mid(6),_T(","));
// Read name // Read name, font name and size
name = tkn.GetString(); name = tkn.GetString(true);
name.Trim(true); font = tkn.GetString(true);
name.Trim(false);
// Read font name and size
font = tkn.GetString();
font.Trim(true);
font.Trim(false);
fontSize = tkn.GetFloat(); fontSize = tkn.GetFloat();
// Read colours // Read colours
for (int i=0;i<5;i++) { for (int i=0;i<5;i++) {
if ((i == 4 && version == 0) || (i == 2 && version != 0)) colour[i] = colour[i-1]; if ((i == 4 && version == 0) || (i == 2 && version != 0)) colour[i] = colour[i-1];
else { else colour[i].Parse(tkn.GetString(),true);
colour[i].Parse(tkn.GetString(),true);
}
} }
// Read bold and italics // Read bold and italics
@ -115,43 +107,24 @@ bool StyleASS::Parse(String data,int version)
shadow_w = tkn.GetFloat(); shadow_w = tkn.GetFloat();
// Read alignment // Read alignment
int align = tkn.GetInt(); alignment = tkn.GetInt();
if (version == 0) { if (version == 0) alignment = AlignSSAtoASS(alignment);
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;
// Read margins // Read margins
for (int i=0;i<4;i++) { for (int i=0;i<4;i++) {
if (i == 3 && version < 2) margin[i] = margin[i-1]; if (i == 3 && version < 2) margin[i] = margin[i-1];
else { else margin[i] = tkn.GetInt();
margin[i] = tkn.GetInt();
}
} }
// Read and discard alpha level // Read and discard alpha level on SSA
if (version == 0) { // TODO: do something with this?
tkn.GetString(); if (version == 0) tkn.GetString();
}
// Read encoding // Read encoding
encoding = tkn.GetInt(); encoding = tkn.GetInt();
// Read relative to // Read relative to
if (version == 2) { if (version == 2) relativeTo = tkn.GetInt();
relativeTo = tkn.GetInt();
}
// End // End
if (tkn.HasMore()) return false; if (tkn.HasMore()) return false;
@ -162,3 +135,31 @@ bool StyleASS::Parse(String data,int version)
return false; 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;
}

View file

@ -84,9 +84,9 @@ Manipulator Model::CreateAntiManipulator(const Manipulator &src)
} }
/////////////// //////////////////
// Load a file // Load subtitles
void Model::LoadFile(wxInputStream &input,const Format *format,const String encoding) void Model::Load(wxInputStream &input,const Format *format,const String encoding)
{ {
// Autodetect format // Autodetect format
if (format == NULL) { if (format == NULL) {
@ -109,8 +109,8 @@ void Model::LoadFile(wxInputStream &input,const Format *format,const String enco
////////////////// //////////////////
// Save to a file // Save subtitles
void Model::SaveFile(wxOutputStream &output,const Format *format,const String encoding) void Model::Save(wxOutputStream &output,const Format *format,const String encoding)
{ {
(void) output; (void) output;
(void) format; (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 // Gets a section
Section* Model::GetSection(String name) const Section* Model::GetSection(String name) const

View file

@ -68,9 +68,13 @@ int Tokenizer::GetPosition()
///////////// /////////////
// Get token // 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() int Tokenizer::GetInt()
{ {

View file

@ -54,9 +54,7 @@ int main () {
// Load subtitles // Load subtitles
cout << "Loading file... "; cout << "Loading file... ";
String filename = L"subs_in.ass"; subs.LoadFile(L"subs_in.ass",L"UTF-8");
const Format *handler = FormatManager::GetFormatFromFilename(filename,true);
subs.LoadFile(wxFileInputStream(filename),handler,L"UTF-8");
cout << "Done.\n"; cout << "Done.\n";
// Modify subtitles // Modify subtitles
@ -65,9 +63,7 @@ int main () {
// Save subtitles // Save subtitles
cout << "Saving file... "; cout << "Saving file... ";
filename = L"subs_out.ass"; subs.SaveFile(L"subs_out.ass",L"UTF-8");
handler = FormatManager::GetFormatFromFilename(filename,false);
subs.SaveFile(wxFileOutputStream(filename),handler);
cout << "Done.\n"; cout << "Done.\n";
} }

View file

@ -46,7 +46,7 @@
BasicRuntimeChecks="3" BasicRuntimeChecks="3"
RuntimeLibrary="3" RuntimeLibrary="3"
UsePrecompiledHeader="0" UsePrecompiledHeader="0"
WarningLevel="3" WarningLevel="4"
Detect64BitPortabilityProblems="true" Detect64BitPortabilityProblems="true"
DebugInformationFormat="4" DebugInformationFormat="4"
DisableSpecificWarnings="4996" DisableSpecificWarnings="4996"
@ -121,7 +121,7 @@
PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE" PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE"
RuntimeLibrary="2" RuntimeLibrary="2"
UsePrecompiledHeader="0" UsePrecompiledHeader="0"
WarningLevel="3" WarningLevel="4"
Detect64BitPortabilityProblems="true" Detect64BitPortabilityProblems="true"
DebugInformationFormat="3" DebugInformationFormat="3"
DisableSpecificWarnings="4996" DisableSpecificWarnings="4996"