ExcludeList: convert to a class

This commit is contained in:
Max Kellermann 2013-01-03 10:01:34 +01:00
parent 47fc08bffe
commit 93f0bb8307
3 changed files with 61 additions and 47 deletions

View File

@ -34,8 +34,8 @@ extern "C" {
#include <stdio.h>
#include <errno.h>
GSList *
exclude_list_load(const char *path_fs)
bool
ExcludeList::LoadFile(const char *path_fs)
{
assert(path_fs != NULL);
@ -48,10 +48,9 @@ exclude_list_load(const char *path_fs)
g_free(path_utf8);
}
return NULL;
return false;
}
GSList *list = NULL;
char line[1024];
while (fgets(line, sizeof(line), file) != NULL) {
char *p = strchr(line, '#');
@ -60,37 +59,24 @@ exclude_list_load(const char *path_fs)
p = g_strstrip(line);
if (*p != 0)
list = g_slist_prepend(list, g_pattern_spec_new(p));
patterns.emplace_front(p);
}
fclose(file);
return list;
}
void
exclude_list_free(GSList *list)
{
while (list != NULL) {
GPatternSpec *pattern = (GPatternSpec *)list->data;
g_pattern_spec_free(pattern);
list = g_slist_remove(list, list->data);
}
return true;
}
bool
exclude_list_check(GSList *list, const char *name_fs)
ExcludeList::Check(const char *name_fs) const
{
assert(name_fs != NULL);
/* XXX include full path name in check */
for (; list != NULL; list = list->next) {
GPatternSpec *pattern = (GPatternSpec *)list->data;
if (g_pattern_match_string(pattern, name_fs))
for (const auto &i : patterns)
if (i.Check(name_fs))
return true;
}
return false;
}

View File

@ -25,25 +25,54 @@
#ifndef MPD_EXCLUDE_H
#define MPD_EXCLUDE_H
#include "gcc.h"
#include <forward_list>
#include <glib.h>
/**
* Loads and parses a .mpdignore file.
*/
GSList *
exclude_list_load(const char *path_fs);
class ExcludeList {
class Pattern {
GPatternSpec *pattern;
/**
* Frees a list returned by exclude_list_load().
*/
void
exclude_list_free(GSList *list);
public:
Pattern(const char *_pattern)
:pattern(g_pattern_spec_new(_pattern)) {}
Pattern(Pattern &&other)
:pattern(other.pattern) {
other.pattern = nullptr;
}
~Pattern() {
g_pattern_spec_free(pattern);
}
gcc_pure
bool Check(const char *name_fs) const {
return g_pattern_match_string(pattern, name_fs);
}
};
std::forward_list<Pattern> patterns;
public:
gcc_pure
bool IsEmpty() const {
return patterns.empty();
}
/**
* Loads and parses a .mpdignore file.
*/
bool LoadFile(const char *path_fs);
/**
* Checks whether one of the patterns in the .mpdignore file matches
* the specified file name.
*/
bool Check(const char *name_fs) const;
};
/**
* Checks whether one of the patterns in the .mpdignore file matches
* the specified file name.
*/
bool
exclude_list_check(GSList *list, const char *name_fs);
#endif

View File

@ -95,7 +95,8 @@ directory_set_stat(Directory *dir, const struct stat *st)
}
static void
remove_excluded_from_directory(Directory *directory, GSList *exclude_list)
remove_excluded_from_directory(Directory *directory,
const ExcludeList &exclude_list)
{
db_lock();
@ -103,7 +104,7 @@ remove_excluded_from_directory(Directory *directory, GSList *exclude_list)
directory_for_each_child_safe(child, n, directory) {
char *name_fs = utf8_to_fs_charset(child->GetName());
if (exclude_list_check(exclude_list, name_fs)) {
if (exclude_list.Check(name_fs)) {
delete_directory(child);
modified = true;
}
@ -116,7 +117,7 @@ remove_excluded_from_directory(Directory *directory, GSList *exclude_list)
assert(song->parent == directory);
char *name_fs = utf8_to_fs_charset(song->uri);
if (exclude_list_check(exclude_list, name_fs)) {
if (exclude_list.Check(name_fs)) {
delete_song(directory, song);
modified = true;
}
@ -371,12 +372,13 @@ update_directory(Directory *directory, const struct stat *st)
}
char *exclude_path_fs = g_build_filename(path_fs, ".mpdignore", NULL);
GSList *exclude_list = exclude_list_load(exclude_path_fs);
ExcludeList exclude_list;
exclude_list.LoadFile(exclude_path_fs);
g_free(exclude_path_fs);
g_free(path_fs);
if (exclude_list != NULL)
if (!exclude_list.IsEmpty())
remove_excluded_from_directory(directory, exclude_list);
purge_deleted_from_directory(directory);
@ -386,8 +388,7 @@ update_directory(Directory *directory, const struct stat *st)
char *utf8;
struct stat st2;
if (skip_path(ent->d_name) ||
exclude_list_check(exclude_list, ent->d_name))
if (skip_path(ent->d_name) || exclude_list.Check(ent->d_name))
continue;
utf8 = fs_charset_to_utf8(ent->d_name);
@ -408,8 +409,6 @@ update_directory(Directory *directory, const struct stat *st)
g_free(utf8);
}
exclude_list_free(exclude_list);
closedir(dir);
directory->mtime = st->st_mtime;