Initial support for low-level access and file I/O methods are included as well. Originally committed to SVN as r4331.
204 lines
4.9 KiB
C++
204 lines
4.9 KiB
C++
// 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
|
|
|
|
#ifndef LAGI_PRE
|
|
#include <fstream>
|
|
#include <time.h>
|
|
#endif
|
|
|
|
#include "libaegisub/access.h"
|
|
#include "libaegisub/mru.h"
|
|
#include "libaegisub/io.h"
|
|
|
|
namespace agi {
|
|
|
|
MRUManager::MRUManager(const std::string &config, const std::string &default_config): config_name(config) {
|
|
|
|
json::UnknownElement root;
|
|
std::istream *stream;
|
|
|
|
try {
|
|
stream = io::Open(config);
|
|
} catch (const acs::AcsNotFound&) {
|
|
stream = new std::istringstream(default_config);
|
|
}
|
|
|
|
try {
|
|
json::Reader::Read(root, *stream);
|
|
} catch (const json::Exception&) {
|
|
/// @todo Do something better here, maybe print the exact error
|
|
// std::cout << "json::Exception: " << e.what() << std::endl;
|
|
|
|
stream = new std::istringstream(default_config);
|
|
json::Reader::Read(root, *stream);
|
|
}
|
|
|
|
const json::Object& root_new = (json::Object)root;
|
|
|
|
json::Object::const_iterator index_object(root_new.Begin()), index_objectEnd(root_new.End());
|
|
|
|
for (; index_object != index_objectEnd; ++index_object) {
|
|
const json::Object::Member& member = *index_object;
|
|
const std::string &member_name = member.name;
|
|
const json::UnknownElement& element = member.element;
|
|
|
|
Load(member_name, (json::Array)element);
|
|
}
|
|
|
|
delete stream;
|
|
}
|
|
|
|
|
|
MRUManager::~MRUManager() {
|
|
Flush();
|
|
}
|
|
|
|
|
|
void MRUManager::Add(const std::string &key, const std::string &entry) {
|
|
|
|
MRUMap::iterator index;
|
|
|
|
if ((index = mru.find(key)) != mru.end()) {
|
|
MRUListMap &map = *index->second;
|
|
|
|
// Remove the file before adding it.
|
|
Remove(key, entry);
|
|
|
|
map.insert(std::pair<time_t, std::string>(time(NULL), entry));
|
|
|
|
Prune(map);
|
|
|
|
} else {
|
|
throw MRUErrorInvalidKey("Invalid key value");
|
|
}
|
|
}
|
|
|
|
|
|
void MRUManager::Remove(const std::string &key, const std::string &entry) {
|
|
|
|
MRUMap::iterator index;
|
|
|
|
if ((index = mru.find(key)) != mru.end()) {
|
|
MRUListMap map = *index->second;
|
|
for (MRUListMap::iterator map_idx = map.begin(); map_idx != map.end();) {
|
|
if (map_idx->second == entry)
|
|
map.erase(map_idx++);
|
|
else
|
|
++map_idx;
|
|
}
|
|
} else {
|
|
throw MRUErrorInvalidKey("Invalid key value");
|
|
}
|
|
|
|
}
|
|
|
|
|
|
const MRUManager::MRUListMap* MRUManager::Get(const std::string &key) {
|
|
|
|
MRUMap::iterator index;
|
|
|
|
if ((index = mru.find(key)) != mru.end()) {
|
|
return index->second;
|
|
} else {
|
|
throw MRUErrorInvalidKey("Invalid key value");
|
|
}
|
|
}
|
|
|
|
|
|
const std::string MRUManager::GetEntry(const std::string &key, const int entry) {
|
|
|
|
const MRUManager::MRUListMap *map = Get(key);
|
|
|
|
MRUListMap::const_iterator index = map->begin();;
|
|
|
|
if ((unsigned int)entry > map->size())
|
|
throw MRUErrorIndexOutOfRange("Requested element index is out of range.");
|
|
|
|
std::advance(index, entry);
|
|
|
|
return index->second;
|
|
}
|
|
|
|
|
|
void MRUManager::Flush() {
|
|
|
|
json::Object out;
|
|
|
|
for (MRUMap::const_iterator i = mru.begin(); i != mru.end(); ++i) {
|
|
json::Array array;
|
|
MRUListMap *map_list = i->second;
|
|
|
|
for (MRUListMap::const_iterator i_lst = map_list->begin(); i_lst != map_list->end(); ++i_lst) {
|
|
json::Object obj;
|
|
obj["time"] = json::Number((double)i_lst->first);
|
|
obj["entry"] = json::String(i_lst->second);
|
|
array.Insert(obj);
|
|
}
|
|
|
|
out[i->first] = array;
|
|
}
|
|
|
|
io::Save file(config_name);
|
|
std::ofstream& ofp = file.Get();
|
|
json::Writer::Write(out, ofp);
|
|
|
|
}
|
|
|
|
|
|
/// @brief Prune MRUListMap to the desired length.
|
|
/// This uses the user-set values for MRU list length.
|
|
inline void MRUManager::Prune(MRUListMap& map) {
|
|
unsigned int size = 16;
|
|
|
|
MRUListMap::iterator index = map.begin();;
|
|
|
|
if (map.size() >= size) {
|
|
std::advance(index, size);
|
|
|
|
// Use a range incase the storage number shrinks.
|
|
map.erase(index, map.end());
|
|
}
|
|
}
|
|
|
|
/// @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) {
|
|
|
|
json::Array::const_iterator index(array.Begin()), indexEnd(array.End());
|
|
|
|
MRUListMap *map = new MRUListMap();
|
|
|
|
for (; index != indexEnd; ++index) {
|
|
const json::Object& obj = *index;
|
|
|
|
time_t time = (time_t)(json::Number)obj["time"];
|
|
std::string entry = (json::String)obj["entry"];
|
|
|
|
map->insert(std::pair<time_t, std::string>(time, entry));
|
|
}
|
|
|
|
mru.insert(std::pair<std::string, MRUListMap*>(key, map));
|
|
Prune(*map);
|
|
}
|
|
|
|
|
|
}
|