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>
34 #include <simgear/debug/logstream.hxx>
42 Dir::Dir(const SGPath& path) :
47 Dir::Dir(const Dir& rel, const SGPath& relPath) :
48 _path(rel.file(relPath.str()))
52 PathList Dir::children(int types, const std::string& nameFilter) const
56 types = TYPE_FILE | TYPE_DIR | NO_DOT_OR_DOTDOT;
60 std::string search(_path.str());
61 if (nameFilter.empty()) {
62 search += "\\*"; // everything
64 search += "\\*" + nameFilter;
67 WIN32_FIND_DATA fData;
68 HANDLE find = FindFirstFile(search.c_str(), &fData);
69 if (find == INVALID_HANDLE_VALUE) {
70 SG_LOG(SG_GENERAL, SG_WARN, "Dir::children: FindFirstFile failed:" << _path.str());
75 for (bool done = false; !done; done = (FindNextFile(find, &fData) == 0)) {
76 if (fData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) {
77 if (!(types & INCLUDE_HIDDEN)) {
82 if (fData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
83 if (types & NO_DOT_OR_DOTDOT) {
84 if (!strcmp(fData.cFileName,".") || !strcmp(fData.cFileName,"..")) {
89 if (!(types & TYPE_DIR)) {
92 } else if ((fData.dwFileAttributes & FILE_ATTRIBUTE_DEVICE) ||
93 (fData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT))
95 continue; // always ignore device files
96 } else if (!(types & TYPE_FILE)) {
100 result.push_back(file(fData.cFileName));
105 DIR* dp = opendir(_path.c_str());
107 SG_LOG(SG_GENERAL, SG_WARN, "Dir::children: opendir failed:" << _path.str());
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] == '.')) {
123 if (stat(file(entry->d_name).c_str(), &s)) {
124 continue; // stat() failed
127 if (S_ISDIR(s.st_mode)) {
128 // directory handling
129 if (!(types & TYPE_DIR)) {
133 if (types & NO_DOT_OR_DOTDOT) {
134 if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) {
138 } else if (S_ISREG(s.st_mode)) {
139 // regular file handling
140 if (!(types & TYPE_FILE)) {
144 // block device /fifo/char file, ignore
148 if (!nameFilter.empty()) {
149 if (strstr(entry->d_name, nameFilter.c_str()) == NULL) {
154 // passed all criteria, add to our result vector
155 result.push_back(file(entry->d_name));
163 bool Dir::exists() const
168 if (_stat (_path.c_str(), &buf ) < 0) {
172 return ((S_IFDIR & buf.st_mode ) !=0);
176 if (stat(_path.c_str(), &buf ) < 0) {
180 return ((S_ISDIR(buf.st_mode )) != 0);
184 SGPath Dir::file(const std::string& name) const
186 SGPath childPath = _path;
187 childPath.append(name);
191 } // of namespace simgear