Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/tools/bcp/fileview.cpp @ 30

Last change on this file since 30 was 29, checked in by landauf, 17 years ago

updated boost from 1_33_1 to 1_34_1

File size: 2.6 KB
Line 
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
19struct fileview::implementation
20{
21   std::vector<char> m_data;
22};
23
24
25// construct:
26fileview::fileview()
27{
28   pimpl.reset(new implementation());
29}
30
31fileview::fileview(const boost::filesystem::path& p)
32{
33   pimpl.reset(new implementation());
34   open(p);
35}
36
37fileview::~fileview()
38{
39}
40
41fileview::fileview(const fileview& that)
42{
43}
44
45fileview& fileview::operator=(const fileview& that)
46{
47   pimpl = that.pimpl;
48   return *this;
49}
50
51void fileview::close()
52{
53   cow();
54   pimpl->m_data.clear();
55}
56
57void 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:
74fileview::const_iterator         fileview::begin() const
75{
76   return &(pimpl->m_data[0]);
77}
78
79fileview::const_iterator         fileview::end() const
80{
81   return begin() + pimpl->m_data.size();
82}
83
84#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
85fileview::const_reverse_iterator fileview::rbegin() const
86{
87   return const_reverse_iterator(end());
88}
89
90fileview::const_reverse_iterator fileview::rend() const
91{
92   return const_reverse_iterator(begin());
93}
94#endif
95
96// capacity:
97fileview::size_type fileview::size() const
98{
99   return pimpl->m_data.size();
100}
101
102fileview::size_type fileview::max_size() const
103{
104   return pimpl->m_data.max_size();
105}
106
107bool      fileview::empty() const
108{
109   return pimpl->m_data.empty();
110}
111
112// element access:
113fileview::const_reference fileview::operator[](fileview::size_type n) const
114{
115   return pimpl->m_data[n];
116}
117
118fileview::const_reference fileview::at(size_type n) const
119{
120   return pimpl->m_data.at(n);
121}
122
123fileview::const_reference fileview::front() const
124{
125   return pimpl->m_data.front();
126}
127
128fileview::const_reference fileview::back() const
129{
130   return pimpl->m_data.back();
131}
132
133void fileview::swap(fileview& that)
134{
135   pimpl.swap(that.pimpl);
136}
137
138void fileview::cow()
139{
140   if(!pimpl.unique())
141      pimpl.reset(new implementation(*pimpl));
142}
Note: See TracBrowser for help on using the repository browser.