1 // This file is part of the ustl library, an STL implementation.
3 // Copyright (C) 2005 by Mike Sharov <msharov@users.sourceforge.net>
4 // This file is free software, distributed under the MIT License.
8 // Helper classes to read and write packed binary streams.
18 //--------------------------------------------------------------------
20 /// \brief Constructs a stream attached to nothing.
21 /// A stream attached to nothing is not usable. Call Link() functions
22 /// inherited from cmemlink to attach to some memory block.
24 istream::istream (void)
30 /// Attaches the stream to a block at \p p of size \p n.
31 istream::istream (const void* p
, size_type n
)
37 /// Attaches to the block pointed to by \p source.
38 istream::istream (const cmemlink
& source
)
44 /// Attaches to the block pointed to by source of size source.pos()
45 istream::istream (const ostream
& source
)
46 : cmemlink (source
.begin(), source
.pos()),
51 /// Swaps contents with \p is
52 void istream::swap (istream
& is
)
55 ::ustl::swap (m_Pos
, is
.m_Pos
);
58 /// Checks that \p n bytes are available in the stream, or else throws.
59 void istream::verify_remaining (const char* op
, const char* type
, size_t n
) const
62 throw stream_bounds_exception (op
, type
, pos(), n
, remaining());
65 /// Reads \p n bytes into \p buffer.
66 void istream::read (void* buffer
, size_type n
)
68 #ifdef WANT_STREAM_BOUNDS_CHECKING
69 verify_remaining ("read", "binary data", n
);
71 assert (remaining() >= n
&& "Reading past end of buffer. Make sure you are reading the right format.");
73 copy_n (ipos(), n
, reinterpret_cast<value_type
*>(buffer
));
77 /// Reads a null-terminated string into \p str.
78 void istream::read_strz (string
& str
)
80 const_iterator zp
= find (ipos(), end(), string::c_Terminator
);
83 const size_type strl
= distance (ipos(), zp
);
85 copy (ipos(), zp
, str
.begin());
89 /// Reads at most \p n bytes into \p s.
90 istream::size_type
istream::readsome (void* s
, size_type n
)
94 const size_type
ntr (min (n
, remaining()));
99 /// Writes all unread bytes into \p os.
100 void istream::write (ostream
& os
) const
102 os
.write (ipos(), remaining());
105 /// Writes the object to stream \p os.
106 void istream::text_write (ostringstream
& os
) const
108 os
.write (ipos(), remaining());
111 /// Links to \p p of size \p n
112 void istream::unlink (void) throw()
118 //--------------------------------------------------------------------
120 /// \brief Constructs a stream attached to nothing.
121 /// A stream attached to nothing is not usable. Call Link() functions
122 /// inherited from memlink to attach to some memory block.
124 ostream::ostream (void)
130 /// Attaches the stream to a block at \p p of size \p n.
131 ostream::ostream (void* p
, size_type n
)
137 /// Attaches to the block pointed to by \p source.
138 ostream::ostream (const memlink
& source
)
144 /// Links to \p p of size \p n
145 void ostream::unlink (void) throw()
151 /// Checks that \p n bytes are available in the stream, or else throws.
152 void ostream::verify_remaining (const char* op
, const char* type
, size_t n
) const
155 throw stream_bounds_exception (op
, type
, pos(), n
, remaining());
158 /// Aligns the write pointer on \p grain. The skipped bytes are zeroed.
159 void ostream::align (size_type grain
)
161 const size_t r
= pos() % grain
;
162 size_t nb
= grain
- r
;
164 #ifdef WANT_STREAM_BOUNDS_CHECKING
165 verify_remaining ("align", "padding", nb
);
167 assert (remaining() >= nb
&& "Buffer overrun. Check your stream size calculations.");
169 fill_n (ipos(), nb
, '\x0');
173 /// Writes \p n bytes from \p buffer.
174 void ostream::write (const void* buffer
, size_type n
)
176 #ifdef WANT_STREAM_BOUNDS_CHECKING
177 verify_remaining ("write", "binary data", n
);
179 assert (remaining() >= n
&& "Buffer overrun. Check your stream size calculations.");
181 copy_n (const_iterator(buffer
), n
, ipos());
185 /// Writes \p str as a null-terminated string.
186 void ostream::write_strz (const char* str
)
188 write (str
, strlen(str
));
189 iwrite (string::c_Terminator
);
192 /// Writes all available data from \p is.
193 void ostream::read (istream
& is
)
199 /// Writes all written data to \p os.
200 void ostream::text_write (ostringstream
& os
) const
202 os
.write (begin(), pos());
205 /// Inserts an empty area of \p size, at \p start.
206 void ostream::insert (iterator start
, size_type s
)
208 memlink::insert (start
, s
);
212 /// Erases an area of \p size, at \p start.
213 void ostream::erase (iterator start
, size_type s
)
216 memlink::erase (start
, s
);
220 void ostream::swap (ostream
& os
)
223 ::ustl::swap (m_Pos
, os
.m_Pos
);
226 //--------------------------------------------------------------------