annotate packages/language/cxx/ustl/current/src/mistream.cpp @ 3292:7f8e529b4d82 default tip

Fix FREESCALE_EDMA_NBYTES_MLOFFYES_MLOFF() so it works with negative offsets.
author vae
date Wed, 29 Apr 2015 23:31:48 +0000
parents 4885e0722884
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2904
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
1 // This file is part of the uSTL library, an STL implementation.
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
2 //
3152
4885e0722884 * include/*:
sergeig
parents: 2904
diff changeset
3 // Copyright (c) 2005 by Mike Sharov <msharov@users.sourceforge.net>
2904
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
4 // This file is free software, distributed under the MIT License.
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
5
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
6 #include "mistream.h"
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
7 #include "sostream.h"
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
8 #include "ustring.h"
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
9 #include "ualgo.h"
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
10
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
11 namespace ustl {
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
12
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
13 //--------------------------------------------------------------------
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
14
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
15 /// Checks that \p n bytes are available in the stream, or else throws.
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
16 void ios_base::overrun (const char* op, const char* type, uint32_t n, uint32_t pos, uint32_t rem)
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
17 {
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
18 if (set_and_throw (rem ? failbit : (failbit | eofbit)))
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
19 USTL_THROW(stream_bounds_exception (op, type, pos, n, rem));
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
20 }
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
21
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
22 //--------------------------------------------------------------------
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
23
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
24 /// Attaches to the block pointed to by source of size source.pos()
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
25 istream::istream (const ostream& source)
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
26 : cmemlink (source.begin(), source.pos()),
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
27 m_Pos (0)
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
28 {
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
29 }
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
30
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
31 void istream::unlink (void) throw() { cmemlink::unlink(); m_Pos = 0; }
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
32 void ostream::unlink (void) throw() { memlink::unlink(); m_Pos = 0; }
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
33
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
34 /// Writes all unread bytes into \p os.
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
35 void istream::write (ostream& os) const
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
36 {
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
37 os.write (ipos(), remaining());
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
38 }
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
39
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
40 /// Writes the object to stream \p os.
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
41 void istream::text_write (ostringstream& os) const
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
42 {
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
43 os.write (ipos(), remaining());
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
44 }
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
45
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
46 /// Reads a null-terminated string into \p str.
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
47 void istream::read_strz (string& str)
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
48 {
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
49 const_iterator zp = find (ipos(), end(), '\0');
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
50 if (zp == end())
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
51 zp = ipos();
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
52 const size_type strl = distance (ipos(), zp);
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
53 str.assign (ipos(), strl);
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
54 m_Pos += strl + 1;
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
55 }
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
56
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
57 /// Reads at most \p n bytes into \p s.
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
58 istream::size_type istream::readsome (void* s, size_type n)
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
59 {
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
60 if (remaining() < n)
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
61 underflow (n);
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
62 const size_type ntr (min (n, remaining()));
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
63 read (s, ntr);
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
64 return (ntr);
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
65 }
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
66
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
67 //--------------------------------------------------------------------
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
68
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
69 /// Aligns the write pointer on \p grain. The skipped bytes are zeroed.
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
70 void ostream::align (size_type grain)
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
71 {
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
72 assert (!((grain-1)&grain) && "grain must be a power of 2");
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
73 iterator ip = ipos();
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
74 iterator ipa = iterator((uintptr_t(ip) + (grain-1)) & ~(grain-1));
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
75 size_t nb = distance (ip, ipa);
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
76 #if WANT_STREAM_BOUNDS_CHECKING
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
77 if (!verify_remaining ("align", "padding", nb))
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
78 return;
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
79 #else
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
80 assert (remaining() >= nb && "Buffer overrun. Check your stream size calculations.");
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
81 #endif
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
82 memset (ip, '\x0', nb);
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
83 m_Pos += nb;
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
84 }
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
85
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
86 /// Writes \p str as a null-terminated string.
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
87 void ostream::write_strz (const char* str)
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
88 {
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
89 write (str, strlen(str)+1);
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
90 }
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
91
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
92 /// Writes all available data from \p is.
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
93 void ostream::read (istream& is)
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
94 {
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
95 write (is.ipos(), is.remaining());
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
96 is.seek (is.size());
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
97 }
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
98
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
99 /// Writes all written data to \p os.
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
100 void ostream::text_write (ostringstream& os) const
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
101 {
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
102 os.write (begin(), pos());
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
103 }
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
104
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
105 /// Inserts an empty area of \p size, at \p start.
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
106 void ostream::insert (iterator start, size_type s)
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
107 {
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
108 m_Pos += s;
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
109 memlink::insert (start, s);
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
110 }
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
111
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
112 /// Erases an area of \p size, at \p start.
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
113 void ostream::erase (iterator start, size_type s)
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
114 {
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
115 m_Pos -= s;
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
116 memlink::erase (start, s);
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
117 }
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
118
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
119 //--------------------------------------------------------------------
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
120
88af52c64ce4 * ecos.db: Add uSTL library package record.
jld
parents:
diff changeset
121 } // namespace ustl