Early separation of the subtitle format parser/writer code

Originally committed to SVN as r180.
This commit is contained in:
Rodrigo Braz Monteiro 2006-02-26 22:45:34 +00:00
parent b34c69891c
commit 20c783f83d
10 changed files with 402 additions and 86 deletions

View file

@ -48,6 +48,7 @@
#include "text_file_reader.h" #include "text_file_reader.h"
#include "text_file_writer.h" #include "text_file_writer.h"
#include "version.h" #include "version.h"
#include "subtitle_format_srt.h"
////////////////////// AssFile ////////////////////// ////////////////////// AssFile //////////////////////
@ -105,7 +106,10 @@ void AssFile::Load (const wxString _filename,const wxString charset) {
// SRT // SRT
else if (extension == _T("srt")) { else if (extension == _T("srt")) {
LoadSRT(_filename,enc); //LoadSRT(_filename,enc);
SRTSubtitleFormatReader srt;
srt.SetTarget(this);
srt.ReadFile(_filename,enc);
} }
// SSA // SSA
@ -202,83 +206,6 @@ void AssFile::LoadASS (const wxString _filename,const wxString encoding,bool IsS
} }
////////////////////////////
// Loads SRT subs from disk
void AssFile::LoadSRT (wxString _filename,wxString encoding) {
using namespace std;
// Reader
TextFileReader file(_filename,encoding);
// Default
LoadDefault(false);
IsASS = false;
// Parse file
int linen = 1;
int fileLine = 0;
int mode = 0;
long templ;
AssDialogue *line = NULL;
while (file.HasMoreLines()) {
// Reads line
wxString curline = file.ReadLineFromFile();
fileLine++;
switch (mode) {
case 0:
// Checks if there is anything to read
if (curline.IsEmpty()) continue;
// Check if it's a line number
if (!curline.IsNumber()) {
Clear();
throw wxString::Format(_T("Parse error on entry %i at line %i (expecting line number). Possible malformed file."),linen,fileLine);
}
// Read line number
curline.ToLong(&templ);
if (templ != linen) {
linen = templ;
}
line = new AssDialogue();
mode = 1;
break;
case 1:
// Read timestamps
if (curline.substr(13,3) != _T("-->")) {
Clear();
throw wxString::Format(_T("Parse error on entry %i at line %i (expecting timestamps). Possible malformed file."),linen,fileLine);
}
line->Start.ParseSRT(curline.substr(0,12));
line->End.ParseSRT(curline.substr(17,12));
mode = 2;
break;
case 2:
// Checks if it's done
if (curline.IsEmpty()) {
mode = 0;
linen++;
line->group = _T("[Events]");
line->Style = _T("Default");
line->Comment = false;
line->UpdateData();
line->ParseSRTTags();
line->StartMS = line->Start.GetMS();
Line.push_back(line);
break;
}
// Append text
if (line->Text != _T("")) line->Text += _T("\\N");
line->Text += curline;
break;
}
}
}
//////////////////////////// ////////////////////////////
// Loads TXT subs from disk // Loads TXT subs from disk
void AssFile::LoadTXT (wxString _filename,wxString encoding) { void AssFile::LoadTXT (wxString _filename,wxString encoding) {

View file

@ -69,7 +69,6 @@ private:
// I/O operations // I/O operations
void LoadASS(const wxString file,const wxString encoding,bool IsSSA=false); void LoadASS(const wxString file,const wxString encoding,bool IsSSA=false);
void LoadSRT(const wxString file,const wxString encoding);
void LoadTXT(const wxString file,const wxString encoding); void LoadTXT(const wxString file,const wxString encoding);
void SaveASS(const wxString file,bool setfilename,const wxString encoding=_T("")); void SaveASS(const wxString file,bool setfilename,const wxString encoding=_T(""));
void SaveSSA(const wxString file,const wxString encoding=_T("")); void SaveSSA(const wxString file,const wxString encoding=_T(""));
@ -77,10 +76,10 @@ private:
// Manipulation operations // Manipulation operations
int AddLine(wxString data,wxString group,int lasttime,bool &IsSSA); int AddLine(wxString data,wxString group,int lasttime,bool &IsSSA);
void ConvertToSRT();
void DialogueToSRT(AssDialogue *current,std::list<AssEntry*>::iterator prev); void DialogueToSRT(AssDialogue *current,std::list<AssEntry*>::iterator prev);
// Format conversion operations // Format conversion operations
void ConvertToSRT();
void ConvertToSSA(); void ConvertToSSA();
public: public:

View file

@ -70,9 +70,9 @@ bool AegisubApp::OnInit() {
// App name // App name
SetAppName(_T("Aegisub")); SetAppName(_T("Aegisub"));
//#ifndef _DEBUG #ifndef _DEBUG
wxHandleFatalExceptions(true); wxHandleFatalExceptions(true);
//#endif #endif
// Set config file // Set config file
GetFullPath(argv[0]); GetFullPath(argv[0]);
@ -126,7 +126,7 @@ bool AegisubApp::OnInit() {
} }
//#ifndef _DEBUG #ifndef _DEBUG
/////////////////////// ///////////////////////
// Unhandled exception // Unhandled exception
void AegisubApp::OnUnhandledException() { void AegisubApp::OnUnhandledException() {
@ -155,7 +155,7 @@ void AegisubApp::OnFatalException() {
// Inform user of crash // Inform user of crash
wxMessageBox(_T("Aegisub has encountered a fatal error and will terminate now. The subtitles you were working on were saved to \"") + filename + _T("\", but they might be corrupt."), _T("Fatal error"), wxOK | wxICON_ERROR, NULL); wxMessageBox(_T("Aegisub has encountered a fatal error and will terminate now. The subtitles you were working on were saved to \"") + filename + _T("\", but they might be corrupt."), _T("Fatal error"), wxOK | wxICON_ERROR, NULL);
} }
//#endif #endif
//////////////// ////////////////

View file

@ -73,10 +73,10 @@ public:
bool OnInit(); bool OnInit();
int OnRun(); int OnRun();
//#ifndef _DEBUG #ifndef _DEBUG
void OnUnhandledException(); void OnUnhandledException();
void OnFatalException(); void OnFatalException();
//#endif #endif
//int OnRun(); //int OnRun();
DECLARE_EVENT_TABLE() DECLARE_EVENT_TABLE()

View file

@ -0,0 +1,127 @@
// Copyright (c) 2006, 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 "subtitle_format_reader.h"
#include "ass_file.h"
///////////////
// Constructor
SubtitleFormatReader::SubtitleFormatReader() {
Line = NULL;
Register();
}
//////////////
// Destructor
SubtitleFormatReader::~SubtitleFormatReader() {
Remove();
}
//////////////
// Set target
void SubtitleFormatReader::SetTarget(AssFile *file) {
if (!file) Line = NULL;
else Line = &file->Line;
assFile = file;
}
////////
// List
std::list<SubtitleFormatReader*> SubtitleFormatReader::readers;
/////////////////////////////
// Get an appropriate reader
SubtitleFormatReader *SubtitleFormatReader::GetReader(wxString filename) {
std::list<SubtitleFormatReader*>::iterator cur;
SubtitleFormatReader *reader;
for (cur=readers.begin();cur!=readers.end();cur++) {
reader = *cur;
if (reader->CanReadFile(filename)) return reader;
}
return NULL;
}
////////////
// Register
void SubtitleFormatReader::Register() {
std::list<SubtitleFormatReader*>::iterator cur;
for (cur=readers.begin();cur!=readers.end();cur++) {
if (*cur == this) return;
}
readers.push_back(this);
}
//////////
// Remove
void SubtitleFormatReader::Remove() {
std::list<SubtitleFormatReader*>::iterator cur;
for (cur=readers.begin();cur!=readers.end();cur++) {
if (*cur == this) {
readers.erase(cur);
return;
}
}
}
///////////////////
// Clear subtitles
void SubtitleFormatReader::Clear() {
assFile->Clear();
}
////////////////
// Load default
void SubtitleFormatReader::LoadDefault() {
assFile->LoadDefault();
}
///////////////////
// Set if it's ASS
void SubtitleFormatReader::SetIsASS(bool isASS) {
assFile->IsASS = isASS;
}

View file

@ -0,0 +1,77 @@
// Copyright (c) 2006, 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 <wx/wxprec.h>
#include <list>
//////////////
// Prototypes
class AssFile;
class AssEntry;
///////////////////
// Subtitle reader
class SubtitleFormatReader {
private:
void Register();
void Remove();
static std::list<SubtitleFormatReader*> readers;
AssFile *assFile;
protected:
std::list<AssEntry*> *Line;
public:
SubtitleFormatReader();
virtual ~SubtitleFormatReader();
virtual bool CanReadFile(wxString filename)=0;
virtual void ReadFile(wxString filename,wxString forceEncoding=_T(""))=0;
void SetTarget(AssFile *file);
void Clear();
void LoadDefault();
void SetIsASS(bool isASS);
static SubtitleFormatReader *GetReader(wxString filename);
};

View file

@ -0,0 +1,124 @@
// Copyright (c) 2006, 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 "subtitle_format_srt.h"
#include "text_file_reader.h"
#include "ass_dialogue.h"
/////////////
// Can read?
bool SRTSubtitleFormatReader::CanReadFile(wxString filename) {
return (filename.Right(4).Lower() == _T(".srt"));
}
/////////////
// Read file
void SRTSubtitleFormatReader::ReadFile(wxString filename,wxString encoding) {
using namespace std;
// Reader
TextFileReader file(filename,encoding);
// Default
LoadDefault();
// Parse file
int linen = 1;
int fileLine = 0;
int mode = 0;
long templ;
AssDialogue *line = NULL;
while (file.HasMoreLines()) {
// Reads line
wxString curLine = file.ReadLineFromFile();
fileLine++;
switch (mode) {
case 0:
// Checks if there is anything to read
if (curLine.IsEmpty()) continue;
// Check if it's a line number
if (!curLine.IsNumber()) {
Clear();
throw wxString::Format(_T("Parse error on entry %i at line %i (expecting line number). Possible malformed file."),linen,fileLine);
}
// Read line number
curLine.ToLong(&templ);
if (templ != linen) {
linen = templ;
}
line = new AssDialogue();
mode = 1;
break;
case 1:
// Read timestamps
if (curLine.substr(13,3) != _T("-->")) {
Clear();
throw wxString::Format(_T("Parse error on entry %i at line %i (expecting timestamps). Possible malformed file."),linen,fileLine);
}
line->Start.ParseSRT(curLine.substr(0,12));
line->End.ParseSRT(curLine.substr(17,12));
mode = 2;
break;
case 2:
// Checks if it's done
if (curLine.IsEmpty()) {
mode = 0;
linen++;
line->group = _T("[Events]");
line->Style = _T("Default");
line->Comment = false;
line->UpdateData();
line->ParseSRTTags();
line->StartMS = line->Start.GetMS();
Line->push_back(line);
break;
}
// Append text
if (line->Text != _T("")) line->Text += _T("\\N");
line->Text += curLine;
break;
}
}
}

View file

@ -0,0 +1,58 @@
// Copyright (c) 2006, 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 "subtitle_format_reader.h"
//////////////
// Prototypes
class AssDialogue;
//////////////
// SRT reader
class SRTSubtitleFormatReader : public SubtitleFormatReader {
private:
public:
bool CanReadFile(wxString filename);
void ReadFile(wxString filename,wxString forceEncoding);
};

View file

@ -42,6 +42,7 @@
// Headers // Headers
#include <wx/wxprec.h> #include <wx/wxprec.h>
#include <wx/dynarray.h> #include <wx/dynarray.h>
#include <fstream>
///////// /////////

View file

@ -47,6 +47,9 @@
/////////////// ///////////////
// Constructor // Constructor
LAVCVideoProvider::LAVCVideoProvider(wxString filename, wxString subfilename) { LAVCVideoProvider::LAVCVideoProvider(wxString filename, wxString subfilename) {
char temp[1024];
strcpy(temp,subfilename.mb_str(wxConvUTF8));
// Init variables // Init variables
codecContext = NULL; codecContext = NULL;
formatContext = NULL; formatContext = NULL;