mpd/src/ExcludeList.cxx

84 lines
1.9 KiB
C++
Raw Normal View History

2009-10-16 18:11:43 +02:00
/*
2011-01-29 10:13:54 +01:00
* Copyright (C) 2003-2011 The Music Player Daemon Project
2009-10-16 18:11:43 +02:00
* 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.
*
*/
#include "config.h"
2013-01-03 09:51:35 +01:00
#include "ExcludeList.hxx"
2013-01-21 19:14:37 +01:00
#include "fs/Path.hxx"
2013-02-02 15:20:24 +01:00
#include "fs/FileSystem.hxx"
#include "util/Domain.hxx"
#include "Log.hxx"
2009-10-16 18:11:43 +02:00
#include <assert.h>
#include <string.h>
#include <errno.h>
static constexpr Domain exclude_list_domain("exclude_list");
2013-01-03 10:01:34 +01:00
bool
ExcludeList::LoadFile(Path path_fs)
2009-10-16 18:11:43 +02:00
{
2013-02-02 15:20:24 +01:00
FILE *file = FOpen(path_fs, FOpenMode::ReadText);
2013-10-19 18:19:03 +02:00
if (file == nullptr) {
const int e = errno;
if (e != ENOENT) {
2013-01-23 19:48:14 +01:00
const auto path_utf8 = path_fs.ToUTF8();
FormatErrno(exclude_list_domain,
"Failed to open %s",
path_utf8.c_str());
2009-10-16 18:11:43 +02:00
}
2013-01-03 10:01:34 +01:00
return false;
2009-10-16 18:11:43 +02:00
}
2013-01-03 10:01:48 +01:00
char line[1024];
2013-10-19 18:19:03 +02:00
while (fgets(line, sizeof(line), file) != nullptr) {
2009-10-16 18:11:43 +02:00
char *p = strchr(line, '#');
2013-10-19 18:19:03 +02:00
if (p != nullptr)
2009-10-16 18:11:43 +02:00
*p = 0;
p = g_strstrip(line);
if (*p != 0)
2013-01-03 10:01:34 +01:00
patterns.emplace_front(p);
2009-10-16 18:11:43 +02:00
}
fclose(file);
2013-01-03 10:01:34 +01:00
return true;
2009-10-16 18:11:43 +02:00
}
bool
ExcludeList::Check(Path name_fs) const
2009-10-16 18:11:43 +02:00
{
assert(!name_fs.IsNull());
2009-10-16 18:11:43 +02:00
/* XXX include full path name in check */
2013-01-03 10:01:34 +01:00
for (const auto &i : patterns)
if (i.Check(name_fs.c_str()))
2009-10-16 18:11:43 +02:00
return true;
return false;
}