root/library/tests/unittest-cpp/MemoryOutStream.cpp @ 706

Revision 706, 2.5 kB (checked in by smidl, 15 years ago)

eol-native

  • Property svn:eol-style set to native
Line 
1#include "MemoryOutStream.h"
2
3#ifndef UNITTEST_USE_CUSTOM_STREAMS
4
5
6namespace UnitTest {
7
8char 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
23namespace UnitTest {
24
25namespace {
26
27template<typename ValueType>
28void 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
36int RoundUpToMultipleOfPow2Number ( int n, int pow2Number ) {
37        return ( n + ( pow2Number - 1 ) ) & ~ ( pow2Number - 1 );
38}
39
40}
41
42
43MemoryOutStream::MemoryOutStream ( int const size )
44                : m_capacity ( 0 )
45                , m_buffer ( 0 )
46
47{
48        GrowBuffer ( size );
49}
50
51MemoryOutStream::~MemoryOutStream() {
52        delete [] m_buffer;
53}
54
55char const* MemoryOutStream::GetText() const {
56        return m_buffer;
57}
58
59MemoryOutStream& 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
74MemoryOutStream& MemoryOutStream::operator << ( int const n ) {
75        FormatToStream ( *this, "%i", n );
76        return *this;
77}
78
79MemoryOutStream& MemoryOutStream::operator << ( long const n ) {
80        FormatToStream ( *this, "%li", n );
81        return *this;
82}
83
84MemoryOutStream& MemoryOutStream::operator << ( unsigned long const n ) {
85        FormatToStream ( *this, "%lu", n );
86        return *this;
87}
88
89MemoryOutStream& MemoryOutStream::operator << ( float const f ) {
90        FormatToStream ( *this, "%ff", f );
91        return *this;
92}
93
94MemoryOutStream& MemoryOutStream::operator << ( void const* p ) {
95        FormatToStream ( *this, "%p", p );
96        return *this;
97}
98
99MemoryOutStream& MemoryOutStream::operator << ( unsigned int const s ) {
100        FormatToStream ( *this, "%u", s );
101        return *this;
102}
103
104MemoryOutStream& MemoryOutStream::operator << ( double const d ) {
105        FormatToStream ( *this, "%f", d );
106        return *this;
107}
108
109int MemoryOutStream::GetCapacity() const {
110        return m_capacity;
111}
112
113
114void 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
Note: See TracBrowser for help on using the browser.