1 /*
2 *
3 * Copyright (c) 1998-2002
4 * John Maddock
5 *
6 * Use, modification and distribution are subject to the
7 * Boost Software License, Version 1.0. (See accompanying file
8 * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 *
10 */
11
12 /*
13 * LOCATION: see http://www.boost.org for most recent version.
14 * FILE fileiter.hpp
15 * VERSION see <boost/version.hpp>
16 * DESCRIPTION: Declares various platform independent file and
17 * directory iterators, plus binary file input in
18 * the form of class map_file.
19 */
20
21 #ifndef BOOST_RE_FILEITER_HPP_INCLUDED
22 #define BOOST_RE_FILEITER_HPP_INCLUDED
23
24 #ifndef BOOST_REGEX_CONFIG_HPP
25 #include <boost/regex/config.hpp>
26 #endif
27 #include <boost/assert.hpp>
28
29 #ifndef BOOST_REGEX_NO_FILEITER
30
31 #if (defined(__CYGWIN__) || defined(__CYGWIN32__)) && !defined(BOOST_REGEX_NO_W32)
32 #error "Sorry, can't mix <windows.h> with STL code and gcc compiler: if you ran configure, try again with configure --disable-ms-windows"
33 #define BOOST_REGEX_FI_WIN32_MAP
34 #define BOOST_REGEX_FI_POSIX_DIR
35 #elif (defined(__WIN32__) || defined(_WIN32) || defined(WIN32)) && !defined(BOOST_REGEX_NO_W32)
36 #define BOOST_REGEX_FI_WIN32_MAP
37 #define BOOST_REGEX_FI_WIN32_DIR
38 #else
39 #define BOOST_REGEX_FI_POSIX_MAP
40 #define BOOST_REGEX_FI_POSIX_DIR
41 #endif
42
43 #if defined(BOOST_REGEX_FI_WIN32_MAP)||defined(BOOST_REGEX_FI_WIN32_DIR)
44 #include <windows.h>
45 #endif
46
47 #if defined(BOOST_REGEX_FI_WIN32_DIR)
48
49 #include <cstddef>
50
51 namespace boost{
52 namespace BOOST_REGEX_DETAIL_NS{
53
54 #ifndef BOOST_NO_ANSI_APIS
55 typedef WIN32_FIND_DATAA _fi_find_data;
56 #else
57 typedef WIN32_FIND_DATAW _fi_find_data;
58 #endif
59 typedef HANDLE _fi_find_handle;
60
61 } // namespace BOOST_REGEX_DETAIL_NS
62
63 } // namespace boost
64
65 #define _fi_invalid_handle INVALID_HANDLE_VALUE
66 #define _fi_dir FILE_ATTRIBUTE_DIRECTORY
67
68 #elif defined(BOOST_REGEX_FI_POSIX_DIR)
69
70 #include <cstddef>
71 #include <cstdio>
72 #include <cctype>
73 #include <iterator>
74 #include <list>
75 #include <cassert>
76 #include <dirent.h>
77
78 #if defined(__SUNPRO_CC)
79 using std::list;
80 #endif
81
82 #ifndef MAX_PATH
83 #define MAX_PATH 256
84 #endif
85
86 namespace boost{
87 namespace BOOST_REGEX_DETAIL_NS{
88
89 #ifdef BOOST_HAS_ABI_HEADERS
90 # include BOOST_ABI_PREFIX
91 #endif
92
93 struct _fi_find_data
94 {
95 unsigned dwFileAttributes;
96 char cFileName[MAX_PATH];
97 };
98
99 struct _fi_priv_data;
100
101 typedef _fi_priv_data* _fi_find_handle;
102 #define _fi_invalid_handle 0
103 #define _fi_dir 1
104
105 _fi_find_handle _fi_FindFirstFile(const char* lpFileName, _fi_find_data* lpFindFileData);
106 bool _fi_FindNextFile(_fi_find_handle hFindFile, _fi_find_data* lpFindFileData);
107 bool _fi_FindClose(_fi_find_handle hFindFile);
108
109 #ifdef BOOST_HAS_ABI_HEADERS
110 # include BOOST_ABI_SUFFIX
111 #endif
112
113 } // namespace BOOST_REGEX_DETAIL_NS
114 } // namespace boost
115
116 #ifdef FindFirstFile
117 #undef FindFirstFile
118 #endif
119 #ifdef FindNextFile
120 #undef FindNextFile
121 #endif
122 #ifdef FindClose
123 #undef FindClose
124 #endif
125
126 #define FindFirstFileA _fi_FindFirstFile
127 #define FindNextFileA _fi_FindNextFile
128 #define FindClose _fi_FindClose
129
130 #endif
131
132 namespace boost{
133 namespace BOOST_REGEX_DETAIL_NS{
134
135 #ifdef BOOST_HAS_ABI_HEADERS
136 # include BOOST_ABI_PREFIX
137 #endif
138
139 #ifdef BOOST_REGEX_FI_WIN32_MAP // win32 mapfile
140
141 class BOOST_REGEX_DECL mapfile
142 {
143 HANDLE hfile;
144 HANDLE hmap;
145 const char* _first;
146 const char* _last;
147 public:
148
149 typedef const char* iterator;
150
mapfile()151 mapfile(){ hfile = hmap = 0; _first = _last = 0; }
mapfile(const char * file)152 mapfile(const char* file){ hfile = hmap = 0; _first = _last = 0; open(file); }
~mapfile()153 ~mapfile(){ close(); }
154 void open(const char* file);
155 void close();
begin()156 const char* begin(){ return _first; }
end()157 const char* end(){ return _last; }
size()158 size_t size(){ return _last - _first; }
valid()159 bool valid(){ return (hfile != 0) && (hfile != INVALID_HANDLE_VALUE); }
160 };
161
162
163 #else
164
165 class BOOST_REGEX_DECL mapfile_iterator;
166
167 class BOOST_REGEX_DECL mapfile
168 {
169 typedef char* pointer;
170 std::FILE* hfile;
171 long int _size;
172 pointer* _first;
173 pointer* _last;
174 mutable std::list<pointer*> condemed;
175 enum sizes
176 {
177 buf_size = 4096
178 };
179 void lock(pointer* node)const;
180 void unlock(pointer* node)const;
181 public:
182
183 typedef mapfile_iterator iterator;
184
185 mapfile(){ hfile = 0; _size = 0; _first = _last = 0; }
186 mapfile(const char* file){ hfile = 0; _size = 0; _first = _last = 0; open(file); }
187 ~mapfile(){ close(); }
188 void open(const char* file);
189 void close();
190 iterator begin()const;
191 iterator end()const;
192 unsigned long size()const{ return _size; }
193 bool valid()const{ return hfile != 0; }
194 friend class mapfile_iterator;
195 };
196
197 class BOOST_REGEX_DECL mapfile_iterator
198 {
199 typedef mapfile::pointer internal_pointer;
200 internal_pointer* node;
201 const mapfile* file;
202 unsigned long offset;
203 long position()const
204 {
205 return file ? ((node - file->_first) * mapfile::buf_size + offset) : 0;
206 }
207 void position(long pos)
208 {
209 if(file)
210 {
211 node = file->_first + (pos / mapfile::buf_size);
212 offset = pos % mapfile::buf_size;
213 }
214 }
215 public:
216 typedef std::ptrdiff_t difference_type;
217 typedef char value_type;
218 typedef const char* pointer;
219 typedef const char& reference;
220 typedef std::random_access_iterator_tag iterator_category;
221
222 mapfile_iterator() { node = 0; file = 0; offset = 0; }
223 mapfile_iterator(const mapfile* f, long arg_position)
224 {
225 BOOST_ASSERT(f);
226 file = f;
227 node = f->_first + arg_position / mapfile::buf_size;
228 offset = arg_position % mapfile::buf_size;
229 file->lock(node);
230 }
231 mapfile_iterator(const mapfile_iterator& i)
232 {
233 file = i.file;
234 node = i.node;
235 offset = i.offset;
236 if(file)
237 file->lock(node);
238 }
239 ~mapfile_iterator()
240 {
241 if(file && node)
242 file->unlock(node);
243 }
244 mapfile_iterator& operator = (const mapfile_iterator& i);
245 char operator* ()const
246 {
247 BOOST_ASSERT(node >= file->_first);
248 BOOST_ASSERT(node < file->_last);
249 return file ? *(*node + sizeof(int) + offset) : char(0);
250 }
251 char operator[] (long off)const
252 {
253 mapfile_iterator tmp(*this);
254 tmp += off;
255 return *tmp;
256 }
257 mapfile_iterator& operator++ ();
258 mapfile_iterator operator++ (int);
259 mapfile_iterator& operator-- ();
260 mapfile_iterator operator-- (int);
261
262 mapfile_iterator& operator += (long off)
263 {
264 position(position() + off);
265 return *this;
266 }
267 mapfile_iterator& operator -= (long off)
268 {
269 position(position() - off);
270 return *this;
271 }
272
273 friend inline bool operator==(const mapfile_iterator& i, const mapfile_iterator& j)
274 {
275 return (i.file == j.file) && (i.node == j.node) && (i.offset == j.offset);
276 }
277
278 friend inline bool operator!=(const mapfile_iterator& i, const mapfile_iterator& j)
279 {
280 return !(i == j);
281 }
282
283 friend inline bool operator<(const mapfile_iterator& i, const mapfile_iterator& j)
284 {
285 return i.position() < j.position();
286 }
287 friend inline bool operator>(const mapfile_iterator& i, const mapfile_iterator& j)
288 {
289 return i.position() > j.position();
290 }
291 friend inline bool operator<=(const mapfile_iterator& i, const mapfile_iterator& j)
292 {
293 return i.position() <= j.position();
294 }
295 friend inline bool operator>=(const mapfile_iterator& i, const mapfile_iterator& j)
296 {
297 return i.position() >= j.position();
298 }
299
300 friend mapfile_iterator operator + (const mapfile_iterator& i, long off);
301 friend mapfile_iterator operator + (long off, const mapfile_iterator& i)
302 {
303 mapfile_iterator tmp(i);
304 return tmp += off;
305 }
306 friend mapfile_iterator operator - (const mapfile_iterator& i, long off);
307 friend inline long operator - (const mapfile_iterator& i, const mapfile_iterator& j)
308 {
309 return i.position() - j.position();
310 }
311 };
312
313 #endif
314
315 // _fi_sep determines the directory separator, either '\\' or '/'
316 BOOST_REGEX_DECL extern const char* _fi_sep;
317
318 struct file_iterator_ref
319 {
320 _fi_find_handle hf;
321 _fi_find_data _data;
322 long count;
323 };
324
325
326 class BOOST_REGEX_DECL file_iterator
327 {
328 char* _root;
329 char* _path;
330 char* ptr;
331 file_iterator_ref* ref;
332
333 public:
334 typedef std::ptrdiff_t difference_type;
335 typedef const char* value_type;
336 typedef const char** pointer;
337 typedef const char*& reference;
338 typedef std::input_iterator_tag iterator_category;
339
340 file_iterator();
341 file_iterator(const char* wild);
342 ~file_iterator();
343 file_iterator(const file_iterator&);
344 file_iterator& operator=(const file_iterator&);
root() const345 const char* root()const { return _root; }
path() const346 const char* path()const { return _path; }
name() const347 const char* name()const { return ptr; }
data()348 _fi_find_data* data() { return &(ref->_data); }
349 void next();
operator ++()350 file_iterator& operator++() { next(); return *this; }
351 file_iterator operator++(int);
operator *()352 const char* operator*() { return path(); }
353
operator ==(const file_iterator & f1,const file_iterator & f2)354 friend inline bool operator == (const file_iterator& f1, const file_iterator& f2)
355 {
356 return ((f1.ref->hf == _fi_invalid_handle) && (f2.ref->hf == _fi_invalid_handle));
357 }
358
operator !=(const file_iterator & f1,const file_iterator & f2)359 friend inline bool operator != (const file_iterator& f1, const file_iterator& f2)
360 {
361 return !(f1 == f2);
362 }
363
364 };
365
366 // dwa 9/13/00 - suppress unused parameter warning
operator <(const file_iterator &,const file_iterator &)367 inline bool operator < (const file_iterator&, const file_iterator&)
368 {
369 return false;
370 }
371
372
373 class BOOST_REGEX_DECL directory_iterator
374 {
375 char* _root;
376 char* _path;
377 char* ptr;
378 file_iterator_ref* ref;
379
380 public:
381 typedef std::ptrdiff_t difference_type;
382 typedef const char* value_type;
383 typedef const char** pointer;
384 typedef const char*& reference;
385 typedef std::input_iterator_tag iterator_category;
386
387 directory_iterator();
388 directory_iterator(const char* wild);
389 ~directory_iterator();
390 directory_iterator(const directory_iterator& other);
391 directory_iterator& operator=(const directory_iterator& other);
392
root() const393 const char* root()const { return _root; }
path() const394 const char* path()const { return _path; }
name() const395 const char* name()const { return ptr; }
data()396 _fi_find_data* data() { return &(ref->_data); }
397 void next();
operator ++()398 directory_iterator& operator++() { next(); return *this; }
399 directory_iterator operator++(int);
operator *()400 const char* operator*() { return path(); }
401
separator()402 static const char* separator() { return _fi_sep; }
403
operator ==(const directory_iterator & f1,const directory_iterator & f2)404 friend inline bool operator == (const directory_iterator& f1, const directory_iterator& f2)
405 {
406 return ((f1.ref->hf == _fi_invalid_handle) && (f2.ref->hf == _fi_invalid_handle));
407 }
408
409
operator !=(const directory_iterator & f1,const directory_iterator & f2)410 friend inline bool operator != (const directory_iterator& f1, const directory_iterator& f2)
411 {
412 return !(f1 == f2);
413 }
414
415 };
416
operator <(const directory_iterator &,const directory_iterator &)417 inline bool operator < (const directory_iterator&, const directory_iterator&)
418 {
419 return false;
420 }
421
422 #ifdef BOOST_HAS_ABI_HEADERS
423 # include BOOST_ABI_SUFFIX
424 #endif
425
426
427 } // namespace BOOST_REGEX_DETAIL_NS
428 using boost::BOOST_REGEX_DETAIL_NS::directory_iterator;
429 using boost::BOOST_REGEX_DETAIL_NS::file_iterator;
430 using boost::BOOST_REGEX_DETAIL_NS::mapfile;
431 } // namespace boost
432
433 #endif // BOOST_REGEX_NO_FILEITER
434 #endif // BOOST_RE_FILEITER_HPP
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453