Uncrustify AssStyleStorage a bit

Originally committed to SVN as r6413.
This commit is contained in:
Thomas Goyne 2012-02-01 00:48:24 +00:00
parent ac5af24014
commit 3be4693480
3 changed files with 63 additions and 106 deletions

View file

@ -34,56 +34,47 @@
/// @ingroup style_editor /// @ingroup style_editor
/// ///
////////////
// Includes
#include "config.h" #include "config.h"
#include "ass_style_storage.h"
#ifndef AGI_PRE #ifndef AGI_PRE
#include <fstream> #include <tr1/functional>
#endif #endif
#include "ass_file.h"
#include "ass_style.h" #include "ass_style.h"
#include "ass_style_storage.h"
#include "standard_paths.h" #include "standard_paths.h"
#include "text_file_reader.h" #include "text_file_reader.h"
#include "text_file_writer.h" #include "text_file_writer.h"
#include "utils.h"
AssStyleStorage::~AssStyleStorage() {
/// @brief Save styles to disk delete_clear(style);
/// @param name
/// @return
///
void AssStyleStorage::Save(wxString name) {
if (name.IsEmpty()) return;
TextFileWriter file(StandardPaths::DecodePath("?user/catalog/"+name+".sty"), "UTF-8");
for (std::list<AssStyle*>::iterator cur=style.begin();cur!=style.end();cur++) {
file.WriteLineToFile((*cur)->GetEntryData());
}
} }
void AssStyleStorage::Save(wxString const& name) {
if (name.empty()) return;
wxString dirname = StandardPaths::DecodePath("?user/catalog/");
if (!wxDirExists(dirname) && !wxMkdir(dirname))
throw "Failed creating directory for style catalogs";
/// @brief Load styles from disk TextFileWriter file(StandardPaths::DecodePath("?user/catalog/" + name + ".sty"), "UTF-8");
/// @param name for (std::list<AssStyle*>::iterator cur = style.begin(); cur != style.end(); ++cur)
/// @return file.WriteLineToFile((*cur)->GetEntryData());
/// }
void AssStyleStorage::Load(wxString name) {
if (name.IsEmpty()) return; void AssStyleStorage::Load(wxString const& name) {
if (name.empty()) return;
Clear(); Clear();
TextFileReader file(StandardPaths::DecodePath("?user/catalog/"+name+".sty"), "UTF-8"); TextFileReader file(StandardPaths::DecodePath("?user/catalog/" + name + ".sty"), "UTF-8");
AssStyle *curStyle;
while (file.HasMoreLines()) { while (file.HasMoreLines()) {
wxString data = file.ReadLineFromFile(); wxString data = file.ReadLineFromFile();
if (data.substr(0,6) == "Style:") { if (data.StartsWith("Style:")) {
try { try {
curStyle = new AssStyle(data); style.push_back(new AssStyle(data));
style.push_back(curStyle);
} catch(...) { } catch(...) {
/* just ignore invalid lines for now */ /* just ignore invalid lines for now */
} }
@ -91,23 +82,10 @@ void AssStyleStorage::Load(wxString name) {
} }
} }
/// @brief Clear
///
void AssStyleStorage::Clear () { void AssStyleStorage::Clear () {
using std::list; delete_clear(style);
for (list<AssStyle*>::iterator cur=style.begin();cur!=style.end();cur++) {
delete *cur;
}
style.clear();
} }
/// @brief Get names
/// @return
///
wxArrayString AssStyleStorage::GetNames() { wxArrayString AssStyleStorage::GetNames() {
wxArrayString names; wxArrayString names;
for (std::list<AssStyle*>::iterator cur=style.begin();cur!=style.end();cur++) { for (std::list<AssStyle*>::iterator cur=style.begin();cur!=style.end();cur++) {
@ -116,16 +94,10 @@ wxArrayString AssStyleStorage::GetNames() {
return names; return names;
} }
/// @brief Get a style by name
/// @param name
///
AssStyle *AssStyleStorage::GetStyle(wxString name) { AssStyle *AssStyleStorage::GetStyle(wxString name) {
for (std::list<AssStyle*>::iterator cur=style.begin();cur!=style.end();cur++) { for (std::list<AssStyle*>::iterator cur = style.begin(); cur != style.end(); ++cur) {
if ((*cur)->name == name) return *cur; if ((*cur)->name.CmpNoCase(name) == 0)
return *cur;
} }
return NULL; return 0;
} }

View file

@ -34,23 +34,14 @@
/// @ingroup style_editor /// @ingroup style_editor
/// ///
////////////
// Includes
#ifndef AGI_PRE #ifndef AGI_PRE
#include <list> #include <list>
#include <wx/arrstr.h> #include <wx/arrstr.h>
#endif #endif
//////////////
// Prototypes
class AssStyle; class AssStyle;
/// DOCME /// DOCME
/// @class AssStyleStorage /// @class AssStyleStorage
/// @brief DOCME /// @brief DOCME
@ -58,13 +49,26 @@ class AssStyle;
/// DOCME /// DOCME
class AssStyleStorage { class AssStyleStorage {
public: public:
~AssStyleStorage();
/// DOCME
std::list<AssStyle*> style; std::list<AssStyle*> style;
/// Get the names of all styles in this storage
wxArrayString GetNames(); wxArrayString GetNames();
AssStyle *GetStyle(wxString name);
/// Delete all styles in this storage
void Clear(); void Clear();
void Save(wxString name);
void Load(wxString name); /// Get the style with the given name
/// @param name Case-insensitive style name
/// @return Style or NULL if the requested style is not found
AssStyle *GetStyle(wxString name);
/// Save stored styles to a file
/// @param name Catalog name (note: not file name)
void Save(wxString const& name);
/// Load stored styles from a file
/// @param name Catalog name (note: not file name)
void Load(wxString const& name);
}; };

View file

@ -253,26 +253,19 @@ DialogStyleManager::~DialogStyleManager() {
if (sel != wxNOT_FOUND) { if (sel != wxNOT_FOUND) {
c->ass->SetScriptInfo("Last Style Storage",CatalogList->GetString(sel)); c->ass->SetScriptInfo("Last Style Storage",CatalogList->GetString(sel));
} }
Store.Clear();
} }
/// @brief Load the catalog of stored styles /// @brief Load the catalog of stored styles
void DialogStyleManager::LoadCatalog () { void DialogStyleManager::LoadCatalog() {
CatalogList->Clear(); CatalogList->Clear();
// Create catalog if it doesn't exist // Create catalog if it doesn't exist
wxString dirname = StandardPaths::DecodePath("?user/catalog/"); wxString dirname = StandardPaths::DecodePath("?user/catalog/");
if (!wxDirExists(dirname)) { if (!wxDirExists(dirname)) {
if (!wxMkdir(dirname)) { // Create default style
throw "Failed creating directory for style catalogues"; Store.Clear();
} Store.style.push_back(new AssStyle);
else { Store.Save("Default");
// Create default style
Store.Clear();
AssStyle *defstyle = new AssStyle;
Store.style.push_back(defstyle);
Store.Save("Default");
}
} }
// Get dir // Get dir
@ -319,10 +312,9 @@ void DialogStyleManager::LoadStorageStyles () {
styleStorageMap.clear(); styleStorageMap.clear();
for (std::list<AssStyle*>::iterator cur=Store.style.begin();cur!=Store.style.end();cur++) { for (std::list<AssStyle*>::iterator cur=Store.style.begin();cur!=Store.style.end();cur++) {
if (AssStyle *style = *cur) { AssStyle *style = *cur;
StorageList->Append(style->name); StorageList->Append(style->name);
styleStorageMap.push_back(style); styleStorageMap.push_back(style);
}
} }
// Flag change // Flag change
@ -440,12 +432,6 @@ void DialogStyleManager::OnCatalogNew (wxCommandEvent &) {
StorageActions(true); StorageActions(true);
// Save // Save
wxString dirname = StandardPaths::DecodePath("?user/catalog/");
if (!wxDirExists(dirname)) {
if (!wxMkdir(dirname)) {
throw "Failed creating directory for style catalogues";
}
}
Store.Save(name); Store.Save(name);
} }
UpdateMoveButtons(); UpdateMoveButtons();
@ -518,37 +504,32 @@ void DialogStyleManager::OnStorageChange (wxCommandEvent &) {
} }
/// @brief Copy to Storage /// @brief Copy to Storage
void DialogStyleManager::OnCopyToStorage (wxCommandEvent &) { void DialogStyleManager::OnCopyToStorage(wxCommandEvent &) {
using std::list;
// Check if there is actually a storage // Check if there is actually a storage
if (!StorageNew->IsEnabled()) return; if (!StorageNew->IsEnabled()) return;
list<wxString> copied; std::list<wxString> copied;
wxArrayInt selections; wxArrayInt selections;
int n = CurrentList->GetSelections(selections); int n = CurrentList->GetSelections(selections);
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
wxString styleName = CurrentList->GetString(selections[i]); wxString styleName = CurrentList->GetString(selections[i]);
bool addStyle = true; bool addStyle = true;
for (list<AssStyle *>::iterator style = Store.style.begin(); style != Store.style.end(); ++style) { if (AssStyle *style = Store.GetStyle(styleName)) {
if ((*style)->name.CmpNoCase(styleName) == 0) { addStyle = false;
addStyle = false; if (wxYES == wxMessageBox(wxString::Format("There is already a style with the name \"%s\" on the current storage. Proceed and overwrite anyway?",styleName), "Style name collision.", wxYES_NO)) {
if (wxYES == wxMessageBox(wxString::Format("There is already a style with the name \"%s\" on the current storage. Proceed and overwrite anyway?",styleName), "Style name collision.", wxYES_NO)) { *style = *styleMap.at(selections[i]);
**style = *styleMap.at(selections[i]); copied.push_back(styleName);
copied.push_back(styleName);
}
break;
} }
} }
if (addStyle) { if (addStyle) {
AssStyle *temp = new AssStyle(*styleMap.at(selections[i])); Store.style.push_back(new AssStyle(*styleMap.at(selections[i])));
Store.style.push_back(temp);
copied.push_back(styleName); copied.push_back(styleName);
} }
} }
Store.Save(CatalogList->GetString(CatalogList->GetSelection())); Store.Save(CatalogList->GetString(CatalogList->GetSelection()));
LoadStorageStyles(); LoadStorageStyles();
for (list<wxString>::iterator name = copied.begin(); name != copied.end(); ++name) { for (std::list<wxString>::iterator name = copied.begin(); name != copied.end(); ++name) {
StorageList->SetStringSelection(*name, true); StorageList->SetStringSelection(*name, true);
} }
wxCommandEvent dummy; wxCommandEvent dummy;
@ -684,7 +665,7 @@ void DialogStyleManager::PasteToStorage() {
while (st.HasMoreTokens()) { while (st.HasMoreTokens()) {
try { try {
AssStyle *s = new AssStyle(st.GetNextToken().Trim(true)); AssStyle *s = new AssStyle(st.GetNextToken().Trim(true));
while (Store.GetStyle(s->name) != NULL) while (Store.GetStyle(s->name))
s->name = "Copy of " + s->name; s->name = "Copy of " + s->name;
Store.style.push_back(s); Store.style.push_back(s);
@ -998,7 +979,7 @@ void DialogStyleManager::MoveStyles(bool storage, int type) {
if (storage) { if (storage) {
// Rewrite storage // Rewrite storage
Store.style.clear(); Store.style.clear();
for (unsigned int i=0;i<styls.size();i++) Store.style.push_back(styls[i]); copy(styls.begin(), styls.end(), back_inserter(Store.style));
// Save storage // Save storage
Store.Save(CatalogList->GetString(CatalogList->GetSelection())); Store.Save(CatalogList->GetString(CatalogList->GetSelection()));