source: NonGTP/Boost/boost/regex/v4/fileiter.hpp @ 857

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