Remove AssStyleStorage's dependencies on other Aegisub classes

This commit is contained in:
Thomas Goyne 2013-02-07 08:31:08 -08:00
parent 2bad9029cb
commit 14afce006e
3 changed files with 24 additions and 23 deletions

View file

@ -37,41 +37,39 @@
#include "ass_style_storage.h" #include "ass_style_storage.h"
#include "ass_style.h" #include "ass_style.h"
#include "options.h"
#include "text_file_reader.h"
#include "text_file_writer.h"
#include "utils.h"
#include <libaegisub/fs.h> #include <libaegisub/fs.h>
#include <libaegisub/path.h> #include <libaegisub/io.h>
#include <libaegisub/line_iterator.h>
#include <libaegisub/util.h>
#include <boost/algorithm/string/predicate.hpp> #include <boost/algorithm/string/predicate.hpp>
#include <boost/filesystem.hpp>
AssStyleStorage::~AssStyleStorage() { AssStyleStorage::~AssStyleStorage() {
delete_clear(style); agi::util::delete_clear(style);
} }
void AssStyleStorage::Save() const { void AssStyleStorage::Save() const {
if (storage_name.empty()) return; if (file.empty()) return;
agi::fs::CreateDirectory(config::path->Decode("?user/catalog/")); agi::fs::CreateDirectory(file.parent_path());
agi::io::Save out(file);
out.Get() << "\xEF\xBB\xBF";
TextFileWriter file(config::path->Decode("?user/catalog/" + storage_name + ".sty"), "UTF-8");
for (const AssStyle *cur : style) for (const AssStyle *cur : style)
file.WriteLineToFile(cur->GetEntryData()); out.Get() << cur->GetEntryData() << std::endl;
} }
void AssStyleStorage::Load(std::string const& name) { void AssStyleStorage::Load(agi::fs::path const& filename) {
storage_name = name; file = filename;
Clear(); Clear();
try { try {
TextFileReader file(config::path->Decode("?user/catalog/" + name + ".sty"), "UTF-8"); std::unique_ptr<std::ifstream> in(agi::io::Open(file));
for (auto const& line : agi::line_iterator<std::string>(*in)) {
while (file.HasMoreLines()) {
try { try {
style.push_back(new AssStyle(file.ReadLineFromFile())); style.push_back(new AssStyle(line));
} catch(...) { } catch(...) {
/* just ignore invalid lines for now */ /* just ignore invalid lines for now */
} }
@ -83,7 +81,7 @@ void AssStyleStorage::Load(std::string const& name) {
} }
void AssStyleStorage::Clear() { void AssStyleStorage::Clear() {
delete_clear(style); agi::util::delete_clear(style);
} }
void AssStyleStorage::Delete(int idx) { void AssStyleStorage::Delete(int idx) {

View file

@ -32,6 +32,9 @@
/// @ingroup style_editor /// @ingroup style_editor
/// ///
#include <libaegisub/fs_fwd.h>
#include <boost/filesystem/path.hpp>
#include <deque> #include <deque>
#include <string> #include <string>
#include <vector> #include <vector>
@ -39,7 +42,7 @@
class AssStyle; class AssStyle;
class AssStyleStorage { class AssStyleStorage {
std::string storage_name; agi::fs::path file;
std::deque<AssStyle*> style; std::deque<AssStyle*> style;
public: public:
@ -74,6 +77,6 @@ public:
void Save() const; void Save() const;
/// Load stored styles from a file /// Load stored styles from a file
/// @param name Catalog name (note: not file name) /// @param filename Catalog filename. Does not have to exist.
void Load(std::string const& name); void Load(agi::fs::path const& filename);
}; };

View file

@ -314,7 +314,7 @@ void DialogStyleManager::UpdateStorage() {
void DialogStyleManager::OnChangeCatalog() { void DialogStyleManager::OnChangeCatalog() {
std::string catalog(from_wx(CatalogList->GetStringSelection())); std::string catalog(from_wx(CatalogList->GetStringSelection()));
c->ass->SetScriptInfo("Last Style Storage", catalog); c->ass->SetScriptInfo("Last Style Storage", catalog);
Store.Load(catalog); Store.Load(config::path->Decode("?user/catalog/" + catalog + ".sty"));
UpdateStorage(); UpdateStorage();
} }
@ -327,7 +327,7 @@ void DialogStyleManager::LoadCatalog() {
// Create a default storage if there are none // Create a default storage if there are none
if (CatalogList->IsListEmpty()) { if (CatalogList->IsListEmpty()) {
Store.Load("Default"); Store.Load(config::path->Decode("?user/catalog/Default.sty"));
Store.push_back(new AssStyle); Store.push_back(new AssStyle);
Store.Save(); Store.Save();
CatalogList->Append("Default"); CatalogList->Append("Default");