TPIE

2362a60
byte_stream_accessor.h
1 // -*- mode: c++; tab-width: 4; indent-tabs-mode: t; c-file-style: "stroustrup"; -*-
2 // vi:set ts=4 sts=4 sw=4 noet :
3 // Copyright 2013, The TPIE development team
4 //
5 // This file is part of TPIE.
6 //
7 // TPIE is free software: you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License as published by the
9 // Free Software Foundation, either version 3 of the License, or (at your
10 // option) any later version.
11 //
12 // TPIE is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 // License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with TPIE. If not, see <http://www.gnu.org/licenses/>
19 
20 #ifndef TPIE_FILE_ACCESSOR_BYTE_STREAM_ACCESSOR_H
21 #define TPIE_FILE_ACCESSOR_BYTE_STREAM_ACCESSOR_H
22 
24 #include <tpie/tpie_log.h>
25 #include <algorithm>
26 
27 namespace tpie {
28 namespace file_accessor {
29 
30 template <typename file_accessor_t>
31 class byte_stream_accessor : public stream_accessor_base<file_accessor_t> {
33 public:
34  virtual memory_size_type read_block(void * /*data*/,
35  stream_size_type /*blockNumber*/,
36  memory_size_type /*itemCount*/) override
37  {
38  throw exception("Block operations not supported");
39  }
40 
41  virtual void write_block(const void * /*data*/,
42  stream_size_type /*blockNumber*/,
43  memory_size_type /*itemCount*/) override
44  {
45  throw exception("Block operations not supported");
46  }
47 
48  void set_size(stream_size_type amt) {
49  p_t::set_size(amt);
50  }
51 
52  bool empty() {
53  return this->size() == 0;
54  }
55 
56  stream_size_type file_size() {
57  return std::max(this->m_fileAccessor.file_size_i(),
58  static_cast<stream_size_type>(this->header_size()))
59  - this->header_size();
60  }
61 
62  void truncate_bytes(stream_size_type size) {
63  this->m_fileAccessor.truncate_i(this->header_size() + size);
64  }
65 
66  void write(const stream_size_type byteOffset, const void * data, const memory_size_type size) {
67  stream_size_type position = byteOffset + this->header_size();
68  this->m_fileAccessor.seek_i(position);
69  this->m_fileAccessor.write_i(data, size);
70  }
71 
72  void append(const void * data, memory_size_type size) {
73  stream_size_type position = this->m_fileAccessor.file_size_i();
74 
75  if (position < this->header_size())
76  position = this->header_size();
77 
78  this->m_fileAccessor.seek_i(position);
79  this->m_fileAccessor.write_i(data, size);
80  }
81 
82  memory_size_type read(const stream_size_type byteOffset, void * data, memory_size_type size) {
83  stream_size_type sz
84  = std::max(static_cast<stream_size_type>(this->header_size()),
85  this->m_fileAccessor.file_size_i())
86  - this->header_size();
87 
88  if (byteOffset + size > sz)
89  size = sz - byteOffset;
90 
91  stream_size_type position = this->header_size() + byteOffset;
92 
93  this->m_fileAccessor.seek_i(position);
94  this->m_fileAccessor.read_i(data, size);
95  return size;
96  }
97 
98  memory_size_type block_items() const {
99  return p_t::block_items();
100  }
101 
102  memory_size_type block_size() const {
103  return p_t::block_size();
104  }
105 };
106 
107 } // namespace file_accessor
108 } // namespace tpie
109 
110 #endif // TPIE_FILE_ACCESSOR_BYTE_STREAM_ACCESSOR_H
virtual void write_block(const void *, stream_size_type, memory_size_type) override
Write the given number of items from the given buffer into the given block.
stream_size_type size() const
Number of items in stream.
memory_size_type header_size() const
The size of header and user data with padding included.
Logging functionality and log_level codes for different priorities of log messages.
Reads and writes stream headers, user data and blocks from TPIE streams.
virtual memory_size_type read_block(void *, stream_size_type, memory_size_type) override
Read the given number of items from the given block into the given buffer.