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>
27 #include <boost/assert.hpp>
29 #ifndef BOOST_REGEX_NO_FILEITER
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
39 #define BOOST_REGEX_FI_POSIX_MAP
40 #define BOOST_REGEX_FI_POSIX_DIR
43 #if defined(BOOST_REGEX_FI_WIN32_MAP)||defined(BOOST_REGEX_FI_WIN32_DIR)
47 #if defined(BOOST_REGEX_FI_WIN32_DIR)
52 typedef WIN32_FIND_DATAA _fi_find_data;
53 typedef HANDLE _fi_find_handle;
55 } // namespace re_detail
59 #define _fi_invalid_handle INVALID_HANDLE_VALUE
60 #define _fi_dir FILE_ATTRIBUTE_DIRECTORY
62 #elif defined(BOOST_REGEX_FI_POSIX_DIR)
72 #if defined(__SUNPRO_CC)
83 #ifdef BOOST_HAS_ABI_HEADERS
84 # include BOOST_ABI_PREFIX
89 unsigned dwFileAttributes;
90 char cFileName[MAX_PATH];
95 typedef _fi_priv_data* _fi_find_handle;
96 #define _fi_invalid_handle 0
99 _fi_find_handle _fi_FindFirstFile(const char* lpFileName, _fi_find_data* lpFindFileData);
100 bool _fi_FindNextFile(_fi_find_handle hFindFile, _fi_find_data* lpFindFileData);
101 bool _fi_FindClose(_fi_find_handle hFindFile);
103 #ifdef BOOST_HAS_ABI_HEADERS
104 # include BOOST_ABI_SUFFIX
107 } // namespace re_detail
120 #define FindFirstFileA _fi_FindFirstFile
121 #define FindNextFileA _fi_FindNextFile
122 #define FindClose _fi_FindClose
129 #ifdef BOOST_HAS_ABI_HEADERS
130 # include BOOST_ABI_PREFIX
133 #ifdef BOOST_REGEX_FI_WIN32_MAP // win32 mapfile
135 class BOOST_REGEX_DECL mapfile
143 typedef const char* iterator;
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);
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); }
159 class BOOST_REGEX_DECL mapfile_iterator;
161 class BOOST_REGEX_DECL mapfile
163 typedef char* pointer;
168 mutable std::list<pointer*> condemed;
173 void lock(pointer* node)const;
174 void unlock(pointer* node)const;
177 typedef mapfile_iterator iterator;
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);
184 iterator begin()const;
186 unsigned long size()const{ return _size; }
187 bool valid()const{ return hfile != 0; }
188 friend class mapfile_iterator;
191 class 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>
196 typedef mapfile::pointer internal_pointer;
197 internal_pointer* node;
199 unsigned long offset;
202 return file ? ((node - file->_first) * mapfile::buf_size + offset) : 0;
204 void position(long pos)
208 node = file->_first + (pos / mapfile::buf_size);
209 offset = pos % mapfile::buf_size;
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;
219 mapfile_iterator() { node = 0; file = 0; offset = 0; }
220 mapfile_iterator(const mapfile* f, long arg_position)
223 node = f->_first + arg_position / mapfile::buf_size;
224 offset = arg_position % mapfile::buf_size;
228 mapfile_iterator(const mapfile_iterator& i)
241 mapfile_iterator& operator = (const mapfile_iterator& i);
242 char operator* ()const
244 BOOST_ASSERT(node >= file->_first);
245 BOOST_ASSERT(node < file->_last);
246 return file ? *(*node + sizeof(int) + offset) : char(0);
248 char operator[] (long off)const
250 mapfile_iterator tmp(*this);
254 mapfile_iterator& operator++ ();
255 mapfile_iterator operator++ (int);
256 mapfile_iterator& operator-- ();
257 mapfile_iterator operator-- (int);
259 mapfile_iterator& operator += (long off)
261 position(position() + off);
264 mapfile_iterator& operator -= (long off)
266 position(position() - off);
270 friend inline bool operator==(const mapfile_iterator& i, const mapfile_iterator& j)
272 return (i.file == j.file) && (i.node == j.node) && (i.offset == j.offset);
275 friend inline bool operator!=(const mapfile_iterator& i, const mapfile_iterator& j)
280 friend inline bool operator<(const mapfile_iterator& i, const mapfile_iterator& j)
282 return i.position() < j.position();
284 friend inline bool operator>(const mapfile_iterator& i, const mapfile_iterator& j)
286 return i.position() > j.position();
288 friend inline bool operator<=(const mapfile_iterator& i, const mapfile_iterator& j)
290 return i.position() <= j.position();
292 friend inline bool operator>=(const mapfile_iterator& i, const mapfile_iterator& j)
294 return i.position() >= j.position();
297 friend mapfile_iterator operator + (const mapfile_iterator& i, long off);
298 friend mapfile_iterator operator + (long off, const mapfile_iterator& i)
300 mapfile_iterator tmp(i);
303 friend mapfile_iterator operator - (const mapfile_iterator& i, long off);
304 friend inline long operator - (const mapfile_iterator& i, const mapfile_iterator& j)
306 return i.position() - j.position();
312 // _fi_sep determines the directory separator, either '\\' or '/'
313 BOOST_REGEX_DECL extern const char* _fi_sep;
315 struct file_iterator_ref
323 class BOOST_REGEX_DECL file_iterator
328 file_iterator_ref* ref;
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;
338 file_iterator(const char* wild);
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); }
347 file_iterator& operator++() { next(); return *this; }
348 file_iterator operator++(int);
349 const char* operator*() { return path(); }
351 friend inline bool operator == (const file_iterator& f1, const file_iterator& f2)
353 return ((f1.ref->hf == _fi_invalid_handle) && (f2.ref->hf == _fi_invalid_handle));
356 friend inline bool operator != (const file_iterator& f1, const file_iterator& f2)
363 // dwa 9/13/00 - suppress unused parameter warning
364 inline bool operator < (const file_iterator&, const file_iterator&)
370 class BOOST_REGEX_DECL directory_iterator
375 file_iterator_ref* ref;
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;
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);
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); }
395 directory_iterator& operator++() { next(); return *this; }
396 directory_iterator operator++(int);
397 const char* operator*() { return path(); }
399 static const char* separator() { return _fi_sep; }
401 friend inline bool operator == (const directory_iterator& f1, const directory_iterator& f2)
403 return ((f1.ref->hf == _fi_invalid_handle) && (f2.ref->hf == _fi_invalid_handle));
407 friend inline bool operator != (const directory_iterator& f1, const directory_iterator& f2)
414 inline bool operator < (const directory_iterator&, const directory_iterator&)
419 #ifdef BOOST_HAS_ABI_HEADERS
420 # include BOOST_ABI_SUFFIX
424 } // namespace re_detail
425 using boost::re_detail::directory_iterator;
426 using boost::re_detail::file_iterator;
427 using boost::re_detail::mapfile;
430 #endif // BOOST_REGEX_NO_FILEITER
431 #endif // BOOST_RE_FILEITER_HPP