2010-05-21 03:13:36 +02:00
|
|
|
// Copyright (c) 2010, Amar Takhar <verm@aegisub.org>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
//
|
|
|
|
// $Id$
|
|
|
|
|
|
|
|
/// @file mru.cpp
|
|
|
|
/// @brief Most Recently Used (MRU) Lists
|
|
|
|
/// @ingroup libaegisub
|
|
|
|
|
2010-06-11 04:25:07 +02:00
|
|
|
#include "libaegisub/cajun/writer.h"
|
2010-05-21 03:13:36 +02:00
|
|
|
|
|
|
|
#include "libaegisub/access.h"
|
2011-07-26 21:51:56 +02:00
|
|
|
#include "libaegisub/io.h"
|
2011-07-26 21:51:38 +02:00
|
|
|
#include "libaegisub/json.h"
|
2010-06-03 20:09:00 +02:00
|
|
|
#include "libaegisub/log.h"
|
2010-05-21 03:13:36 +02:00
|
|
|
#include "libaegisub/mru.h"
|
|
|
|
|
|
|
|
namespace agi {
|
|
|
|
|
|
|
|
MRUManager::MRUManager(const std::string &config, const std::string &default_config): config_name(config) {
|
2010-06-03 20:09:00 +02:00
|
|
|
LOG_D("agi/mru") << "Loading MRU List";
|
2010-05-21 03:13:36 +02:00
|
|
|
|
2011-12-22 22:10:50 +01:00
|
|
|
json::Object root(json_util::file(config, default_config));
|
|
|
|
for (json::Object::const_iterator it(root.begin()); it != root.end(); ++it)
|
|
|
|
Load(it->first, it->second);
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MRUManager::~MRUManager() {
|
2011-07-26 21:51:56 +02:00
|
|
|
}
|
2010-05-23 08:58:06 +02:00
|
|
|
|
2011-07-26 21:51:56 +02:00
|
|
|
MRUManager::MRUListMap &MRUManager::Find(std::string const& key) {
|
|
|
|
MRUMap::iterator index = mru.find(key);
|
|
|
|
if (index == mru.end())
|
|
|
|
throw MRUErrorInvalidKey("Invalid key value");
|
|
|
|
return index->second;
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MRUManager::Add(const std::string &key, const std::string &entry) {
|
2011-07-26 21:51:56 +02:00
|
|
|
MRUListMap &map = Find(key);
|
|
|
|
map.remove(entry);
|
|
|
|
map.push_front(entry);
|
|
|
|
Prune(map);
|
2011-09-28 21:52:46 +02:00
|
|
|
|
|
|
|
Flush();
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MRUManager::Remove(const std::string &key, const std::string &entry) {
|
2011-07-26 21:51:56 +02:00
|
|
|
Find(key).remove(entry);
|
2011-09-28 21:52:46 +02:00
|
|
|
|
|
|
|
Flush();
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const MRUManager::MRUListMap* MRUManager::Get(const std::string &key) {
|
2011-07-26 21:51:56 +02:00
|
|
|
return &Find(key);
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
2011-07-26 21:51:56 +02:00
|
|
|
std::string const& MRUManager::GetEntry(const std::string &key, size_t entry) {
|
2010-05-21 03:13:36 +02:00
|
|
|
const MRUManager::MRUListMap *map = Get(key);
|
|
|
|
|
2011-12-22 22:11:01 +01:00
|
|
|
if (entry >= map->size())
|
2010-05-21 03:13:36 +02:00
|
|
|
throw MRUErrorIndexOutOfRange("Requested element index is out of range.");
|
|
|
|
|
2011-07-26 21:51:56 +02:00
|
|
|
MRUListMap::const_iterator index = map->begin();
|
|
|
|
advance(index, entry);
|
|
|
|
return *index;
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MRUManager::Flush() {
|
|
|
|
json::Object out;
|
|
|
|
|
|
|
|
for (MRUMap::const_iterator i = mru.begin(); i != mru.end(); ++i) {
|
2011-07-26 21:51:56 +02:00
|
|
|
json::Array &array = out[i->first];
|
2011-10-18 00:00:28 +02:00
|
|
|
copy(i->second.begin(), i->second.end(), std::back_inserter(array));
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
2011-07-26 21:51:56 +02:00
|
|
|
json::Writer::Write(out, io::Save(config_name).Get());
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// @brief Prune MRUListMap to the desired length.
|
|
|
|
/// This uses the user-set values for MRU list length.
|
|
|
|
inline void MRUManager::Prune(MRUListMap& map) {
|
2011-09-28 21:52:46 +02:00
|
|
|
map.resize(std::min<size_t>(16, map.size()));
|
2011-07-26 21:51:56 +02:00
|
|
|
}
|
2010-05-21 03:13:36 +02:00
|
|
|
|
|
|
|
/// @brief Load MRU Lists.
|
|
|
|
/// @param key List name.
|
|
|
|
/// @param array json::Array of values.
|
|
|
|
void MRUManager::Load(const std::string &key, const json::Array& array) {
|
2011-09-29 02:37:18 +02:00
|
|
|
try {
|
2011-10-18 00:00:28 +02:00
|
|
|
copy(array.begin(), array.end(), back_inserter(mru[key]));
|
2011-09-29 02:37:18 +02:00
|
|
|
}
|
|
|
|
catch (json::Exception const&) {
|
|
|
|
// Out of date MRU file; just discard the data and skip it
|
|
|
|
}
|
2011-07-26 21:51:56 +02:00
|
|
|
Prune(mru[key]);
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|