2013-01-21 18:56:40 +01:00
|
|
|
/*
|
2021-01-01 19:54:25 +01:00
|
|
|
* Copyright 2003-2021 The Music Player Daemon Project
|
2013-01-21 18:56:40 +01: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef MPD_FS_DIRECTORY_READER_HXX
|
|
|
|
#define MPD_FS_DIRECTORY_READER_HXX
|
|
|
|
|
2014-01-30 22:05:03 +01:00
|
|
|
#include "Path.hxx"
|
2013-01-21 18:56:40 +01:00
|
|
|
|
2017-12-12 10:22:20 +01:00
|
|
|
#ifdef _WIN32
|
2015-02-26 18:11:02 +01:00
|
|
|
|
2021-05-19 17:24:02 +02:00
|
|
|
#include <fileapi.h>
|
2015-02-26 18:11:02 +01:00
|
|
|
#include <tchar.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reader for directory entries.
|
|
|
|
*/
|
|
|
|
class DirectoryReader {
|
|
|
|
const HANDLE handle;
|
|
|
|
WIN32_FIND_DATA data;
|
2015-12-29 12:42:08 +01:00
|
|
|
bool first = true;
|
2015-02-26 18:11:02 +01:00
|
|
|
|
|
|
|
class MakeWildcardPath {
|
2020-01-03 15:49:29 +01:00
|
|
|
PathTraitsFS::pointer path;
|
2015-02-26 18:11:02 +01:00
|
|
|
|
|
|
|
public:
|
2020-01-03 15:49:29 +01:00
|
|
|
MakeWildcardPath(PathTraitsFS::const_pointer _path) {
|
2015-02-26 18:11:02 +01:00
|
|
|
auto l = _tcslen(_path);
|
|
|
|
path = new PathTraitsFS::value_type[l + 3];
|
|
|
|
_tcscpy(path, _path);
|
|
|
|
path[l] = _T('\\');
|
|
|
|
path[l + 1] = _T('*');
|
|
|
|
path[l + 2] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
~MakeWildcardPath() {
|
|
|
|
delete[] path;
|
|
|
|
}
|
|
|
|
|
2020-01-03 15:49:29 +01:00
|
|
|
operator PathTraitsFS::const_pointer() const {
|
2015-02-26 18:11:02 +01:00
|
|
|
return path;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Creates new directory reader for the specified #dir.
|
2015-12-29 12:41:45 +01:00
|
|
|
*
|
|
|
|
* Throws std::system_error on error.
|
2015-02-26 18:11:02 +01:00
|
|
|
*/
|
2015-12-29 12:41:45 +01:00
|
|
|
explicit DirectoryReader(Path dir);
|
2015-02-26 18:11:02 +01:00
|
|
|
|
|
|
|
DirectoryReader(const DirectoryReader &other) = delete;
|
|
|
|
DirectoryReader &operator=(const DirectoryReader &other) = delete;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroys this instance.
|
|
|
|
*/
|
|
|
|
~DirectoryReader() {
|
2015-12-29 12:41:45 +01:00
|
|
|
FindClose(handle);
|
2015-02-26 18:11:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads next directory entry.
|
|
|
|
*/
|
|
|
|
bool ReadEntry() {
|
|
|
|
if (first) {
|
|
|
|
first = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FindNextFile(handle, &data) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extracts directory entry that was previously read by #ReadEntry.
|
|
|
|
*/
|
|
|
|
Path GetEntry() const {
|
|
|
|
return Path::FromFS(data.cFileName);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2013-01-21 18:56:40 +01:00
|
|
|
#include <dirent.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reader for directory entries.
|
|
|
|
*/
|
|
|
|
class DirectoryReader {
|
|
|
|
DIR *const dirp;
|
2015-12-29 12:42:08 +01:00
|
|
|
dirent *ent = nullptr;
|
|
|
|
|
2013-01-21 18:56:40 +01:00
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Creates new directory reader for the specified #dir.
|
2015-12-29 12:41:45 +01:00
|
|
|
*
|
|
|
|
* Throws std::system_error on error.
|
2013-01-21 18:56:40 +01:00
|
|
|
*/
|
2015-12-29 12:41:45 +01:00
|
|
|
explicit DirectoryReader(Path dir);
|
2013-01-21 18:56:40 +01:00
|
|
|
|
|
|
|
DirectoryReader(const DirectoryReader &other) = delete;
|
|
|
|
DirectoryReader &operator=(const DirectoryReader &other) = delete;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroys this instance.
|
|
|
|
*/
|
|
|
|
~DirectoryReader() {
|
2015-12-29 12:41:45 +01:00
|
|
|
closedir(dirp);
|
2013-01-21 18:56:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if directory entry is available.
|
|
|
|
*/
|
|
|
|
bool HasEntry() const {
|
|
|
|
return ent != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads next directory entry.
|
|
|
|
*/
|
|
|
|
bool ReadEntry() {
|
|
|
|
ent = readdir(dirp);
|
|
|
|
return HasEntry();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extracts directory entry that was previously read by #ReadEntry.
|
|
|
|
*/
|
2014-01-30 22:05:03 +01:00
|
|
|
Path GetEntry() const {
|
2013-01-21 18:56:40 +01:00
|
|
|
assert(HasEntry());
|
2014-01-30 22:05:03 +01:00
|
|
|
return Path::FromFS(ent->d_name);
|
2013-01-21 18:56:40 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
2015-02-26 18:11:02 +01:00
|
|
|
|
|
|
|
#endif
|