Changeset 477 for library/tests/unittest-cpp/MemoryOutStream.cpp
- Timestamp:
- 08/05/09 14:40:03 (15 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
library/tests/unittest-cpp/MemoryOutStream.cpp
r418 r477 6 6 namespace UnitTest { 7 7 8 char const* MemoryOutStream::GetText() const 9 { 10 m_text = this->str(); 11 return m_text.c_str(); 8 char const* MemoryOutStream::GetText() const { 9 m_text = this->str(); 10 return m_text.c_str(); 12 11 } 13 12 … … 27 26 28 27 template<typename ValueType> 29 void FormatToStream(MemoryOutStream& stream, char const* format, ValueType const& value) 30 { 28 void FormatToStream ( MemoryOutStream& stream, char const* format, ValueType const& value ) { 31 29 using namespace std; 32 30 33 34 sprintf(txt, format, value);35 31 char txt[32]; 32 sprintf ( txt, format, value ); 33 stream << txt; 36 34 } 37 35 38 int RoundUpToMultipleOfPow2Number (int n, int pow2Number) 39 { 40 return (n + (pow2Number - 1)) & ~(pow2Number - 1); 36 int RoundUpToMultipleOfPow2Number ( int n, int pow2Number ) { 37 return ( n + ( pow2Number - 1 ) ) & ~ ( pow2Number - 1 ); 41 38 } 42 39 … … 44 41 45 42 46 MemoryOutStream::MemoryOutStream (int const size)47 : m_capacity (0)48 , m_buffer (0)43 MemoryOutStream::MemoryOutStream ( int const size ) 44 : m_capacity ( 0 ) 45 , m_buffer ( 0 ) 49 46 50 47 { 51 GrowBuffer(size);48 GrowBuffer ( size ); 52 49 } 53 50 54 MemoryOutStream::~MemoryOutStream() 55 { 56 delete [] m_buffer; 51 MemoryOutStream::~MemoryOutStream() { 52 delete [] m_buffer; 57 53 } 58 54 59 char const* MemoryOutStream::GetText() const 60 { 61 return m_buffer; 55 char const* MemoryOutStream::GetText() const { 56 return m_buffer; 62 57 } 63 58 64 MemoryOutStream& MemoryOutStream::operator << (char const* txt) 65 { 59 MemoryOutStream& MemoryOutStream::operator << ( char const* txt ) { 66 60 using namespace std; 67 61 68 int const bytesLeft = m_capacity - (int)strlen(m_buffer);69 int const bytesRequired = (int)strlen(txt) + 1;62 int const bytesLeft = m_capacity - ( int ) strlen ( m_buffer ); 63 int const bytesRequired = ( int ) strlen ( txt ) + 1; 70 64 71 if (bytesRequired > bytesLeft) 72 { 73 int const requiredCapacity = bytesRequired + m_capacity - bytesLeft; 74 GrowBuffer(requiredCapacity); 75 } 65 if ( bytesRequired > bytesLeft ) { 66 int const requiredCapacity = bytesRequired + m_capacity - bytesLeft; 67 GrowBuffer ( requiredCapacity ); 68 } 76 69 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); 70 strcat ( m_buffer, txt ); 120 71 return *this; 121 72 } 122 73 123 int MemoryOutStream::GetCapacity() const 124 { 125 return m_capacity; 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; 126 111 } 127 112 128 113 129 void MemoryOutStream::GrowBuffer(int const desiredCapacity) 130 { 131 int const newCapacity = RoundUpToMultipleOfPow2Number(desiredCapacity, GROW_CHUNK_SIZE); 114 void MemoryOutStream::GrowBuffer ( int const desiredCapacity ) { 115 int const newCapacity = RoundUpToMultipleOfPow2Number ( desiredCapacity, GROW_CHUNK_SIZE ); 132 116 133 117 using namespace std; 134 118 135 136 if (m_buffer)137 strcpy(buffer, m_buffer);138 139 strcpy(buffer, "");119 char* buffer = new char[newCapacity]; 120 if ( m_buffer ) 121 strcpy ( buffer, m_buffer ); 122 else 123 strcpy ( buffer, "" ); 140 124 141 142 143 125 delete [] m_buffer; 126 m_buffer = buffer; 127 m_capacity = newCapacity; 144 128 } 145 129