summaryrefslogtreecommitdiffstats
blob: 36e785062c97c5d62c94b42fab06918174f957a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
 *
 * Copyright (c) 2003 Dr John Maddock
 * Use, modification and distribution is subject to the
 * Boost Software License, Version 1.0. (See accompanying file
 * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
 *
 * This file implements the fileview class
 */

#include "fileview.hpp"
#include <boost/filesystem/fstream.hpp>
#include <vector>
#include <algorithm>
#include <string>
#include <fstream>
#include <istream>
#include <stdexcept>

struct fileview::implementation
{
   std::vector<char> m_data;
};


// construct:
fileview::fileview()
{
   pimpl.reset(new implementation());
}

fileview::fileview(const boost::filesystem::path& p)
{
   pimpl.reset(new implementation());
   open(p);
}

fileview::~fileview()
{
}

fileview::fileview(const fileview& )
{
}

fileview& fileview::operator=(const fileview& that)
{
   pimpl = that.pimpl;
   return *this;
}

void fileview::close()
{
   cow();
   pimpl->m_data.clear();
}

void fileview::open(const boost::filesystem::path& p)
{
   cow();
   boost::filesystem::ifstream is(p);
   if(!is)
   {
      std::string msg("Bad file name: ");
      msg += p.string();
      std::runtime_error e(msg);
      boost::throw_exception(e);
   }
   std::istreambuf_iterator<char> in(is);
   std::istreambuf_iterator<char> end;
   std::copy(in, end, std::back_inserter(pimpl->m_data));
}

// iterators:
fileview::const_iterator         fileview::begin() const
{
   return pimpl->m_data.size() ? &(pimpl->m_data[0]) : 0;
}

fileview::const_iterator         fileview::end() const
{
   return begin() + pimpl->m_data.size();
}

#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
fileview::const_reverse_iterator fileview::rbegin() const
{
   return const_reverse_iterator(end());
}

fileview::const_reverse_iterator fileview::rend() const
{
   return const_reverse_iterator(begin());
}
#endif

// capacity:
fileview::size_type fileview::size() const
{
   return pimpl->m_data.size();
}

fileview::size_type fileview::max_size() const
{
   return pimpl->m_data.max_size();
}

bool      fileview::empty() const
{
   return pimpl->m_data.empty();
}

// element access:
fileview::const_reference fileview::operator[](fileview::size_type n) const
{
   return pimpl->m_data[n];
}

fileview::const_reference fileview::at(size_type n) const
{
   return pimpl->m_data.at(n);
}

fileview::const_reference fileview::front() const
{
   return pimpl->m_data.front();
}

fileview::const_reference fileview::back() const
{
   return pimpl->m_data.back();
}

void fileview::swap(fileview& that)
{
   pimpl.swap(that.pimpl);
}

void fileview::cow()
{
   if(!pimpl.unique())
      pimpl.reset(new implementation(*pimpl));
}