Files
@ f2a6ba12fc29
Branch filter:
Location: libtransport.git/3rdparty/cpprestsdk/tests/common/UnitTestpp/src/MemoryOutStream.cpp
f2a6ba12fc29
4.7 KiB
text/x-c++hdr
Slack frontend stub
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | /***
* This file is based on or incorporates material from the UnitTest++ r30 open source project.
* Microsoft is not the original author of this code but has modified it and is licensing the code under
* the MIT License. Microsoft reserves all other rights not expressly granted under the MIT License,
* whether by implication, estoppel or otherwise.
*
* UnitTest++ r30
*
* Copyright (c) 2006 Noel Llopis and Charles Nicholson
* Portions Copyright (c) Microsoft Corporation
*
* All Rights Reserved.
*
* MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
***/
#include "stdafx.h"
#ifdef UNITTEST_MEMORYOUTSTREAM_IS_STD_OSTRINGSTREAM
namespace UnitTest {
MemoryOutStream::MemoryOutStream() {}
MemoryOutStream::~MemoryOutStream() {}
char const* MemoryOutStream::GetText() const
{
m_text = this->str();
return m_text.c_str();
}
void MemoryOutStream::Clear()
{
this->str(std::string());
m_text = this->str();
}
}
#else
namespace UnitTest {
namespace {
template<typename ValueType>
void FormatToStream(MemoryOutStream& stream, char const* format, ValueType const& value)
{
using namespace std;
char txt[32];
sprintf(txt, format, value);
stream << txt;
}
int RoundUpToMultipleOfPow2Number (int n, int pow2Number)
{
return (n + (pow2Number - 1)) & ~(pow2Number - 1);
}
}
MemoryOutStream::MemoryOutStream(int const size)
: m_capacity (0)
, m_buffer (0)
{
GrowBuffer(size);
}
MemoryOutStream::~MemoryOutStream()
{
delete [] m_buffer;
}
void MemoryOutStream::Clear()
{
m_buffer[0] = '\0';
}
char const* MemoryOutStream::GetText() const
{
return m_buffer;
}
MemoryOutStream& MemoryOutStream::operator <<(char const* txt)
{
using namespace std;
int const bytesLeft = m_capacity - (int)strlen(m_buffer);
int const bytesRequired = (int)strlen(txt) + 1;
if (bytesRequired > bytesLeft)
{
int const requiredCapacity = bytesRequired + m_capacity - bytesLeft;
GrowBuffer(requiredCapacity);
}
strcat(m_buffer, txt);
return *this;
}
MemoryOutStream& MemoryOutStream::operator <<(int const n)
{
FormatToStream(*this, "%i", n);
return *this;
}
MemoryOutStream& MemoryOutStream::operator <<(long const n)
{
FormatToStream(*this, "%li", n);
return *this;
}
MemoryOutStream& MemoryOutStream::operator <<(unsigned long const n)
{
FormatToStream(*this, "%lu", n);
return *this;
}
MemoryOutStream& MemoryOutStream::operator <<(long long const n)
{
#ifdef UNITTEST_WIN32
FormatToStream(*this, "%I64d", n);
#else
FormatToStream(*this, "%lld", n);
#endif
return *this;
}
MemoryOutStream& MemoryOutStream::operator <<(unsigned long long const n)
{
#ifdef UNITTEST_WIN32
FormatToStream(*this, "%I64u", n);
#else
FormatToStream(*this, "%llu", n);
#endif
return *this;
}
MemoryOutStream& MemoryOutStream::operator <<(float const f)
{
FormatToStream(*this, "%ff", f);
return *this;
}
MemoryOutStream& MemoryOutStream::operator <<(void const* p)
{
FormatToStream(*this, "%p", p);
return *this;
}
MemoryOutStream& MemoryOutStream::operator <<(unsigned int const s)
{
FormatToStream(*this, "%u", s);
return *this;
}
MemoryOutStream& MemoryOutStream::operator <<(double const d)
{
FormatToStream(*this, "%f", d);
return *this;
}
int MemoryOutStream::GetCapacity() const
{
return m_capacity;
}
void MemoryOutStream::GrowBuffer(int const desiredCapacity)
{
int const newCapacity = RoundUpToMultipleOfPow2Number(desiredCapacity, GROW_CHUNK_SIZE);
using namespace std;
char* buffer = new char[newCapacity];
if (m_buffer)
strcpy(buffer, m_buffer);
else
strcpy(buffer, "");
delete [] m_buffer;
m_buffer = buffer;
m_capacity = newCapacity;
}
}
#endif
|