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 #if !defined(BOOST_EMBTC)
274
275 friend inline bool operator==(const mapfile_iterator& i, const mapfile_iterator& j)
276 {
277 return (i.file == j.file) && (i.node == j.node) && (i.offset == j.offset);
278 }
279
280 friend inline bool operator!=(const mapfile_iterator& i, const mapfile_iterator& j)
281 {
282 return !(i == j);
283 }
284
285 friend inline bool operator<(const mapfile_iterator& i, const mapfile_iterator& j)
286 {
287 return i.position() < j.position();
288 }
289 friend inline bool operator>(const mapfile_iterator& i, const mapfile_iterator& j)
290 {
291 return i.position() > j.position();
292 }
293 friend inline bool operator<=(const mapfile_iterator& i, const mapfile_iterator& j)
294 {
295 return i.position() <= j.position();
296 }
297 friend inline bool operator>=(const mapfile_iterator& i, const mapfile_iterator& j)
298 {
299 return i.position() >= j.position();
300 }
301
302 friend mapfile_iterator operator + (const mapfile_iterator& i, long off);
303 friend mapfile_iterator operator + (long off, const mapfile_iterator& i)
304 {
305 mapfile_iterator tmp(i);
306 return tmp += off;
307 }
308 friend mapfile_iterator operator - (const mapfile_iterator& i, long off);
309 friend inline long operator - (const mapfile_iterator& i, const mapfile_iterator& j)
310 {
311 return i.position() - j.position();
312 }
313
314 #else
315
316 friend bool operator==(const mapfile_iterator& i, const mapfile_iterator& j);
317 friend bool operator!=(const mapfile_iterator& i, const mapfile_iterator& j);
318 friend bool operator<(const mapfile_iterator& i, const mapfile_iterator& j);
319 friend bool operator>(const mapfile_iterator& i, const mapfile_iterator& j);
320 friend bool operator<=(const mapfile_iterator& i, const mapfile_iterator& j);
321 friend bool operator>=(const mapfile_iterator& i, const mapfile_iterator& j);
322 friend mapfile_iterator operator + (const mapfile_iterator& i, long off);
323 friend mapfile_iterator operator + (long off, const mapfile_iterator& i);
324 friend mapfile_iterator operator - (const mapfile_iterator& i, long off);
325 friend long operator - (const mapfile_iterator& i, const mapfile_iterator& j);
326
327 #endif
328
329 };
330
331 #if defined(BOOST_EMBTC)
332
333 inline bool operator==(const mapfile_iterator& i, const mapfile_iterator& j)
334 {
335 return (i.file == j.file) && (i.node == j.node) && (i.offset == j.offset);
336 }
337
338 inline bool operator!=(const mapfile_iterator& i, const mapfile_iterator& j)
339 {
340 return !(i == j);
341 }
342
343 inline bool operator<(const mapfile_iterator& i, const mapfile_iterator& j)
344 {
345 return i.position() < j.position();
346 }
347 inline bool operator>(const mapfile_iterator& i, const mapfile_iterator& j)
348 {
349 return i.position() > j.position();
350 }
351 inline bool operator<=(const mapfile_iterator& i, const mapfile_iterator& j)
352 {
353 return i.position() <= j.position();
354 }
355 inline bool operator>=(const mapfile_iterator& i, const mapfile_iterator& j)
356 {
357 return i.position() >= j.position();
358 }
359 mapfile_iterator operator + (long off, const mapfile_iterator& i)
360 {
361 mapfile_iterator tmp(i);
362 return tmp += off;
363 }
364 inline long operator - (const mapfile_iterator& i, const mapfile_iterator& j)
365 {
366 return i.position() - j.position();
367 }
368
369 #endif
370
371 #endif
372
373 // _fi_sep determines the directory separator, either '\\' or '/'
374 BOOST_REGEX_DECL extern const char* _fi_sep;
375
376 struct file_iterator_ref
377 {
378 _fi_find_handle hf;
379 _fi_find_data _data;
380 long count;
381 };
382
383
384 class BOOST_REGEX_DECL file_iterator
385 {
386 char* _root;
387 char* _path;
388 char* ptr;
389 file_iterator_ref* ref;
390
391 public:
392 typedef std::ptrdiff_t difference_type;
393 typedef const char* value_type;
394 typedef const char** pointer;
395 typedef const char*& reference;
396 typedef std::input_iterator_tag iterator_category;
397
398 file_iterator();
399 file_iterator(const char* wild);
400 ~file_iterator();
401 file_iterator(const file_iterator&);
402 file_iterator& operator=(const file_iterator&);
root() const403 const char* root()const { return _root; }
path() const404 const char* path()const { return _path; }
name() const405 const char* name()const { return ptr; }
data()406 _fi_find_data* data() { return &(ref->_data); }
407 void next();
operator ++()408 file_iterator& operator++() { next(); return *this; }
409 file_iterator operator++(int);
operator *()410 const char* operator*() { return path(); }
411
412 #if !defined(BOOST_EMBTC)
413
operator ==(const file_iterator & f1,const file_iterator & f2)414 friend inline bool operator == (const file_iterator& f1, const file_iterator& f2)
415 {
416 return ((f1.ref->hf == _fi_invalid_handle) && (f2.ref->hf == _fi_invalid_handle));
417 }
418
operator !=(const file_iterator & f1,const file_iterator & f2)419 friend inline bool operator != (const file_iterator& f1, const file_iterator& f2)
420 {
421 return !(f1 == f2);
422 }
423
424 #else
425
426 friend bool operator == (const file_iterator& f1, const file_iterator& f2);
427 friend bool operator != (const file_iterator& f1, const file_iterator& f2);
428
429 #endif
430
431 };
432
433 #if defined(BOOST_EMBTC)
434
operator ==(const file_iterator & f1,const file_iterator & f2)435 inline bool operator == (const file_iterator& f1, const file_iterator& f2)
436 {
437 return ((f1.ref->hf == _fi_invalid_handle) && (f2.ref->hf == _fi_invalid_handle));
438 }
439
operator !=(const file_iterator & f1,const file_iterator & f2)440 inline bool operator != (const file_iterator& f1, const file_iterator& f2)
441 {
442 return !(f1 == f2);
443 }
444
445 #endif
446
447 // dwa 9/13/00 - suppress unused parameter warning
operator <(const file_iterator &,const file_iterator &)448 inline bool operator < (const file_iterator&, const file_iterator&)
449 {
450 return false;
451 }
452
453
454 class BOOST_REGEX_DECL directory_iterator
455 {
456 char* _root;
457 char* _path;
458 char* ptr;
459 file_iterator_ref* ref;
460
461 public:
462 typedef std::ptrdiff_t difference_type;
463 typedef const char* value_type;
464 typedef const char** pointer;
465 typedef const char*& reference;
466 typedef std::input_iterator_tag iterator_category;
467
468 directory_iterator();
469 directory_iterator(const char* wild);
470 ~directory_iterator();
471 directory_iterator(const directory_iterator& other);
472 directory_iterator& operator=(const directory_iterator& other);
473
root() const474 const char* root()const { return _root; }
path() const475 const char* path()const { return _path; }
name() const476 const char* name()const { return ptr; }
data()477 _fi_find_data* data() { return &(ref->_data); }
478 void next();
operator ++()479 directory_iterator& operator++() { next(); return *this; }
480 directory_iterator operator++(int);
operator *()481 const char* operator*() { return path(); }
482
separator()483 static const char* separator() { return _fi_sep; }
484
485 #if !defined(BOOST_EMBTC)
486
operator ==(const directory_iterator & f1,const directory_iterator & f2)487 friend inline bool operator == (const directory_iterator& f1, const directory_iterator& f2)
488 {
489 return ((f1.ref->hf == _fi_invalid_handle) && (f2.ref->hf == _fi_invalid_handle));
490 }
491
492
operator !=(const directory_iterator & f1,const directory_iterator & f2)493 friend inline bool operator != (const directory_iterator& f1, const directory_iterator& f2)
494 {
495 return !(f1 == f2);
496 }
497
498 #else
499
500 friend bool operator == (const directory_iterator& f1, const directory_iterator& f2);
501 friend bool operator != (const directory_iterator& f1, const directory_iterator& f2);
502
503 #endif
504
505 };
506
507 #if defined(BOOST_EMBTC)
508
operator ==(const directory_iterator & f1,const directory_iterator & f2)509 inline bool operator == (const directory_iterator& f1, const directory_iterator& f2)
510 {
511 return ((f1.ref->hf == _fi_invalid_handle) && (f2.ref->hf == _fi_invalid_handle));
512 }
513
514
operator !=(const directory_iterator & f1,const directory_iterator & f2)515 inline bool operator != (const directory_iterator& f1, const directory_iterator& f2)
516 {
517 return !(f1 == f2);
518 }
519
520 #endif
521
operator <(const directory_iterator &,const directory_iterator &)522 inline bool operator < (const directory_iterator&, const directory_iterator&)
523 {
524 return false;
525 }
526
527 #ifdef BOOST_HAS_ABI_HEADERS
528 # include BOOST_ABI_SUFFIX
529 #endif
530
531
532 } // namespace BOOST_REGEX_DETAIL_NS
533 using boost::BOOST_REGEX_DETAIL_NS::directory_iterator;
534 using boost::BOOST_REGEX_DETAIL_NS::file_iterator;
535 using boost::BOOST_REGEX_DETAIL_NS::mapfile;
536 } // namespace boost
537
538 #endif // BOOST_REGEX_NO_FILEITER
539 #endif // BOOST_RE_FILEITER_HPP
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558