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
51namespace boost{
52 namespace BOOST_REGEX_DETAIL_NS{
53
54#ifndef BOOST_NO_ANSI_APIS
55typedef WIN32_FIND_DATAA _fi_find_data;
56#else
57typedef WIN32_FIND_DATAW _fi_find_data;
58#endif
59typedef 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)
79using std::list;
80#endif
81
82#ifndef MAX_PATH
83#define MAX_PATH 256
84#endif
85
86namespace boost{
87 namespace BOOST_REGEX_DETAIL_NS{
88
89#ifdef BOOST_HAS_ABI_HEADERS
90# include BOOST_ABI_PREFIX
91#endif
92
93struct _fi_find_data
94{
95 unsigned dwFileAttributes;
96 char cFileName[MAX_PATH];
97};
98
99struct _fi_priv_data;
100
101typedef _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);
106bool _fi_FindNextFile(_fi_find_handle hFindFile, _fi_find_data* lpFindFileData);
107bool _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
132namespace 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
141class BOOST_REGEX_DECL mapfile
142{
143 HANDLE hfile;
144 HANDLE hmap;
145 const char* _first;
146 const char* _last;
147public:
148
149 typedef const char* iterator;
150
151 mapfile(){ hfile = hmap = 0; _first = _last = 0; }
152 mapfile(const char* file){ hfile = hmap = 0; _first = _last = 0; open(file); }
153 ~mapfile(){ close(); }
154 void open(const char* file);
155 void close();
156 const char* begin(){ return _first; }
157 const char* end(){ return _last; }
158 size_t size(){ return _last - _first; }
159 bool valid(){ return (hfile != 0) && (hfile != INVALID_HANDLE_VALUE); }
160};
161
162
163#else
164
165class BOOST_REGEX_DECL mapfile_iterator;
166
167class 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;
181public:
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
197class BOOST_REGEX_DECL mapfile_iterator
198#if !defined(BOOST_NO_STD_ITERATOR) || defined(BOOST_MSVC_STD_ITERATOR)
199: public std::iterator<std::random_access_iterator_tag, char>
200#endif
201{
202 typedef mapfile::pointer internal_pointer;
203 internal_pointer* node;
204 const mapfile* file;
205 unsigned long offset;
206 long position()const
207 {
208 return file ? ((node - file->_first) * mapfile::buf_size + offset) : 0;
209 }
210 void position(long pos)
211 {
212 if(file)
213 {
214 node = file->_first + (pos / mapfile::buf_size);
215 offset = pos % mapfile::buf_size;
216 }
217 }
218public:
219 typedef std::ptrdiff_t difference_type;
220 typedef char value_type;
221 typedef const char* pointer;
222 typedef const char& reference;
223 typedef std::random_access_iterator_tag iterator_category;
224
225 mapfile_iterator() { node = 0; file = 0; offset = 0; }
226 mapfile_iterator(const mapfile* f, long arg_position)
227 {
228 file = f;
229 node = f->_first + arg_position / mapfile::buf_size;
230 offset = arg_position % mapfile::buf_size;
231 if(file)
232 file->lock(node);
233 }
234 mapfile_iterator(const mapfile_iterator& i)
235 {
236 file = i.file;
237 node = i.node;
238 offset = i.offset;
239 if(file)
240 file->lock(node);
241 }
242 ~mapfile_iterator()
243 {
244 if(file && node)
245 file->unlock(node);
246 }
247 mapfile_iterator& operator = (const mapfile_iterator& i);
248 char operator* ()const
249 {
250 BOOST_ASSERT(node >= file->_first);
251 BOOST_ASSERT(node < file->_last);
252 return file ? *(*node + sizeof(int) + offset) : char(0);
253 }
254 char operator[] (long off)const
255 {
256 mapfile_iterator tmp(*this);
257 tmp += off;
258 return *tmp;
259 }
260 mapfile_iterator& operator++ ();
261 mapfile_iterator operator++ (int);
262 mapfile_iterator& operator-- ();
263 mapfile_iterator operator-- (int);
264
265 mapfile_iterator& operator += (long off)
266 {
267 position(pos: position() + off);
268 return *this;
269 }
270 mapfile_iterator& operator -= (long off)
271 {
272 position(pos: position() - off);
273 return *this;
274 }
275
276 friend inline bool operator==(const mapfile_iterator& i, const mapfile_iterator& j)
277 {
278 return (i.file == j.file) && (i.node == j.node) && (i.offset == j.offset);
279 }
280
281 friend inline bool operator!=(const mapfile_iterator& i, const mapfile_iterator& j)
282 {
283 return !(i == j);
284 }
285
286 friend inline bool operator<(const mapfile_iterator& i, const mapfile_iterator& j)
287 {
288 return i.position() < j.position();
289 }
290 friend inline bool operator>(const mapfile_iterator& i, const mapfile_iterator& j)
291 {
292 return i.position() > j.position();
293 }
294 friend inline bool operator<=(const mapfile_iterator& i, const mapfile_iterator& j)
295 {
296 return i.position() <= j.position();
297 }
298 friend inline bool operator>=(const mapfile_iterator& i, const mapfile_iterator& j)
299 {
300 return i.position() >= j.position();
301 }
302
303 friend mapfile_iterator operator + (const mapfile_iterator& i, long off);
304 friend mapfile_iterator operator + (long off, const mapfile_iterator& i)
305 {
306 mapfile_iterator tmp(i);
307 return tmp += off;
308 }
309 friend mapfile_iterator operator - (const mapfile_iterator& i, long off);
310 friend inline long operator - (const mapfile_iterator& i, const mapfile_iterator& j)
311 {
312 return i.position() - j.position();
313 }
314};
315
316#endif
317
318// _fi_sep determines the directory separator, either '\\' or '/'
319BOOST_REGEX_DECL extern const char* _fi_sep;
320
321struct file_iterator_ref
322{
323 _fi_find_handle hf;
324 _fi_find_data _data;
325 long count;
326};
327
328
329class BOOST_REGEX_DECL file_iterator
330{
331 char* _root;
332 char* _path;
333 char* ptr;
334 file_iterator_ref* ref;
335
336public:
337 typedef std::ptrdiff_t difference_type;
338 typedef const char* value_type;
339 typedef const char** pointer;
340 typedef const char*& reference;
341 typedef std::input_iterator_tag iterator_category;
342
343 file_iterator();
344 file_iterator(const char* wild);
345 ~file_iterator();
346 file_iterator(const file_iterator&);
347 file_iterator& operator=(const file_iterator&);
348 const char* root()const { return _root; }
349 const char* path()const { return _path; }
350 const char* name()const { return ptr; }
351 _fi_find_data* data() { return &(ref->_data); }
352 void next();
353 file_iterator& operator++() { next(); return *this; }
354 file_iterator operator++(int);
355 const char* operator*() { return path(); }
356
357 friend inline bool operator == (const file_iterator& f1, const file_iterator& f2)
358 {
359 return ((f1.ref->hf == _fi_invalid_handle) && (f2.ref->hf == _fi_invalid_handle));
360 }
361
362 friend inline bool operator != (const file_iterator& f1, const file_iterator& f2)
363 {
364 return !(f1 == f2);
365 }
366
367};
368
369// dwa 9/13/00 - suppress unused parameter warning
370inline bool operator < (const file_iterator&, const file_iterator&)
371{
372 return false;
373}
374
375
376class BOOST_REGEX_DECL directory_iterator
377{
378 char* _root;
379 char* _path;
380 char* ptr;
381 file_iterator_ref* ref;
382
383public:
384 typedef std::ptrdiff_t difference_type;
385 typedef const char* value_type;
386 typedef const char** pointer;
387 typedef const char*& reference;
388 typedef std::input_iterator_tag iterator_category;
389
390 directory_iterator();
391 directory_iterator(const char* wild);
392 ~directory_iterator();
393 directory_iterator(const directory_iterator& other);
394 directory_iterator& operator=(const directory_iterator& other);
395
396 const char* root()const { return _root; }
397 const char* path()const { return _path; }
398 const char* name()const { return ptr; }
399 _fi_find_data* data() { return &(ref->_data); }
400 void next();
401 directory_iterator& operator++() { next(); return *this; }
402 directory_iterator operator++(int);
403 const char* operator*() { return path(); }
404
405 static const char* separator() { return _fi_sep; }
406
407 friend inline bool operator == (const directory_iterator& f1, const directory_iterator& f2)
408 {
409 return ((f1.ref->hf == _fi_invalid_handle) && (f2.ref->hf == _fi_invalid_handle));
410 }
411
412
413 friend inline bool operator != (const directory_iterator& f1, const directory_iterator& f2)
414 {
415 return !(f1 == f2);
416 }
417
418 };
419
420inline bool operator < (const directory_iterator&, const directory_iterator&)
421{
422 return false;
423}
424
425#ifdef BOOST_HAS_ABI_HEADERS
426# include BOOST_ABI_SUFFIX
427#endif
428
429
430} // namespace BOOST_REGEX_DETAIL_NS
431using boost::BOOST_REGEX_DETAIL_NS::directory_iterator;
432using boost::BOOST_REGEX_DETAIL_NS::file_iterator;
433using boost::BOOST_REGEX_DETAIL_NS::mapfile;
434} // namespace boost
435
436#endif // BOOST_REGEX_NO_FILEITER
437#endif // BOOST_RE_FILEITER_HPP
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456

source code of boost/boost/regex/v4/fileiter.hpp