ExcludeList.cxx 1.88 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/*
 * The .mpdignore backend code.
 *
 */

25
#include "config.h"
Max Kellermann's avatar
Max Kellermann committed
26
#include "ExcludeList.hxx"
27
#include "fs/Path.hxx"
28
#include "fs/NarrowPath.hxx"
29
#include "input/TextInputStream.hxx"
30
#include "util/StringStrip.hxx"
31
#include "Log.hxx"
32

33
#include <exception>
34

35 36 37
#include <assert.h>
#include <string.h>

38 39 40
inline void
ExcludeList::ParseLine(char *line) noexcept
{
41 42
	char *p = Strip(line);
	if (*p != 0 && *p != '#')
43 44 45
		patterns.emplace_front(p);
}

46
bool
47 48
ExcludeList::Load(InputStreamPtr is)
{
49
#ifdef HAVE_CLASS_GLOB
50
	TextInputStream tis(std::move(is));
51

52
	char *line;
53
	while ((line = tis.ReadLine()) != nullptr)
54
		ParseLine(line);
55
#else
56
	/* not implemented */
57 58
	(void)path_fs;
#endif
59

60
	return true;
61 62 63
}

bool
64
ExcludeList::Check(Path name_fs) const noexcept
65
{
66
	assert(!name_fs.IsNull());
67 68 69

	/* XXX include full path name in check */

70
#ifdef HAVE_CLASS_GLOB
71 72 73 74 75 76
	if (parent != nullptr) {
		if (parent->Check(name_fs)) {
			return true;
		}
	}

77 78 79 80
	for (const auto &i : patterns) {
		try {
			if (i.Check(NarrowPath(name_fs).c_str()))
				return true;
81
		} catch (...) {
82 83
		}
	}
84
#else
85
	/* not implemented */
86 87
	(void)name_fs;
#endif
88 89 90

	return false;
}