1 | #include "MemoryOutStream.h" |
---|
2 | |
---|
3 | #ifndef UNITTEST_USE_CUSTOM_STREAMS |
---|
4 | |
---|
5 | |
---|
6 | namespace UnitTest { |
---|
7 | |
---|
8 | char const* MemoryOutStream::GetText() const { |
---|
9 | m_text = this->str(); |
---|
10 | return m_text.c_str(); |
---|
11 | } |
---|
12 | |
---|
13 | |
---|
14 | } |
---|
15 | |
---|
16 | |
---|
17 | #else |
---|
18 | |
---|
19 | |
---|
20 | #include <cstring> |
---|
21 | #include <cstdio> |
---|
22 | |
---|
23 | namespace UnitTest { |
---|
24 | |
---|
25 | namespace { |
---|
26 | |
---|
27 | template<typename ValueType> |
---|
28 | void FormatToStream ( MemoryOutStream& stream, char const* format, ValueType const& value ) { |
---|
29 | using namespace std; |
---|
30 | |
---|
31 | char txt[32]; |
---|
32 | sprintf ( txt, format, value ); |
---|
33 | stream << txt; |
---|
34 | } |
---|
35 | |
---|
36 | int RoundUpToMultipleOfPow2Number ( int n, int pow2Number ) { |
---|
37 | return ( n + ( pow2Number - 1 ) ) & ~ ( pow2Number - 1 ); |
---|
38 | } |
---|
39 | |
---|
40 | } |
---|
41 | |
---|
42 | |
---|
43 | MemoryOutStream::MemoryOutStream ( int const size ) |
---|
44 | : m_capacity ( 0 ) |
---|
45 | , m_buffer ( 0 ) |
---|
46 | |
---|
47 | { |
---|
48 | GrowBuffer ( size ); |
---|
49 | } |
---|
50 | |
---|
51 | MemoryOutStream::~MemoryOutStream() { |
---|
52 | delete [] m_buffer; |
---|
53 | } |
---|
54 | |
---|
55 | char const* MemoryOutStream::GetText() const { |
---|
56 | return m_buffer; |
---|
57 | } |
---|
58 | |
---|
59 | MemoryOutStream& MemoryOutStream::operator << ( char const* txt ) { |
---|
60 | using namespace std; |
---|
61 | |
---|
62 | int const bytesLeft = m_capacity - ( int ) strlen ( m_buffer ); |
---|
63 | int const bytesRequired = ( int ) strlen ( txt ) + 1; |
---|
64 | |
---|
65 | if ( bytesRequired > bytesLeft ) { |
---|
66 | int const requiredCapacity = bytesRequired + m_capacity - bytesLeft; |
---|
67 | GrowBuffer ( requiredCapacity ); |
---|
68 | } |
---|
69 | |
---|
70 | strcat ( m_buffer, txt ); |
---|
71 | return *this; |
---|
72 | } |
---|
73 | |
---|
74 | MemoryOutStream& MemoryOutStream::operator << ( int const n ) { |
---|
75 | FormatToStream ( *this, "%i", n ); |
---|
76 | return *this; |
---|
77 | } |
---|
78 | |
---|
79 | MemoryOutStream& MemoryOutStream::operator << ( long const n ) { |
---|
80 | FormatToStream ( *this, "%li", n ); |
---|
81 | return *this; |
---|
82 | } |
---|
83 | |
---|
84 | MemoryOutStream& MemoryOutStream::operator << ( unsigned long const n ) { |
---|
85 | FormatToStream ( *this, "%lu", n ); |
---|
86 | return *this; |
---|
87 | } |
---|
88 | |
---|
89 | MemoryOutStream& MemoryOutStream::operator << ( float const f ) { |
---|
90 | FormatToStream ( *this, "%ff", f ); |
---|
91 | return *this; |
---|
92 | } |
---|
93 | |
---|
94 | MemoryOutStream& MemoryOutStream::operator << ( void const* p ) { |
---|
95 | FormatToStream ( *this, "%p", p ); |
---|
96 | return *this; |
---|
97 | } |
---|
98 | |
---|
99 | MemoryOutStream& MemoryOutStream::operator << ( unsigned int const s ) { |
---|
100 | FormatToStream ( *this, "%u", s ); |
---|
101 | return *this; |
---|
102 | } |
---|
103 | |
---|
104 | MemoryOutStream& MemoryOutStream::operator << ( double const d ) { |
---|
105 | FormatToStream ( *this, "%f", d ); |
---|
106 | return *this; |
---|
107 | } |
---|
108 | |
---|
109 | int MemoryOutStream::GetCapacity() const { |
---|
110 | return m_capacity; |
---|
111 | } |
---|
112 | |
---|
113 | |
---|
114 | void MemoryOutStream::GrowBuffer ( int const desiredCapacity ) { |
---|
115 | int const newCapacity = RoundUpToMultipleOfPow2Number ( desiredCapacity, GROW_CHUNK_SIZE ); |
---|
116 | |
---|
117 | using namespace std; |
---|
118 | |
---|
119 | char* buffer = new char[newCapacity]; |
---|
120 | if ( m_buffer ) |
---|
121 | strcpy ( buffer, m_buffer ); |
---|
122 | else |
---|
123 | strcpy ( buffer, "" ); |
---|
124 | |
---|
125 | delete [] m_buffer; |
---|
126 | m_buffer = buffer; |
---|
127 | m_capacity = newCapacity; |
---|
128 | } |
---|
129 | |
---|
130 | } |
---|
131 | |
---|
132 | |
---|
133 | #endif |
---|