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