]> git.mxchange.org Git - simgear.git/blob - simgear/misc/sg_dir.cxx
Initial work on simgear::Dir, replacement for PLIB ulDir functions.
[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 <iostream>
35
36 namespace simgear
37 {
38
39 Dir::Dir(const SGPath& path) :
40   _path(path)
41 {
42 }
43
44 Dir::Dir(const Dir& rel, const SGPath& relPath) :
45   _path(rel.file(relPath.str()))
46 {
47 }
48
49 PathList Dir::children(int types, const std::string& nameFilter) const
50 {
51   PathList result;
52   if (types == 0) {
53     types = TYPE_FILE | TYPE_DIR | NO_DOT_OR_DOTDOT;
54   }
55   
56 #ifdef _WIN32
57   std::string search(_path.str());
58   if (nameFilter.empty()) {
59     search += "\\*"; // everything
60   } else {
61     search += "\\*" + nameFilter;
62   }
63   
64   WIN32_FIND_DATA fData;
65   HANDLE find = FindFirstFile(search.c_str(), &fData);
66   if (find == INVALID_HANDLE_VALUE) {
67     SG_LOG(SG_GENERAL, SG_WARN, "Dir::children: FindFirstFile failed:" << _path.str());
68     return result;
69   }
70   
71   bool done = false;
72   for (bool done = false; !done; done = (FindNextFile(find, &fData) == 0)) {
73     if (fData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) {
74       if (!(types & INCLUDE_HIDDEN)) {
75         continue;
76       }
77     }
78     
79     if (fData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
80       if (!(types & TYPE_DIR)) {
81         continue;
82       }
83         } else if ((fData.dwFileAttributes & FILE_ATTRIBUTE_DEVICE) ||
84                                 (fData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT))
85         {
86                 continue; // always ignore device files
87     } else if (!(types & TYPE_FILE)) {
88        continue;
89     }
90
91     result.push_back(file(fData.cFileName));
92   }
93
94   FindClose(find);
95 #else
96   DIR* dp = opendir(_path.c_str());
97   if (!dp) {
98     SG_LOG(SG_GENERAL, SG_WARN, "Dir::children: opendir failed:" << _path.str());
99     return result;
100   }
101   
102   while (true) {
103     struct dirent* entry = readdir(dp);
104     if (!entry) {
105       break; // done iteration
106     }
107     
108     // skip hidden files (names beginning with '.') unless requested
109     if (!(types & INCLUDE_HIDDEN) && (entry->d_name[0] == '.')) {
110       continue;
111     }
112         
113     if (entry->d_type == DT_DIR) {
114       if (!(types & TYPE_DIR)) {
115         continue;
116       }
117       
118       if (types & NO_DOT_OR_DOTDOT) {
119         if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) {
120           continue;
121         }
122       }
123     } else if (entry->d_type == DT_REG) {
124       if (!(types & TYPE_FILE)) {
125         continue;
126       }
127     } else {
128       continue; // ignore char/block devices, fifos, etc
129     }
130     
131     if (!nameFilter.empty()) {
132       if (strstr(entry->d_name, nameFilter.c_str()) == NULL) {
133         continue;
134       }
135     }
136     
137   // passed all criteria, add to our result vector
138     result.push_back(file(entry->d_name));
139   }
140   
141   closedir(dp);
142 #endif
143   return result;
144 }
145
146 SGPath Dir::file(const std::string& name) const
147 {
148   SGPath childPath = _path;
149   childPath.append(name);
150   return childPath;  
151 }
152
153 } // of namespace simgear