| 1 | //  cvs_iterator  ------------------------------------------------------------// | 
|---|
| 2 |  | 
|---|
| 3 | //  Copyright Beman Dawes 2003. | 
|---|
| 4 | // | 
|---|
| 5 | //  Distributed under the Boost Software License, Version 1.0. | 
|---|
| 6 | //  (See accompanying file LICENSE_1_0.txt or copy at | 
|---|
| 7 | //  http://www.boost.org/LICENSE_1_0.txt) | 
|---|
| 8 |  | 
|---|
| 9 | //  WARNING: This is just a quick hack. It doesn't conform to C++ Standard | 
|---|
| 10 | //  Library iterator requirements. | 
|---|
| 11 |  | 
|---|
| 12 | #ifndef BOOST_CVS_ITERATOR_HPP | 
|---|
| 13 | #define BOOST_CVS_ITERATOR_HPP | 
|---|
| 14 |  | 
|---|
| 15 | #include <string> | 
|---|
| 16 | #include <assert.h> | 
|---|
| 17 |  | 
|---|
| 18 | #include "boost/filesystem/path.hpp" | 
|---|
| 19 | #include "boost/filesystem/operations.hpp" | 
|---|
| 20 | #include "boost/filesystem/fstream.hpp" | 
|---|
| 21 | #include "boost/noncopyable.hpp" | 
|---|
| 22 |  | 
|---|
| 23 | namespace hack | 
|---|
| 24 | { | 
|---|
| 25 |   class cvs_iterator : boost::noncopyable | 
|---|
| 26 |   { | 
|---|
| 27 |     boost::filesystem::ifstream  entries_file; | 
|---|
| 28 |     boost::filesystem::path      dir_path; | 
|---|
| 29 |     boost::filesystem::path      value_path; | 
|---|
| 30 |   public: | 
|---|
| 31 |  | 
|---|
| 32 |     cvs_iterator(){} // end iterator | 
|---|
| 33 |  | 
|---|
| 34 |     ~cvs_iterator() { if ( !!entries_file ) entries_file.close(); } | 
|---|
| 35 |  | 
|---|
| 36 |     cvs_iterator( const boost::filesystem::path & dir_path ) : dir_path(dir_path) | 
|---|
| 37 |     { | 
|---|
| 38 |       boost::filesystem::path entries_file_path( dir_path / "CVS/Entries" ); | 
|---|
| 39 |       entries_file.open( entries_file_path ); | 
|---|
| 40 |       if ( !entries_file ) | 
|---|
| 41 |         throw std::string( "could not open: " ) + entries_file_path.string(); | 
|---|
| 42 |       ++*this; | 
|---|
| 43 |     } | 
|---|
| 44 |  | 
|---|
| 45 |     const boost::filesystem::path & operator*() const { return value_path; } | 
|---|
| 46 |  | 
|---|
| 47 |     cvs_iterator & operator++() | 
|---|
| 48 |     { | 
|---|
| 49 |       assert( !!entries_file ); | 
|---|
| 50 |       std::string contents; | 
|---|
| 51 |       do | 
|---|
| 52 |       { | 
|---|
| 53 |         do | 
|---|
| 54 |         { | 
|---|
| 55 |           std::getline( entries_file, contents ); | 
|---|
| 56 |           if ( entries_file.eof() ) | 
|---|
| 57 |           { | 
|---|
| 58 |             entries_file.close(); | 
|---|
| 59 |             value_path = ""; | 
|---|
| 60 |             return *this; | 
|---|
| 61 |           } | 
|---|
| 62 |         } while ( contents == "D" ); | 
|---|
| 63 |         if ( contents[0] == 'D' ) contents.erase( 0, 1 ); | 
|---|
| 64 |         value_path = dir_path | 
|---|
| 65 |           / boost::filesystem::path( contents.substr( 1, contents.find( '/', 1 ) ), boost::filesystem::no_check ); | 
|---|
| 66 |  | 
|---|
| 67 |       // in case entries file is mistaken, do until value_path actually found | 
|---|
| 68 |       } while ( !boost::filesystem::exists( value_path ) ); | 
|---|
| 69 |       return *this; | 
|---|
| 70 |     } | 
|---|
| 71 |  | 
|---|
| 72 |     bool operator==( const cvs_iterator & rhs ) | 
|---|
| 73 |       { return value_path.string() == rhs.value_path.string(); } | 
|---|
| 74 |  | 
|---|
| 75 |     bool operator!=( const cvs_iterator & rhs ) | 
|---|
| 76 |       { return value_path.string() != rhs.value_path.string(); } | 
|---|
| 77 |  | 
|---|
| 78 |   }; | 
|---|
| 79 | } | 
|---|
| 80 |  | 
|---|
| 81 | #endif // include guard | 
|---|