TPIE

2362a60
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_STREAM_ACCESSOR_H
21 #define TPIE_FILE_ACCESSOR_STREAM_ACCESSOR_H
22 
24 
25 namespace tpie {
26 namespace file_accessor {
27 
28 template <typename file_accessor_t>
29 class stream_accessor : public stream_accessor_base<file_accessor_t> {
30 public:
31  virtual memory_size_type read_block(void * data,
32  stream_size_type blockNumber,
33  memory_size_type itemCount) override
34  {
35  stream_size_type loc = this->header_size() + blockNumber*this->block_size();
36  this->m_fileAccessor.seek_i(loc);
37  stream_size_type offset = blockNumber*this->block_items();
38  if (offset + itemCount > this->size()) itemCount = static_cast<memory_size_type>(this->size() - offset);
39  memory_size_type z=itemCount*this->item_size();
40  this->m_fileAccessor.read_i(data, z);
41  return itemCount;
42  }
43 
44  virtual void write_block(const void * data,
45  stream_size_type blockNumber,
46  memory_size_type itemCount) override
47  {
48  stream_size_type loc = this->header_size() + blockNumber*this->block_size();
49  // Here, we may seek beyond the file size.
50  // However, lseek(2) specifies that the file will be padded with zeroes in this case,
51  // and on Windows, the file is padded with arbitrary garbage (which is ok).
52  this->m_fileAccessor.seek_i(loc);
53  stream_size_type offset = blockNumber*this->block_items();
54  memory_size_type z=itemCount*this->item_size();
55  this->m_fileAccessor.write_i(data, z);
56  if (offset+itemCount > this->size()) this->set_size(offset+itemCount);
57  }
58 };
59 
60 } // namespace tpie
61 } // namespace file_accessor
62 
63 #endif // TPIE_FILE_ACCESSOR_STREAM_ACCESSOR_H
virtual memory_size_type read_block(void *data, stream_size_type blockNumber, memory_size_type itemCount) override
Read the given number of items from the given block into the given buffer.
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.
Reads and writes stream headers, user data and blocks from TPIE streams.
virtual void write_block(const void *data, stream_size_type blockNumber, memory_size_type itemCount) override
Write the given number of items from the given buffer into the given block.