]> git.mxchange.org Git - simgear.git/blob - simgear/misc/sg_dir.cxx
Fix simgear::Dir compilation on some Unix setups, thanks Anders.
[simgear.git] / simgear / misc / sg_dir.cxx
1 // Written by James Turner, started July 2010.
2 //
3 // Copyright (C) 2010  Curtis L. Olson - http://www.flightgear.org/~curt
4 //
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.
9 //
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.
14 //
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.
18 //
19 // $Id$
20
21
22 #include <simgear/misc/sg_dir.hxx>
23
24 #ifdef _WIN32
25 #  define WIN32_LEAN_AND_MEAN
26 #  include <windows.h>
27 #else
28 #  include <sys/types.h>
29 #  include <dirent.h>
30 #endif
31
32 #include <simgear/debug/logstream.hxx>
33
34 #include <cstring>
35 #include <iostream>
36
37 namespace simgear
38 {
39
40 Dir::Dir(const SGPath& path) :
41   _path(path)
42 {
43 }
44
45 Dir::Dir(const Dir& rel, const SGPath& relPath) :
46   _path(rel.file(relPath.str()))
47 {
48 }
49
50 PathList Dir::children(int types, const std::string& nameFilter) const
51 {
52   PathList result;
53   if (types == 0) {
54     types = TYPE_FILE | TYPE_DIR | NO_DOT_OR_DOTDOT;
55   }
56   
57 #ifdef _WIN32
58   std::string search(_path.str());
59   if (nameFilter.empty()) {
60     search += "\\*"; // everything
61   } else {
62     search += "\\*" + nameFilter;
63   }
64   
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());
69     return result;
70   }
71   
72   bool done = false;
73   for (bool done = false; !done; done = (FindNextFile(find, &fData) == 0)) {
74     if (fData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) {
75       if (!(types & INCLUDE_HIDDEN)) {
76         continue;
77       }
78     }
79     
80     if (fData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
81       if (!(types & TYPE_DIR)) {
82         continue;
83       }
84         } else if ((fData.dwFileAttributes & FILE_ATTRIBUTE_DEVICE) ||
85                                 (fData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT))
86         {
87                 continue; // always ignore device files
88     } else if (!(types & TYPE_FILE)) {
89        continue;
90     }
91
92     result.push_back(file(fData.cFileName));
93   }
94
95   FindClose(find);
96 #else
97   DIR* dp = opendir(_path.c_str());
98   if (!dp) {
99     SG_LOG(SG_GENERAL, SG_WARN, "Dir::children: opendir failed:" << _path.str());
100     return result;
101   }
102   
103   while (true) {
104     struct dirent* entry = readdir(dp);
105     if (!entry) {
106       break; // done iteration
107     }
108     
109     // skip hidden files (names beginning with '.') unless requested
110     if (!(types & INCLUDE_HIDDEN) && (entry->d_name[0] == '.')) {
111       continue;
112     }
113         
114     if (entry->d_type == DT_DIR) {
115       if (!(types & TYPE_DIR)) {
116         continue;
117       }
118       
119       if (types & NO_DOT_OR_DOTDOT) {
120         if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) {
121           continue;
122         }
123       }
124     } else if (entry->d_type == DT_REG) {
125       if (!(types & TYPE_FILE)) {
126         continue;
127       }
128     } else {
129       continue; // ignore char/block devices, fifos, etc
130     }
131     
132     if (!nameFilter.empty()) {
133       if (strstr(entry->d_name, nameFilter.c_str()) == NULL) {
134         continue;
135       }
136     }
137     
138   // passed all criteria, add to our result vector
139     result.push_back(file(entry->d_name));
140   }
141   
142   closedir(dp);
143 #endif
144   return result;
145 }
146
147 SGPath Dir::file(const std::string& name) const
148 {
149   SGPath childPath = _path;
150   childPath.append(name);
151   return childPath;  
152 }
153
154 } // of namespace simgear