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 namespace BOOST_REGEX_DETAIL_NS{
54 #ifndef BOOST_NO_ANSI_APIS
55 typedef WIN32_FIND_DATAA _fi_find_data;
57 typedef WIN32_FIND_DATAW _fi_find_data;
59 typedef HANDLE _fi_find_handle;
61 } // namespace BOOST_REGEX_DETAIL_NS
65 #define _fi_invalid_handle INVALID_HANDLE_VALUE
66 #define _fi_dir FILE_ATTRIBUTE_DIRECTORY
68 #elif defined(BOOST_REGEX_FI_POSIX_DIR)
78 #if defined(__SUNPRO_CC)
87 namespace BOOST_REGEX_DETAIL_NS{
89 #ifdef BOOST_HAS_ABI_HEADERS
90 # include BOOST_ABI_PREFIX
95 unsigned dwFileAttributes;
96 char cFileName[MAX_PATH];
101 typedef _fi_priv_data* _fi_find_handle;
102 #define _fi_invalid_handle 0
105 _fi_find_handle _fi_FindFirstFile(const char* lpFileName, _fi_find_data* lpFindFileData);
106 bool _fi_FindNextFile(_fi_find_handle hFindFile, _fi_find_data* lpFindFileData);
107 bool _fi_FindClose(_fi_find_handle hFindFile);
109 #ifdef BOOST_HAS_ABI_HEADERS
110 # include BOOST_ABI_SUFFIX
113 } // namespace BOOST_REGEX_DETAIL_NS
126 #define FindFirstFileA _fi_FindFirstFile
127 #define FindNextFileA _fi_FindNextFile
128 #define FindClose _fi_FindClose
133 namespace BOOST_REGEX_DETAIL_NS{
135 #ifdef BOOST_HAS_ABI_HEADERS
136 # include BOOST_ABI_PREFIX
139 #ifdef BOOST_REGEX_FI_WIN32_MAP // win32 mapfile
141 class BOOST_REGEX_DECL mapfile
149 typedef const char* iterator;
151 mapfile(){ hfile = hmap = 0; _first = _last = 0; }
152 mapfile(const char* file){ hfile = hmap = 0; _first = _last = 0; open(file); }
153 ~mapfile(){ close(); }
154 void open(const char* file);
156 const char* begin(){ return _first; }
157 const char* end(){ return _last; }
158 size_t size(){ return _last - _first; }
159 bool valid(){ return (hfile != 0) && (hfile != INVALID_HANDLE_VALUE); }
165 class BOOST_REGEX_DECL mapfile_iterator;
167 class BOOST_REGEX_DECL mapfile
169 typedef char* pointer;
174 mutable std::list<pointer*> condemed;
179 void lock(pointer* node)const;
180 void unlock(pointer* node)const;
183 typedef mapfile_iterator iterator;
185 mapfile(){ hfile = 0; _size = 0; _first = _last = 0; }
186 mapfile(const char* file){ hfile = 0; _size = 0; _first = _last = 0; open(file); }
187 ~mapfile(){ close(); }
188 void open(const char* file);
190 iterator begin()const;
192 unsigned long size()const{ return _size; }
193 bool valid()const{ return hfile != 0; }
194 friend class mapfile_iterator;
197 class BOOST_REGEX_DECL mapfile_iterator
199 typedef mapfile::pointer internal_pointer;
200 internal_pointer* node;
202 unsigned long offset;
205 return file ? ((node - file->_first) * mapfile::buf_size + offset) : 0;
207 void position(long pos)
211 node = file->_first + (pos / mapfile::buf_size);
212 offset = pos % mapfile::buf_size;
216 typedef std::ptrdiff_t difference_type;
217 typedef char value_type;
218 typedef const char* pointer;
219 typedef const char& reference;
220 typedef std::random_access_iterator_tag iterator_category;
222 mapfile_iterator() { node = 0; file = 0; offset = 0; }
223 mapfile_iterator(const mapfile* f, long arg_position)
227 node = f->_first + arg_position / mapfile::buf_size;
228 offset = arg_position % mapfile::buf_size;
231 mapfile_iterator(const mapfile_iterator& i)
244 mapfile_iterator& operator = (const mapfile_iterator& i);
245 char operator* ()const
247 BOOST_ASSERT(node >= file->_first);
248 BOOST_ASSERT(node < file->_last);
249 return file ? *(*node + sizeof(int) + offset) : char(0);
251 char operator[] (long off)const
253 mapfile_iterator tmp(*this);
257 mapfile_iterator& operator++ ();
258 mapfile_iterator operator++ (int);
259 mapfile_iterator& operator-- ();
260 mapfile_iterator operator-- (int);
262 mapfile_iterator& operator += (long off)
264 position(position() + off);
267 mapfile_iterator& operator -= (long off)
269 position(position() - off);
273 friend inline bool operator==(const mapfile_iterator& i, const mapfile_iterator& j)
275 return (i.file == j.file) && (i.node == j.node) && (i.offset == j.offset);
278 friend inline bool operator!=(const mapfile_iterator& i, const mapfile_iterator& j)
283 friend inline bool operator<(const mapfile_iterator& i, const mapfile_iterator& j)
285 return i.position() < j.position();
287 friend inline bool operator>(const mapfile_iterator& i, const mapfile_iterator& j)
289 return i.position() > j.position();
291 friend inline bool operator<=(const mapfile_iterator& i, const mapfile_iterator& j)
293 return i.position() <= j.position();
295 friend inline bool operator>=(const mapfile_iterator& i, const mapfile_iterator& j)
297 return i.position() >= j.position();
300 friend mapfile_iterator operator + (const mapfile_iterator& i, long off);
301 friend mapfile_iterator operator + (long off, const mapfile_iterator& i)
303 mapfile_iterator tmp(i);
306 friend mapfile_iterator operator - (const mapfile_iterator& i, long off);
307 friend inline long operator - (const mapfile_iterator& i, const mapfile_iterator& j)
309 return i.position() - j.position();
315 // _fi_sep determines the directory separator, either '\\' or '/'
316 BOOST_REGEX_DECL extern const char* _fi_sep;
318 struct file_iterator_ref
326 class BOOST_REGEX_DECL file_iterator
331 file_iterator_ref* ref;
334 typedef std::ptrdiff_t difference_type;
335 typedef const char* value_type;
336 typedef const char** pointer;
337 typedef const char*& reference;
338 typedef std::input_iterator_tag iterator_category;
341 file_iterator(const char* wild);
343 file_iterator(const file_iterator&);
344 file_iterator& operator=(const file_iterator&);
345 const char* root()const { return _root; }
346 const char* path()const { return _path; }
347 const char* name()const { return ptr; }
348 _fi_find_data* data() { return &(ref->_data); }
350 file_iterator& operator++() { next(); return *this; }
351 file_iterator operator++(int);
352 const char* operator*() { return path(); }
354 friend inline bool operator == (const file_iterator& f1, const file_iterator& f2)
356 return ((f1.ref->hf == _fi_invalid_handle) && (f2.ref->hf == _fi_invalid_handle));
359 friend inline bool operator != (const file_iterator& f1, const file_iterator& f2)
366 // dwa 9/13/00 - suppress unused parameter warning
367 inline bool operator < (const file_iterator&, const file_iterator&)
373 class BOOST_REGEX_DECL directory_iterator
378 file_iterator_ref* ref;
381 typedef std::ptrdiff_t difference_type;
382 typedef const char* value_type;
383 typedef const char** pointer;
384 typedef const char*& reference;
385 typedef std::input_iterator_tag iterator_category;
387 directory_iterator();
388 directory_iterator(const char* wild);
389 ~directory_iterator();
390 directory_iterator(const directory_iterator& other);
391 directory_iterator& operator=(const directory_iterator& other);
393 const char* root()const { return _root; }
394 const char* path()const { return _path; }
395 const char* name()const { return ptr; }
396 _fi_find_data* data() { return &(ref->_data); }
398 directory_iterator& operator++() { next(); return *this; }
399 directory_iterator operator++(int);
400 const char* operator*() { return path(); }
402 static const char* separator() { return _fi_sep; }
404 friend inline bool operator == (const directory_iterator& f1, const directory_iterator& f2)
406 return ((f1.ref->hf == _fi_invalid_handle) && (f2.ref->hf == _fi_invalid_handle));
410 friend inline bool operator != (const directory_iterator& f1, const directory_iterator& f2)
417 inline bool operator < (const directory_iterator&, const directory_iterator&)
422 #ifdef BOOST_HAS_ABI_HEADERS
423 # include BOOST_ABI_SUFFIX
427 } // namespace BOOST_REGEX_DETAIL_NS
428 using boost::BOOST_REGEX_DETAIL_NS::directory_iterator;
429 using boost::BOOST_REGEX_DETAIL_NS::file_iterator;
430 using boost::BOOST_REGEX_DETAIL_NS::mapfile;
433 #endif // BOOST_REGEX_NO_FILEITER
434 #endif // BOOST_RE_FILEITER_HPP