[29] | 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 <vector> |
---|
| 13 | #include <algorithm> |
---|
| 14 | #include <string> |
---|
| 15 | #include <fstream> |
---|
| 16 | #include <istream> |
---|
| 17 | #include <stdexcept> |
---|
| 18 | |
---|
| 19 | struct fileview::implementation |
---|
| 20 | { |
---|
| 21 | std::vector<char> m_data; |
---|
| 22 | }; |
---|
| 23 | |
---|
| 24 | |
---|
| 25 | // construct: |
---|
| 26 | fileview::fileview() |
---|
| 27 | { |
---|
| 28 | pimpl.reset(new implementation()); |
---|
| 29 | } |
---|
| 30 | |
---|
| 31 | fileview::fileview(const boost::filesystem::path& p) |
---|
| 32 | { |
---|
| 33 | pimpl.reset(new implementation()); |
---|
| 34 | open(p); |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | fileview::~fileview() |
---|
| 38 | { |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | fileview::fileview(const fileview& that) |
---|
| 42 | { |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | fileview& fileview::operator=(const fileview& that) |
---|
| 46 | { |
---|
| 47 | pimpl = that.pimpl; |
---|
| 48 | return *this; |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | void fileview::close() |
---|
| 52 | { |
---|
| 53 | cow(); |
---|
| 54 | pimpl->m_data.clear(); |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | void fileview::open(const boost::filesystem::path& p) |
---|
| 58 | { |
---|
| 59 | cow(); |
---|
| 60 | std::ifstream is(p.native_file_string().c_str()); |
---|
| 61 | if(!is) |
---|
| 62 | { |
---|
| 63 | std::string msg("Bad file name: "); |
---|
| 64 | msg += p.native_file_string(); |
---|
| 65 | std::runtime_error e(msg); |
---|
| 66 | boost::throw_exception(e); |
---|
| 67 | } |
---|
| 68 | std::istreambuf_iterator<char> in(is); |
---|
| 69 | std::istreambuf_iterator<char> end; |
---|
| 70 | std::copy(in, end, std::back_inserter(pimpl->m_data)); |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | // iterators: |
---|
| 74 | fileview::const_iterator fileview::begin() const |
---|
| 75 | { |
---|
| 76 | return &(pimpl->m_data[0]); |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | fileview::const_iterator fileview::end() const |
---|
| 80 | { |
---|
| 81 | return begin() + pimpl->m_data.size(); |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION |
---|
| 85 | fileview::const_reverse_iterator fileview::rbegin() const |
---|
| 86 | { |
---|
| 87 | return const_reverse_iterator(end()); |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | fileview::const_reverse_iterator fileview::rend() const |
---|
| 91 | { |
---|
| 92 | return const_reverse_iterator(begin()); |
---|
| 93 | } |
---|
| 94 | #endif |
---|
| 95 | |
---|
| 96 | // capacity: |
---|
| 97 | fileview::size_type fileview::size() const |
---|
| 98 | { |
---|
| 99 | return pimpl->m_data.size(); |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | fileview::size_type fileview::max_size() const |
---|
| 103 | { |
---|
| 104 | return pimpl->m_data.max_size(); |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | bool fileview::empty() const |
---|
| 108 | { |
---|
| 109 | return pimpl->m_data.empty(); |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | // element access: |
---|
| 113 | fileview::const_reference fileview::operator[](fileview::size_type n) const |
---|
| 114 | { |
---|
| 115 | return pimpl->m_data[n]; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | fileview::const_reference fileview::at(size_type n) const |
---|
| 119 | { |
---|
| 120 | return pimpl->m_data.at(n); |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | fileview::const_reference fileview::front() const |
---|
| 124 | { |
---|
| 125 | return pimpl->m_data.front(); |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | fileview::const_reference fileview::back() const |
---|
| 129 | { |
---|
| 130 | return pimpl->m_data.back(); |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | void fileview::swap(fileview& that) |
---|
| 134 | { |
---|
| 135 | pimpl.swap(that.pimpl); |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | void fileview::cow() |
---|
| 139 | { |
---|
| 140 | if(!pimpl.unique()) |
---|
| 141 | pimpl.reset(new implementation(*pimpl)); |
---|
| 142 | } |
---|