MPD: fs/DirectoryReader.hxx Source File

MPD 0.20.18

DirectoryReader.hxx
Go to the documentation of this file.
1 /*
2 * Copyright 2003-2017 The Music Player Daemon Project
3 * http://www.musicpd.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef MPD_FS_DIRECTORY_READER_HXX
21 #define MPD_FS_DIRECTORY_READER_HXX
22
23 #include "check.h"
24 #include "Path.hxx"
25
26 #ifdef _WIN32
27
28 #include <windows.h>
29 #include <tchar.h>
30
34 class DirectoryReader {
35 const HANDLE handle;
36 WIN32_FIND_DATA data;
37 bool first = true;
38
39 class MakeWildcardPath {
40 PathTraitsFS::pointer_type path;
41
42 public:
43 MakeWildcardPath(PathTraitsFS::const_pointer_type _path) {
44 auto l = _tcslen(_path);
45 path = new PathTraitsFS::value_type[l + 3];
46 _tcscpy(path, _path);
47 path[l] = _T('\\');
48 path[l + 1] = _T('*');
49 path[l + 2] = 0;
50 }
51
52 ~MakeWildcardPath() {
53 delete[] path;
54 }
55
56 operator PathTraitsFS::const_pointer_type() const {
57 return path;
58 }
59 };
60
61 public:
67 explicit DirectoryReader(Path dir);
68
69 DirectoryReader(const DirectoryReader &other) = delete;
70 DirectoryReader &operator=(const DirectoryReader &other) = delete;
71
75 ~DirectoryReader() {
76 FindClose(handle);
77 }
78
82 bool ReadEntry() {
83 if (first) {
84 first = false;
85 return true;
86 }
87
88 return FindNextFile(handle, &data) != 0;
89 }
90
94 Path GetEntry() const {
95 return Path::FromFS(data.cFileName);
96 }
97 };
98
99 #else
100
101 #include <dirent.h>
102
106 class DirectoryReader {
107 DIR *const dirp;
108 dirent *ent = nullptr;
109
110 public:
116 explicit DirectoryReader(Path dir);
117
118 DirectoryReader(const DirectoryReader &other) = delete;
119 DirectoryReader &operator=(const DirectoryReader &other) = delete;
120
124 ~DirectoryReader() {
125 closedir(dirp);
126 }
127
131 bool HasEntry() const {
132 return ent != nullptr;
133 }
134
138 bool ReadEntry() {
139 ent = readdir(dirp);
140 return HasEntry();
141 }
142
146 Path GetEntry() const {
147 assert(HasEntry());
148 return Path::FromFS(ent->d_name);
149 }
150 };
151
152 #endif
153
154 #endif
check.h
DirectoryReader::GetEntry
Path GetEntry() const
Extracts directory entry that was previously read by ReadEntry.
Definition: DirectoryReader.hxx:146
DirectoryReader::HasEntry
bool HasEntry() const
Checks if directory entry is available.
Definition: DirectoryReader.hxx:131
DirectoryReader::ReadEntry
bool ReadEntry()
Reads next directory entry.
Definition: DirectoryReader.hxx:138
PathTraitsFS::pointer_type
Pointer::pointer_type pointer_type
Definition: Traits.hxx:55
Path::FromFS
static constexpr Path FromFS(const_pointer_type fs)
Create a new instance pointing to the specified path string.
Definition: Path.hxx:64
DirectoryReader::operator=
DirectoryReader & operator=(const DirectoryReader &other)=delete
PathTraitsFS::const_pointer_type
Pointer::const_pointer_type const_pointer_type
Definition: Traits.hxx:56
Path
A path name in the native file system character set.
Definition: Path.hxx:39
Path.hxx
DirectoryReader
Reader for directory entries.
Definition: DirectoryReader.hxx:106
DirectoryReader::~DirectoryReader
~DirectoryReader()
Destroys this instance.
Definition: DirectoryReader.hxx:124
DirectoryReader::DirectoryReader
DirectoryReader(Path dir)
Creates new directory reader for the specified #dir.
PathTraitsFS::value_type
char_traits::char_type value_type
Definition: Traits.hxx:53