1 #include "MemoryOutStream.h"
2
3 #ifdef UNITTEST_MEMORYOUTSTREAM_IS_STD_OSTRINGSTREAM
4
5 namespace UnitTest {
6
GetText() const7 char const* MemoryOutStream::GetText() const
8 {
9 m_text = this->str();
10 return m_text.c_str();
11 }
12
Clear()13 void MemoryOutStream::Clear()
14 {
15 this->str(std::string());
16 m_text = this->str();
17 }
18
19 #ifdef UNITTEST_COMPILER_IS_MSVC6
20
21 #define snprintf _snprintf
22
23 template<typename ValueType>
FormatToStream(std::ostream & stream,char const * format,ValueType const & value)24 std::ostream& FormatToStream(std::ostream& stream, char const* format, ValueType const& value)
25 {
26 using namespace std;
27
28 const size_t BUFFER_SIZE=32;
29 char txt[BUFFER_SIZE];
30 snprintf(txt, BUFFER_SIZE, format, value);
31 return stream << txt;
32 }
33
operator <<(std::ostream & stream,__int64 const n)34 std::ostream& operator<<(std::ostream& stream, __int64 const n)
35 {
36 return FormatToStream(stream, "%I64d", n);
37 }
38
operator <<(std::ostream & stream,unsigned __int64 const n)39 std::ostream& operator<<(std::ostream& stream, unsigned __int64 const n)
40 {
41 return FormatToStream(stream, "%I64u", n);
42 }
43
44 #endif
45
46 }
47
48 #else
49
50 #include <cstring>
51 #include <cstdio>
52
53 #if _MSC_VER
54 #define snprintf _snprintf
55 #endif
56
57 namespace UnitTest {
58
59 namespace {
60
61 template<typename ValueType>
FormatToStream(MemoryOutStream & stream,char const * format,ValueType const & value)62 void FormatToStream(MemoryOutStream& stream, char const* format, ValueType const& value)
63 {
64 using namespace std;
65
66 const size_t BUFFER_SIZE=32;
67 char txt[BUFFER_SIZE];
68 snprintf(txt, BUFFER_SIZE, format, value);
69 stream << txt;
70 }
71
RoundUpToMultipleOfPow2Number(int n,int pow2Number)72 int RoundUpToMultipleOfPow2Number (int n, int pow2Number)
73 {
74 return (n + (pow2Number - 1)) & ~(pow2Number - 1);
75 }
76
77 }
78
79
MemoryOutStream(int const size)80 MemoryOutStream::MemoryOutStream(int const size)
81 : m_capacity (0)
82 , m_buffer (0)
83
84 {
85 GrowBuffer(size);
86 }
87
~MemoryOutStream()88 MemoryOutStream::~MemoryOutStream()
89 {
90 delete [] m_buffer;
91 }
92
Clear()93 void MemoryOutStream::Clear()
94 {
95 m_buffer[0] = '\0';
96 }
97
GetText() const98 char const* MemoryOutStream::GetText() const
99 {
100 return m_buffer;
101 }
102
operator <<(char const * txt)103 MemoryOutStream& MemoryOutStream::operator <<(char const* txt)
104 {
105 using namespace std;
106
107 int const bytesLeft = m_capacity - (int)strlen(m_buffer);
108 int const bytesRequired = (int)strlen(txt) + 1;
109
110 if (bytesRequired > bytesLeft)
111 {
112 int const requiredCapacity = bytesRequired + m_capacity - bytesLeft;
113 GrowBuffer(requiredCapacity);
114 }
115
116 strcat(m_buffer, txt);
117 return *this;
118 }
119
operator <<(int const n)120 MemoryOutStream& MemoryOutStream::operator <<(int const n)
121 {
122 FormatToStream(*this, "%i", n);
123 return *this;
124 }
125
operator <<(long const n)126 MemoryOutStream& MemoryOutStream::operator <<(long const n)
127 {
128 FormatToStream(*this, "%li", n);
129 return *this;
130 }
131
operator <<(unsigned long const n)132 MemoryOutStream& MemoryOutStream::operator <<(unsigned long const n)
133 {
134 FormatToStream(*this, "%lu", n);
135 return *this;
136 }
137
138 #ifdef UNITTEST_COMPILER_IS_MSVC6
operator <<(__int64 const n)139 MemoryOutStream& MemoryOutStream::operator <<(__int64 const n)
140 #else
141 MemoryOutStream& MemoryOutStream::operator <<(long long const n)
142 #endif
143 {
144 #ifdef UNITTEST_WIN32
145 FormatToStream(*this, "%I64d", n);
146 #else
147 FormatToStream(*this, "%lld", n);
148 #endif
149
150 return *this;
151 }
152
153 #ifdef UNITTEST_COMPILER_IS_MSVC6
operator <<(unsigned __int64 const n)154 MemoryOutStream& MemoryOutStream::operator <<(unsigned __int64 const n)
155 #else
156 MemoryOutStream& MemoryOutStream::operator <<(unsigned long long const n)
157 #endif
158 {
159 #ifdef UNITTEST_WIN32
160 FormatToStream(*this, "%I64u", n);
161 #else
162 FormatToStream(*this, "%llu", n);
163 #endif
164
165 return *this;
166 }
167
operator <<(float const f)168 MemoryOutStream& MemoryOutStream::operator <<(float const f)
169 {
170 FormatToStream(*this, "%0.6f", f);
171 return *this;
172 }
173
operator <<(void const * p)174 MemoryOutStream& MemoryOutStream::operator <<(void const* p)
175 {
176 FormatToStream(*this, "%p", p);
177 return *this;
178 }
179
operator <<(unsigned int const s)180 MemoryOutStream& MemoryOutStream::operator <<(unsigned int const s)
181 {
182 FormatToStream(*this, "%u", s);
183 return *this;
184 }
185
operator <<(double const d)186 MemoryOutStream& MemoryOutStream::operator <<(double const d)
187 {
188 FormatToStream(*this, "%0.6f", d);
189 return *this;
190 }
191
GetCapacity() const192 int MemoryOutStream::GetCapacity() const
193 {
194 return m_capacity;
195 }
196
197
GrowBuffer(int const desiredCapacity)198 void MemoryOutStream::GrowBuffer(int const desiredCapacity)
199 {
200 int const newCapacity = RoundUpToMultipleOfPow2Number(desiredCapacity, GROW_CHUNK_SIZE);
201
202 using namespace std;
203
204 char* buffer = new char[newCapacity];
205 if (m_buffer)
206 strcpy(buffer, m_buffer);
207 else
208 strcpy(buffer, "");
209
210 delete [] m_buffer;
211 m_buffer = buffer;
212 m_capacity = newCapacity;
213 }
214
215 }
216
217
218 #endif
219