1 Star 0 Fork 3

jinzhu6/agv_dispatch

forked from ros-org/agv_dispatch 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
qyhbuffer.cpp 2.78 KB
一键复制 编辑 原始数据 按行查看 历史
qintxwd 提交于 2018-10-18 20:52 . 新的电梯协议实现
#include "qyhbuffer.h"
#include <memory.h>
QyhBuffer::QyhBuffer(int size)
{
buf.reserve(size);
clear();
}
QyhBuffer::QyhBuffer(const QyhBuffer &b)
{
buf = b.buf;
}
QyhBuffer::QyhBuffer(const char *data,int len)
{
if (data == NULL) {
buf.reserve(len);
clear();
} else { // Consume the provided array
buf.reserve(len);
clear();
append(data, len);
}
}
QyhBuffer::QyhBuffer(const std::vector<char> &b)
{
buf = b;
}
std::unique_ptr<QyhBuffer> QyhBuffer::clone()
{
std::unique_ptr<QyhBuffer> ret = std::make_unique<QyhBuffer>(buf);
return ret;
}
uint32_t QyhBuffer::size()
{
return length();
}
int QyhBuffer::indexof(char *key, int key_len, int start)
{
if (key == nullptr || key_len <= 0)return -1;
int ret = -1;
uint32_t len = buf.size();
for (uint32_t i = start; i < len; i++) {
if (buf[i] == key[0]) {
bool same = true;
for (int j = 1; j < key_len; ++j) {
if (buf[i + j] != key[j]) {
same = false;
}
}
if(same){
ret = (int)i;
break;
}
}
}
return ret;
}
int QyhBuffer::find(char key,int start)
{
int ret = -1;
uint32_t len = buf.size();
for (uint32_t i = start; i < len; i++) {
if (buf[i] == key) {
ret = (int) i;
break;
}
}
return ret;
}
std::string QyhBuffer::substr(int start, int len)
{
std::string ret;
if(start<size()){
if(len<0){
ret = std::string(data(start));
}else{
ret = std::string(data(start),len);
}
}
return ret;
}
int QyhBuffer::getInt32(int start) const
{
int ret;
if(start + sizeof(int32_t)>buf.size())return -1;
memcpy(&ret,&buf[start],sizeof(int32_t));
return ret;
// return *((int32_t*) &buf[start]);
}
const char *QyhBuffer::data(int start) const
{
if(empty()) return nullptr;
if(start>buf.size())return nullptr;
return &buf[start];
}
void QyhBuffer::append(const char *data,int len)
{
if(data == NULL || len == 0) return;
buf.resize(length() + len,0);
memcpy(&buf[0] + length() - len,data,len);
}
QyhBuffer &QyhBuffer::operator=(const QyhBuffer &other)
{
buf = other.buf;
return *this;
}
QyhBuffer &QyhBuffer::operator+=(const QyhBuffer &other)
{
if(!other.empty()){
buf.insert(buf.end(), other.buf.begin(), other.buf.end());
}
return *this;
}
bool QyhBuffer::operator == (const QyhBuffer &other)
{
return buf == other.buf;
}
void QyhBuffer::clear()
{
buf.clear();
}
void QyhBuffer::removeFront(int len)
{
if(len<=0)return ;
if(len > length())
{
clear();
}else{
buf.erase(buf.begin(),buf.begin()+len);
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/jinzhu6/agv_dispatch.git
git@gitee.com:jinzhu6/agv_dispatch.git
jinzhu6
agv_dispatch
agv_dispatch
master

搜索帮助