]> git.lyx.org Git - lyx.git/blob - boost/boost/re_detail/fileiter.hpp
cvsignore ++
[lyx.git] / boost / boost / re_detail / fileiter.hpp
1 /*
2  *
3  * Copyright (c) 1998-2000
4  * Dr John Maddock
5  *
6  * Permission to use, copy, modify, distribute and sell this software
7  * and its documentation for any purpose is hereby granted without fee,
8  * provided that the above copyright notice appear in all copies and
9  * that both that copyright notice and this permission notice appear
10  * in supporting documentation.  Dr John Maddock makes no representations
11  * about the suitability of this software for any purpose.  
12  * It is provided "as is" without express or implied warranty.
13  *
14  */
15  
16  /*
17   *   LOCATION:    see http://www.boost.org for most recent version.
18   *   FILE         fileiter.hpp
19   *   VERSION      3.03
20   *   DESCRIPTION: Declares various platform independent file and
21   *                directory iterators, plus binary file input in
22   *                the form of class map_file.
23   */
24
25 #ifndef BOOST_RE_FILEITER_HPP
26 #define BOOST_RE_FILEITER_HPP
27
28 #include <boost/re_detail/regex_config.hpp>
29
30 #if (defined(__CYGWIN__) || defined(__CYGWIN32__)) && !defined(BOOST_RE_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 FI_WIN32_MAP
33 #define FI_POSIX_DIR
34 #elif (defined(__WIN32__) || defined(_WIN32) || defined(WIN32)) && !defined(BOOST_RE_NO_W32)
35 #define FI_WIN32_MAP
36 #define FI_WIN32_DIR
37 #else
38 #define FI_POSIX_MAP
39 #define FI_POSIX_DIR
40 #endif
41
42 #if defined(FI_WIN32_MAP)||defined(FI_WIN32_DIR)
43 #include <windows.h>
44 #endif
45
46 #if defined(FI_WIN32_DIR)
47
48 namespace boost{
49    namespace re_detail{
50
51 typedef WIN32_FIND_DATA _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(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 __BORLANDC__
82    #if __BORLANDC__ == 0x530
83     #pragma option push -a4 -b -Ve
84    #elif __BORLANDC__ > 0x530
85     #pragma option push -a8 -b -Ve
86    #endif
87 #endif
88
89 struct _fi_find_data
90 {
91    unsigned dwFileAttributes;
92    char cFileName[MAX_PATH];
93 };
94
95 struct _fi_priv_data;
96
97 typedef _fi_priv_data* _fi_find_handle;
98 #define _fi_invalid_handle NULL
99 #define _fi_dir 1
100
101 _fi_find_handle _fi_FindFirstFile(const char* lpFileName, _fi_find_data* lpFindFileData);
102 bool _fi_FindNextFile(_fi_find_handle hFindFile,   _fi_find_data* lpFindFileData);
103 bool _fi_FindClose(_fi_find_handle hFindFile);
104
105 #ifdef __BORLANDC__
106  #if __BORLANDC__ > 0x520
107   #pragma option pop
108  #endif
109 #endif
110
111    } // namespace re_detail
112 } // namespace boost
113
114 #ifdef FindFirstFile
115  #undef FindFirstFile
116 #endif
117 #ifdef FindNextFile
118  #undef FindNextFile
119 #endif
120 #ifdef FindClose
121  #undef FindClose
122 #endif
123
124 #define FindFirstFile _fi_FindFirstFile
125 #define FindNextFile _fi_FindNextFile
126 #define FindClose _fi_FindClose
127
128 #endif
129
130 namespace boost{
131    namespace re_detail{
132
133 #ifdef __BORLANDC__
134    #if __BORLANDC__ == 0x530
135     #pragma option push -a4 -b
136    #elif __BORLANDC__ > 0x530
137     #pragma option push -a8 -b
138    #endif
139 #endif
140
141 #ifdef FI_WIN32_MAP // win32 mapfile
142
143 class BOOST_RE_IX_DECL mapfile
144 {
145    HANDLE hfile;
146    HANDLE hmap;
147    const char* _first;
148    const char* _last;
149 public:
150
151    typedef const char* iterator;
152
153    mapfile(){ hfile = hmap = 0; _first = _last = 0; }
154    mapfile(const char* file){ hfile = hmap = 0; _first = _last = 0; open(file); }
155    ~mapfile(){ close(); }
156    void open(const char* file);
157    void close();
158    const char* begin(){ return _first; }
159    const char* end(){ return _last; }
160    size_t size(){ return _last - _first; }
161    bool valid(){ return (hfile != 0) && (hfile != INVALID_HANDLE_VALUE); }
162 };
163
164
165 #elif !defined(BOOST_RE_NO_STL)  // use C API to emulate the memory map:
166
167 class BOOST_RE_IX_DECL mapfile_iterator;
168
169 class BOOST_RE_IX_DECL mapfile
170 {
171    typedef char* pointer;
172    std::FILE* hfile;
173    long int _size;
174    pointer* _first;
175    pointer* _last;
176    mutable std::list<pointer*> condemed;
177    enum sizes
178    {
179       buf_size = 4096
180    };
181    void lock(pointer* node)const;
182    void unlock(pointer* node)const;
183 public:
184
185    typedef mapfile_iterator iterator;
186
187    mapfile(){ hfile = 0; _size = 0; _first = _last = 0; }
188    mapfile(const char* file){ hfile = 0; _size = 0; _first = _last = 0; open(file); }
189    ~mapfile(){ close(); }
190    void open(const char* file);
191    void close();
192    iterator begin()const;
193    iterator end()const;
194    unsigned long size()const{ return _size; }
195    bool valid()const{ return hfile != 0; }
196    friend class mapfile_iterator;
197 };
198
199 class BOOST_RE_IX_DECL mapfile_iterator : public BOOST_RE_RA_ITERATOR(char, long)
200 {
201    typedef mapfile::pointer internal_pointer;
202    internal_pointer* node;
203    const mapfile* file;
204    unsigned long offset;
205    long position()const
206    {
207       return file ? ((node - file->_first) * mapfile::buf_size + offset) : 0;
208    }
209    void position(long pos)
210    {
211       if(file)
212       {
213          node = file->_first + (pos / mapfile::buf_size);
214          offset = pos % mapfile::buf_size;
215       }
216    }
217 public:
218    typedef std::ptrdiff_t                  difference_type;
219    typedef char                            value_type;
220    typedef const char*                     pointer;
221    typedef const char&                     reference;
222    typedef std::random_access_iterator_tag iterator_category;
223
224    mapfile_iterator() { node = 0; file = 0; offset = 0; }
225    mapfile_iterator(const mapfile* f, long position)
226    {
227       file = f;
228       node = f->_first + position / mapfile::buf_size;
229       offset = position % mapfile::buf_size;
230       if(file)
231          file->lock(node);
232    }
233    mapfile_iterator(const mapfile_iterator& i)
234    {
235       file = i.file;
236       node = i.node;
237       offset = i.offset;
238       if(file)
239          file->lock(node);
240    }
241    ~mapfile_iterator()
242    {
243       if(file && node)
244          file->unlock(node);
245    }
246    mapfile_iterator& operator = (const mapfile_iterator& i);
247    char operator* ()const
248    {
249       assert(node >= file->_first);
250       assert(node < file->_last);
251       return file ? *(*node + sizeof(int) + offset) : char(0);
252    }
253    char operator[] (long off)const
254    {
255       mapfile_iterator tmp(*this);
256       tmp += off;
257       return *tmp;
258    }
259    mapfile_iterator& operator++ ();
260    mapfile_iterator operator++ (int);
261    mapfile_iterator& operator-- ();
262    mapfile_iterator operator-- (int);
263
264    mapfile_iterator& operator += (long off)
265    {
266       position(position() + off);
267       return *this;
268    }
269    mapfile_iterator& operator -= (long off)
270    {
271       position(position() - off);
272       return *this;
273    }
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 #ifndef BOOST_RE_NO_NOT_EQUAL
280    friend inline bool operator!=(const mapfile_iterator& i, const mapfile_iterator& j)
281    {
282       return !(i == j);
283    }
284 #endif
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
315 #endif
316
317 // _fi_sep determines the directory separator, either '\\' or '/'
318 BOOST_RE_IX_DECL extern const char* _fi_sep;
319
320 struct file_iterator_ref
321 {
322    _fi_find_handle hf;
323    _fi_find_data _data;
324    long count;
325 };
326
327
328 class BOOST_RE_IX_DECL file_iterator : public BOOST_RE_INPUT_ITERATOR(const char*, std::ptrdiff_t)
329 {
330    char* _root;
331    char* _path;
332    char* ptr;
333    file_iterator_ref* ref;
334
335 public:
336    file_iterator();
337    file_iterator(const char* wild);
338    ~file_iterator();
339    file_iterator(const file_iterator&);
340    file_iterator& operator=(const file_iterator&);
341    const char* root()const { return _root; }
342    const char* path()const { return _path; }
343    const char* name()const { return ptr; }
344    _fi_find_data* data() { return &(ref->_data); }
345    void next();
346    file_iterator& operator++() { next(); return *this; }
347    file_iterator operator++(int);
348    const char* operator*() { return path(); }
349
350    friend inline bool operator == (const file_iterator& f1, const file_iterator& f2)
351    {
352       return ((f1.ref->hf == _fi_invalid_handle) && (f2.ref->hf == _fi_invalid_handle));
353    }
354 #ifndef BOOST_RE_NO_NOT_EQUAL
355    friend inline bool operator != (const file_iterator& f1, const file_iterator& f2)
356    {
357       return !(f1 == f2);
358    }
359 #endif
360 };
361
362 // dwa 9/13/00 - suppress unused parameter warning
363 inline bool operator < (const file_iterator&, const file_iterator&)
364 {
365    return false;
366 }
367
368
369 class BOOST_RE_IX_DECL directory_iterator : public BOOST_RE_INPUT_ITERATOR(const char*, std::ptrdiff_t)
370 {
371    char* _root;
372    char* _path;
373    char* ptr;
374    file_iterator_ref* ref;
375
376 public:
377    directory_iterator();
378    directory_iterator(const char* wild);
379    ~directory_iterator();
380    directory_iterator(const directory_iterator& other);
381    directory_iterator& operator=(const directory_iterator& other);
382
383    const char* root()const { return _root; }
384    const char* path()const { return _path; }
385    const char* name()const { return ptr; }
386    _fi_find_data* data() { return &(ref->_data); }
387    void next();
388    directory_iterator& operator++() { next(); return *this; }
389    directory_iterator operator++(int);
390    const char* operator*() { return path(); }
391
392    static const char* separator() { return _fi_sep; }
393
394    friend inline bool operator == (const directory_iterator& f1, const directory_iterator& f2)
395    {
396       return ((f1.ref->hf == _fi_invalid_handle) && (f2.ref->hf == _fi_invalid_handle));
397    }
398
399 #ifndef BOOST_RE_NO_NOT_EQUAL
400    friend inline bool operator != (const directory_iterator& f1, const directory_iterator& f2)
401    {
402       return !(f1 == f2);
403    }
404 #endif
405 };
406
407 inline bool operator < (const directory_iterator&, const directory_iterator&)
408 {
409    return false;
410 }
411
412 #ifdef __BORLANDC__
413  #if __BORLANDC__ > 0x520
414   #pragma option pop
415  #endif
416 #endif
417
418
419 } // namespace re_detail
420 using boost::re_detail::directory_iterator;
421 using boost::re_detail::file_iterator;
422 using boost::re_detail::mapfile;
423 } // namespace boost
424
425 #endif     // _FILEITER_H
426
427
428
429
430
431
432
433
434
435
436
437
438
439