MemoryOutStream.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include "MemoryOutStream.h"
  2. #ifndef UNITTEST_USE_CUSTOM_STREAMS
  3. namespace UnitTest {
  4. char const* MemoryOutStream::GetText() const
  5. {
  6. m_text = this->str();
  7. return m_text.c_str();
  8. }
  9. }
  10. #else
  11. #include <cstring>
  12. #include <cstdio>
  13. namespace UnitTest {
  14. namespace {
  15. template<typename ValueType>
  16. void FormatToStream(MemoryOutStream& stream, char const* format, ValueType const& value)
  17. {
  18. using namespace std;
  19. char txt[32];
  20. sprintf(txt, format, value);
  21. stream << txt;
  22. }
  23. int RoundUpToMultipleOfPow2Number (int n, int pow2Number)
  24. {
  25. return (n + (pow2Number - 1)) & ~(pow2Number - 1);
  26. }
  27. }
  28. MemoryOutStream::MemoryOutStream(int const size)
  29. : m_capacity (0)
  30. , m_buffer (0)
  31. {
  32. GrowBuffer(size);
  33. }
  34. MemoryOutStream::~MemoryOutStream()
  35. {
  36. delete [] m_buffer;
  37. }
  38. char const* MemoryOutStream::GetText() const
  39. {
  40. return m_buffer;
  41. }
  42. MemoryOutStream& MemoryOutStream::operator << (char const* txt)
  43. {
  44. using namespace std;
  45. int const bytesLeft = m_capacity - (int)strlen(m_buffer);
  46. int const bytesRequired = (int)strlen(txt) + 1;
  47. if (bytesRequired > bytesLeft)
  48. {
  49. int const requiredCapacity = bytesRequired + m_capacity - bytesLeft;
  50. GrowBuffer(requiredCapacity);
  51. }
  52. strcat(m_buffer, txt);
  53. return *this;
  54. }
  55. MemoryOutStream& MemoryOutStream::operator << (int const n)
  56. {
  57. FormatToStream(*this, "%i", n);
  58. return *this;
  59. }
  60. MemoryOutStream& MemoryOutStream::operator << (long const n)
  61. {
  62. FormatToStream(*this, "%li", n);
  63. return *this;
  64. }
  65. MemoryOutStream& MemoryOutStream::operator << (unsigned long const n)
  66. {
  67. FormatToStream(*this, "%lu", n);
  68. return *this;
  69. }
  70. MemoryOutStream& MemoryOutStream::operator << (float const f)
  71. {
  72. FormatToStream(*this, "%ff", f);
  73. return *this;
  74. }
  75. MemoryOutStream& MemoryOutStream::operator << (void const* p)
  76. {
  77. FormatToStream(*this, "%p", p);
  78. return *this;
  79. }
  80. MemoryOutStream& MemoryOutStream::operator << (unsigned int const s)
  81. {
  82. FormatToStream(*this, "%u", s);
  83. return *this;
  84. }
  85. MemoryOutStream& MemoryOutStream::operator <<(double const d)
  86. {
  87. FormatToStream(*this, "%f", d);
  88. return *this;
  89. }
  90. int MemoryOutStream::GetCapacity() const
  91. {
  92. return m_capacity;
  93. }
  94. void MemoryOutStream::GrowBuffer(int const desiredCapacity)
  95. {
  96. int const newCapacity = RoundUpToMultipleOfPow2Number(desiredCapacity, GROW_CHUNK_SIZE);
  97. using namespace std;
  98. char* buffer = new char[newCapacity];
  99. if (m_buffer)
  100. strcpy(buffer, m_buffer);
  101. else
  102. strcpy(buffer, "");
  103. delete [] m_buffer;
  104. m_buffer = buffer;
  105. m_capacity = newCapacity;
  106. }
  107. }
  108. #endif