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

Revision 418, 2.6 kB (checked in by vbarta, 15 years ago)

#27: added UnitTest?++ to bdm sources, changed test_user_data to use it

Line 
1#include "MemoryOutStream.h"
2
3#ifndef UNITTEST_USE_CUSTOM_STREAMS
4
5
6namespace UnitTest {
7
8char 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
24namespace UnitTest {
25
26namespace {
27
28template<typename ValueType>
29void 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
38int RoundUpToMultipleOfPow2Number (int n, int pow2Number)
39{
40    return (n + (pow2Number - 1)) & ~(pow2Number - 1);
41}
42
43}
44
45
46MemoryOutStream::MemoryOutStream(int const size)
47    : m_capacity (0)
48    , m_buffer (0)
49
50{
51    GrowBuffer(size);
52}
53
54MemoryOutStream::~MemoryOutStream()
55{
56    delete [] m_buffer;
57}
58
59char const* MemoryOutStream::GetText() const
60{
61    return m_buffer;
62}
63
64MemoryOutStream& 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
81MemoryOutStream& MemoryOutStream::operator << (int const n)
82{
83    FormatToStream(*this, "%i", n);
84    return *this;
85}
86
87MemoryOutStream& MemoryOutStream::operator << (long const n)
88{
89    FormatToStream(*this, "%li", n);
90    return *this;
91}
92
93MemoryOutStream& MemoryOutStream::operator << (unsigned long const n)
94{
95    FormatToStream(*this, "%lu", n);
96    return *this;
97}
98
99MemoryOutStream& MemoryOutStream::operator << (float const f)
100{
101    FormatToStream(*this, "%ff", f);
102    return *this;   
103}
104
105MemoryOutStream& MemoryOutStream::operator << (void const* p)
106{
107    FormatToStream(*this, "%p", p);
108    return *this;   
109}
110
111MemoryOutStream& MemoryOutStream::operator << (unsigned int const s)
112{
113    FormatToStream(*this, "%u", s);
114    return *this;   
115}
116
117MemoryOutStream& MemoryOutStream::operator <<(double const d)
118{
119        FormatToStream(*this, "%f", d);
120        return *this;
121}
122
123int MemoryOutStream::GetCapacity() const
124{
125    return m_capacity;
126}
127
128
129void 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
Note: See TracBrowser for help on using the browser.