9
3

buffer.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include "buffer.h"
  5. Buffer::Buffer() {
  6. buf = NULL;
  7. capacity = 0;
  8. len = 0;
  9. }
  10. Buffer::~Buffer() {
  11. if (buf != NULL)
  12. free(buf);
  13. }
  14. unsigned int Buffer::GetLength() {
  15. return len;
  16. }
  17. int Buffer::IndexOf(char c) {
  18. unsigned int i;
  19. for (i = 0; i < len; i++) {
  20. if (buf[i] == c)
  21. return (int)i;
  22. }
  23. return -1;
  24. }
  25. void Buffer::Clear() {
  26. if (buf != NULL) {
  27. free(buf);
  28. buf = NULL;
  29. }
  30. capacity = 0;
  31. len = 0;
  32. }
  33. const char * Buffer::Peek() {
  34. return buf;
  35. }
  36. void Buffer::PeekReadPrimitive(void *buf, unsigned int size, bool peek) {
  37. if (len >= size) {
  38. memcpy(buf, this->buf, size);
  39. if (!peek) {
  40. memmove(this->buf, this->buf + size, len - size);
  41. len -= size;
  42. }
  43. }
  44. }
  45. uint32_t Buffer::PeekUInt32() {
  46. uint32_t ret = 0;
  47. PeekReadPrimitive(&ret, sizeof(ret), true);
  48. return ret;
  49. }
  50. unsigned int Buffer::Read(unsigned int len, char **dst) {
  51. unsigned int actual = len;
  52. if (actual > this->len)
  53. actual = this->len;
  54. *dst = (char *)malloc(actual);
  55. if (*dst == NULL)
  56. return 0;
  57. return Read(actual, *dst, actual);
  58. }
  59. unsigned int Buffer::Read(unsigned int len, char *dst, unsigned int size) {
  60. unsigned int actual = len;
  61. if (actual > size)
  62. actual = size;
  63. if (actual > this->len)
  64. actual = this->len;
  65. memcpy(dst, buf, actual);
  66. memmove(buf, buf + actual, this->len - actual);
  67. this->len -= actual;
  68. return actual;
  69. }
  70. uint32_t Buffer::ReadUInt32() {
  71. uint32_t ret = 0;
  72. PeekReadPrimitive(&ret, sizeof(ret), false);
  73. return ret;
  74. }
  75. bool Buffer::Write(const char *data, unsigned int len) {
  76. unsigned int new_capacity;
  77. char *new_buf;
  78. while (this->len + len > capacity) {
  79. new_capacity = capacity == 0 ? 512 : capacity * 2;
  80. new_buf = (char *)realloc(buf, new_capacity);
  81. if (new_buf == NULL)
  82. return false;
  83. capacity = new_capacity;
  84. buf = new_buf;
  85. }
  86. memcpy(buf + this->len, data, len);
  87. this->len += len;
  88. return true;
  89. }
  90. bool Buffer::Write(const char *data) {
  91. return Write(data, (unsigned int)strlen(data));
  92. }
  93. bool Buffer::Write(const unsigned char *data, unsigned int len) {
  94. return Write((const char *)data, len);
  95. }
  96. bool Buffer::WriteInt8(int8_t data) {
  97. char buf[sizeof(data)];
  98. memcpy(buf, &data, sizeof(buf));
  99. return Write(buf, sizeof(buf));
  100. }
  101. bool Buffer::WriteUInt8(uint8_t data) {
  102. char buf[sizeof(data)];
  103. memcpy(buf, &data, sizeof(buf));
  104. return Write(buf, sizeof(buf));
  105. }
  106. bool Buffer::WriteInt16(int16_t data) {
  107. char buf[sizeof(data)];
  108. memcpy(buf, &data, sizeof(buf));
  109. return Write(buf, sizeof(buf));
  110. }
  111. bool Buffer::WriteUInt16(uint16_t data) {
  112. char buf[sizeof(data)];
  113. memcpy(buf, &data, sizeof(buf));
  114. return Write(buf, sizeof(buf));
  115. }
  116. bool Buffer::WriteInt32(int32_t data) {
  117. char buf[sizeof(data)];
  118. memcpy(buf, &data, sizeof(buf));
  119. return Write(buf, sizeof(buf));
  120. }
  121. bool Buffer::WriteUInt32(uint32_t data) {
  122. char buf[sizeof(data)];
  123. memcpy(buf, &data, sizeof(buf));
  124. return Write(buf, sizeof(buf));
  125. }
  126. bool Buffer::WriteInt64(int64_t data) {
  127. char buf[sizeof(data)];
  128. memcpy(buf, &data, sizeof(buf));
  129. return Write(buf, sizeof(buf));
  130. }
  131. bool Buffer::WriteUInt64(uint64_t data) {
  132. char buf[sizeof(data)];
  133. memcpy(buf, &data, sizeof(buf));
  134. return Write(buf, sizeof(buf));
  135. }
  136. bool Buffer::WriteChar(char data) {
  137. return Write(&data, sizeof(data));
  138. }
  139. bool Buffer::WriteDouble(double data) {
  140. char buf[sizeof(data)];
  141. memcpy(buf, &data, sizeof(buf));
  142. return Write(buf, sizeof(buf));
  143. }
  144. bool Buffer::WriteAt(unsigned int index, const char *data, unsigned int len) {
  145. unsigned int new_capacity;
  146. char *new_buf;
  147. while (this->len + len > capacity) {
  148. new_capacity = capacity == 0 ? 512 : capacity * 2;
  149. new_buf = (char *)realloc(buf, new_capacity);
  150. if (new_buf == NULL)
  151. return false;
  152. capacity = new_capacity;
  153. buf = new_buf;
  154. }
  155. memmove(buf + index + len, buf + index, this->len - index);
  156. memcpy(buf + index, data, len);
  157. this->len += len;
  158. return true;
  159. }
  160. bool Buffer::WriteAtUInt32(unsigned int index, uint32_t data) {
  161. char buf[sizeof(data)];
  162. memcpy(buf, &data, sizeof(buf));
  163. return WriteAt(index, buf, sizeof(buf));
  164. }