binary_buf.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. {
  19. mp_buf = NULL;
  20. m_length = 0;
  21. }
  22. Binary_buf::Binary_buf(const Binary_buf& other)
  23. {
  24. mp_buf = NULL;
  25. m_length = 0;
  26. if ( other.m_length > 0 && other.mp_buf != NULL)
  27. {
  28. mp_buf = (char*)malloc(other.m_length);
  29. memcpy(mp_buf, other.mp_buf, other.m_length);
  30. m_length = other.m_length;
  31. }
  32. }
  33. Binary_buf::~Binary_buf()
  34. {
  35. if ( mp_buf )
  36. {
  37. free(mp_buf);
  38. mp_buf = NULL;
  39. }
  40. m_length = 0;
  41. }
  42. //使用参数构造,深拷贝,len为0时,使用strlen(buf),不存储结束符'\0'
  43. Binary_buf::Binary_buf(const char* p_buf, int len)
  44. {
  45. mp_buf = NULL;
  46. m_length = 0;
  47. if ( p_buf != NULL)
  48. {
  49. if (len <= 0)
  50. {
  51. len = strlen(p_buf);
  52. }
  53. mp_buf = (char*)malloc(len);
  54. memcpy(mp_buf, p_buf, len);
  55. m_length = len;
  56. }
  57. }
  58. //重载=,深拷贝,
  59. Binary_buf& Binary_buf::operator=(const Binary_buf& other)
  60. {
  61. clear();
  62. if ( other.m_length > 0 && other.mp_buf != NULL)
  63. {
  64. mp_buf = (char*)malloc(other.m_length);
  65. memcpy(mp_buf, other.mp_buf, other.m_length);
  66. m_length = other.m_length;
  67. }
  68. return *this;
  69. }
  70. //重载=,深拷贝,使用strlen(buf),不存储结束符'\0'
  71. Binary_buf& Binary_buf::operator=(const char* p_buf)
  72. {
  73. clear();
  74. if ( p_buf != NULL)
  75. {
  76. int len = strlen(p_buf);
  77. mp_buf = (char*)malloc(len);
  78. memcpy(mp_buf, p_buf, len);
  79. m_length = len;
  80. }
  81. return *this;
  82. }
  83. //重载+,other追加在this的后面,
  84. Binary_buf& Binary_buf::operator+(Binary_buf& other)
  85. {
  86. if (other.mp_buf != NULL && other.m_length > 0)
  87. {
  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. {
  101. if (p_buf != NULL )
  102. {
  103. int t_length_back = strlen(p_buf);
  104. int t_length_total = m_length + t_length_back;
  105. char* tp_buf_total = (char*)malloc(t_length_total);
  106. memcpy(tp_buf_total, mp_buf, m_length);
  107. memcpy(tp_buf_total + m_length, p_buf, t_length_back);
  108. free(mp_buf);
  109. mp_buf = tp_buf_total;
  110. m_length = t_length_total;
  111. }
  112. return *this;
  113. }
  114. //重载[],允许直接使用数组的形式,直接访问buf的内存。注意,n值必须在0~m_length之间,
  115. char& Binary_buf::operator[](int n)
  116. {
  117. if (n >= 0 && n < m_length)
  118. {
  119. return mp_buf[n];
  120. }
  121. else
  122. {
  123. throw (n);
  124. }
  125. }
  126. //判空
  127. bool Binary_buf::is_empty()
  128. {
  129. if ( mp_buf != NULL && m_length > 0 )
  130. {
  131. return false;
  132. }
  133. else
  134. {
  135. return true;
  136. }
  137. }
  138. //清空
  139. void Binary_buf::clear()
  140. {
  141. if ( mp_buf )
  142. {
  143. free(mp_buf);
  144. mp_buf = NULL;
  145. }
  146. m_length = 0;
  147. }
  148. //比较前面部分的buf是否相等,使用 other.m_length 为标准
  149. bool Binary_buf::is_equal_front(const Binary_buf& other)
  150. {
  151. if ( other.mp_buf == NULL || other.m_length <= 0 )
  152. {
  153. if ( mp_buf == NULL || m_length <= 0 )
  154. {
  155. return true;
  156. }
  157. else
  158. {
  159. return false;
  160. }
  161. }
  162. else
  163. {
  164. if ( mp_buf != NULL && m_length > 0 )
  165. {
  166. if ( other.m_length > m_length )
  167. {
  168. return false;
  169. }
  170. return (strncmp((const char*)mp_buf, other.mp_buf, other.m_length) == 0);
  171. }
  172. else
  173. {
  174. return false;
  175. }
  176. }
  177. }
  178. //比较前面部分的buf是否相等,len为0时,使用strlen(buf)为标准,不比较结束符'\0'
  179. bool Binary_buf::is_equal_front(const char* p_buf, int len)
  180. {
  181. if ( p_buf == NULL )
  182. {
  183. if ( mp_buf == NULL || m_length <= 0 )
  184. {
  185. return true;
  186. }
  187. else
  188. {
  189. return false;
  190. }
  191. }
  192. else
  193. {
  194. if ( mp_buf != NULL && m_length > 0 )
  195. {
  196. if ( len == 0 )
  197. {
  198. len = strlen(p_buf);
  199. }
  200. if ( len > m_length )
  201. {
  202. return false;
  203. }
  204. return (strncmp((const char*)mp_buf, p_buf, len) == 0);
  205. }
  206. else
  207. {
  208. return false;
  209. }
  210. }
  211. }
  212. //比较的buf是否全部相等,
  213. bool Binary_buf::is_equal_all(const Binary_buf& other)
  214. {
  215. if ( other.mp_buf == NULL || other.m_length <= 0 )
  216. {
  217. if ( mp_buf == NULL || m_length <= 0 )
  218. {
  219. return true;
  220. }
  221. else
  222. {
  223. return false;
  224. }
  225. }
  226. else
  227. {
  228. if ( mp_buf != NULL && m_length > 0 )
  229. {
  230. if ( other.m_length != m_length )
  231. {
  232. return false;
  233. }
  234. return (strncmp((const char*)mp_buf, other.mp_buf, other.m_length) == 0);
  235. }
  236. else
  237. {
  238. return false;
  239. }
  240. }
  241. }
  242. //比较的buf是否全部相等,不比较结束符'\0'
  243. bool Binary_buf::is_equal_all(const char* p_buf)
  244. {
  245. if ( p_buf == NULL )
  246. {
  247. if ( mp_buf == NULL || m_length <= 0 )
  248. {
  249. return true;
  250. }
  251. else
  252. {
  253. return false;
  254. }
  255. }
  256. else
  257. {
  258. if ( mp_buf != NULL && m_length > 0 )
  259. {
  260. int len = strlen(p_buf);
  261. if ( len != m_length )
  262. {
  263. return false;
  264. }
  265. return (strncmp((const char*)mp_buf, p_buf, len) == 0);
  266. }
  267. else
  268. {
  269. return false;
  270. }
  271. }
  272. }
  273. char* Binary_buf::get_buf()const
  274. {
  275. return mp_buf;
  276. }
  277. int Binary_buf::get_length()const
  278. {
  279. return m_length;
  280. }