sl@0: /* sl@0: * sl@0: * Copyright (c) 1998-2002 sl@0: * John Maddock sl@0: * sl@0: * Use, modification and distribution are subject to the sl@0: * Boost Software License, Version 1.0. (See accompanying file sl@0: * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) sl@0: * sl@0: */ sl@0: sl@0: /* sl@0: * LOCATION: see http://www.boost.org for most recent version. sl@0: * FILE fileiter.hpp sl@0: * VERSION see sl@0: * DESCRIPTION: Declares various platform independent file and sl@0: * directory iterators, plus binary file input in sl@0: * the form of class map_file. sl@0: */ sl@0: sl@0: #ifndef BOOST_RE_FILEITER_HPP_INCLUDED sl@0: #define BOOST_RE_FILEITER_HPP_INCLUDED sl@0: sl@0: #ifndef BOOST_REGEX_CONFIG_HPP sl@0: #include sl@0: #endif sl@0: #include sl@0: sl@0: #ifndef BOOST_REGEX_NO_FILEITER sl@0: sl@0: #if (defined(__CYGWIN__) || defined(__CYGWIN32__)) && !defined(BOOST_REGEX_NO_W32) sl@0: #error "Sorry, can't mix with STL code and gcc compiler: if you ran configure, try again with configure --disable-ms-windows" sl@0: #define BOOST_REGEX_FI_WIN32_MAP sl@0: #define BOOST_REGEX_FI_POSIX_DIR sl@0: #elif (defined(__WIN32__) || defined(_WIN32) || defined(WIN32)) && !defined(BOOST_REGEX_NO_W32) sl@0: #define BOOST_REGEX_FI_WIN32_MAP sl@0: #define BOOST_REGEX_FI_WIN32_DIR sl@0: #else sl@0: #define BOOST_REGEX_FI_POSIX_MAP sl@0: #define BOOST_REGEX_FI_POSIX_DIR sl@0: #endif sl@0: sl@0: #if defined(BOOST_REGEX_FI_WIN32_MAP)||defined(BOOST_REGEX_FI_WIN32_DIR) sl@0: #include sl@0: #endif sl@0: sl@0: #if defined(BOOST_REGEX_FI_WIN32_DIR) sl@0: sl@0: namespace boost{ sl@0: namespace re_detail{ sl@0: sl@0: typedef WIN32_FIND_DATAA _fi_find_data; sl@0: typedef HANDLE _fi_find_handle; sl@0: sl@0: } // namespace re_detail sl@0: sl@0: } // namespace boost sl@0: sl@0: #define _fi_invalid_handle INVALID_HANDLE_VALUE sl@0: #define _fi_dir FILE_ATTRIBUTE_DIRECTORY sl@0: sl@0: #elif defined(BOOST_REGEX_FI_POSIX_DIR) sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #if defined(__SUNPRO_CC) sl@0: using std::list; sl@0: #endif sl@0: sl@0: #ifndef MAX_PATH sl@0: #define MAX_PATH 256 sl@0: #endif sl@0: sl@0: namespace boost{ sl@0: namespace re_detail{ sl@0: sl@0: #ifdef BOOST_HAS_ABI_HEADERS sl@0: # include BOOST_ABI_PREFIX sl@0: #endif sl@0: sl@0: struct _fi_find_data sl@0: { sl@0: unsigned dwFileAttributes; sl@0: char cFileName[MAX_PATH]; sl@0: }; sl@0: sl@0: struct _fi_priv_data; sl@0: sl@0: typedef _fi_priv_data* _fi_find_handle; sl@0: #define _fi_invalid_handle 0 sl@0: #define _fi_dir 1 sl@0: sl@0: _fi_find_handle _fi_FindFirstFile(const char* lpFileName, _fi_find_data* lpFindFileData); sl@0: bool _fi_FindNextFile(_fi_find_handle hFindFile, _fi_find_data* lpFindFileData); sl@0: bool _fi_FindClose(_fi_find_handle hFindFile); sl@0: sl@0: #ifdef BOOST_HAS_ABI_HEADERS sl@0: # include BOOST_ABI_SUFFIX sl@0: #endif sl@0: sl@0: } // namespace re_detail sl@0: } // namespace boost sl@0: sl@0: #ifdef FindFirstFile sl@0: #undef FindFirstFile sl@0: #endif sl@0: #ifdef FindNextFile sl@0: #undef FindNextFile sl@0: #endif sl@0: #ifdef FindClose sl@0: #undef FindClose sl@0: #endif sl@0: sl@0: #define FindFirstFileA _fi_FindFirstFile sl@0: #define FindNextFileA _fi_FindNextFile sl@0: #define FindClose _fi_FindClose sl@0: sl@0: #endif sl@0: sl@0: namespace boost{ sl@0: namespace re_detail{ sl@0: sl@0: #ifdef BOOST_HAS_ABI_HEADERS sl@0: # include BOOST_ABI_PREFIX sl@0: #endif sl@0: sl@0: #ifdef BOOST_REGEX_FI_WIN32_MAP // win32 mapfile sl@0: sl@0: class BOOST_REGEX_DECL mapfile sl@0: { sl@0: HANDLE hfile; sl@0: HANDLE hmap; sl@0: const char* _first; sl@0: const char* _last; sl@0: public: sl@0: sl@0: typedef const char* iterator; sl@0: sl@0: mapfile(){ hfile = hmap = 0; _first = _last = 0; } sl@0: mapfile(const char* file){ hfile = hmap = 0; _first = _last = 0; open(file); } sl@0: ~mapfile(){ close(); } sl@0: void open(const char* file); sl@0: void close(); sl@0: const char* begin(){ return _first; } sl@0: const char* end(){ return _last; } sl@0: size_t size(){ return _last - _first; } sl@0: bool valid(){ return (hfile != 0) && (hfile != INVALID_HANDLE_VALUE); } sl@0: }; sl@0: sl@0: sl@0: #else sl@0: sl@0: class BOOST_REGEX_DECL mapfile_iterator; sl@0: sl@0: class BOOST_REGEX_DECL mapfile sl@0: { sl@0: typedef char* pointer; sl@0: std::FILE* hfile; sl@0: long int _size; sl@0: pointer* _first; sl@0: pointer* _last; sl@0: mutable std::list condemed; sl@0: enum sizes sl@0: { sl@0: buf_size = 4096 sl@0: }; sl@0: void lock(pointer* node)const; sl@0: void unlock(pointer* node)const; sl@0: public: sl@0: sl@0: typedef mapfile_iterator iterator; sl@0: sl@0: mapfile(){ hfile = 0; _size = 0; _first = _last = 0; } sl@0: mapfile(const char* file){ hfile = 0; _size = 0; _first = _last = 0; open(file); } sl@0: ~mapfile(){ close(); } sl@0: void open(const char* file); sl@0: void close(); sl@0: iterator begin()const; sl@0: iterator end()const; sl@0: unsigned long size()const{ return _size; } sl@0: bool valid()const{ return hfile != 0; } sl@0: friend class mapfile_iterator; sl@0: }; sl@0: sl@0: class BOOST_REGEX_DECL mapfile_iterator sl@0: #if !defined(BOOST_NO_STD_ITERATOR) || defined(BOOST_MSVC_STD_ITERATOR) sl@0: : public std::iterator sl@0: #endif sl@0: { sl@0: typedef mapfile::pointer internal_pointer; sl@0: internal_pointer* node; sl@0: const mapfile* file; sl@0: unsigned long offset; sl@0: long position()const sl@0: { sl@0: return file ? ((node - file->_first) * mapfile::buf_size + offset) : 0; sl@0: } sl@0: void position(long pos) sl@0: { sl@0: if(file) sl@0: { sl@0: node = file->_first + (pos / mapfile::buf_size); sl@0: offset = pos % mapfile::buf_size; sl@0: } sl@0: } sl@0: public: sl@0: typedef std::ptrdiff_t difference_type; sl@0: typedef char value_type; sl@0: typedef const char* pointer; sl@0: typedef const char& reference; sl@0: typedef std::random_access_iterator_tag iterator_category; sl@0: sl@0: mapfile_iterator() { node = 0; file = 0; offset = 0; } sl@0: mapfile_iterator(const mapfile* f, long arg_position) sl@0: { sl@0: file = f; sl@0: node = f->_first + arg_position / mapfile::buf_size; sl@0: offset = arg_position % mapfile::buf_size; sl@0: if(file) sl@0: file->lock(node); sl@0: } sl@0: mapfile_iterator(const mapfile_iterator& i) sl@0: { sl@0: file = i.file; sl@0: node = i.node; sl@0: offset = i.offset; sl@0: if(file) sl@0: file->lock(node); sl@0: } sl@0: ~mapfile_iterator() sl@0: { sl@0: if(file && node) sl@0: file->unlock(node); sl@0: } sl@0: mapfile_iterator& operator = (const mapfile_iterator& i); sl@0: char operator* ()const sl@0: { sl@0: BOOST_ASSERT(node >= file->_first); sl@0: BOOST_ASSERT(node < file->_last); sl@0: return file ? *(*node + sizeof(int) + offset) : char(0); sl@0: } sl@0: char operator[] (long off)const sl@0: { sl@0: mapfile_iterator tmp(*this); sl@0: tmp += off; sl@0: return *tmp; sl@0: } sl@0: mapfile_iterator& operator++ (); sl@0: mapfile_iterator operator++ (int); sl@0: mapfile_iterator& operator-- (); sl@0: mapfile_iterator operator-- (int); sl@0: sl@0: mapfile_iterator& operator += (long off) sl@0: { sl@0: position(position() + off); sl@0: return *this; sl@0: } sl@0: mapfile_iterator& operator -= (long off) sl@0: { sl@0: position(position() - off); sl@0: return *this; sl@0: } sl@0: sl@0: friend inline bool operator==(const mapfile_iterator& i, const mapfile_iterator& j) sl@0: { sl@0: return (i.file == j.file) && (i.node == j.node) && (i.offset == j.offset); sl@0: } sl@0: sl@0: friend inline bool operator!=(const mapfile_iterator& i, const mapfile_iterator& j) sl@0: { sl@0: return !(i == j); sl@0: } sl@0: sl@0: friend inline bool operator<(const mapfile_iterator& i, const mapfile_iterator& j) sl@0: { sl@0: return i.position() < j.position(); sl@0: } sl@0: friend inline bool operator>(const mapfile_iterator& i, const mapfile_iterator& j) sl@0: { sl@0: return i.position() > j.position(); sl@0: } sl@0: friend inline bool operator<=(const mapfile_iterator& i, const mapfile_iterator& j) sl@0: { sl@0: return i.position() <= j.position(); sl@0: } sl@0: friend inline bool operator>=(const mapfile_iterator& i, const mapfile_iterator& j) sl@0: { sl@0: return i.position() >= j.position(); sl@0: } sl@0: sl@0: friend mapfile_iterator operator + (const mapfile_iterator& i, long off); sl@0: friend mapfile_iterator operator + (long off, const mapfile_iterator& i) sl@0: { sl@0: mapfile_iterator tmp(i); sl@0: return tmp += off; sl@0: } sl@0: friend mapfile_iterator operator - (const mapfile_iterator& i, long off); sl@0: friend inline long operator - (const mapfile_iterator& i, const mapfile_iterator& j) sl@0: { sl@0: return i.position() - j.position(); sl@0: } sl@0: }; sl@0: sl@0: #endif sl@0: sl@0: // _fi_sep determines the directory separator, either '\\' or '/' sl@0: BOOST_REGEX_DECL extern const char* _fi_sep; sl@0: sl@0: struct file_iterator_ref sl@0: { sl@0: _fi_find_handle hf; sl@0: _fi_find_data _data; sl@0: long count; sl@0: }; sl@0: sl@0: sl@0: class BOOST_REGEX_DECL file_iterator sl@0: { sl@0: char* _root; sl@0: char* _path; sl@0: char* ptr; sl@0: file_iterator_ref* ref; sl@0: sl@0: public: sl@0: typedef std::ptrdiff_t difference_type; sl@0: typedef const char* value_type; sl@0: typedef const char** pointer; sl@0: typedef const char*& reference; sl@0: typedef std::input_iterator_tag iterator_category; sl@0: sl@0: file_iterator(); sl@0: file_iterator(const char* wild); sl@0: ~file_iterator(); sl@0: file_iterator(const file_iterator&); sl@0: file_iterator& operator=(const file_iterator&); sl@0: const char* root()const { return _root; } sl@0: const char* path()const { return _path; } sl@0: const char* name()const { return ptr; } sl@0: _fi_find_data* data() { return &(ref->_data); } sl@0: void next(); sl@0: file_iterator& operator++() { next(); return *this; } sl@0: file_iterator operator++(int); sl@0: const char* operator*() { return path(); } sl@0: sl@0: friend inline bool operator == (const file_iterator& f1, const file_iterator& f2) sl@0: { sl@0: return ((f1.ref->hf == _fi_invalid_handle) && (f2.ref->hf == _fi_invalid_handle)); sl@0: } sl@0: sl@0: friend inline bool operator != (const file_iterator& f1, const file_iterator& f2) sl@0: { sl@0: return !(f1 == f2); sl@0: } sl@0: sl@0: }; sl@0: sl@0: // dwa 9/13/00 - suppress unused parameter warning sl@0: inline bool operator < (const file_iterator&, const file_iterator&) sl@0: { sl@0: return false; sl@0: } sl@0: sl@0: sl@0: class BOOST_REGEX_DECL directory_iterator sl@0: { sl@0: char* _root; sl@0: char* _path; sl@0: char* ptr; sl@0: file_iterator_ref* ref; sl@0: sl@0: public: sl@0: typedef std::ptrdiff_t difference_type; sl@0: typedef const char* value_type; sl@0: typedef const char** pointer; sl@0: typedef const char*& reference; sl@0: typedef std::input_iterator_tag iterator_category; sl@0: sl@0: directory_iterator(); sl@0: directory_iterator(const char* wild); sl@0: ~directory_iterator(); sl@0: directory_iterator(const directory_iterator& other); sl@0: directory_iterator& operator=(const directory_iterator& other); sl@0: sl@0: const char* root()const { return _root; } sl@0: const char* path()const { return _path; } sl@0: const char* name()const { return ptr; } sl@0: _fi_find_data* data() { return &(ref->_data); } sl@0: void next(); sl@0: directory_iterator& operator++() { next(); return *this; } sl@0: directory_iterator operator++(int); sl@0: const char* operator*() { return path(); } sl@0: sl@0: static const char* separator() { return _fi_sep; } sl@0: sl@0: friend inline bool operator == (const directory_iterator& f1, const directory_iterator& f2) sl@0: { sl@0: return ((f1.ref->hf == _fi_invalid_handle) && (f2.ref->hf == _fi_invalid_handle)); sl@0: } sl@0: sl@0: sl@0: friend inline bool operator != (const directory_iterator& f1, const directory_iterator& f2) sl@0: { sl@0: return !(f1 == f2); sl@0: } sl@0: sl@0: }; sl@0: sl@0: inline bool operator < (const directory_iterator&, const directory_iterator&) sl@0: { sl@0: return false; sl@0: } sl@0: sl@0: #ifdef BOOST_HAS_ABI_HEADERS sl@0: # include BOOST_ABI_SUFFIX sl@0: #endif sl@0: sl@0: sl@0: } // namespace re_detail sl@0: using boost::re_detail::directory_iterator; sl@0: using boost::re_detail::file_iterator; sl@0: using boost::re_detail::mapfile; sl@0: } // namespace boost sl@0: sl@0: #endif // BOOST_REGEX_NO_FILEITER sl@0: #endif // BOOST_RE_FILEITER_HPP sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: