1 /* 2 * 3 * Copyright (c) 2003 Dr John Maddock 4 * Use, modification and distribution is subject to the 5 * Boost Software License, Version 1.0. (See accompanying file 6 * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 7 * 8 * This file implements the fileview class 9 */ 10 11 #include "fileview.hpp" 12 #include <boost/filesystem/fstream.hpp> 13 #include <vector> 14 #include <algorithm> 15 #include <string> 16 #include <fstream> 17 #include <istream> 18 #include <stdexcept> 19 20 struct fileview::implementation 21 { 22 std::vector<char> m_data; 23 }; 24 25 26 // construct: fileview()27fileview::fileview() 28 { 29 pimpl.reset(new implementation()); 30 } 31 fileview(const boost::filesystem::path & p)32fileview::fileview(const boost::filesystem::path& p) 33 { 34 pimpl.reset(new implementation()); 35 open(p); 36 } 37 ~fileview()38fileview::~fileview() 39 { 40 } 41 fileview(const fileview &)42fileview::fileview(const fileview& ) 43 { 44 } 45 operator =(const fileview & that)46fileview& fileview::operator=(const fileview& that) 47 { 48 pimpl = that.pimpl; 49 return *this; 50 } 51 close()52void fileview::close() 53 { 54 cow(); 55 pimpl->m_data.clear(); 56 } 57 open(const boost::filesystem::path & p)58void fileview::open(const boost::filesystem::path& p) 59 { 60 cow(); 61 boost::filesystem::ifstream is(p); 62 if(!is) 63 { 64 std::string msg("Bad file name: "); 65 msg += p.string(); 66 std::runtime_error e(msg); 67 boost::throw_exception(e); 68 } 69 std::istreambuf_iterator<char> in(is); 70 std::istreambuf_iterator<char> end; 71 std::copy(in, end, std::back_inserter(pimpl->m_data)); 72 } 73 74 // iterators: begin() const75fileview::const_iterator fileview::begin() const 76 { 77 return pimpl->m_data.size() ? &(pimpl->m_data[0]) : 0; 78 } 79 end() const80fileview::const_iterator fileview::end() const 81 { 82 return begin() + pimpl->m_data.size(); 83 } 84 85 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION rbegin() const86fileview::const_reverse_iterator fileview::rbegin() const 87 { 88 return const_reverse_iterator(end()); 89 } 90 rend() const91fileview::const_reverse_iterator fileview::rend() const 92 { 93 return const_reverse_iterator(begin()); 94 } 95 #endif 96 97 // capacity: size() const98fileview::size_type fileview::size() const 99 { 100 return pimpl->m_data.size(); 101 } 102 max_size() const103fileview::size_type fileview::max_size() const 104 { 105 return pimpl->m_data.max_size(); 106 } 107 empty() const108bool fileview::empty() const 109 { 110 return pimpl->m_data.empty(); 111 } 112 113 // element access: operator [](fileview::size_type n) const114fileview::const_reference fileview::operator[](fileview::size_type n) const 115 { 116 return pimpl->m_data[n]; 117 } 118 at(size_type n) const119fileview::const_reference fileview::at(size_type n) const 120 { 121 return pimpl->m_data.at(n); 122 } 123 front() const124fileview::const_reference fileview::front() const 125 { 126 return pimpl->m_data.front(); 127 } 128 back() const129fileview::const_reference fileview::back() const 130 { 131 return pimpl->m_data.back(); 132 } 133 swap(fileview & that)134void fileview::swap(fileview& that) 135 { 136 pimpl.swap(that.pimpl); 137 } 138 cow()139void fileview::cow() 140 { 141 if(!pimpl.unique()) 142 pimpl.reset(new implementation(*pimpl)); 143 } 144