binary_buf.cpp 5.7 KB

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