Library of Assembled Shared Sources
binary_i_memory_block.cpp
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2011 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43#include "lass_common.h"
45#include <string.h>
46
47namespace lass
48{
49
50namespace io
51{
52
53/** Construct an "empty" closed stream..
54 */
56 BinaryIStream(),
57 begin_(0),
58 size_(0),
59 position_(0)
60{
61}
62
63
64
65/** Construct stream by pointer to memory block and length of block
66 */
67BinaryIMemoryBlock::BinaryIMemoryBlock( const void* begin, pos_type size ):
68 BinaryIStream(),
69 begin_(static_cast<const TByte*>(begin)),
70 size_(size),
71 position_(0)
72{
73}
74
75
76
77/** Construct stream by pointer to memory block and to one byte past end of block.
78 */
79BinaryIMemoryBlock::BinaryIMemoryBlock( const void* begin, const void* end ):
80 BinaryIStream(),
81 begin_(static_cast<const TByte*>(begin)),
82 size_(0),
83 position_(0)
84{
85 off_type size = static_cast<const TByte*>(end) - static_cast<const TByte*>(begin);
86 if (size >= 0)
87 {
88 size_ = static_cast<pos_type>(size);
89 }
90 else
91 {
92 setstate(std::ios_base::badbit);
93 }
94}
95
96
97
98// --- private -------------------------------------------------------------------------------------
99
100size_t BinaryIMemoryBlock::doRead(void* out, size_t numberOfBytes)
101{
102 if (!begin_)
103 {
104 setstate(std::ios_base::badbit);
105 return 0;
106 }
107 if (!good())
108 {
109 return 0;
110 }
111 if (position_ >= size_)
112 {
113 setstate(std::ios_base::eofbit);
114 return 0;
115 }
116 pos_type next = position_ + numberOfBytes;
117 if (next > size_ || next < position_)
118 {
119 next = size_;
120 numberOfBytes = size_ - position_;
121 }
122 memcpy(out, &begin_[position_], numberOfBytes);
123 position_ = next;
124 return numberOfBytes;
125}
126
127
128
129BinaryIMemoryBlock::pos_type BinaryIMemoryBlock::doTellg() const
130{
131 return position_;
132}
133
134
135
136void BinaryIMemoryBlock::doSeekg(pos_type position)
137{
138 LASS_ASSERT(good());
139 position_ = position;
140}
141
142
143
144void BinaryIMemoryBlock::doSeekg(off_type offset, std::ios_base::seekdir direction)
145{
146 LASS_ASSERT(good());
147
148 auto seek = [this](pos_type current, off_type offset)
149 {
150 if (offset < 0)
151 {
152 const pos_type negoffset = static_cast<pos_type>(-offset);
153 if (negoffset > current)
154 {
155 setstate(std::ios_base::failbit);
156 return;
157 }
158 this->position_ = current - negoffset;
159 }
160 else
161 {
162 const pos_type posoffset = static_cast<pos_type>(offset);
163 if (current > num::NumTraits<pos_type>::max - posoffset)
164 {
165 setstate(std::ios_base::failbit);
166 return;
167 }
168 this->position_ = current + posoffset;
169 }
170 };
171
172 switch (direction)
173 {
174 case std::ios_base::beg:
175 if (offset < 0)
176 {
177 setstate(std::ios_base::failbit);
178 return;
179 }
180 position_ = static_cast<pos_type>(offset);
181 break;
182 case std::ios_base::cur:
183 seek(position_, offset);
184 break;
185 case std::ios_base::end:
186 seek(size_, offset);
187 break;
188 default:
189 LASS_ASSERT(false);
190 };
191}
192
193}
194
195}
BinaryIMemoryBlock()
Construct an "empty" closed stream.
const lass::python::impl::IterNextSlot next("__next__", Py_tp_iternext)
__next__ method (iterator next)
streams, binary streams, vrmlstreams, ...
Library for Assembled Shared Sources.
Definition config.h:53