/* * binary_buf是二进制缓存 * 这里用字符串,来存储雷达的通信消息的原始数据 * Binary_buf 的内容格式:消息类型 + 消息数据 * * 例如思科的雷达的消息类型 * ready->ready->start->data->data->data->stop->ready->ready * * 提供了 is_equal 系列的函数,来进行判断前面的消息类型 * * 注意了:m_buf是中间可以允许有‘\0’的,不是单纯的字符串格式 * 末尾也不一定是‘\0’ */ #include "binary_buf.h" #include #include Binary_buf::Binary_buf() { mp_buf = NULL; m_length = 0; } Binary_buf::Binary_buf(const Binary_buf& other) { mp_buf = NULL; m_length = 0; if ( other.m_length > 0 && other.mp_buf != NULL) { mp_buf = (char*)malloc(other.m_length); memcpy(mp_buf, other.mp_buf, other.m_length); m_length = other.m_length; } } Binary_buf::~Binary_buf() { if ( mp_buf ) { free(mp_buf); mp_buf = NULL; } m_length = 0; // std::cout << "Binary_buf::~Binary_buf()" << std::endl; } //使用参数构造,深拷贝,len为0时,使用strlen(buf),不存储结束符'\0' Binary_buf::Binary_buf(const char* p_buf, int len) { mp_buf = NULL; m_length = 0; if ( p_buf != NULL) { if (len <= 0) { len = strlen(p_buf); } mp_buf = (char*)malloc(len); memcpy(mp_buf, p_buf, len); m_length = len; } } //使用参数构造,深拷贝,len为0时,使用strlen(buf),不存储结束符'\0' Binary_buf::Binary_buf(char* p_buf, int len) { mp_buf = NULL; m_length = 0; if ( p_buf != NULL) { if (len <= 0) { len = strlen(p_buf); } mp_buf = (char*)malloc(len); memcpy(mp_buf, p_buf, len); m_length = len; } } //重载=,深拷贝, Binary_buf& Binary_buf::operator=(const Binary_buf& other) { clear(); if ( other.m_length > 0 && other.mp_buf != NULL) { mp_buf = (char*)malloc(other.m_length); memcpy(mp_buf, other.mp_buf, other.m_length); m_length = other.m_length; } return *this; } //重载=,深拷贝,使用strlen(buf),不存储结束符'\0' Binary_buf& Binary_buf::operator=(const char* p_buf) { clear(); if ( p_buf != NULL) { int len = strlen(p_buf); mp_buf = (char*)malloc(len); memcpy(mp_buf, p_buf, len); m_length = len; } return *this; } //重载+,other追加在this的后面, Binary_buf& Binary_buf::operator+(Binary_buf& other) { if (other.mp_buf != NULL && other.m_length > 0) { int t_length_total = m_length + other.m_length; char* tp_buf_total = (char*)malloc(t_length_total); memcpy(tp_buf_total, mp_buf, m_length); memcpy(tp_buf_total + m_length, other.mp_buf, other.m_length); free(mp_buf); mp_buf = tp_buf_total; m_length = t_length_total; } return *this; } //重载+,追加在this的后面,使用strlen(buf),不存储结束符'\0' Binary_buf& Binary_buf::operator+(const char* p_buf) { if (p_buf != NULL ) { int t_length_back = strlen(p_buf); int t_length_total = m_length + t_length_back; char* tp_buf_total = (char*)malloc(t_length_total); memcpy(tp_buf_total, mp_buf, m_length); memcpy(tp_buf_total + m_length, p_buf, t_length_back); free(mp_buf); mp_buf = tp_buf_total; m_length = t_length_total; } return *this; } //重载[],允许直接使用数组的形式,直接访问buf的内存。注意,n值必须在0~m_length之间, char& Binary_buf::operator[](int n) { if (n >= 0 && n < m_length) { return mp_buf[n]; } else { throw (n); } } //判空 bool Binary_buf::is_empty() { if ( mp_buf != NULL && m_length > 0 ) { return false; } else { return true; } } //清空 void Binary_buf::clear() { if ( mp_buf ) { free(mp_buf); mp_buf = NULL; } m_length = 0; } //比较前面部分的buf是否相等,使用 other.m_length 为标准 bool Binary_buf::is_equal_front(const Binary_buf& other) { if ( other.mp_buf == NULL || other.m_length <= 0 ) { if ( mp_buf == NULL || m_length <= 0 ) { return true; } else { return false; } } else { if ( mp_buf != NULL && m_length > 0 ) { if ( other.m_length > m_length ) { return false; } return (strncmp((const char*)mp_buf, other.mp_buf, other.m_length) == 0); } else { return false; } } } //比较前面部分的buf是否相等,len为0时,使用strlen(buf)为标准,不比较结束符'\0' bool Binary_buf::is_equal_front(const char* p_buf, int len) { if ( p_buf == NULL ) { if ( mp_buf == NULL || m_length <= 0 ) { return true; } else { return false; } } else { if ( mp_buf != NULL && m_length > 0 ) { if ( len == 0 ) { len = strlen(p_buf); } if ( len > m_length ) { return false; } return (strncmp((const char*)mp_buf, p_buf, len) == 0); } else { return false; } } } //比较的buf是否全部相等, bool Binary_buf::is_equal_all(const Binary_buf& other) { if ( other.mp_buf == NULL || other.m_length <= 0 ) { if ( mp_buf == NULL || m_length <= 0 ) { return true; } else { return false; } } else { if ( mp_buf != NULL && m_length > 0 ) { if ( other.m_length != m_length ) { return false; } return (strncmp((const char*)mp_buf, other.mp_buf, other.m_length) == 0); } else { return false; } } } //比较的buf是否全部相等,不比较结束符'\0' bool Binary_buf::is_equal_all(const char* p_buf) { if ( p_buf == NULL ) { if ( mp_buf == NULL || m_length <= 0 ) { return true; } else { return false; } } else { if ( mp_buf != NULL && m_length > 0 ) { int len = strlen(p_buf); if ( len != m_length ) { return false; } return (strncmp((const char*)mp_buf, p_buf, len) == 0); } else { return false; } } } char* Binary_buf::get_buf()const { return mp_buf; } int Binary_buf::get_length()const { return m_length; }