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 & TYPE_DIR)) {
84 } else if ((fData.dwFileAttributes & FILE_ATTRIBUTE_DEVICE) ||
85 (fData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT))
87 continue; // always ignore device files
88 } else if (!(types & TYPE_FILE)) {
92 result.push_back(file(fData.cFileName));
97 DIR* dp = opendir(_path.c_str());
99 SG_LOG(SG_GENERAL, SG_WARN, "Dir::children: opendir failed:" << _path.str());
104 struct dirent* entry = readdir(dp);
106 break; // done iteration
109 // skip hidden files (names beginning with '.') unless requested
110 if (!(types & INCLUDE_HIDDEN) && (entry->d_name[0] == '.')) {
114 if (entry->d_type == DT_DIR) {
115 if (!(types & TYPE_DIR)) {
119 if (types & NO_DOT_OR_DOTDOT) {
120 if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) {
124 } else if (entry->d_type == DT_REG) {
125 if (!(types & TYPE_FILE)) {
129 continue; // ignore char/block devices, fifos, etc
132 if (!nameFilter.empty()) {
133 if (strstr(entry->d_name, nameFilter.c_str()) == NULL) {
138 // passed all criteria, add to our result vector
139 result.push_back(file(entry->d_name));
147 SGPath Dir::file(const std::string& name) const
149 SGPath childPath = _path;
150 childPath.append(name);
154 } // of namespace simgear