]> git.mxchange.org Git - simgear.git/blob - simgear/misc/sg_dir.cxx
9c5aa78a8f5b9ac5885f75f286a81f5bc0b7033d
[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 <sys/stat.h>
33
34 #include <simgear/debug/logstream.hxx>
35
36 #include <cstring>
37 #include <iostream>
38
39 namespace simgear
40 {
41
42 Dir::Dir(const SGPath& path) :
43   _path(path)
44 {
45 }
46
47 Dir::Dir(const Dir& rel, const SGPath& relPath) :
48   _path(rel.file(relPath.str()))
49 {
50 }
51
52 PathList Dir::children(int types, const std::string& nameFilter) const
53 {
54   PathList result;
55   if (types == 0) {
56     types = TYPE_FILE | TYPE_DIR | NO_DOT_OR_DOTDOT;
57   }
58   
59 #ifdef _WIN32
60   std::string search(_path.str());
61   if (nameFilter.empty()) {
62     search += "\\*"; // everything
63   } else {
64     search += "\\*" + nameFilter;
65   }
66   
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());
71     return result;
72   }
73   
74   bool done = false;
75   for (bool done = false; !done; done = (FindNextFile(find, &fData) == 0)) {
76     if (fData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) {
77       if (!(types & INCLUDE_HIDDEN)) {
78         continue;
79       }
80     }
81     
82     if (fData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
83           if (types & NO_DOT_OR_DOTDOT) {
84                 if (!strcmp(fData.cFileName,".") || !strcmp(fData.cFileName,"..")) {
85                   continue;
86                 }
87           }
88
89       if (!(types & TYPE_DIR)) {
90         continue;
91       }
92         } else if ((fData.dwFileAttributes & FILE_ATTRIBUTE_DEVICE) ||
93                                 (fData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT))
94         {
95                 continue; // always ignore device files
96     } else if (!(types & TYPE_FILE)) {
97        continue;
98     }
99
100     result.push_back(file(fData.cFileName));
101   }
102
103   FindClose(find);
104 #else
105   DIR* dp = opendir(_path.c_str());
106   if (!dp) {
107     SG_LOG(SG_GENERAL, SG_WARN, "Dir::children: opendir failed:" << _path.str());
108     return result;
109   }
110   
111   while (true) {
112     struct dirent* entry = readdir(dp);
113     if (!entry) {
114       break; // done iteration
115     }
116     
117     // skip hidden files (names beginning with '.') unless requested
118     if (!(types & INCLUDE_HIDDEN) && (entry->d_name[0] == '.')) {
119       continue;
120     }
121     
122     int type = entry->d_type;
123     if (type == DT_LNK) {
124       // find symlink target type using stat()
125       struct stat s;
126       if (stat(file(entry->d_name).c_str(), &s)) {
127         continue; // stat() failed
128       }
129       
130       if (S_ISDIR(s.st_mode)) {
131         type = DT_DIR;
132       } else if (S_ISREG(s.st_mode)) {
133         type = DT_REG;
134       } else {
135         // symlink to block/fifo/char file, ignore
136         continue;
137       }
138     } // of symlink look-through
139     
140     if (type == DT_DIR) {
141       if (!(types & TYPE_DIR)) {
142         continue;
143       }
144       
145       if (types & NO_DOT_OR_DOTDOT) {
146         if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) {
147           continue;
148         }
149       }
150     } else if (type == DT_REG) {
151       if (!(types & TYPE_FILE)) {
152         continue;
153       }
154     } else {
155       continue; // ignore char/block devices, fifos, etc
156     }
157     
158     if (!nameFilter.empty()) {
159       if (strstr(entry->d_name, nameFilter.c_str()) == NULL) {
160         continue;
161       }
162     }
163     
164   // passed all criteria, add to our result vector
165     result.push_back(file(entry->d_name));
166   }
167   
168   closedir(dp);
169 #endif
170   return result;
171 }
172
173 bool Dir::exists() const
174 {
175 #ifdef _WIN32
176   struct _stat buf ;
177
178   if (_stat (_path.c_str(), &buf ) < 0) {
179     return false;
180   }
181   
182   return ((S_IFDIR & buf.st_mode ) !=0);
183 #else
184   struct stat buf ;
185
186   if (stat(_path.c_str(), &buf ) < 0) {
187     return false ;
188   }
189   
190   return ((S_ISDIR(buf.st_mode )) != 0);
191 #endif
192 }
193
194 SGPath Dir::file(const std::string& name) const
195 {
196   SGPath childPath = _path;
197   childPath.append(name);
198   return childPath;  
199 }
200
201 } // of namespace simgear