1 // Written by James Turner, started July 2010.
3 // Copyright (C) 2010 Curtis L. Olson - http://www.flightgear.org/~curt
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Library General Public
7 // License as published by the Free Software Foundation; either
8 // version 2 of the License, or (at your option) any later version.
10 // This library 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 GNU
13 // Library General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include <simgear/misc/sg_dir.hxx>
25 # define WIN32_LEAN_AND_MEAN
28 # include <sys/types.h>
32 #include <simgear/debug/logstream.hxx>
40 Dir::Dir(const SGPath& path) :
45 Dir::Dir(const Dir& rel, const SGPath& relPath) :
46 _path(rel.file(relPath.str()))
50 PathList Dir::children(int types, const std::string& nameFilter) const
54 types = TYPE_FILE | TYPE_DIR | NO_DOT_OR_DOTDOT;
58 std::string search(_path.str());
59 if (nameFilter.empty()) {
60 search += "\\*"; // everything
62 search += "\\*" + nameFilter;
65 WIN32_FIND_DATA fData;
66 HANDLE find = FindFirstFile(search.c_str(), &fData);
67 if (find == INVALID_HANDLE_VALUE) {
68 SG_LOG(SG_GENERAL, SG_WARN, "Dir::children: FindFirstFile failed:" << _path.str());
73 for (bool done = false; !done; done = (FindNextFile(find, &fData) == 0)) {
74 if (fData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) {
75 if (!(types & INCLUDE_HIDDEN)) {
80 if (fData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
81 if (types & NO_DOT_OR_DOTDOT) {
82 if (!strcmp(fData.cFileName,".") || !strcmp(fData.cFileName,"..")) {
87 if (!(types & TYPE_DIR)) {
90 } else if ((fData.dwFileAttributes & FILE_ATTRIBUTE_DEVICE) ||
91 (fData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT))
93 continue; // always ignore device files
94 } else if (!(types & TYPE_FILE)) {
98 result.push_back(file(fData.cFileName));
103 DIR* dp = opendir(_path.c_str());
105 SG_LOG(SG_GENERAL, SG_WARN, "Dir::children: opendir failed:" << _path.str());
109 int filterLen = nameFilter.size();
112 struct dirent* entry = readdir(dp);
114 break; // done iteration
117 // skip hidden files (names beginning with '.') unless requested
118 if (!(types & INCLUDE_HIDDEN) && (entry->d_name[0] == '.') &&
119 strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")) {
123 SGPath f = file(entry->d_name);
125 continue; // stat() failed
129 // directory handling
130 if (!(types & TYPE_DIR)) {
134 if (types & NO_DOT_OR_DOTDOT) {
135 if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) {
139 } else if (f.isFile()) {
140 // regular file handling
141 if (!(types & TYPE_FILE)) {
145 // block device /fifo/char file, ignore
149 if (!nameFilter.empty()) {
150 int nameLen = strlen(entry->d_name);
151 if (nameLen < filterLen) {
152 continue; // name is shorter than the filter
155 char* nameSuffix = entry->d_name + (nameLen - filterLen);
156 if (strcmp(nameSuffix, nameFilter.c_str())) {
161 // passed all criteria, add to our result vector
162 result.push_back(file(entry->d_name));
170 bool Dir::exists() const
172 return _path.isDir();
175 SGPath Dir::file(const std::string& name) const
177 SGPath childPath = _path;
178 childPath.append(name);
182 } // of namespace simgear