binary_buf.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * binary_buf是二进制缓存
  3. * 这里用字符串,来存储雷达的通信消息的原始数据
  4. * Binary_buf 的内容格式:消息类型 + 消息数据
  5. *
  6. * 例如思科的雷达的消息类型
  7. * ready->ready->start->data->data->data->stop->ready->ready
  8. *
  9. * 提供了 is_equal 系列的函数,来进行判断前面的消息类型
  10. *
  11. * 注意了:m_buf是中间可以允许有‘\0’的,不是单纯的字符串格式
  12. * 末尾也不一定是‘\0’
  13. */
  14. #include "binary_buf.h"
  15. #include <string>
  16. #include <string.h>
  17. Binary_buf::Binary_buf() {
  18. mp_buf = NULL;
  19. m_length = 0;
  20. }
  21. Binary_buf::Binary_buf(const Binary_buf &other) {
  22. mp_buf = NULL;
  23. m_length = 0;
  24. if (other.m_length > 0 && other.mp_buf != NULL) {
  25. mp_buf = (char *) malloc(other.m_length);
  26. memcpy(mp_buf, other.mp_buf, other.m_length);
  27. m_length = other.m_length;
  28. }
  29. }
  30. Binary_buf::~Binary_buf() {
  31. if (mp_buf) {
  32. free(mp_buf);
  33. mp_buf = NULL;
  34. }
  35. m_length = 0;
  36. // std::cout << "Binary_buf::~Binary_buf()" << std::endl;
  37. }
  38. //使用参数构造,深拷贝,len为0时,使用strlen(buf),不存储结束符'\0'
  39. Binary_buf::Binary_buf(const char *p_buf, int len) {
  40. mp_buf = NULL;
  41. m_length = 0;
  42. if (p_buf != NULL) {
  43. if (len <= 0) {
  44. len = strlen(p_buf);
  45. }
  46. mp_buf = (char *) malloc(len);
  47. memcpy(mp_buf, p_buf, len);
  48. m_length = len;
  49. }
  50. }
  51. //使用参数构造,深拷贝,len为0时,使用strlen(buf),不存储结束符'\0'
  52. Binary_buf::Binary_buf(char *p_buf, int len) {
  53. mp_buf = NULL;
  54. m_length = 0;
  55. if (p_buf != NULL) {
  56. if (len <= 0) {
  57. len = strlen(p_buf);
  58. }
  59. mp_buf = (char *) malloc(len);
  60. memcpy(mp_buf, p_buf, len);
  61. m_length = len;
  62. }
  63. }
  64. //重载=,深拷贝,
  65. Binary_buf &Binary_buf::operator=(const Binary_buf &other) {
  66. clear();
  67. if (other.m_length > 0 && other.mp_buf != NULL) {
  68. mp_buf = (char *) malloc(other.m_length);
  69. memcpy(mp_buf, other.mp_buf, other.m_length);
  70. m_length = other.m_length;
  71. }
  72. return *this;
  73. }
  74. //重载=,深拷贝,使用strlen(buf),不存储结束符'\0'
  75. Binary_buf &Binary_buf::operator=(const char *p_buf) {
  76. clear();
  77. if (p_buf != NULL) {
  78. int len = strlen(p_buf);
  79. mp_buf = (char *) malloc(len);
  80. memcpy(mp_buf, p_buf, len);
  81. m_length = len;
  82. }
  83. return *this;
  84. }
  85. //重载+,other追加在this的后面,
  86. Binary_buf &Binary_buf::operator+(Binary_buf &other) {
  87. if (other.mp_buf != NULL && other.m_length > 0) {
  88. int t_length_total = m_length + other.m_length;
  89. char *tp_buf_total = (char *) malloc(t_length_total);
  90. memcpy(tp_buf_total, mp_buf, m_length);
  91. memcpy(tp_buf_total + m_length, other.mp_buf, other.m_length);
  92. free(mp_buf);
  93. mp_buf = tp_buf_total;
  94. m_length = t_length_total;
  95. }
  96. return *this;
  97. }
  98. //重载+,追加在this的后面,使用strlen(buf),不存储结束符'\0'
  99. Binary_buf &Binary_buf::operator+(const char *p_buf) {
  100. if (p_buf != NULL) {
  101. int t_length_back = strlen(p_buf);
  102. int t_length_total = m_length + t_length_back;
  103. char *tp_buf_total = (char *) malloc(t_length_total);
  104. memcpy(tp_buf_total, mp_buf, m_length);
  105. memcpy(tp_buf_total + m_length, p_buf, t_length_back);
  106. free(mp_buf);
  107. mp_buf = tp_buf_total;
  108. m_length = t_length_total;
  109. }
  110. return *this;
  111. }
  112. //重载[],允许直接使用数组的形式,直接访问buf的内存。注意,n值必须在0~m_length之间,
  113. char &Binary_buf::operator[](int n) {
  114. if (n >= 0 && n < m_length) {
  115. return mp_buf[n];
  116. } else {
  117. throw (n);
  118. }
  119. }
  120. //判空
  121. bool Binary_buf::is_empty() {
  122. if (mp_buf != NULL && m_length > 0) {
  123. return false;
  124. } else {
  125. return true;
  126. }
  127. }
  128. //清空
  129. void Binary_buf::clear() {
  130. if (mp_buf) {
  131. free(mp_buf);
  132. mp_buf = NULL;
  133. }
  134. m_length = 0;
  135. }
  136. //比较前面部分的buf是否相等,使用 other.m_length 为标准
  137. bool Binary_buf::is_equal_front(const Binary_buf &other) {
  138. if (other.mp_buf == NULL || other.m_length <= 0) {
  139. if (mp_buf == NULL || m_length <= 0) {
  140. return true;
  141. } else {
  142. return false;
  143. }
  144. } else {
  145. if (mp_buf != NULL && m_length > 0) {
  146. if (other.m_length > m_length) {
  147. return false;
  148. }
  149. return (strncmp((const char *) mp_buf, other.mp_buf, other.m_length) == 0);
  150. } else {
  151. return false;
  152. }
  153. }
  154. }
  155. //比较前面部分的buf是否相等,len为0时,使用strlen(buf)为标准,不比较结束符'\0'
  156. bool Binary_buf::is_equal_front(const char *p_buf, int len) {
  157. if (p_buf == NULL) {
  158. if (mp_buf == NULL || m_length <= 0) {
  159. return true;
  160. } else {
  161. return false;
  162. }
  163. } else {
  164. if (mp_buf != NULL && m_length > 0) {
  165. if (len == 0) {
  166. len = strlen(p_buf);
  167. }
  168. if (len > m_length) {
  169. return false;
  170. }
  171. return (strncmp((const char *) mp_buf, p_buf, len) == 0);
  172. } else {
  173. return false;
  174. }
  175. }
  176. }
  177. //比较的buf是否全部相等,
  178. bool Binary_buf::is_equal_all(const Binary_buf &other) {
  179. if (other.mp_buf == NULL || other.m_length <= 0) {
  180. if (mp_buf == NULL || m_length <= 0) {
  181. return true;
  182. } else {
  183. return false;
  184. }
  185. } else {
  186. if (mp_buf != NULL && m_length > 0) {
  187. if (other.m_length != m_length) {
  188. return false;
  189. }
  190. return (strncmp((const char *) mp_buf, other.mp_buf, other.m_length) == 0);
  191. } else {
  192. return false;
  193. }
  194. }
  195. }
  196. //比较的buf是否全部相等,不比较结束符'\0'
  197. bool Binary_buf::is_equal_all(const char *p_buf) {
  198. if (p_buf == NULL) {
  199. if (mp_buf == NULL || m_length <= 0) {
  200. return true;
  201. } else {
  202. return false;
  203. }
  204. } else {
  205. if (mp_buf != NULL && m_length > 0) {
  206. int len = strlen(p_buf);
  207. if (len != m_length) {
  208. return false;
  209. }
  210. return (strncmp((const char *) mp_buf, p_buf, len) == 0);
  211. } else {
  212. return false;
  213. }
  214. }
  215. }
  216. char *Binary_buf::get_buf() const {
  217. return mp_buf;
  218. }
  219. int Binary_buf::get_length() const {
  220. return m_length;
  221. }