Ignore slash type and case to compare paths in RecentFiles

This commit is contained in:
David Capello 2015-03-11 17:48:28 -03:00
parent 7724c212e8
commit 30a90c88e9
3 changed files with 43 additions and 36 deletions

View File

@ -19,6 +19,18 @@
#include <cstring>
#include <set>
namespace {
struct compare_path {
std::string a;
compare_path(const std::string& a) : a(a) { }
bool operator()(const std::string& b) const {
return base::compare_filenames(a, b) == 0;
}
};
}
namespace app {
RecentFiles::RecentFiles()
@ -31,34 +43,19 @@ RecentFiles::RecentFiles()
sprintf(buf, "Filename%02d", c);
const char* filename = get_config_string("RecentFiles", buf, NULL);
if (filename && *filename && base::is_file(filename))
m_files.addItem(filename);
if (filename && *filename && base::is_file(filename)) {
std::string fn = base::fix_path_separators(filename);
m_files.addItem(fn, compare_path(fn));
}
}
for (int c=m_paths.limit()-1; c>=0; c--) {
sprintf(buf, "Path%02d", c);
const char* path = get_config_string("RecentPaths", buf, NULL);
if (path && *path)
m_paths.addItem(path);
}
// Create recent list of paths from filenames (for backward
// compatibility with previous versions of ASEPRITE).
if (m_paths.empty()) {
std::set<std::string> included;
// For each recent file...
const_iterator it = files_begin();
const_iterator end = files_end();
for (; it != end; ++it) {
std::string path = base::get_file_path(*it);
// Check if the path was not already included in the list
if (included.find(path) == included.end()) {
included.insert(path);
m_paths.addItem(path);
}
if (path && *path) {
std::string p = base::fix_path_separators(path);
m_paths.addItem(p, compare_path(p));
}
}
}
@ -68,34 +65,38 @@ RecentFiles::~RecentFiles()
char buf[512];
int c = 0;
for (const_iterator it = files_begin(); it != files_end(); ++it) {
const char* filename = it->c_str();
for (auto const& filename : m_files) {
sprintf(buf, "Filename%02d", c);
set_config_string("RecentFiles", buf, filename);
set_config_string("RecentFiles", buf, filename.c_str());
c++;
}
c = 0;
for (const_iterator it = paths_begin(); it != paths_end(); ++it) {
const char* path = it->c_str();
for (auto const& path : m_paths) {
sprintf(buf, "Path%02d", c);
set_config_string("RecentPaths", buf, path);
set_config_string("RecentPaths", buf, path.c_str());
c++;
}
}
void RecentFiles::addRecentFile(const char* filename)
{
m_files.addItem(filename);
m_paths.addItem(base::get_file_path(filename));
std::string fn = base::fix_path_separators(filename);
m_files.addItem(fn, compare_path(fn));
std::string path = base::get_file_path(fn);
m_paths.addItem(path, compare_path(path));
Changed();
}
void RecentFiles::removeRecentFile(const char* filename)
{
m_files.removeItem(filename);
m_paths.removeItem(base::get_file_path(filename));
std::string fn = base::fix_path_separators(filename);
m_files.removeItem(fn, compare_path(fn));
std::string path = base::get_file_path(filename);
m_paths.removeItem(path, compare_path(path));
Changed();
}

View File

@ -186,6 +186,10 @@ int compare_filenames(const std::string& a, const std::string& b)
a_it = a_it2;
b_it = b_it2;
}
else if (is_path_separator(a_chr) && is_path_separator(b_chr)) {
++a_it;
++b_it;
}
else {
a_chr = std::tolower(a_chr);
b_chr = std::tolower(b_chr);

View File

@ -29,8 +29,9 @@ public:
std::size_t size() const { return m_items.size(); }
std::size_t limit() const { return m_limit; }
void addItem(const T& item) {
iterator it = std::find(m_items.begin(), m_items.end(), item);
template<typename T, typename Predicate>
void addItem(const T& item, Predicate p) {
iterator it = std::find_if(m_items.begin(), m_items.end(), p);
// If the item already exist in the list...
if (it != m_items.end()) {
@ -49,8 +50,9 @@ public:
m_items.insert(m_items.begin(), item);
}
void removeItem(const T& item) {
iterator it = std::find(m_items.begin(), m_items.end(), item);
template<typename T, typename Predicate>
void removeItem(const T& item, Predicate p) {
iterator it = std::find_if(m_items.begin(), m_items.end(), p);
if (it != m_items.end())
m_items.erase(it);
}