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

View file

@ -80,71 +80,54 @@ AssFile::~AssFile() {
} }
/// @brief Load generic subs /// @brief Load generic subs
void AssFile::Load (const wxString &_filename,wxString charset,bool addToRecent) { void AssFile::Load(const wxString &_filename,wxString charset,bool addToRecent) {
bool ok = false;
Clear();
try { 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()) { if (charset.empty()) {
charset = CharSetDetect::GetEncoding(_filename); charset = CharSetDetect::GetEncoding(_filename);
} }
// Generic preparation
Clear();
// Get proper format reader // Get proper format reader
SubtitleFormat *reader = SubtitleFormat::GetReader(_filename); SubtitleFormat *reader = SubtitleFormat::GetReader(_filename);
// Read file if (!reader) {
if (reader) { wxMessageBox(L"Unknown file type","Error loading file",wxICON_ERROR | wxOK);
AssFile temp; return;
reader->SetTarget(&temp);
reader->ReadFile(_filename,charset);
swap(temp);
ok = true;
} }
// Couldn't find a type // Read file
else throw _T("Unknown file type."); AssFile temp;
reader->SetTarget(&temp);
reader->ReadFile(_filename,charset);
swap(temp);
}
catch (agi::UserCancelException const&) {
return;
} }
catch (agi::UserCancelException const&) { }
catch (const wchar_t *except) { catch (const wchar_t *except) {
wxMessageBox(except,_T("Error loading file"),wxICON_ERROR | wxOK); 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); wxMessageBox(except,_T("Error loading file"),wxICON_ERROR | wxOK);
return;
} }
// Real exception // Real exception
catch (agi::Exception &e) { catch (agi::Exception &e) {
wxMessageBox(wxString(e.GetChainedMessage().c_str(), wxConvUTF8), L"Error loading file", wxICON_ERROR|wxOK); wxMessageBox(wxString(e.GetChainedMessage().c_str(), wxConvUTF8), L"Error loading file", wxICON_ERROR|wxOK);
return;
} }
// Other error // Other error
catch (...) { catch (...) {
wxMessageBox(_T("Unknown error"),_T("Error loading file"),wxICON_ERROR | wxOK); wxMessageBox(_T("Unknown error"),_T("Error loading file"),wxICON_ERROR | wxOK);
return;
} }
// Verify loading
if (ok) filename = _filename;
else LoadDefault();
// Set general data // Set general data
loaded = true; loaded = true;
filename = _filename;
// Add comments and set vars // Add comments and set vars
AddComment(_T("Script generated by Aegisub ") + GetAegisubLongVersionString()); 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+")); SetScriptInfo(_T("ScriptType"),_T("v4.00+"));
// Push the initial state of the file onto the undo stack // Push the initial state of the file onto the undo stack
Commit("", commitId); UndoStack.clear();
savedCommitId = commitId; RedoStack.clear();
undoDescription.clear();
commitId = -1;
savedCommitId = 0;
Commit("");
// Add to recent // Add to recent
if (addToRecent && ok) AddToRecent(_filename); if (addToRecent) AddToRecent(_filename);
} }
void AssFile::Save(wxString _filename,bool setfilename,bool addToRecent,const wxString encoding) { 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) { int AssFile::Commit(wxString desc, int amendId) {
++commitId; ++commitId;
// Allow coalescing only if it's the last change and the file has not been // 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 // saved since the last change
if (commitId == amendId+1 && RedoStack.empty() && savedCommitId != commitId && !UndoStack.empty()) { if (commitId == amendId+1 && RedoStack.empty() && savedCommitId != commitId) {
UndoStack.back() = *this; UndoStack.back() = *this;
return commitId; return commitId;
} }