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

Revision 477, 2.5 kB (checked in by mido, 15 years ago)

panove, vite, jak jsem peclivej na upravu kodu.. snad se vam bude libit:) konfigurace je v souboru /system/astylerc

RevLine 
[418]1#include "MemoryOutStream.h"
2
3#ifndef UNITTEST_USE_CUSTOM_STREAMS
4
5
6namespace UnitTest {
7
[477]8char const* MemoryOutStream::GetText() const {
9        m_text = this->str();
10        return m_text.c_str();
[418]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>
[477]28void FormatToStream ( MemoryOutStream& stream, char const* format, ValueType const& value ) {
[418]29        using namespace std;
30
[477]31        char txt[32];
32        sprintf ( txt, format, value );
33        stream << txt;
[418]34}
35
[477]36int RoundUpToMultipleOfPow2Number ( int n, int pow2Number ) {
37        return ( n + ( pow2Number - 1 ) ) & ~ ( pow2Number - 1 );
[418]38}
39
40}
41
42
[477]43MemoryOutStream::MemoryOutStream ( int const size )
44                : m_capacity ( 0 )
45                , m_buffer ( 0 )
[418]46
47{
[477]48        GrowBuffer ( size );
[418]49}
50
[477]51MemoryOutStream::~MemoryOutStream() {
52        delete [] m_buffer;
[418]53}
54
[477]55char const* MemoryOutStream::GetText() const {
56        return m_buffer;
[418]57}
58
[477]59MemoryOutStream& MemoryOutStream::operator << ( char const* txt ) {
[418]60        using namespace std;
61
[477]62        int const bytesLeft = m_capacity - ( int ) strlen ( m_buffer );
63        int const bytesRequired = ( int ) strlen ( txt ) + 1;
[418]64
[477]65        if ( bytesRequired > bytesLeft ) {
66                int const requiredCapacity = bytesRequired + m_capacity - bytesLeft;
67                GrowBuffer ( requiredCapacity );
68        }
[418]69
[477]70        strcat ( m_buffer, txt );
71        return *this;
[418]72}
73
[477]74MemoryOutStream& MemoryOutStream::operator << ( int const n ) {
75        FormatToStream ( *this, "%i", n );
76        return *this;
[418]77}
78
[477]79MemoryOutStream& MemoryOutStream::operator << ( long const n ) {
80        FormatToStream ( *this, "%li", n );
81        return *this;
[418]82}
83
[477]84MemoryOutStream& MemoryOutStream::operator << ( unsigned long const n ) {
85        FormatToStream ( *this, "%lu", n );
86        return *this;
[418]87}
88
[477]89MemoryOutStream& MemoryOutStream::operator << ( float const f ) {
90        FormatToStream ( *this, "%ff", f );
91        return *this;
[418]92}
93
[477]94MemoryOutStream& MemoryOutStream::operator << ( void const* p ) {
95        FormatToStream ( *this, "%p", p );
96        return *this;
[418]97}
98
[477]99MemoryOutStream& MemoryOutStream::operator << ( unsigned int const s ) {
100        FormatToStream ( *this, "%u", s );
101        return *this;
[418]102}
103
[477]104MemoryOutStream& MemoryOutStream::operator << ( double const d ) {
105        FormatToStream ( *this, "%f", d );
[418]106        return *this;
107}
108
[477]109int MemoryOutStream::GetCapacity() const {
110        return m_capacity;
[418]111}
112
113
[477]114void MemoryOutStream::GrowBuffer ( int const desiredCapacity ) {
115        int const newCapacity = RoundUpToMultipleOfPow2Number ( desiredCapacity, GROW_CHUNK_SIZE );
[418]116
117        using namespace std;
118
[477]119        char* buffer = new char[newCapacity];
120        if ( m_buffer )
121                strcpy ( buffer, m_buffer );
122        else
123                strcpy ( buffer, "" );
[418]124
[477]125        delete [] m_buffer;
126        m_buffer = buffer;
127        m_capacity = newCapacity;
[418]128}
129
130}
131
132
133#endif
Note: See TracBrowser for help on using the browser.