字符串操作类,主要用在808协议数据的处理方面

808协议数据处理类

直接上代码,头文件如下:
/* 
 * File:   ByteBuffer.h
 * Author: shaozg
 *
 * Created on 2013年11月19日, 上午10:04
 */

#ifndef BYTEBUFFER_H
#define	BYTEBUFFER_H

class ByteBuffer {
public:
    ByteBuffer();
    ByteBuffer(const ByteBuffer& orig);
    virtual ~ByteBuffer();
    
    void appendChar(const char ch);
    char getChar(const int index);
    
    void appendShort(const short sh);
    short getShort(const int index);
    
    void appendInt(const int i);
    int getInt(const int index);
    
    void appendStr(const char * str, const int count);
    char * subBytes(const int index, const int count);

    void insertFirstChar(const char ch);
    
    void reduce();
    void escapse();
private:
    char *m_data;
    int m_size;
};

#endif	/* BYTEBUFFER_H */

源文件内容如下:
/* 
 * File:   ByteBuffer.cpp
 * Author: shaozg
 * 
 * Created on 2013年11月19日, 上午10:04
 */


#include "ByteBuffer.h"


#include <string.h>
#include <stdio.h>
#include <stdlib.h>


#define DELETE_BUFFER(poi) if (poi != NULL) { \
                        delete poi; \
                        poi = NULL; \
                        }


ByteBuffer::ByteBuffer() {
    m_data = NULL;
    m_size = 0;
}


ByteBuffer::ByteBuffer(const ByteBuffer& orig) {
    if (orig.m_data != NULL && strlen(orig.m_data) > 0) {
        const int len = strlen(orig.m_data);
        m_size = len;
        m_data = (char *)malloc(len);
        memset(m_data, 0, len);
        memcpy(m_data, orig.m_data, len);
    }
}


ByteBuffer::~ByteBuffer() {
    fprintf(stdout, "Destructor");
    DELETE_BUFFER(m_data);
}


void ByteBuffer::appendChar(const char ch) {
    if (m_data == NULL) {
        m_data = (char *)malloc(1);
        *m_data = ch;
    } else {
        char *p = (char *)malloc(strlen(m_data) + 1);
        memcpy(p, m_data, strlen(m_data));
        p[strlen(m_data)] = ch;
        DELETE_BUFFER(m_data);
        m_data = p;
    }
    m_size += 1;
}


char ByteBuffer::getChar(const int index) {
    char ch = 0;
    if (m_data == NULL || m_size < index) {
        fprintf(stderr, "error index.");
        ch = 0;
    } else {
        ch = m_data[index];
    } 
    return ch;
}


void ByteBuffer::appendShort(const short sh) {
    if (m_data == NULL) {
        m_data = (char *)malloc(sizeof(short));
        m_data[0] = sh >> 8;
        m_data[1] = sh & 0xff;
    } else {
        const int len = m_size;
        char *p = (char *)malloc(len + 2);
        memcpy(p, m_data, len);
        p[len] = sh >> 8;
        p[len + 1] = sh & 0xff;
        DELETE_BUFFER(m_data);
        m_data = p;
    }
    m_size += 2;
}


short ByteBuffer::getShort(const int index) {
    short sh = 0;
    if (m_data == NULL || m_size < index + 1) {
        fprintf(stderr, "error index.");
        sh = 0;
    } else {
        sh = getChar(index) & 0xff;
        sh <<= 8;
        sh |= getChar(index + 1) & 0xff;
    }
    return sh;
}


void ByteBuffer::appendInt(const int i) {
     if (m_data == NULL) {
        m_data = (char *)malloc(sizeof(int));
        m_data[0] = i >> 24;
        m_data[1] = (i >> 16) & 0xff;
        m_data[2] = (i >> 8) & 0xff;
        m_data[3] = i & 0xff;
    } else {
        const int len = m_size;
        char *p = (char *)malloc(len + 4);
        memcpy(p, m_data, len);
        p[len] = i >> 24;
        p[len + 1] = (i >> 16) & 0xff;
        p[len + 2] = (i >> 8) & 0xff;
        p[len + 3] = i & 0xff;
        DELETE_BUFFER(m_data);
        m_data = p;
    }
     m_size += 4;
}


int ByteBuffer::getInt(const int index) {
    int i = 0;
    if (m_data == NULL || m_size < index + 1) {
        fprintf(stderr, "error index.");
        i = 0;
    } else {
        i = getChar(index) & 0xff;
        i <<= 8;
        i |= getChar(index + 1) & 0xff;
        i <<= 8;
        i |= getChar(index + 2) & 0xff;
        i <<= 8;
        i |= getChar(index + 3) & 0xff;
    }
    return i;
}
    
void ByteBuffer::appendStr(const char * str, const int count) {
    if (str != NULL && count > 0) {
        char *p = (char *)malloc(m_size + count);
        memcpy(p, m_data, m_size);
        memcpy(&p[m_size], str, count);
        DELETE_BUFFER(m_data);
        m_data = p;
        m_size += count;
    }
}


char * ByteBuffer::subBytes(const int index, const int count) {
    char *p = (char *)malloc(count);
    bcopy(&m_data[index], p, count);
    return p;
}
    
void ByteBuffer::insertFirstChar(const char ch) {
    if (m_size == 0) {
        appendChar(ch);
    } else {
        char *p = (char *)malloc(m_size + 1);
        
        bcopy(p, m_data, m_size);
        *(p + m_size) = ch;
        m_size += 1;
        DELETE_BUFFER(m_data);
        m_data = p;
    }
}


// replace 0x7d 0x01 with 0x7d and 0x0d 0x02 with 0x7e
void ByteBuffer::reduce() {
    if (m_size == 0) {
        return ;
    }
    
    char *p = (char *)malloc(m_size);
    int data_len = 0;
    char *q = p;
    for (int i = 0; i < m_size - 1; i ++) {
        if (getChar(i) == 0x7d && getChar(i + 1) == 0x01) {
            *q ++ = 0x7d;
        } else if (getChar(i) == 0x7d && getChar(i + 1) == 0x02) {
            *q ++ = 0x7e;
        } else {
            *q ++ = getChar(i);
        }
        data_len ++;
    }
    DELETE_BUFFER(m_data);
    m_data = (char *)malloc(data_len);
    memcpy(m_data, p, data_len);
    m_size = data_len;
    DELETE_BUFFER(p);
}


// loop every character, replace 0x7d with 0x7d 0x01 and 0x7e with 0x7d 0x02
void ByteBuffer::escapse() {
    if (m_size == 0) {
        return ;
    }
    
    char * p = (char *)malloc(m_size * 2);
    int data_len = 0;
    memset(p, 0, m_size * 2);
    char * q = p;
    for (int i = 0; i < m_size; i ++) {
        if (getChar(i) == 0x7d) {
            *q ++ = 0x7d;
            *q ++ = 0x01;
            data_len += 2;
        } else if (getChar(i) == 0x7e) {
            *q ++ = 0x7d;
            *q ++ = 0x02;
            data_len += 2;
        } else {
            *q ++ = getChar(i);
            data_len += 1;
        }
    }
    
    DELETE_BUFFER(m_data);
    m_data = (char *)malloc(data_len);
    memcpy(m_data, p, data_len);
    DELETE_BUFFER(p);
    m_size = data_len;
}

测试函数如下:
/* 
 * File:   main.cpp
 * Author: dev
 *
 * Created on 2013年11月19日, 上午10:03
 */

#include <cstdlib>
#include <stdio.h>
#include "ByteBuffer.h"

using namespace std;

/*
 * 
 */
int main(int argc, char** argv) {
    ByteBuffer b;
    b.appendChar('a');
    b.appendShort(12);
    b.appendInt(456);
    b.appendStr("hello", 5);
    
    int index = 0;
    char ch = b.getChar(index);
    index += 1;
    short sh = b.getShort(index);
    index += 2;
    int i = b.getInt(index);
    index += 4;
    char * str = b.subBytes(index, 5);
    
    printf("ch = %c\nsh = %d\ni = %d\nstr = ", ch, sh, i);
    for (int i = 0; i < 5; i ++) {
        printf("%c", str[i]);
    }
    printf("\n");
    free(str);
    return 0;
}

测试结果如下:


注意:reduce和escape这两个函数没有测试!
谢谢大家!!
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值