mpd/src/ExcludeList.cxx

78 lines
1.7 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"
2009-10-16 18:11:43 +02:00
#include <assert.h>
#include <string.h>
#include <errno.h>
2013-01-03 10:01:34 +01:00
bool
ExcludeList::LoadFile(const 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);
2009-10-16 18:11:43 +02:00
if (file == NULL) {
if (errno != ENOENT) {
2013-01-23 19:48:14 +01:00
const char *msg = g_strerror(errno);
const auto path_utf8 = path_fs.ToUTF8();
2009-10-16 18:11:43 +02:00
g_debug("Failed to open %s: %s",
2013-01-23 19:48:14 +01:00
path_utf8.c_str(), msg);
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];
2009-10-16 18:11:43 +02:00
while (fgets(line, sizeof(line), file) != NULL) {
char *p = strchr(line, '#');
if (p != NULL)
*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
2013-01-03 10:01:34 +01:00
ExcludeList::Check(const char *name_fs) const
2009-10-16 18:11:43 +02:00
{
assert(name_fs != NULL);
/* 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))
2009-10-16 18:11:43 +02:00
return true;
return false;
}