Also remove empty attachment sections not at the end of the file

Originally committed to SVN as r6043.
This commit is contained in:
Thomas Goyne 2011-12-22 21:16:14 +00:00
parent 921f6c3bcf
commit ef32c75609

View file

@ -91,8 +91,7 @@ DialogAttachments::DialogAttachments(wxWindow *parent, AssFile *ass)
wxSizer *mainSizer = new wxBoxSizer(wxVERTICAL); wxSizer *mainSizer = new wxBoxSizer(wxVERTICAL);
mainSizer->Add(listView,1,wxTOP | wxLEFT | wxRIGHT | wxEXPAND,5); mainSizer->Add(listView,1,wxTOP | wxLEFT | wxRIGHT | wxEXPAND,5);
mainSizer->Add(buttonSizer,0,wxALL | wxEXPAND,5); mainSizer->Add(buttonSizer,0,wxALL | wxEXPAND,5);
mainSizer->SetSizeHints(this); SetSizerAndFit(mainSizer);
SetSizer(mainSizer);
CenterOnParent(); CenterOnParent();
} }
@ -211,44 +210,30 @@ void DialogAttachments::OnDelete(wxCommandEvent &) {
i = listView->GetNextSelected(i); i = listView->GetNextSelected(i);
} }
// Remove empty attachment sections at the end of the file // Remove empty attachment sections in the file
std::list<AssEntry*>::iterator cur = ass->Line.end(); for (std::list<AssEntry*>::iterator it = ass->Line.begin(); it != ass->Line.end(); ) {
--cur; if ((*it)->GetType() == ENTRY_BASE && ((*it)->group == "[Fonts]" || (*it)->group == "[Graphics]")) {
wxString group = (*it)->group;
std::list<AssEntry*>::iterator header = it;
bool found_attachments = false; bool has_attachments = false;
wxString last_section_name; for (++it; it != ass->Line.end() && (*it)->group == group; ++it) {
if ((*it)->GetType() == ENTRY_ATTACHMENT) {
while (cur != ass->Line.begin()) { has_attachments = true;
if (!((*cur)->group == "[Fonts]" || (*cur)->group == "[Graphics]"))
break; break;
if ((*cur)->GetEntryData() == "[Fonts]" || (*cur)->GetEntryData() == "[Graphics]") {
if (found_attachments) {
--cur;
continue;
} }
// found section heading with no attachments in, remove it
wxString delgroup = (*cur)->group;
std::list<AssEntry*>::iterator di = cur;
while (++di != ass->Line.end() && (*di)->group == delgroup) {
delete *di;
ass->Line.erase(di);
di = cur;
}
di = cur;
--cur;
delete *di;
ass->Line.erase(di);
continue;
} }
if (last_section_name != (*cur)->group) // Empty group found, delete it
found_attachments = false; if (!has_attachments) {
if (dynamic_cast<AssAttachment*>(*cur) != 0) while (header != it) {
found_attachments = true; delete *header;
last_section_name = (*cur)->group; ass->Line.erase(header++);
}
--cur; }
}
else
++it;
} }
ass->Commit(_("remove attachment"), AssFile::COMMIT_ATTACHMENT); ass->Commit(_("remove attachment"), AssFile::COMMIT_ATTACHMENT);