]> git.mxchange.org Git - simgear.git/blob - simgear/misc/sg_dir.cxx
Fix sg_dir dot-file handling
[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 & NO_DOT_OR_DOTDOT) {
82                 if (!strcmp(fData.cFileName,".") || !strcmp(fData.cFileName,"..")) {
83                   continue;
84                 }
85           }
86
87       if (!(types & TYPE_DIR)) {
88         continue;
89       }
90         } else if ((fData.dwFileAttributes & FILE_ATTRIBUTE_DEVICE) ||
91                                 (fData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT))
92         {
93                 continue; // always ignore device files
94     } else if (!(types & TYPE_FILE)) {
95        continue;
96     }
97
98     result.push_back(file(fData.cFileName));
99   }
100
101   FindClose(find);
102 #else
103   DIR* dp = opendir(_path.c_str());
104   if (!dp) {
105     SG_LOG(SG_GENERAL, SG_WARN, "Dir::children: opendir failed:" << _path.str());
106     return result;
107   }
108   
109   int filterLen = nameFilter.size();
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          strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")) {
120       continue;
121     }
122     
123     SGPath f = file(entry->d_name);
124     if (!f.exists()) {
125       continue; // stat() failed
126     }
127     
128     if (f.isDir()) {
129       // directory handling
130       if (!(types & TYPE_DIR)) {
131         continue;
132       }
133       
134       if (types & NO_DOT_OR_DOTDOT) {
135         if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) {
136           continue;
137         }
138       }
139     } else if (f.isFile()) {
140       // regular file handling
141       if (!(types & TYPE_FILE)) {
142         continue;
143       }
144     } else {
145       // block device /fifo/char file, ignore
146       continue;
147     }
148
149     if (!nameFilter.empty()) {
150       int nameLen = strlen(entry->d_name);
151       if (nameLen < filterLen) {
152         continue; // name is shorter than the filter
153       }
154     
155       char* nameSuffix = entry->d_name + (nameLen - filterLen);
156       if (strcmp(nameSuffix, nameFilter.c_str())) {
157         continue;
158       }
159     }
160     
161   // passed all criteria, add to our result vector
162     result.push_back(file(entry->d_name));
163   }
164   
165   closedir(dp);
166 #endif
167   return result;
168 }
169
170 bool Dir::exists() const
171 {
172   return _path.isDir();
173 }
174
175 SGPath Dir::file(const std::string& name) const
176 {
177   SGPath childPath = _path;
178   childPath.append(name);
179   return childPath;  
180 }
181
182 } // of namespace simgear