Aegisub/aegisub/src/ass_file.cpp

744 lines
20 KiB
C++
Raw Normal View History

2006-01-16 22:02:54 +01:00
// Copyright (c) 2005, 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 Project http://www.aegisub.org/
2006-01-16 22:02:54 +01:00
//
// $Id$
/// @file ass_file.cpp
/// @brief Overall storage of subtitle files, undo management and more
/// @ingroup subs_storage
2006-01-16 22:02:54 +01:00
#include "config.h"
#ifndef AGI_PRE
#include <algorithm>
#include <fstream>
#include <inttypes.h>
#include <list>
#include <wx/filename.h>
#include <wx/log.h>
#include <wx/msgdlg.h>
#endif
#include "ass_attachment.h"
#include "ass_dialogue.h"
#include "ass_file.h"
#include "ass_override.h"
#include "ass_style.h"
#include "charset_detect.h"
#include "compat.h"
#include "main.h"
#include "standard_paths.h"
#include "subtitle_format.h"
2006-01-16 22:02:54 +01:00
#include "text_file_reader.h"
#include "text_file_writer.h"
#include "utils.h"
2006-01-16 22:02:54 +01:00
#include "version.h"
namespace std {
template<>
void swap(AssFile &lft, AssFile &rgt) {
lft.swap(rgt);
}
}
/// @brief AssFile constructor
AssFile::AssFile ()
: commitId(0)
, loaded(false)
{
2006-01-16 22:02:54 +01:00
}
/// @brief AssFile destructor
2006-01-16 22:02:54 +01:00
AssFile::~AssFile() {
delete_clear(Line);
2006-01-16 22:02:54 +01:00
}
/// @brief Load generic subs
void AssFile::Load(const wxString &_filename,wxString charset,bool addToRecent) {
2006-01-16 22:02:54 +01:00
try {
if (charset.empty()) {
charset = CharSetDetect::GetEncoding(_filename);
}
2006-01-16 22:02:54 +01:00
// Get proper format reader
SubtitleFormat *reader = SubtitleFormat::GetReader(_filename);
2006-01-16 22:02:54 +01:00
if (!reader) {
wxMessageBox("Unknown file type","Error loading file",wxICON_ERROR | wxOK);
return;
2006-01-16 22:02:54 +01:00
}
// Read file
AssFile temp;
reader->SetTarget(&temp);
reader->ReadFile(_filename,charset);
swap(temp);
}
catch (agi::UserCancelException const&) {
return;
2006-01-16 22:02:54 +01:00
}
// Real exception
catch (agi::Exception &e) {
wxMessageBox(lagi_wxString(e.GetChainedMessage()), "Error loading file", wxICON_ERROR|wxOK);
return;
2006-01-16 22:02:54 +01:00
}
// Other error
catch (...) {
wxMessageBox("Unknown error","Error loading file",wxICON_ERROR | wxOK);
return;
2006-01-16 22:02:54 +01:00
}
// Set general data
loaded = true;
filename = _filename;
wxFileName fn(filename);
StandardPaths::SetPathValue("?script", fn.GetPath());
// Save backup of file
if (CanSave() && OPT_GET("App/Auto/Backup")->GetBool()) {
wxFileName file(filename);
if (file.FileExists()) {
wxString path = lagi_wxString(OPT_GET("Path/Auto/Backup")->GetString());
if (path.empty()) path = file.GetPath();
wxFileName dstpath(StandardPaths::DecodePath(path));
if (!dstpath.DirExists())
wxMkdir(dstpath.GetPath());
dstpath.SetFullName(file.GetName() + ".ORIGINAL." + file.GetExt());
wxCopyFile(file.GetFullPath(), dstpath.GetFullPath(), true);
}
}
2006-01-16 22:02:54 +01:00
// Add comments and set vars
AddComment(wxString("Script generated by Aegisub ") + GetAegisubLongVersionString());
AddComment("http://www.aegisub.org/");
SetScriptInfo("ScriptType","v4.00+");
// Push the initial state of the file onto the undo stack
UndoStack.clear();
RedoStack.clear();
undoDescription.clear();
Commit("", COMMIT_NEW);
savedCommitId = commitId;
// Add to recent
if (addToRecent) AddToRecent(filename);
FileOpen(filename);
2006-01-16 22:02:54 +01:00
}
void AssFile::Save(wxString filename, bool setfilename, bool addToRecent, wxString encoding) {
SubtitleFormat *writer = SubtitleFormat::GetWriter(filename);
if (!writer)
throw "Unknown file type.";
2006-01-16 22:02:54 +01:00
if (setfilename) {
savedCommitId = commitId;
this->filename = filename;
StandardPaths::SetPathValue("?script", wxFileName(filename).GetPath());
2006-01-16 22:02:54 +01:00
}
FileSave();
writer->SetTarget(this);
writer->WriteFile(filename, encoding);
if (addToRecent) {
AddToRecent(filename);
2006-01-16 22:02:54 +01:00
}
}
wxString AssFile::AutoSave() {
if (!loaded || commitId == autosavedCommitId)
return "";
wxFileName origfile(filename);
wxString path = lagi_wxString(OPT_GET("Path/Auto/Save")->GetString());
if (!path)
path = origfile.GetPath();
path = StandardPaths::DecodePath(path);
wxFileName dstpath(path);
if (!dstpath.DirExists())
wxMkdir(path);
wxString name = origfile.GetName();
if (name.empty())
dstpath.SetFullName("Untitled.AUTOSAVE.ass");
else
dstpath.SetFullName(name + ".AUTOSAVE.ass");
Save(dstpath.GetFullPath(), false, false);
autosavedCommitId = commitId;
return dstpath.GetFullPath();
}
void AssFile::SaveMemory(std::vector<char> &dst) {
// Check if subs contain at least one style
// Add a default style if they don't for compatibility with libass/asa
if (GetStyles().Count() == 0)
InsertStyle(new AssStyle);
// Prepare vector
dst.clear();
dst.reserve(0x4000);
// Write file
for (entryIter cur = Line.begin(); cur != Line.end(); ++cur) {
wxCharBuffer buffer = ((*cur)->GetEntryData() + "\r\n").utf8_str();
copy(buffer.data(), buffer.data() + buffer.length(), back_inserter(dst));
}
}
bool AssFile::CanSave() {
// ASS format?
wxString ext = filename.Lower().Right(4);
if (ext == ".ass") return true;
// Never save texts
if (ext == ".txt") return false;
// Check if it's a known extension
SubtitleFormat *writer = SubtitleFormat::GetWriter(filename);
if (!writer) return false;
// Scan through the lines
AssStyle defstyle;
AssStyle *curstyle;
AssDialogue *curdiag;
for (entryIter cur=Line.begin();cur!=Line.end();cur++) {
// Check style, if anything non-default is found, return false
curstyle = dynamic_cast<AssStyle*>(*cur);
if (curstyle) {
if (curstyle->GetEntryData() != defstyle.GetEntryData()) return false;
}
2006-07-01 07:15:56 +02:00
// Check for attachments, if any is found, return false
if (dynamic_cast<AssAttachment*>(*cur)) return false;
2006-07-01 07:15:56 +02:00
// Check dialog
curdiag = dynamic_cast<AssDialogue*>(*cur);
if (curdiag) {
// Timed?
if (curdiag->Start != 0 || curdiag->End != 0) return false;
// Overrides?
curdiag->ParseASSTags();
for (size_t i=0;i<curdiag->Blocks.size();i++) {
if (curdiag->Blocks[i]->GetType() != BLOCK_PLAIN) return false;
}
curdiag->ClearBlocks();
}
}
// Success
return true;
}
void AssFile::AddLine(wxString data, int *version, AssAttachment **attach) {
// Is this line an attachment filename?
bool isFilename = data.StartsWith("fontname: ") || data.StartsWith("filename: ");
2006-01-16 22:02:54 +01:00
// If there's an attachment in progress, deal with it first as an
// attachment data line can appear to be other things
if (*attach) {
// Check if it's valid data
bool validData = data.size() > 0 && data.size() <= 80;
for (size_t i = 0; i < data.size(); ++i) {
if (data[i] < 33 || data[i] >= 97) {
validData = false;
break;
}
}
// Data is over, add attachment to the file
if (!validData || isFilename) {
(*attach)->Finish();
Line.push_back(*attach);
*attach = NULL;
}
else {
// Insert data
(*attach)->AddData(data);
// Done building
if (data.Length() < 80) {
(*attach)->Finish();
Line.push_back(*attach);
*attach = NULL;
return;
}
}
}
if (data.empty()) return;
// Section header
if (data[0] == '[' && data.Last() == ']') {
// Ugly hacks to allow intermixed v4 and v4+ style sections
wxString low = data.Lower();
if (low == "[v4 styles]") {
data = "[V4+ Styles]";
*version = 0;
}
else if (low == "[v4+ styles]") {
data = "[V4+ Styles]";
*version = 1;
}
else if (low == "[v4++ styles]") {
data = "[V4+ Styles]";
*version = 2;
}
Line.push_back(new AssEntry(data, data));
return;
}
// If the first nonblank line isn't a header pretend it starts with [Script Info]
if (Line.empty())
Line.push_back(new AssEntry("[Script Info]", "[Script Info]"));
wxString group = Line.back()->group;
wxString lowGroup = group.Lower();
// Attachment
if (lowGroup == "[fonts]" || lowGroup == "[graphics]") {
if (isFilename) {
*attach = new AssAttachment(data.Mid(10));
(*attach)->group = group;
}
}
// Dialogue
else if (lowGroup == "[events]") {
if (data.StartsWith("Dialogue:") || data.StartsWith("Comment:"))
Line.push_back(new AssDialogue(data, *version));
else if (data.StartsWith("Format:"))
Line.push_back(new AssEntry("Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text", group));
}
// Style
else if (lowGroup == "[v4+ styles]") {
if (data.StartsWith("Style:"))
Line.push_back(new AssStyle(data, *version));
else if (data.StartsWith("Format:"))
Line.push_back(new AssEntry("Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding", group));
}
// Script info
else if (lowGroup == "[script info]") {
// Comment
if (data.StartsWith(";")) {
2006-01-16 22:02:54 +01:00
// Skip stupid comments added by other programs
// Of course, we'll add our own in place later... ;)
return;
2006-01-16 22:02:54 +01:00
}
if (data.StartsWith("ScriptType:")) {
wxString versionString = data.Mid(11).Trim(true).Trim(false).Lower();
int trueVersion;
if (versionString == "v4.00")
trueVersion = 0;
else if (versionString == "v4.00+")
trueVersion = 1;
else if (versionString == "v4.00++")
trueVersion = 2;
else throw
"Unknown SSA file format version";
if (trueVersion != *version) {
if (!(trueVersion == 2 && *version == 1))
wxLogMessage("Warning: File has the wrong extension.");
*version = trueVersion;
2006-01-16 22:02:54 +01:00
}
}
// Everything
Line.push_back(new AssEntry(data, group));
2006-01-16 22:02:54 +01:00
}
// Unrecognized group
else {
Line.push_back(new AssEntry(data, group));
2006-01-16 22:02:54 +01:00
}
}
void AssFile::Clear() {
delete_clear(Line);
2006-01-16 22:02:54 +01:00
loaded = false;
filename.clear();
UndoStack.clear();
RedoStack.clear();
undoDescription.clear();
2006-01-16 22:02:54 +01:00
}
void AssFile::LoadDefault(bool defline) {
2006-01-16 22:02:54 +01:00
Clear();
// Write headers
AssAttachment *attach = 0;
int version = 1;
AddLine("[Script Info]", &version, &attach);
AddLine("Title: Default Aegisub file", &version, &attach);
AddLine("ScriptType: v4.00+", &version, &attach);
AddLine("WrapStyle: 0", &version, &attach);
AddLine("ScaledBorderAndShadow: yes", &version, &attach);
AddLine("Collisions: Normal", &version, &attach);
if (!OPT_GET("Subtitle/Default Resolution/Auto")->GetBool()) {
AddLine(wxString::Format("PlayResX: %" PRId64, OPT_GET("Subtitle/Default Resolution/Width")->GetInt()), &version, &attach);
AddLine(wxString::Format("PlayResY: %" PRId64, OPT_GET("Subtitle/Default Resolution/Height")->GetInt()), &version, &attach);
2006-01-16 22:02:54 +01:00
}
AddLine("[V4+ Styles]", &version, &attach);
AddLine("Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding", &version, &attach);
AddLine(AssStyle().GetEntryData(), &version, &attach);
AddLine("[Events]", &version, &attach);
AddLine("Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text", &version, &attach);
if (defline)
AddLine(AssDialogue().GetEntryData(), &version, &attach);
2006-01-16 22:02:54 +01:00
Commit("", COMMIT_NEW);
savedCommitId = commitId;
2006-01-16 22:02:54 +01:00
loaded = true;
StandardPaths::SetPathValue("?script", "");
FileOpen("");
2006-01-16 22:02:54 +01:00
}
void AssFile::swap(AssFile &that) throw() {
// Intentionally does not swap undo stack related things
std::swap(loaded, that.loaded);
std::swap(commitId, that.commitId);
std::swap(undoDescription, that.undoDescription);
std::swap(Line, that.Line);
}
AssFile::AssFile(const AssFile &from)
: undoDescription(from.undoDescription)
, commitId(from.commitId)
, filename(from.filename)
, loaded(from.loaded)
{
std::transform(from.Line.begin(), from.Line.end(), std::back_inserter(Line), std::mem_fun(&AssEntry::Clone));
}
AssFile& AssFile::operator=(AssFile from) {
std::swap(*this, from);
return *this;
2006-01-16 22:02:54 +01:00
}
void AssFile::InsertStyle(AssStyle *style) {
// Search for insertion point
std::list<AssEntry*>::iterator it = Line.end();
do {
--it;
if ((*it)->group == style->group) {
Line.insert(++it, style);
return;
2006-01-16 22:02:54 +01:00
}
} while (it != Line.begin());
2006-01-16 22:02:54 +01:00
// No styles found, add them
Line.push_back(new AssEntry("[V4+ Styles]", "[V4+ Styles]"));
Line.push_back(new AssEntry("Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding", "[V4+ Styles]"));
Line.push_back(style);
2006-01-16 22:02:54 +01:00
}
void AssFile::InsertAttachment(AssAttachment *attach) {
// Search for insertion point
std::list<AssEntry*>::iterator it = Line.end();
do {
--it;
if ((*it)->group == attach->group) {
Line.insert(++it, attach);
return;
}
} while (it != Line.begin());
// Didn't find a group of the appropriate type so create it
Line.push_back(new AssEntry(attach->group, attach->group));
Line.push_back(attach);
}
void AssFile::InsertAttachment (wxString filename) {
std::auto_ptr<AssAttachment> newAttach(new AssAttachment(wxFileName(filename).GetFullName()));
newAttach->Import(filename);
// Insert
wxString ext = filename.Right(4).Lower();
if (ext == ".ttf" || ext == ".ttc" || ext == ".pfb")
newAttach->group = "[Fonts]";
else
newAttach->group = "[Graphics]";
InsertAttachment(newAttach.release());
}
wxString AssFile::GetScriptInfo(wxString key) {
key.MakeLower();
key += ":";
2006-01-16 22:02:54 +01:00
bool GotIn = false;
for (std::list<AssEntry*>::iterator cur = Line.begin(); cur != Line.end(); ++cur) {
if ((*cur)->group == "[Script Info]") {
2006-01-16 22:02:54 +01:00
GotIn = true;
wxString curText = (*cur)->GetEntryData();
if (curText.Lower().StartsWith(key))
return curText.Mid(key.size()).Trim(true).Trim(false);
2006-01-16 22:02:54 +01:00
}
else if (GotIn) return "";
2006-01-16 22:02:54 +01:00
}
return "";
2006-01-16 22:02:54 +01:00
}
int AssFile::GetScriptInfoAsInt(const wxString key) {
long temp = 0;
GetScriptInfo(key).ToLong(&temp);
2006-01-16 22:02:54 +01:00
return temp;
}
void AssFile::SetScriptInfo(wxString const& key, wxString const& value) {
wxString search_key = key.Lower() + ":";
size_t key_size = search_key.size();
std::list<AssEntry*>::iterator script_info_end;
bool found_script_info = false;
2006-01-16 22:02:54 +01:00
for (std::list<AssEntry*>::iterator cur = Line.begin(); cur != Line.end(); ++cur) {
if ((*cur)->group == "[Script Info]") {
found_script_info = true;
wxString cur_text = (*cur)->GetEntryData().Left(key_size).Lower();
2006-01-16 22:02:54 +01:00
if (cur_text == search_key) {
if (value.empty()) {
delete *cur;
Line.erase(cur);
2006-01-16 22:02:54 +01:00
}
else {
(*cur)->SetEntryData(key + ": " + value);
2006-01-16 22:02:54 +01:00
}
return;
}
script_info_end = cur;
2006-01-16 22:02:54 +01:00
}
else if (found_script_info) {
if (value.size()) {
AssEntry *entry = new AssEntry(key + ": " + value);
entry->group = "[Script Info]";
Line.insert(script_info_end, entry);
2006-01-16 22:02:54 +01:00
}
return;
}
}
}
void AssFile::GetResolution(int &sw,int &sh) {
sw = GetScriptInfoAsInt("PlayResX");
sh = GetScriptInfoAsInt("PlayResY");
// Gabest logic?
if (sw == 0 && sh == 0) {
sw = 384;
sh = 288;
} else if (sw == 0) {
if (sh == 1024)
sw = 1280;
else
sw = sh * 4 / 3;
} else if (sh == 0) {
// you are not crazy; this doesn't make any sense
if (sw == 1280)
sh = 1024;
else
sh = sw * 3 / 4;
}
}
void AssFile::AddComment(wxString comment) {
comment.Prepend("; ");
2006-01-16 22:02:54 +01:00
int step = 0;
for (std::list<AssEntry*>::iterator cur = Line.begin(); cur != Line.end(); ++cur) {
2006-01-16 22:02:54 +01:00
// Start of group
if (step == 0 && (*cur)->group == "[Script Info]")
step = 1;
2006-01-16 22:02:54 +01:00
// First line after a ;
else if (step == 1 && !(*cur)->GetEntryData().StartsWith(";")) {
Line.insert(cur, new AssEntry(comment, "[Script Info]"));
2006-01-16 22:02:54 +01:00
break;
}
}
}
wxArrayString AssFile::GetStyles() {
wxArrayString styles;
for (entryIter cur = Line.begin(); cur != Line.end(); ++cur) {
if (AssStyle *curstyle = dynamic_cast<AssStyle*>(*cur))
2006-01-16 22:02:54 +01:00
styles.Add(curstyle->name);
}
return styles;
}
AssStyle *AssFile::GetStyle(wxString name) {
for (entryIter cur=Line.begin();cur!=Line.end();cur++) {
AssStyle *curstyle = dynamic_cast<AssStyle*>(*cur);
if (curstyle && curstyle->name == name)
return curstyle;
2006-01-16 22:02:54 +01:00
}
return NULL;
}
void AssFile::AddToRecent(wxString file) {
config::mru->Add("Subtitle", STD_STR(file));
wxFileName filepath(file);
OPT_SET("Path/Last/Subtitles")->SetString(STD_STR(filepath.GetPath()));
2006-01-16 22:02:54 +01:00
}
wxString AssFile::GetWildcardList(int mode) {
if (mode == 0) return SubtitleFormat::GetWildcards(0);
else if (mode == 1) return "Advanced Substation Alpha (*.ass)|*.ass";
else if (mode == 2) return SubtitleFormat::GetWildcards(1);
else return "";
}
int AssFile::Commit(wxString desc, int type, int amendId, AssEntry *single_line) {
++commitId;
// Allow coalescing only if it's the last change and the file has not been
// saved since the last change
if (commitId == amendId+1 && RedoStack.empty() && savedCommitId+1 != commitId && autosavedCommitId+1 != commitId) {
// If only one line changed just modify it instead of copying the file
if (single_line) {
entryIter this_it = Line.begin(), undo_it = UndoStack.back().Line.begin();
while (*this_it != single_line) {
++this_it;
++undo_it;
}
delete *undo_it;
*undo_it = single_line->Clone();
}
else {
UndoStack.back() = *this;
}
AnnounceCommit(type);
return commitId;
}
RedoStack.clear();
2006-01-16 22:02:54 +01:00
// Place copy on stack
undoDescription = desc;
UndoStack.push_back(*this);
2006-01-16 22:02:54 +01:00
// Cap depth
int depth = OPT_GET("Limits/Undo Levels")->GetInt();
while ((int)UndoStack.size() > depth) {
UndoStack.pop_front();
2006-01-16 22:02:54 +01:00
}
2007-01-26 01:47:42 +01:00
if (OPT_GET("App/Auto/Save on Every Change")->GetBool()) {
if (!filename.empty() && CanSave())
Save(filename);
}
AnnounceCommit(type);
return commitId;
}
void AssFile::Undo() {
if (UndoStack.size() <= 1) return;
RedoStack.push_back(AssFile());
std::swap(RedoStack.back(), *this);
UndoStack.pop_back();
*this = UndoStack.back();
AnnounceCommit(COMMIT_NEW);
2006-01-16 22:02:54 +01:00
}
void AssFile::Redo() {
if (RedoStack.empty()) return;
std::swap(*this, RedoStack.back());
UndoStack.push_back(*this);
RedoStack.pop_back();
AnnounceCommit(COMMIT_NEW);
2006-01-16 22:02:54 +01:00
}
wxString AssFile::GetUndoDescription() const {
return IsUndoStackEmpty() ? "" : UndoStack.back().undoDescription;
2007-01-26 01:47:42 +01:00
}
wxString AssFile::GetRedoDescription() const {
return IsRedoStackEmpty() ? "" : RedoStack.back().undoDescription;
}
2007-01-26 01:47:42 +01:00
bool AssFile::CompStart(const AssDialogue* lft, const AssDialogue* rgt) {
return lft->Start < rgt->Start;
}
bool AssFile::CompEnd(const AssDialogue* lft, const AssDialogue* rgt) {
return lft->End < rgt->End;
}
bool AssFile::CompStyle(const AssDialogue* lft, const AssDialogue* rgt) {
return lft->Style < rgt->Style;
2007-01-26 01:47:42 +01:00
}
2006-01-16 22:02:54 +01:00
void AssFile::Sort(CompFunc comp) {
Sort(Line, comp);
}
namespace {
struct AssEntryComp : public std::binary_function<const AssEntry*, const AssEntry*, bool> {
AssFile::CompFunc comp;
bool operator()(const AssEntry* a, const AssEntry* b) const {
return comp(static_cast<const AssDialogue*>(a), static_cast<const AssDialogue*>(b));
}
};
}
void AssFile::Sort(std::list<AssEntry*> &lst, CompFunc comp) {
AssEntryComp compE;
compE.comp = comp;
// Sort each block of AssDialogues separately, leaving everything else untouched
for (entryIter begin = lst.begin(); begin != lst.end(); ++begin) {
if (!dynamic_cast<AssDialogue*>(*begin)) continue;
entryIter end = begin;
while (end != lst.end() && dynamic_cast<AssDialogue*>(*end)) ++end;
// used instead of std::list::sort for partial list sorting
std::list<AssEntry*> tmp;
tmp.splice(tmp.begin(), lst, begin, end);
tmp.sort(compE);
lst.splice(end, tmp);
begin = --end;
}
}
void AssFile::Sort(std::list<AssDialogue*> &lst, CompFunc comp) {
lst.sort(comp);
}
2006-01-16 22:02:54 +01:00
AssFile *AssFile::top;