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