/usr/include/lexertl/file_shared_iterator.hpp is in libpuma-dev 1:1.1+svn20120529-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 | // file_shared_iterator.hpp
// Copyright (c) 2010-2011 Ben Hanson (http://www.benhanson.net/)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file licence_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef LEXERTL_FILE_SHARED_ITERATOR_H
#define LEXERTL_FILE_SHARED_ITERATOR_H
#include <algorithm>
// memcpy
#include <cstring>
#include <iostream>
#include <list>
#include <math.h>
#include "runtime_error.hpp"
#include "size_t.hpp"
#include <vector>
namespace lexertl
{
template<typename char_type>
class basic_file_shared_iterator
{
public:
typedef std::basic_ifstream<char_type> istream;
typedef std::forward_iterator_tag iterator_category;
typedef std::size_t difference_type;
typedef char_type value_type;
typedef char_type *pointer;
typedef char_type &reference;
basic_file_shared_iterator () :
_master (false),
_live (false),
_index (shared::npos ()),
_shared (0)
{
}
basic_file_shared_iterator (istream &stream_,
const std::size_t buff_size_ = 1024,
const std::size_t increment_ = 1024) :
_master (true),
_live (false),
_index (shared::npos ()),
// For exception safety don't call new yet
_shared (0)
{
// Safe to call potentially throwing new now.
_shared = new shared (stream_, buff_size_, increment_);
++_shared->_ref_count;
// New master iterator (don't store in shared until first usage to
// prevent an ever increasing buffer size)
}
basic_file_shared_iterator (const basic_file_shared_iterator &rhs_) :
_master (false),
_live (false),
_index (rhs_._master ? rhs_._shared->lowest () : rhs_._index),
_shared (rhs_._shared)
{
if (_shared)
{
// New copy of an iterator.
// The assumption is that any copy must be live
// even if the rhs is not (otherwise we will never
// have a record of the start of the current range!)
++_shared->_ref_count;
_shared->push (this);
_live = true;
}
}
~basic_file_shared_iterator ()
{
if (_shared)
{
--_shared->_ref_count;
_shared->erase (this);
if (_shared->_ref_count == 0)
{
delete _shared;
_shared = 0;
}
}
}
basic_file_shared_iterator &operator = (basic_file_shared_iterator &rhs_)
{
if (this != &rhs_)
{
_master = false;
_index = rhs_._master ? rhs_._shared->lowest () : rhs_._index;
if (_live && !rhs_._live)
{
_shared->erase (this);
if (!rhs_._shared)
{
--_shared->_ref_count;
}
}
else if (!_live && rhs_._live)
{
rhs_._shared->push (this);
if (!_shared)
{
++rhs_._shared->_ref_count;
}
}
_live = rhs_._live;
_shared = rhs_._shared;
}
return *this;
}
bool operator == (const basic_file_shared_iterator &rhs_) const
{
return _index == rhs_._index &&
(_shared == rhs_._shared ||
(_index == shared::npos () || rhs_._index == shared::npos ()) &&
(!_shared || !rhs_._shared));
}
bool operator != (const basic_file_shared_iterator &rhs_) const
{
return !(*this == rhs_);
}
const char_type &operator * ()
{
check_master ();
return _shared->_buffer[_index];
}
basic_file_shared_iterator &operator ++ ()
{
check_master ();
++_index;
update_state ();
return *this;
}
basic_file_shared_iterator operator ++ (int)
{
basic_file_shared_iterator iter_ = *this;
check_master ();
++_index;
update_state ();
return iter_;
}
private:
class shared
{
public:
std::size_t _ref_count;
typedef std::vector<char_type> char_vector;
typedef std::list<basic_file_shared_iterator *> iter_list;
istream &_stream;
std::size_t _increment;
std::size_t _len;
char_vector _buffer;
iter_list _clients;
shared (istream &stream_, const std::size_t buff_size_,
const std::size_t increment_) :
_ref_count (0),
_increment (increment_),
_stream (stream_)
{
_buffer.resize (buff_size_);
_stream.read (&_buffer.front (), _buffer.size ());
_len = static_cast<std::size_t>(_stream.gcount ());
}
bool reload_buffer ()
{
const std::size_t lowest_ = lowest ();
std::size_t read_ = 0;
if (lowest_ == 0)
{
// Resize buffer
const std::size_t old_size_ = _buffer.size ();
const std::size_t new_size_ = old_size_ + _increment;
_buffer.resize (new_size_);
_stream.read (&_buffer.front () + old_size_, _increment);
read_ = static_cast<std::size_t>(_stream.gcount ());
if (read_)
{
read_ += old_size_;
_len = read_;
}
}
else
{
const std::size_t start_ = _buffer.size () - lowest_;
const std::size_t len_ = _buffer.size () - start_;
::memcpy (&_buffer.front (), &_buffer[lowest_], start_ *
sizeof (char_type));
_stream.read (&_buffer.front () + start_, len_);
read_ = static_cast<std::size_t>(_stream.gcount ());
subtract (lowest_);
if (read_)
{
read_ += start_;
_len = read_;
}
else
{
_len = highest ();
}
}
return read_ != 0;
}
void push (basic_file_shared_iterator *ptr_)
{
typename iter_list::iterator iter_ = std::find (_clients.begin (),
_clients.end (), ptr_);
if (iter_ == _clients.end ())
{
_clients.push_back (ptr_);
}
}
void erase (const basic_file_shared_iterator *ptr_)
{
typename iter_list::iterator iter_ = std::find (_clients.begin (),
_clients.end (), ptr_);
if (iter_ != _clients.end ())
{
_clients.erase (iter_);
}
}
std::size_t lowest () const
{
std::size_t lowest_ = npos ();
typename iter_list::const_iterator iter_ = _clients.begin ();
typename iter_list::const_iterator end_ = _clients.end ();
for (; iter_ != end_; ++iter_)
{
const basic_file_shared_iterator *ptr_ = *iter_;
if (ptr_->_index < lowest_)
{
lowest_ = ptr_->_index;
}
}
if (lowest_ == npos ())
{
lowest_ = 0;
}
return lowest_;
}
std::size_t highest () const
{
std::size_t highest_ = 0;
typename iter_list::const_iterator iter_ = _clients.begin ();
typename iter_list::const_iterator end_ = _clients.end ();
for (; iter_ != end_; ++iter_)
{
const basic_file_shared_iterator *ptr_ = *iter_;
if (ptr_->_index != npos () && ptr_->_index > highest_)
{
highest_ = ptr_->_index;
}
}
return highest_;
}
void subtract (const std::size_t lowest_)
{
typename iter_list::iterator iter_ = _clients.begin ();
typename iter_list::iterator end_ = _clients.end ();
for (; iter_ != end_; ++iter_)
{
basic_file_shared_iterator *ptr_ = *iter_;
ptr_->_index -= lowest_;
}
}
static std::size_t npos ()
{
return static_cast<std::size_t>(~0);
}
private:
shared &operator = (const shared &rhs_);
};
bool _master;
bool _live;
std::size_t _index;
shared *_shared;
void check_master ()
{
if (!_shared)
{
throw runtime_error ("Cannot manipulate null (end) "
"file_shared_iterators.");
}
if (_master)
{
_shared->push (this);
_master = false;
_live = true;
_index = _shared->lowest ();
}
}
void update_state ()
{
if (_index >= _shared->_len)
{
if (!_shared->reload_buffer ())
{
_shared->erase (this);
_index = shared::npos ();
_live = false;
}
}
}
};
typedef basic_file_shared_iterator<char> file_shared_iterator;
typedef basic_file_shared_iterator<wchar_t> wfile_shared_iterator;
}
#endif
|