datablock.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #include "datablock.h"
  2. #include <string.h>
  3. #include <inttypes.h>
  4. datablock::datablock(int len)
  5. {
  6. initialize(len);
  7. }
  8. datablock::datablock(datastring input)
  9. {
  10. initialize(input.length);
  11. if (input.length > 0) {
  12. memcpy(data,input.data,input.length);
  13. }
  14. }
  15. datablock::datablock(const char *input)
  16. {
  17. size_t len;
  18. if (input == nullptr) {
  19. initialize(0);
  20. } else {
  21. len = strlen(input);
  22. initialize(len);
  23. memcpy(data,input,len);
  24. data[len] = 0;
  25. }
  26. }
  27. datablock::datablock(const char *input,int len)
  28. {
  29. if ((input == nullptr) || (len <= 0)) {
  30. initialize(0);
  31. } else {
  32. initialize(len);
  33. memcpy(data,input,len);
  34. data[len] = 0;
  35. }
  36. }
  37. datablock::~datablock()
  38. {
  39. clear();
  40. }
  41. void datablock::initialize(int len)
  42. {
  43. capacity = len+1;
  44. data = new char[capacity];
  45. length = len;
  46. data[len] = 0;
  47. null_terminated = true;
  48. usage = 1;
  49. return;
  50. }
  51. void datablock::dispose()
  52. {
  53. delete this;
  54. }
  55. void datablock::clear()
  56. {
  57. if (data != nullptr) {
  58. delete data;
  59. data = nullptr;
  60. }
  61. length = 0;
  62. capacity = 0;
  63. return;
  64. }
  65. datablock *datablock::clone()
  66. {
  67. usage++;
  68. return this;
  69. }
  70. datablock &datablock::operator=(datastring input)
  71. {
  72. length = 0;
  73. return operator+=(input);
  74. }
  75. datablock &datablock::operator=(const char *input)
  76. {
  77. length = 0;
  78. return operator+=(input);
  79. }
  80. datablock &datablock::operator+=(datastring input)
  81. {
  82. append(input.data,input.length);
  83. return *this;
  84. }
  85. bool datablock::append(const char *input,int len)
  86. {
  87. char *temp;
  88. int new_length;
  89. int new_capacity;
  90. if (len > 0) {
  91. new_length = length + len;
  92. if (new_length >= capacity) {
  93. // Expand the string.
  94. new_capacity = (new_length << 1) + 1;
  95. temp = new char[new_capacity];
  96. if (temp == nullptr) {
  97. return false;
  98. }
  99. if (length > 0) {
  100. memcpy(temp,data,length);
  101. }
  102. if (input == nullptr) {
  103. memset(temp+length,' ',len);
  104. } else {
  105. memcpy(temp+length,input,len);
  106. }
  107. capacity = new_capacity;
  108. data = temp;
  109. } else {
  110. if (input == nullptr) {
  111. memset(data+length,' ',len);
  112. } else {
  113. memcpy(data+length,input,len);
  114. }
  115. }
  116. length = new_length;
  117. null_terminated = true;
  118. data[length] = 0;
  119. }
  120. return true;
  121. }
  122. bool datablock::alter(datastring &item,int offset)
  123. {
  124. // Alters the data block starting at offset.
  125. int numberofbytestoappend = item.length + offset - length;
  126. if (numberofbytestoappend > 0) {
  127. if (!append(nullptr,numberofbytestoappend)) {
  128. return false;
  129. }
  130. }
  131. memcpy(data+offset,item.data,item.length);
  132. return true;
  133. }
  134. datablock &datablock::operator+=(const char *input)
  135. {
  136. int len = (input == nullptr ? 0 : strlen(input));
  137. append(input,len);
  138. return *this;
  139. }
  140. datablock &datablock::operator=(int64_t input)
  141. {
  142. length = 0;
  143. return operator+=(input);
  144. }
  145. datablock &datablock::operator+=(int64_t input)
  146. {
  147. char buffer[50];
  148. int len;
  149. len = snprintf(buffer,50,"%" PRId64,input);
  150. append(buffer,len);
  151. return *this;
  152. }
  153. datablock &datablock::operator+=(datablock &input)
  154. {
  155. append(input.data,input.length);
  156. return *this;
  157. }
  158. int datablock::compare(datastring &item)
  159. {
  160. int cmp;
  161. int cmp_length = length < item.length ? length : item.length;
  162. if (cmp_length == 0) {
  163. cmp = 0;
  164. } else {
  165. cmp = memcmp(data,item.data,cmp_length);
  166. }
  167. if (cmp == 0) {
  168. if (length > item.length) {
  169. cmp = 1;
  170. } else if (length < item.length) {
  171. cmp = -1;
  172. }
  173. }
  174. return cmp;
  175. }
  176. int datablock::compare(datablock &item)
  177. {
  178. int cmp;
  179. int cmp_length = length < item.length ? length : item.length;
  180. if (cmp_length == 0) {
  181. cmp = 0;
  182. } else {
  183. cmp = memcmp(data,item.data,cmp_length);
  184. }
  185. if (cmp == 0) {
  186. if (length > item.length) {
  187. cmp = 1;
  188. } else if (length < item.length) {
  189. cmp = -1;
  190. }
  191. }
  192. return cmp;
  193. }