Clean up AssFile::Load, eliminating some redundant checks, and fixing a few situations where the undo stack could be left in an inconsistant state

Originally committed to SVN as r4785.
This commit is contained in:
Thomas Goyne 2010-09-15 02:46:19 +00:00
parent 9108ea9b00
commit 84b8877d1d
1 changed files with 27 additions and 40 deletions

View File

@ -81,70 +81,53 @@ AssFile::~AssFile() {
/// @brief Load generic subs
void AssFile::Load(const wxString &_filename,wxString charset,bool addToRecent) {
bool ok = false;
Clear();
try {
// Try to open file
FILE *file;
#ifdef WIN32
file = _tfopen(_filename.c_str(), _T("r"));
#else
file = fopen(_filename.mb_str(*wxConvFileName), "r");
#endif
if (!file) {
throw _T("Unable to open file \"") + _filename + _T("\". Check if it exists and if you have permissions to read it.");
}
fclose(file);
// Find file encoding
if (charset.empty()) {
charset = CharSetDetect::GetEncoding(_filename);
}
// Generic preparation
Clear();
// Get proper format reader
SubtitleFormat *reader = SubtitleFormat::GetReader(_filename);
if (!reader) {
wxMessageBox(L"Unknown file type","Error loading file",wxICON_ERROR | wxOK);
return;
}
// Read file
if (reader) {
AssFile temp;
reader->SetTarget(&temp);
reader->ReadFile(_filename,charset);
swap(temp);
ok = true;
}
// Couldn't find a type
else throw _T("Unknown file type.");
catch (agi::UserCancelException const&) {
return;
}
catch (agi::UserCancelException const&) { }
catch (const wchar_t *except) {
wxMessageBox(except,_T("Error loading file"),wxICON_ERROR | wxOK);
return;
}
catch (wxString except) {
catch (wxString &except) {
wxMessageBox(except,_T("Error loading file"),wxICON_ERROR | wxOK);
return;
}
// Real exception
catch (agi::Exception &e) {
wxMessageBox(wxString(e.GetChainedMessage().c_str(), wxConvUTF8), L"Error loading file", wxICON_ERROR|wxOK);
return;
}
// Other error
catch (...) {
wxMessageBox(_T("Unknown error"),_T("Error loading file"),wxICON_ERROR | wxOK);
return;
}
// Verify loading
if (ok) filename = _filename;
else LoadDefault();
// Set general data
loaded = true;
filename = _filename;
// Add comments and set vars
AddComment(_T("Script generated by Aegisub ") + GetAegisubLongVersionString());
@ -152,11 +135,15 @@ void AssFile::Load (const wxString &_filename,wxString charset,bool addToRecent)
SetScriptInfo(_T("ScriptType"),_T("v4.00+"));
// Push the initial state of the file onto the undo stack
Commit("", commitId);
savedCommitId = commitId;
UndoStack.clear();
RedoStack.clear();
undoDescription.clear();
commitId = -1;
savedCommitId = 0;
Commit("");
// Add to recent
if (addToRecent && ok) AddToRecent(_filename);
if (addToRecent) AddToRecent(_filename);
}
void AssFile::Save(wxString _filename,bool setfilename,bool addToRecent,const wxString encoding) {
@ -771,8 +758,8 @@ wxString AssFile::GetWildcardList(int mode) {
int AssFile::Commit(wxString desc, int amendId) {
++commitId;
// Allow coalescing only if it's the last change and the file has not been
// saved since the last change and the undo stack isn't empty
if (commitId == amendId+1 && RedoStack.empty() && savedCommitId != commitId && !UndoStack.empty()) {
// saved since the last change
if (commitId == amendId+1 && RedoStack.empty() && savedCommitId != commitId) {
UndoStack.back() = *this;
return commitId;
}