InMemoryBuffer.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include "InMemoryBuffer.h"
  2. #include "Either.h"
  3. #include "Text.h"
  4. using namespace Framework;
  5. InMemoryBuffer::InMemoryBuffer()
  6. : ReferenceCounter()
  7. {
  8. buffer = new char*[1];
  9. buffer[0] = new char[256];
  10. numBuffers = 1;
  11. readPos = 0;
  12. writePos = 0;
  13. maxWritePos = 0;
  14. }
  15. InMemoryBuffer::~InMemoryBuffer()
  16. {
  17. for (int i = 0; i < numBuffers; i++)
  18. delete[] buffer[i];
  19. delete[] buffer;
  20. }
  21. //! Reads from the resource
  22. //! \param bytes An array to be filled with bytes from the resource
  23. //! \param len How many bytes should be read from the resource
  24. void InMemoryBuffer::lese(char* bytes, int len)
  25. {
  26. if (readPos < writePos)
  27. {
  28. len = MIN(len, writePos - readPos);
  29. int readCount = 0;
  30. do
  31. {
  32. int curPos = readPos % 256;
  33. int tmp = MIN(len, 256 - curPos);
  34. memcpy(bytes + readCount, buffer[readPos / 256] + curPos, tmp);
  35. readCount += tmp;
  36. readPos += tmp;
  37. len -= tmp;
  38. } while (len > 0);
  39. }
  40. }
  41. //! Writes to the resource
  42. //! \param bytes An array containing the bytes to be written to the resource
  43. //! \param len How many bytes should be written to the resource
  44. void InMemoryBuffer::schreibe(const char* bytes, int len)
  45. {
  46. if (numBuffers * 256 < writePos + len)
  47. {
  48. int newSize = (writePos + len) / 256 + 1;
  49. char** newBuffer = new char*[newSize];
  50. memcpy(newBuffer, buffer, sizeof(char*) * numBuffers);
  51. memset(
  52. newBuffer + numBuffers, 0, sizeof(char*) * (newSize - numBuffers));
  53. delete[] buffer;
  54. buffer = newBuffer;
  55. numBuffers = newSize;
  56. }
  57. int writeCount = 0;
  58. do
  59. {
  60. int curPos = writePos % 256;
  61. int tmp = MIN(len, 256 - curPos);
  62. if (!buffer[writePos / 256]) buffer[writePos / 256] = new char[256];
  63. memcpy(buffer[writePos / 256] + curPos, bytes + writeCount, tmp);
  64. writeCount += tmp;
  65. writePos += tmp;
  66. len -= tmp;
  67. } while (len > 0);
  68. maxWritePos = MAX(maxWritePos, writePos);
  69. }
  70. //! Reads the next line of the resource
  71. //! \return The read line as Text with line break
  72. Text* InMemoryBuffer::leseZeile()
  73. {
  74. if (istEnde()) return 0;
  75. Text* ret = new Text("");
  76. for (char c = 0; c != '\n' && readPos < writePos;)
  77. {
  78. lese(&c, 1);
  79. if (c) ret->append(&c, 1);
  80. }
  81. return ret;
  82. }
  83. //! Checks whether the resource has been fully read
  84. //! return 1 if the resource has been fully read. 0 otherwise
  85. bool InMemoryBuffer::istEnde() const
  86. {
  87. return readPos < writePos;
  88. }
  89. //! Sets the position of the byte to be read next
  90. //! \param pos The index of the byte
  91. //! \param ende 1 if the index counts from the end of the resource. 0 if the index
  92. //! counts from the beginning of the resource
  93. void InMemoryBuffer::setLPosition(__int64 pos, bool ende)
  94. {
  95. if (ende)
  96. readPos = MAX(0, maxWritePos - (int)pos);
  97. else
  98. readPos = MIN(maxWritePos, (int)pos);
  99. }
  100. //! Sets the position of the byte to be written next
  101. //! \param pos The index of the byte
  102. //! \param ende 1 if the index counts from the end of the resource. 0 if the index
  103. //! counts from the beginning of the resource
  104. void InMemoryBuffer::setSPosition(__int64 pos, bool ende)
  105. {
  106. if (ende)
  107. writePos = MAX(0, maxWritePos - (int)pos);
  108. else
  109. writePos = MIN(maxWritePos, (int)pos);
  110. }
  111. //! Returns the index of the byte from the resource that would be read next
  112. //! return -1 if an error occurred. Otherwise the position of the read pointer
  113. __int64 InMemoryBuffer::getLPosition() const
  114. {
  115. return readPos;
  116. }
  117. //! Returns the index of the byte from the resource that would be written next
  118. //! return -1 if an error occurred. Otherwise the position of the write pointer
  119. __int64 InMemoryBuffer::getSPosition() const
  120. {
  121. return writePos;
  122. }
  123. //! Returns the number of bytes to be read
  124. __int64 InMemoryBuffer::getSize() const
  125. {
  126. return maxWritePos;
  127. }