3 * Copyright (c) 1998-2002
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)
13 * LOCATION: see http://www.boost.org for most recent version.
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.
21 #ifndef BOOST_RE_FILEITER_HPP_INCLUDED
22 #define BOOST_RE_FILEITER_HPP_INCLUDED
24 #ifndef BOOST_REGEX_CONFIG_HPP
25 #include <boost/regex/config.hpp>
28 #ifndef BOOST_REGEX_NO_FILEITER
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
38 #define BOOST_REGEX_FI_POSIX_MAP
39 #define BOOST_REGEX_FI_POSIX_DIR
42 #if defined(BOOST_REGEX_FI_WIN32_MAP)||defined(BOOST_REGEX_FI_WIN32_DIR)
46 #if defined(BOOST_REGEX_FI_WIN32_DIR)
51 typedef WIN32_FIND_DATAA _fi_find_data;
52 typedef HANDLE _fi_find_handle;
54 } // namespace re_detail
58 #define _fi_invalid_handle INVALID_HANDLE_VALUE
59 #define _fi_dir FILE_ATTRIBUTE_DIRECTORY
61 #elif defined(BOOST_REGEX_FI_POSIX_DIR)
70 #if defined(__SUNPRO_CC)
81 #ifdef BOOST_HAS_ABI_HEADERS
82 # include BOOST_ABI_PREFIX
87 unsigned dwFileAttributes;
88 char cFileName[MAX_PATH];
93 typedef _fi_priv_data* _fi_find_handle;
94 #define _fi_invalid_handle 0
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);
101 #ifdef BOOST_HAS_ABI_HEADERS
102 # include BOOST_ABI_SUFFIX
105 } // namespace re_detail
118 #define FindFirstFileA _fi_FindFirstFile
119 #define FindNextFileA _fi_FindNextFile
120 #define FindClose _fi_FindClose
127 #ifdef BOOST_HAS_ABI_HEADERS
128 # include BOOST_ABI_PREFIX
131 #ifdef BOOST_REGEX_FI_WIN32_MAP // win32 mapfile
133 class BOOST_REGEX_DECL mapfile
141 typedef const char* iterator;
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);
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); }
157 class BOOST_REGEX_DECL mapfile_iterator;
159 class BOOST_REGEX_DECL mapfile
161 typedef char* pointer;
166 mutable std::list<pointer*> condemed;
171 void lock(pointer* node)const;
172 void unlock(pointer* node)const;
175 typedef mapfile_iterator iterator;
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);
182 iterator begin()const;
184 unsigned long size()const{ return _size; }
185 bool valid()const{ return hfile != 0; }
186 friend class mapfile_iterator;
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>
194 typedef mapfile::pointer internal_pointer;
195 internal_pointer* node;
197 unsigned long offset;
200 return file ? ((node - file->_first) * mapfile::buf_size + offset) : 0;
202 void position(long pos)
206 node = file->_first + (pos / mapfile::buf_size);
207 offset = pos % mapfile::buf_size;
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;
217 mapfile_iterator() { node = 0; file = 0; offset = 0; }
218 mapfile_iterator(const mapfile* f, long arg_position)
221 node = f->_first + arg_position / mapfile::buf_size;
222 offset = arg_position % mapfile::buf_size;
226 mapfile_iterator(const mapfile_iterator& i)
239 mapfile_iterator& operator = (const mapfile_iterator& i);
240 char operator* ()const
242 assert(node >= file->_first);
243 assert(node < file->_last);
244 return file ? *(*node + sizeof(int) + offset) : char(0);
246 char operator[] (long off)const
248 mapfile_iterator tmp(*this);
252 mapfile_iterator& operator++ ();
253 mapfile_iterator operator++ (int);
254 mapfile_iterator& operator-- ();
255 mapfile_iterator operator-- (int);
257 mapfile_iterator& operator += (long off)
259 position(position() + off);
262 mapfile_iterator& operator -= (long off)
264 position(position() - off);
268 friend inline bool operator==(const mapfile_iterator& i, const mapfile_iterator& j)
270 return (i.file == j.file) && (i.node == j.node) && (i.offset == j.offset);
273 friend inline bool operator!=(const mapfile_iterator& i, const mapfile_iterator& j)
278 friend inline bool operator<(const mapfile_iterator& i, const mapfile_iterator& j)
280 return i.position() < j.position();
282 friend inline bool operator>(const mapfile_iterator& i, const mapfile_iterator& j)
284 return i.position() > j.position();
286 friend inline bool operator<=(const mapfile_iterator& i, const mapfile_iterator& j)
288 return i.position() <= j.position();
290 friend inline bool operator>=(const mapfile_iterator& i, const mapfile_iterator& j)
292 return i.position() >= j.position();
295 friend mapfile_iterator operator + (const mapfile_iterator& i, long off);
296 friend mapfile_iterator operator + (long off, const mapfile_iterator& i)
298 mapfile_iterator tmp(i);
301 friend mapfile_iterator operator - (const mapfile_iterator& i, long off);
302 friend inline long operator - (const mapfile_iterator& i, const mapfile_iterator& j)
304 return i.position() - j.position();
310 // _fi_sep determines the directory separator, either '\\' or '/'
311 BOOST_REGEX_DECL extern const char* _fi_sep;
313 struct file_iterator_ref
321 class BOOST_REGEX_DECL file_iterator
326 file_iterator_ref* ref;
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;
336 file_iterator(const char* wild);
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); }
345 file_iterator& operator++() { next(); return *this; }
346 file_iterator operator++(int);
347 const char* operator*() { return path(); }
349 friend inline bool operator == (const file_iterator& f1, const file_iterator& f2)
351 return ((f1.ref->hf == _fi_invalid_handle) && (f2.ref->hf == _fi_invalid_handle));
354 friend inline bool operator != (const file_iterator& f1, const file_iterator& f2)
361 // dwa 9/13/00 - suppress unused parameter warning
362 inline bool operator < (const file_iterator&, const file_iterator&)
368 class BOOST_REGEX_DECL directory_iterator
373 file_iterator_ref* ref;
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;
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);
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); }
393 directory_iterator& operator++() { next(); return *this; }
394 directory_iterator operator++(int);
395 const char* operator*() { return path(); }
397 static const char* separator() { return _fi_sep; }
399 friend inline bool operator == (const directory_iterator& f1, const directory_iterator& f2)
401 return ((f1.ref->hf == _fi_invalid_handle) && (f2.ref->hf == _fi_invalid_handle));
405 friend inline bool operator != (const directory_iterator& f1, const directory_iterator& f2)
412 inline bool operator < (const directory_iterator&, const directory_iterator&)
417 #ifdef BOOST_HAS_ABI_HEADERS
418 # include BOOST_ABI_SUFFIX
422 } // namespace re_detail
423 using boost::re_detail::directory_iterator;
424 using boost::re_detail::file_iterator;
425 using boost::re_detail::mapfile;
428 #endif // BOOST_REGEX_NO_FILEITER
429 #endif // BOOST_RE_FILEITER_HPP