Debian Codegraph @

src/compat/pdir.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2001,2006 Dizzy
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18 #define PDIR_INTERNAL_ACCESS
19 #include "common/setup_before.h"
20 #include "pdir.h"
21
22 #include <cstring>
23
24 #include "common/eventlog.h"
25 #include "common/setup_after.h"
26 #include "compat/strcasecmp.h"
27
28 #ifdef WIN32
29 #include "win32/dirent.h"
30 #else
31 #include <dirent.h>
32 #endif
33
34 namespace pvpgn
35 {
36
37 Directory::Directory(const std::string& path_, bool lazyread_)
38 {
39 open(path_, lazyread_);
40 }
41
42 Directory::~Directory() throw()
43 {
44 close();
45 }
46
47 void
48 Directory::close()
49 {
50 #ifdef WIN32
51 if (lFindHandle >= 0)
52 _findclose(lFindHandle);
53 #else /* POSIX */
54 if (dir) closedir(dir);
55 #endif /* WIN32-POSIX */
56 }
57
58 void
59 Directory::open(const std::string& path_, bool lazyread_)
60 {
61 #ifdef WIN32
62 std::string tmp(path_);
63
64 if (tmp.size() + 1 + 3 >= _MAX_PATH)
65 throw std::runtime_error("pvpgn::Directory::Directory(): WIN32: path too long");
66 tmp += "/*.*";
67
68 status = 0;
69 std::memset(&fileinfo, 0, sizeof(fileinfo));
70 lFindHandle = _findfirst(tmp.c_str(), &fileinfo);
71 if (lFindHandle < 0 && !lazyread_)
72 throw OpenError(tmp);
73
74 path = tmp;
75 #else /* POSIX style */
76 if (!(dir = opendir(path_.c_str())) && !lazyread_)
77 throw OpenError(path);
78 path = path_;
79 #endif /* WIN32-POSIX */
80
81 lazyread = lazyread_;
82 }
83
84 void
85 Directory::rewind()
86 {
87 #ifdef WIN32
88 close();
89 status = 0;
90 std::memset(&fileinfo, 0, sizeof(fileinfo));
91 lFindHandle = _findfirst(path.c_str(), &fileinfo);
92 if (lFindHandle < 0 && !lazyread) {
93 ERROR0("WIN32: couldn't rewind directory");
94 status = -1;
95 }
96 #else /* POSIX */
97 if (dir) rewinddir(dir);
98 #endif
99 }
100
101
102 char const *
103 Directory::read() const
104 {
105 const char * result;
106
107 #ifdef WIN32
108 switch (status) {
109 default:
110 case -1: /* couldn't rewind */
111 ERROR0("got status -1");
112 return 0;
113 case 0: /* freshly opened */
114 status = 1;
115 if (lFindHandle < 0) return 0;
116 result = fileinfo.name;
117 break;
118 case 1: /* reading */
119 if (lFindHandle < 0) return 0;
120
121 if (_findnext(lFindHandle, &fileinfo) < 0) {
122 status = 2;
123 return 0;
124 }
125 else result = fileinfo.name;
126 break;
127 case 2: /* EOF */
128 return 0;
129 }
130 #else /* POSIX */
131 struct dirent *dentry = dir ? readdir(dir) : 0;
132 if (!dentry) return 0;
133
134 result = dentry->d_name;
135 #endif /* WIN32-POSIX */
136
137 if (!(strcmp(result, ".") && strcmp(result, "..")))
138 /* here we presume we don't get an infinite number of "." or ".." ;) */
139 return read();
140 return result;
141 }
142
143 Directory::operator bool() const
144 {
145 #ifdef WIN32
146 return lFindHandle >= 0;
147 #else
148 return dir != 0;
149 #endif
150 }
151
152 bool is_directory(const char* pzPath);
153
154
155
156 /* Returns a list of files in a directory (except the ones that begin with a dot) */
157 extern std::vector<std::string> dir_getfiles(const char * directory, const char* ext, bool recursive)
158 {
159 std::vector<std::string> files, dfiles;
160 const char* _ext;
161
162 DIR *dir;
163 struct dirent* ent;
164
165 dir = opendir(directory);
166 if (!dir)
167 return files;
168
169 while ((ent = readdir(dir)) != NULL)
170 {
171 const std::string file_name = ent->d_name;
172 const std::string full_file_name = std::string(directory) + "/" + file_name;
173
174 if (file_name[0] == '.')
175 continue;
176
177 if (is_directory(full_file_name.c_str()))
178 {
179 // iterate subdirectories
180 if (recursive)
181 {
182 std::vector<std::string> subfiles = dir_getfiles(full_file_name.c_str(), ext, recursive);
183
184 for (std::size_t i = 0; i < subfiles.size(); ++i)
185 dfiles.push_back(subfiles[i]);
186 }
187 continue;
188 }
189
190 // filter by extension
191 _ext = strrchr(file_name.c_str(), '.');
192 if (strcmp(ext, "*") != 0)
193 if (!_ext || strcasecmp(_ext, ext) != 0)
194 continue;
195
196 files.push_back(full_file_name);
197 }
198 closedir(dir);
199
200 // merge files and files from directories, so we will receive directories at begin, files at the end
201 // (otherwise files and directories are read alphabetically - as is)
202 files.insert(files.begin(), dfiles.begin(), dfiles.end());
203
204 return files;
205 }
206
207 bool is_directory(const char* pzPath)
208 {
209 if (pzPath == NULL) return false;
210
211 DIR *pDir;
212 bool bExists = false;
213
214 pDir = opendir(pzPath);
215
216 if (pDir != NULL)
217 {
218 bExists = true;
219 (void)closedir(pDir);
220 }
221
222 return bExists;
223 }
224 }