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