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.
|
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// Aegisub Project http://www.aegisub.org/
|
2006-01-16 22:02:54 +01:00
|
|
|
//
|
2009-07-29 07:43:02 +02: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
|
|
|
|
2009-01-04 07:31:48 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2009-09-10 15:06:40 +02:00
|
|
|
#ifndef AGI_PRE
|
2010-06-22 02:03:33 +02:00
|
|
|
#include <algorithm>
|
2006-01-19 02:42:39 +01:00
|
|
|
#include <fstream>
|
2009-09-10 15:06:40 +02:00
|
|
|
#include <list>
|
|
|
|
|
2006-07-01 06:35:50 +02:00
|
|
|
#include <wx/filename.h>
|
2007-09-12 01:22:26 +02:00
|
|
|
#include <wx/log.h>
|
|
|
|
#include <wx/msgdlg.h>
|
2009-09-10 15:06:40 +02:00
|
|
|
#endif
|
|
|
|
|
2006-06-30 23:59:20 +02:00
|
|
|
#include "ass_attachment.h"
|
2009-09-10 15:06:40 +02:00
|
|
|
#include "ass_dialogue.h"
|
|
|
|
#include "ass_file.h"
|
|
|
|
#include "ass_override.h"
|
|
|
|
#include "ass_style.h"
|
2010-06-03 22:31:43 +02:00
|
|
|
#include "charset_detect.h"
|
2010-05-21 03:13:36 +02:00
|
|
|
#include "compat.h"
|
|
|
|
#include "main.h"
|
2011-01-16 08:16:33 +01:00
|
|
|
#include "standard_paths.h"
|
2009-09-10 15:06:40 +02:00
|
|
|
#include "subtitle_format.h"
|
2006-01-16 22:02:54 +01:00
|
|
|
#include "text_file_reader.h"
|
|
|
|
#include "text_file_writer.h"
|
2010-06-24 03:23:43 +02:00
|
|
|
#include "utils.h"
|
2006-01-16 22:02:54 +01:00
|
|
|
#include "version.h"
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
2010-07-09 09:31:34 +02:00
|
|
|
namespace std {
|
|
|
|
template<>
|
|
|
|
void swap(AssFile &lft, AssFile &rgt) {
|
|
|
|
lft.swap(rgt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-19 02:44:44 +02:00
|
|
|
/// @brief AssFile constructor
|
2010-07-09 09:31:34 +02:00
|
|
|
AssFile::AssFile ()
|
2011-09-29 20:17:27 +02:00
|
|
|
: commitId(0)
|
2010-08-02 22:25:29 +02:00
|
|
|
, loaded(false)
|
2010-07-09 09:31:34 +02:00
|
|
|
{
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2010-07-20 05:11:11 +02:00
|
|
|
/// @brief AssFile destructor
|
2006-01-16 22:02:54 +01:00
|
|
|
AssFile::~AssFile() {
|
2010-06-24 03:24:21 +02:00
|
|
|
delete_clear(Line);
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2010-07-20 05:11:11 +02:00
|
|
|
/// @brief Load generic subs
|
2010-09-15 04:46:19 +02:00
|
|
|
void AssFile::Load(const wxString &_filename,wxString charset,bool addToRecent) {
|
2006-01-16 22:02:54 +01:00
|
|
|
try {
|
2010-06-03 22:31:43 +02:00
|
|
|
if (charset.empty()) {
|
|
|
|
charset = CharSetDetect::GetEncoding(_filename);
|
|
|
|
}
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2006-02-27 01:06:46 +01:00
|
|
|
// Get proper format reader
|
2006-02-27 22:57:10 +01:00
|
|
|
SubtitleFormat *reader = SubtitleFormat::GetReader(_filename);
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2010-09-15 04:46:19 +02:00
|
|
|
if (!reader) {
|
2011-09-28 21:43:11 +02:00
|
|
|
wxMessageBox("Unknown file type","Error loading file",wxICON_ERROR | wxOK);
|
2010-09-15 04:46:19 +02:00
|
|
|
return;
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2010-09-15 04:46:19 +02: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
|
|
|
}
|
2010-06-20 21:03:05 +02:00
|
|
|
// Real exception
|
|
|
|
catch (agi::Exception &e) {
|
2011-09-28 21:43:48 +02:00
|
|
|
wxMessageBox(lagi_wxString(e.GetChainedMessage()), "Error loading file", wxICON_ERROR|wxOK);
|
2010-09-15 04:46:19 +02:00
|
|
|
return;
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Other error
|
|
|
|
catch (...) {
|
2011-09-28 21:43:11 +02:00
|
|
|
wxMessageBox("Unknown error","Error loading file",wxICON_ERROR | wxOK);
|
2010-09-15 04:46:19 +02:00
|
|
|
return;
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set general data
|
|
|
|
loaded = true;
|
2010-09-15 04:46:19 +02:00
|
|
|
filename = _filename;
|
2011-01-16 08:16:33 +01:00
|
|
|
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(path);
|
|
|
|
if (!dstpath.IsAbsolute())
|
|
|
|
path = StandardPaths::DecodePathMaybeRelative(path, "?user/");
|
|
|
|
path += "/";
|
|
|
|
dstpath.Assign(path);
|
|
|
|
|
|
|
|
if (!dstpath.DirExists()) {
|
|
|
|
wxMkdir(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
wxString backup = path + file.GetName() + ".ORIGINAL." + file.GetExt();
|
|
|
|
wxCopyFile(file.GetFullPath(), backup, true);
|
|
|
|
}
|
|
|
|
}
|
2006-01-16 22:02:54 +01:00
|
|
|
|
|
|
|
// Add comments and set vars
|
2011-07-28 00:52:37 +02:00
|
|
|
AddComment(wxString("Script generated by Aegisub ") + GetAegisubLongVersionString());
|
2011-09-28 21:43:11 +02:00
|
|
|
AddComment("http://www.aegisub.org/");
|
|
|
|
SetScriptInfo("ScriptType","v4.00+");
|
2006-12-26 05:48:53 +01:00
|
|
|
|
2010-07-09 09:31:34 +02:00
|
|
|
// Push the initial state of the file onto the undo stack
|
2010-09-15 04:46:19 +02:00
|
|
|
UndoStack.clear();
|
|
|
|
RedoStack.clear();
|
|
|
|
undoDescription.clear();
|
2011-09-15 07:16:32 +02:00
|
|
|
Commit("", COMMIT_NEW);
|
2011-09-29 20:17:27 +02:00
|
|
|
savedCommitId = commitId;
|
2010-07-09 09:31:34 +02:00
|
|
|
|
2006-12-26 05:48:53 +01:00
|
|
|
// Add to recent
|
2011-01-16 08:16:33 +01:00
|
|
|
if (addToRecent) AddToRecent(filename);
|
|
|
|
FileOpen(filename);
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2011-01-16 08:16:40 +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
|
|
|
|
2011-01-16 08:16:40 +01:00
|
|
|
if (setfilename) {
|
|
|
|
savedCommitId = commitId;
|
2011-09-28 21:50:59 +02:00
|
|
|
this->filename = filename;
|
2011-11-06 18:18:14 +01:00
|
|
|
StandardPaths::SetPathValue("?script", wxFileName(filename).GetPath());
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2011-01-16 08:16:40 +01:00
|
|
|
FileSave();
|
2006-02-27 22:57:10 +01:00
|
|
|
|
2011-01-16 08:16:40 +01:00
|
|
|
writer->SetTarget(this);
|
|
|
|
writer->WriteFile(filename, encoding);
|
2006-02-27 21:22:15 +01:00
|
|
|
|
2011-01-16 08:16:40 +01:00
|
|
|
if (addToRecent) {
|
|
|
|
AddToRecent(filename);
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-29 20:17:27 +02: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();
|
|
|
|
|
|
|
|
wxFileName dstpath(path);
|
|
|
|
if (!dstpath.IsAbsolute())
|
|
|
|
path = StandardPaths::DecodePathMaybeRelative(path, "?user/");
|
|
|
|
dstpath.AssignDir(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();
|
|
|
|
}
|
|
|
|
|
2007-01-26 23:55:42 +01:00
|
|
|
void AssFile::SaveMemory(std::vector<char> &dst,const wxString encoding) {
|
|
|
|
// Set encoding
|
|
|
|
wxString enc = encoding;
|
2011-09-28 21:43:11 +02:00
|
|
|
if (enc.IsEmpty()) enc = "UTF-8";
|
|
|
|
if (enc != "UTF-8") throw "Memory writer only supports UTF-8 for now.";
|
2007-01-26 23:55:42 +01:00
|
|
|
|
2007-10-29 03:09:45 +01:00
|
|
|
// 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());
|
|
|
|
|
2007-01-26 23:55:42 +01:00
|
|
|
// Prepare vector
|
|
|
|
dst.clear();
|
|
|
|
dst.reserve(0x4000);
|
|
|
|
|
|
|
|
// Write file
|
|
|
|
entryIter cur;
|
|
|
|
unsigned int lineSize = 0;
|
|
|
|
unsigned int targetSize = 0;
|
|
|
|
unsigned int pos = 0;
|
|
|
|
wxCharBuffer buffer;
|
|
|
|
for (cur=Line.begin();cur!=Line.end();cur++) {
|
|
|
|
// Convert
|
2011-09-28 21:43:11 +02:00
|
|
|
wxString temp = (*cur)->GetEntryData() + "\r\n";
|
2007-01-26 23:55:42 +01:00
|
|
|
buffer = temp.mb_str(wxConvUTF8);
|
|
|
|
lineSize = strlen(buffer);
|
|
|
|
|
|
|
|
// Raise capacity if needed
|
|
|
|
targetSize = dst.size() + lineSize;
|
|
|
|
if (dst.capacity() < targetSize) {
|
|
|
|
unsigned int newSize = dst.capacity();
|
|
|
|
while (newSize < targetSize) newSize *= 2;
|
|
|
|
dst.reserve(newSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append line
|
|
|
|
pos = dst.size();
|
|
|
|
dst.resize(targetSize);
|
|
|
|
memcpy(&dst[pos],buffer,lineSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-06-29 00:51:33 +02:00
|
|
|
bool AssFile::CanSave() {
|
|
|
|
// ASS format?
|
2008-03-07 00:36:43 +01:00
|
|
|
wxString ext = filename.Lower().Right(4);
|
2011-09-28 21:43:11 +02:00
|
|
|
if (ext == ".ass") return true;
|
2008-03-07 00:36:43 +01:00
|
|
|
|
|
|
|
// Never save texts
|
2011-09-28 21:43:11 +02:00
|
|
|
if (ext == ".txt") return false;
|
2006-06-29 00:51:33 +02:00
|
|
|
|
|
|
|
// 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
|
2010-05-19 02:44:52 +02:00
|
|
|
curstyle = dynamic_cast<AssStyle*>(*cur);
|
2006-06-29 00:51:33 +02:00
|
|
|
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
|
2011-10-10 19:29:26 +02:00
|
|
|
if (dynamic_cast<AssAttachment*>(*cur)) return false;
|
2006-07-01 07:15:56 +02:00
|
|
|
|
2006-06-29 00:51:33 +02:00
|
|
|
// Check dialog
|
2010-05-19 02:44:52 +02:00
|
|
|
curdiag = dynamic_cast<AssDialogue*>(*cur);
|
2006-06-29 00:51:33 +02:00
|
|
|
if (curdiag) {
|
2008-03-07 00:36:43 +01:00
|
|
|
// Timed?
|
2011-10-10 19:29:26 +02:00
|
|
|
if (curdiag->Start.GetMS() != 0 || curdiag->End.GetMS() != 0) return false;
|
2008-03-07 00:36:43 +01:00
|
|
|
|
|
|
|
// Overrides?
|
2006-06-29 00:51:33 +02:00
|
|
|
curdiag->ParseASSTags();
|
|
|
|
for (size_t i=0;i<curdiag->Blocks.size();i++) {
|
2008-01-14 01:30:00 +01:00
|
|
|
if (curdiag->Blocks[i]->GetType() != BLOCK_PLAIN) return false;
|
2006-06-29 00:51:33 +02:00
|
|
|
}
|
|
|
|
curdiag->ClearBlocks();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Success
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-06-16 08:20:14 +02:00
|
|
|
// I strongly advice you against touching this function unless you know what you're doing;
|
|
|
|
// even moving things out of order might break ASS parsing - AMZ.
|
2010-06-22 02:03:33 +02:00
|
|
|
void AssFile::AddLine(wxString data,wxString group,int &version,wxString *outGroup) {
|
2010-10-11 22:06:04 +02:00
|
|
|
if (data.empty()) return;
|
|
|
|
|
2006-07-01 02:54:33 +02:00
|
|
|
// Group
|
2006-01-16 22:02:54 +01:00
|
|
|
AssEntry *entry = NULL;
|
2006-07-01 02:54:33 +02:00
|
|
|
wxString origGroup = group;
|
|
|
|
static wxString keepGroup;
|
|
|
|
if (!keepGroup.IsEmpty()) group = keepGroup;
|
2006-07-01 04:27:37 +02:00
|
|
|
if (outGroup) *outGroup = group;
|
2007-04-21 01:07:22 +02:00
|
|
|
wxString lowGroup = group.Lower();
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2006-06-30 23:59:20 +02:00
|
|
|
// Attachment
|
2011-09-28 21:43:11 +02:00
|
|
|
if (lowGroup == "[fonts]" || lowGroup == "[graphics]") {
|
2006-06-30 23:59:20 +02:00
|
|
|
// Check if it's valid data
|
2006-07-01 01:37:30 +02:00
|
|
|
size_t dataLen = data.Length();
|
|
|
|
bool validData = (dataLen > 0) && (dataLen <= 80);
|
|
|
|
for (size_t i=0;i<dataLen;i++) {
|
2006-06-30 23:59:20 +02:00
|
|
|
if (data[i] < 33 || data[i] >= 97) validData = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is the filename line?
|
2011-09-28 21:43:11 +02:00
|
|
|
bool isFilename = (data.StartsWith("fontname: ") || data.StartsWith("filename: "));
|
2006-06-30 23:59:20 +02:00
|
|
|
|
|
|
|
// The attachment file is static, since it is built through several calls to this
|
|
|
|
// After it's done building, it's reset to NULL
|
|
|
|
static AssAttachment *attach = NULL;
|
|
|
|
|
|
|
|
// Attachment exists, and data is over
|
|
|
|
if (attach && (!validData || isFilename)) {
|
|
|
|
attach->Finish();
|
2006-07-01 02:54:33 +02:00
|
|
|
keepGroup.Clear();
|
|
|
|
group = origGroup;
|
2007-04-21 01:07:22 +02:00
|
|
|
lowGroup = group.Lower();
|
2006-06-30 23:59:20 +02:00
|
|
|
Line.push_back(attach);
|
|
|
|
attach = NULL;
|
|
|
|
}
|
|
|
|
|
2006-07-01 01:37:30 +02:00
|
|
|
// Create attachment if needed
|
|
|
|
if (isFilename) {
|
|
|
|
attach = new AssAttachment(data.Mid(10));
|
|
|
|
attach->group = group;
|
2006-07-01 02:54:33 +02:00
|
|
|
keepGroup = group;
|
2010-05-19 02:44:44 +02:00
|
|
|
return;
|
2006-07-01 01:37:30 +02:00
|
|
|
}
|
2006-06-30 23:59:20 +02:00
|
|
|
|
2006-07-01 01:37:30 +02:00
|
|
|
// Valid data?
|
2008-11-23 03:37:25 +01:00
|
|
|
if (attach && validData) {
|
2006-06-30 23:59:20 +02:00
|
|
|
// Insert data
|
|
|
|
attach->AddData(data);
|
|
|
|
|
|
|
|
// Done building
|
|
|
|
if (data.Length() < 80) {
|
|
|
|
attach->Finish();
|
2006-07-01 02:54:33 +02:00
|
|
|
keepGroup.Clear();
|
|
|
|
group = origGroup;
|
2007-04-21 01:07:22 +02:00
|
|
|
lowGroup = group.Lower();
|
2006-06-30 23:59:20 +02:00
|
|
|
entry = attach;
|
|
|
|
attach = NULL;
|
|
|
|
}
|
2006-07-01 01:37:30 +02:00
|
|
|
|
|
|
|
// Not done
|
2006-07-01 02:54:33 +02:00
|
|
|
else {
|
2010-05-19 02:44:44 +02:00
|
|
|
return;
|
2006-07-01 02:54:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dialogue
|
2011-09-28 21:43:11 +02:00
|
|
|
else if (lowGroup == "[events]") {
|
|
|
|
if (data.StartsWith("Dialogue:") || data.StartsWith("Comment:")) {
|
2007-01-08 02:00:44 +01:00
|
|
|
AssDialogue *diag = new AssDialogue(data,version);
|
2006-07-01 02:54:33 +02:00
|
|
|
//diag->ParseASSTags();
|
|
|
|
entry = diag;
|
|
|
|
entry->group = group;
|
|
|
|
}
|
2011-09-28 21:43:11 +02:00
|
|
|
else if (data.StartsWith("Format:")) {
|
|
|
|
entry = new AssEntry("Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text");
|
2006-07-01 02:54:33 +02:00
|
|
|
entry->group = group;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Style
|
2011-09-28 21:43:11 +02:00
|
|
|
else if (lowGroup == "[v4+ styles]") {
|
|
|
|
if (data.StartsWith("Style:")) {
|
2007-01-08 02:00:44 +01:00
|
|
|
AssStyle *style = new AssStyle(data,version);
|
2006-07-01 02:54:33 +02:00
|
|
|
entry = style;
|
|
|
|
entry->group = group;
|
|
|
|
}
|
2011-09-28 21:43:11 +02:00
|
|
|
if (data.StartsWith("Format:")) {
|
|
|
|
entry = 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");
|
2006-07-01 02:54:33 +02:00
|
|
|
entry->group = group;
|
2006-06-30 23:59:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Script info
|
2011-09-28 21:43:11 +02:00
|
|
|
else if (lowGroup == "[script info]") {
|
2006-06-30 23:59:20 +02:00
|
|
|
// Comment
|
2011-09-28 21:43:11 +02:00
|
|
|
if (data.StartsWith(";")) {
|
2006-01-16 22:02:54 +01:00
|
|
|
// Skip stupid comments added by other programs
|
2006-06-30 23:59:20 +02:00
|
|
|
// Of course, we'll add our own in place later... ;)
|
2010-05-19 02:44:44 +02:00
|
|
|
return;
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
2006-06-30 23:59:20 +02:00
|
|
|
|
|
|
|
// Version
|
2011-09-28 21:43:11 +02:00
|
|
|
if (data.StartsWith("ScriptType:")) {
|
2007-01-08 02:00:44 +01:00
|
|
|
wxString versionString = data.Mid(11);
|
|
|
|
versionString.Trim(true);
|
|
|
|
versionString.Trim(false);
|
|
|
|
versionString.MakeLower();
|
|
|
|
int trueVersion;
|
2011-09-28 21:43:11 +02:00
|
|
|
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";
|
2007-01-08 02:00:44 +01:00
|
|
|
if (trueVersion != version) {
|
2011-09-28 21:43:11 +02:00
|
|
|
if (!(trueVersion == 2 && version == 1)) wxLogMessage("Warning: File has the wrong extension.");
|
2007-01-08 02:00:44 +01:00
|
|
|
version = trueVersion;
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
}
|
2006-06-30 23:59:20 +02:00
|
|
|
|
|
|
|
// Everything
|
2006-01-16 22:02:54 +01:00
|
|
|
entry = new AssEntry(data);
|
|
|
|
entry->group = group;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Common entry
|
|
|
|
if (entry == NULL) {
|
|
|
|
entry = new AssEntry(data);
|
|
|
|
entry->group = group;
|
|
|
|
}
|
|
|
|
|
2006-06-30 23:59:20 +02:00
|
|
|
// Insert the line
|
2006-01-16 22:02:54 +01:00
|
|
|
Line.push_back(entry);
|
2010-05-19 02:44:44 +02:00
|
|
|
return;
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2010-06-24 03:23:43 +02:00
|
|
|
void AssFile::Clear() {
|
|
|
|
delete_clear(Line);
|
2006-01-16 22:02:54 +01:00
|
|
|
|
|
|
|
loaded = false;
|
2010-06-24 03:23:43 +02:00
|
|
|
filename.clear();
|
2010-07-09 09:31:34 +02:00
|
|
|
UndoStack.clear();
|
|
|
|
RedoStack.clear();
|
|
|
|
undoDescription.clear();
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2011-01-16 08:16:33 +01:00
|
|
|
void AssFile::LoadDefault(bool defline) {
|
2006-01-16 22:02:54 +01:00
|
|
|
Clear();
|
|
|
|
|
|
|
|
// Write headers
|
|
|
|
AssStyle defstyle;
|
2007-01-08 02:00:44 +01:00
|
|
|
int version = 1;
|
2011-09-28 21:43:11 +02:00
|
|
|
AddLine("[Script Info]","[Script Info]",version);
|
|
|
|
AddLine("Title: Default Aegisub file","[Script Info]",version);
|
|
|
|
AddLine("ScriptType: v4.00+","[Script Info]",version);
|
|
|
|
AddLine("WrapStyle: 0", "[Script Info]",version);
|
|
|
|
AddLine("ScaledBorderAndShadow: yes","[Script Info]",version);
|
|
|
|
AddLine("Collisions: Normal","[Script Info]",version);
|
|
|
|
AddLine("","[Script Info]",version);
|
|
|
|
AddLine("[V4+ Styles]","[V4+ Styles]",version);
|
|
|
|
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","[V4+ Styles]",version);
|
|
|
|
AddLine(defstyle.GetEntryData(),"[V4+ Styles]",version);
|
|
|
|
AddLine("","[V4+ Styles]",version);
|
|
|
|
AddLine("[Events]","[Events]",version);
|
|
|
|
AddLine("Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text","[Events]",version);
|
2006-01-16 22:02:54 +01:00
|
|
|
|
|
|
|
if (defline) {
|
|
|
|
AssDialogue def;
|
2011-09-28 21:43:11 +02:00
|
|
|
AddLine(def.GetEntryData(),"[Events]",version);
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2011-09-15 07:16:32 +02:00
|
|
|
Commit("", COMMIT_NEW);
|
2010-07-09 09:31:34 +02:00
|
|
|
savedCommitId = commitId;
|
2006-01-16 22:02:54 +01:00
|
|
|
loaded = true;
|
2011-01-16 08:16:33 +01:00
|
|
|
StandardPaths::SetPathValue("?script", "");
|
|
|
|
FileOpen("");
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2010-07-09 09:31:34 +02: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)
|
|
|
|
{
|
2010-06-22 02:03:33 +02:00
|
|
|
std::transform(from.Line.begin(), from.Line.end(), std::back_inserter(Line), std::mem_fun(&AssEntry::Clone));
|
|
|
|
}
|
|
|
|
AssFile& AssFile::operator=(AssFile from) {
|
2010-07-09 09:31:34 +02:00
|
|
|
std::swap(*this, from);
|
2010-06-22 02:03:33 +02:00
|
|
|
return *this;
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AssFile::InsertStyle (AssStyle *style) {
|
|
|
|
using std::list;
|
|
|
|
AssEntry *curEntry;
|
2006-01-19 02:42:39 +01:00
|
|
|
list<AssEntry*>::iterator lastStyle = Line.end();
|
2006-01-16 22:02:54 +01:00
|
|
|
list<AssEntry*>::iterator cur;
|
|
|
|
wxString lastGroup;
|
|
|
|
|
|
|
|
// Look for insert position
|
|
|
|
for (cur=Line.begin();cur!=Line.end();cur++) {
|
|
|
|
curEntry = *cur;
|
2011-09-28 21:43:11 +02:00
|
|
|
if (curEntry->GetType() == ENTRY_STYLE || (lastGroup == "[V4+ Styles]" && curEntry->GetEntryData().substr(0,7) == "Format:")) {
|
2006-01-16 22:02:54 +01:00
|
|
|
lastStyle = cur;
|
|
|
|
}
|
|
|
|
lastGroup = curEntry->group;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No styles found, add them
|
2006-01-19 02:42:39 +01:00
|
|
|
if (lastStyle == Line.end()) {
|
2006-01-16 22:02:54 +01:00
|
|
|
// Add space
|
2010-07-20 05:11:11 +02:00
|
|
|
curEntry = new AssEntry("");
|
2006-01-16 22:02:54 +01:00
|
|
|
curEntry->group = lastGroup;
|
|
|
|
Line.push_back(curEntry);
|
|
|
|
|
|
|
|
// Add header
|
2011-09-28 21:43:11 +02:00
|
|
|
curEntry = new AssEntry("[V4+ Styles]");
|
|
|
|
curEntry->group = "[V4+ Styles]";
|
2006-01-16 22:02:54 +01:00
|
|
|
Line.push_back(curEntry);
|
|
|
|
|
|
|
|
// Add format line
|
2011-09-28 21:43:11 +02:00
|
|
|
curEntry = 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");
|
|
|
|
curEntry->group = "[V4+ Styles]";
|
2006-01-16 22:02:54 +01:00
|
|
|
Line.push_back(curEntry);
|
|
|
|
|
|
|
|
// Add style
|
2011-09-28 21:43:11 +02:00
|
|
|
style->group = "[V4+ Styles]";
|
2006-01-16 22:02:54 +01:00
|
|
|
Line.push_back(style);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add to end of list
|
|
|
|
else {
|
|
|
|
lastStyle++;
|
|
|
|
style->group = (*lastStyle)->group;
|
|
|
|
Line.insert(lastStyle,style);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-01 06:08:01 +02:00
|
|
|
void AssFile::InsertAttachment (AssAttachment *attach) {
|
|
|
|
// Search for insertion point
|
|
|
|
std::list<AssEntry*>::iterator insPoint=Line.end(),cur;
|
|
|
|
for (cur=Line.begin();cur!=Line.end();cur++) {
|
|
|
|
// Check if it's another attachment
|
2010-05-19 02:44:52 +02:00
|
|
|
AssAttachment *att = dynamic_cast<AssAttachment*>(*cur);
|
2006-07-01 06:08:01 +02:00
|
|
|
if (att) {
|
|
|
|
if (attach->group == att->group) insPoint = cur;
|
|
|
|
}
|
|
|
|
|
|
|
|
// See if it's the start of group
|
|
|
|
else if ((*cur)->GetType() == ENTRY_BASE) {
|
|
|
|
AssEntry *entry = (AssEntry*) (*cur);
|
|
|
|
if (entry->GetEntryData() == attach->group) insPoint = cur;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Found point, insert there
|
|
|
|
if (insPoint != Line.end()) {
|
|
|
|
insPoint++;
|
|
|
|
Line.insert(insPoint,attach);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, create the [Fonts] group and insert
|
|
|
|
else {
|
2007-01-08 02:00:44 +01:00
|
|
|
int version=1;
|
2010-07-20 05:11:11 +02:00
|
|
|
AddLine("",Line.back()->group,version);
|
2010-05-19 02:44:44 +02:00
|
|
|
AddLine(attach->group,attach->group,version);
|
2006-07-01 06:08:01 +02:00
|
|
|
Line.push_back(attach);
|
2010-07-20 05:11:11 +02:00
|
|
|
AddLine("",attach->group,version);
|
2006-07-01 06:08:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-01 06:35:50 +02:00
|
|
|
void AssFile::InsertAttachment (wxString filename) {
|
|
|
|
wxFileName fname(filename);
|
|
|
|
AssAttachment *newAttach = new AssAttachment(fname.GetFullName());
|
|
|
|
|
|
|
|
try {
|
|
|
|
newAttach->Import(filename);
|
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
delete newAttach;
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert
|
2007-07-30 02:25:26 +02:00
|
|
|
wxString ext = filename.Right(4).Lower();
|
2011-09-28 21:43:11 +02:00
|
|
|
if (ext == ".ttf" || ext == ".ttc" || ext == ".pfb") newAttach->group = "[Fonts]";
|
|
|
|
else newAttach->group = "[Graphics]";
|
2006-07-01 06:35:50 +02:00
|
|
|
InsertAttachment(newAttach);
|
|
|
|
}
|
|
|
|
|
2011-10-25 03:16:36 +02:00
|
|
|
wxString AssFile::GetScriptInfo(wxString key) {
|
2011-11-04 20:41:43 +01:00
|
|
|
key.MakeLower();
|
2011-09-28 21:43:11 +02:00
|
|
|
key += ":";
|
2006-01-16 22:02:54 +01:00
|
|
|
bool GotIn = false;
|
|
|
|
|
2011-10-25 03:16:36 +02:00
|
|
|
for (std::list<AssEntry*>::iterator cur = Line.begin(); cur != Line.end(); ++cur) {
|
2011-09-28 21:43:11 +02:00
|
|
|
if ((*cur)->group == "[Script Info]") {
|
2006-01-16 22:02:54 +01:00
|
|
|
GotIn = true;
|
2006-02-27 03:23:50 +01:00
|
|
|
wxString curText = (*cur)->GetEntryData();
|
2011-11-28 23:16:50 +01:00
|
|
|
if (curText.Lower().StartsWith(key))
|
|
|
|
return curText.Mid(key.size()).Trim(true).Trim(false);
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
2011-10-25 03:16:36 +02:00
|
|
|
else if (GotIn) return "";
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2010-06-16 08:20:14 +02:00
|
|
|
return "";
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int AssFile::GetScriptInfoAsInt(const wxString key) {
|
|
|
|
long temp = 0;
|
2011-10-25 03:16:47 +02:00
|
|
|
GetScriptInfo(key).ToLong(&temp);
|
2006-01-16 22:02:54 +01:00
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
2011-08-27 08:42:10 +02:00
|
|
|
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
|
|
|
|
2011-08-27 08:42:10 +02:00
|
|
|
for (std::list<AssEntry*>::iterator cur = Line.begin(); cur != Line.end(); ++cur) {
|
2011-09-28 21:43:11 +02:00
|
|
|
if ((*cur)->group == "[Script Info]") {
|
2011-08-27 08:42:10 +02:00
|
|
|
found_script_info = true;
|
|
|
|
wxString cur_text = (*cur)->GetEntryData().Left(key_size).Lower();
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2011-08-27 08:42:10 +02:00
|
|
|
if (cur_text == search_key) {
|
|
|
|
if (value.empty()) {
|
|
|
|
delete *cur;
|
|
|
|
Line.erase(cur);
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
else {
|
2011-08-27 08:42:10 +02:00
|
|
|
(*cur)->SetEntryData(key + ": " + value);
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2011-08-27 08:42:10 +02:00
|
|
|
script_info_end = cur;
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
2011-08-27 08:42:10 +02: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-01 09:22:57 +02:00
|
|
|
void AssFile::GetResolution(int &sw,int &sh) {
|
2011-10-25 03:16:56 +02:00
|
|
|
sw = GetScriptInfoAsInt("PlayResX");
|
|
|
|
sh = GetScriptInfoAsInt("PlayResY");
|
2007-05-03 19:19:50 +02:00
|
|
|
|
|
|
|
// 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) {
|
2010-06-16 08:20:14 +02:00
|
|
|
// you are not crazy; this doesn't make any sense
|
2007-05-03 19:19:50 +02:00
|
|
|
if (sw == 1280)
|
|
|
|
sh = 1024;
|
|
|
|
else
|
|
|
|
sh = sw * 3 / 4;
|
|
|
|
}
|
2006-07-01 09:22:57 +02:00
|
|
|
}
|
|
|
|
|
2006-01-16 22:02:54 +01:00
|
|
|
void AssFile::AddComment(const wxString _comment) {
|
2011-09-28 21:43:11 +02:00
|
|
|
wxString comment = "; ";
|
2006-01-16 22:02:54 +01:00
|
|
|
comment += _comment;
|
|
|
|
std::list<AssEntry*>::iterator cur;
|
|
|
|
int step = 0;
|
|
|
|
|
|
|
|
for (cur=Line.begin();cur!=Line.end();cur++) {
|
|
|
|
// Start of group
|
2011-09-28 21:43:11 +02:00
|
|
|
if (step == 0 && (*cur)->group == "[Script Info]") step = 1;
|
2006-01-16 22:02:54 +01:00
|
|
|
|
|
|
|
// First line after a ;
|
2011-09-28 21:43:11 +02:00
|
|
|
else if (step == 1 && !(*cur)->GetEntryData().StartsWith(";")) {
|
2006-01-16 22:02:54 +01:00
|
|
|
AssEntry *prev = *cur;
|
|
|
|
AssEntry *comm = new AssEntry(comment);
|
|
|
|
comm->group = prev->group;
|
|
|
|
Line.insert(cur,comm);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wxArrayString AssFile::GetStyles() {
|
|
|
|
wxArrayString styles;
|
|
|
|
AssStyle *curstyle;
|
|
|
|
for (entryIter cur=Line.begin();cur!=Line.end();cur++) {
|
2010-05-19 02:44:52 +02:00
|
|
|
curstyle = dynamic_cast<AssStyle*>(*cur);
|
2006-01-16 22:02:54 +01:00
|
|
|
if (curstyle) {
|
|
|
|
styles.Add(curstyle->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return styles;
|
|
|
|
}
|
|
|
|
|
|
|
|
AssStyle *AssFile::GetStyle(wxString name) {
|
|
|
|
for (entryIter cur=Line.begin();cur!=Line.end();cur++) {
|
2010-06-16 08:20:14 +02:00
|
|
|
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) {
|
2010-06-18 04:23:27 +02:00
|
|
|
config::mru->Add("Subtitle", STD_STR(file));
|
2011-01-16 08:16:40 +01:00
|
|
|
wxFileName filepath(file);
|
|
|
|
OPT_SET("Path/Last/Subtitles")->SetString(STD_STR(filepath.GetPath()));
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2006-12-26 05:48:53 +01:00
|
|
|
wxString AssFile::GetWildcardList(int mode) {
|
2006-12-26 19:26:13 +01:00
|
|
|
if (mode == 0) return SubtitleFormat::GetWildcards(0);
|
2011-09-28 21:43:11 +02:00
|
|
|
else if (mode == 1) return "Advanced Substation Alpha (*.ass)|*.ass";
|
2006-12-26 19:26:13 +01:00
|
|
|
else if (mode == 2) return SubtitleFormat::GetWildcards(1);
|
2010-07-20 05:11:11 +02:00
|
|
|
else return "";
|
2006-12-26 05:48:53 +01:00
|
|
|
}
|
|
|
|
|
2011-09-28 21:44:24 +02:00
|
|
|
int AssFile::Commit(wxString desc, int type, int amendId, AssEntry *single_line) {
|
2010-07-09 09:31:34 +02:00
|
|
|
++commitId;
|
|
|
|
// Allow coalescing only if it's the last change and the file has not been
|
2010-09-15 04:46:19 +02:00
|
|
|
// saved since the last change
|
2011-09-29 20:17:27 +02:00
|
|
|
if (commitId == amendId+1 && RedoStack.empty() && savedCommitId+1 != commitId && autosavedCommitId+1 != commitId) {
|
2011-09-28 21:44:24 +02:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
**undo_it = *single_line;
|
|
|
|
}
|
|
|
|
else {
|
2011-09-15 07:16:32 +02:00
|
|
|
UndoStack.back() = *this;
|
2011-09-28 21:44:24 +02:00
|
|
|
}
|
2010-12-07 20:09:28 +01:00
|
|
|
AnnounceCommit(type);
|
2010-07-09 09:31:34 +02:00
|
|
|
return commitId;
|
2006-02-20 22:32:58 +01:00
|
|
|
}
|
|
|
|
|
2010-07-09 09:31:34 +02:00
|
|
|
RedoStack.clear();
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2010-07-09 09:31:34 +02:00
|
|
|
// Place copy on stack
|
|
|
|
undoDescription = desc;
|
|
|
|
UndoStack.push_back(*this);
|
2006-01-16 22:02:54 +01:00
|
|
|
|
|
|
|
// Cap depth
|
2010-05-21 03:13:36 +02:00
|
|
|
int depth = OPT_GET("Limits/Undo Levels")->GetInt();
|
2010-07-09 09:31:34 +02:00
|
|
|
while ((int)UndoStack.size() > depth) {
|
2006-02-20 22:32:58 +01:00
|
|
|
UndoStack.pop_front();
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
2007-01-26 01:47:42 +01:00
|
|
|
|
2011-01-20 06:57:23 +01:00
|
|
|
if (OPT_GET("App/Auto/Save on Every Change")->GetBool()) {
|
|
|
|
if (!filename.empty() && CanSave())
|
|
|
|
Save(filename);
|
|
|
|
}
|
|
|
|
|
2010-12-07 20:09:28 +01:00
|
|
|
AnnounceCommit(type);
|
2010-07-09 09:31:34 +02:00
|
|
|
return commitId;
|
2006-02-20 22:32:58 +01:00
|
|
|
}
|
|
|
|
|
2010-07-09 09:31:34 +02:00
|
|
|
void AssFile::Undo() {
|
|
|
|
if (UndoStack.size() <= 1) return;
|
2006-02-20 22:32:58 +01:00
|
|
|
|
2010-07-09 09:31:34 +02:00
|
|
|
RedoStack.push_back(AssFile());
|
|
|
|
std::swap(RedoStack.back(), *this);
|
|
|
|
UndoStack.pop_back();
|
|
|
|
*this = UndoStack.back();
|
2010-12-07 20:09:28 +01:00
|
|
|
|
2011-09-15 07:16:32 +02:00
|
|
|
AnnounceCommit(COMMIT_NEW);
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2010-07-09 09:31:34 +02:00
|
|
|
void AssFile::Redo() {
|
|
|
|
if (RedoStack.empty()) return;
|
2006-02-20 22:32:58 +01:00
|
|
|
|
2010-07-09 09:31:34 +02:00
|
|
|
std::swap(*this, RedoStack.back());
|
|
|
|
UndoStack.push_back(*this);
|
|
|
|
RedoStack.pop_back();
|
2010-12-07 20:09:28 +01:00
|
|
|
|
2011-09-15 07:16:32 +02:00
|
|
|
AnnounceCommit(COMMIT_NEW);
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2010-07-09 09:31:34 +02:00
|
|
|
wxString AssFile::GetUndoDescription() const {
|
|
|
|
return IsUndoStackEmpty() ? "" : UndoStack.back().undoDescription;
|
2007-01-26 01:47:42 +01:00
|
|
|
}
|
|
|
|
|
2010-07-09 09:31:34 +02:00
|
|
|
wxString AssFile::GetRedoDescription() const {
|
|
|
|
return IsRedoStackEmpty() ? "" : RedoStack.back().undoDescription;
|
2010-05-19 02:44:44 +02:00
|
|
|
}
|
2007-01-26 01:47:42 +01:00
|
|
|
|
2010-05-19 02:44:44 +02: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
|
|
|
|
2010-05-19 02:44:44 +02:00
|
|
|
void AssFile::Sort(CompFunc comp) {
|
|
|
|
Sort(Line, comp);
|
|
|
|
}
|
2010-05-19 05:24:07 +02:00
|
|
|
namespace {
|
|
|
|
struct AssEntryComp : public std::binary_function<const AssEntry*, const AssEntry*, bool> {
|
|
|
|
AssFile::CompFunc comp;
|
2010-05-19 02:44:44 +02:00
|
|
|
bool operator()(const AssEntry* a, const AssEntry* b) const {
|
|
|
|
return comp(static_cast<const AssDialogue*>(a), static_cast<const AssDialogue*>(b));
|
|
|
|
}
|
2010-05-19 05:24:07 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
void AssFile::Sort(std::list<AssEntry*> &lst, CompFunc comp) {
|
|
|
|
AssEntryComp compE;
|
2010-05-19 02:44:44 +02:00
|
|
|
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;
|
|
|
|
|
2010-07-20 05:11:11 +02:00
|
|
|
// used instead of std::list::sort for partial list sorting
|
2010-05-19 02:44:44 +02:00
|
|
|
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);
|
|
|
|
}
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
2006-01-16 22:02:54 +01:00
|
|
|
AssFile *AssFile::top;
|