XBee and XBee-PRO ZigBee RF modules provide cost-effective wireless connectivity to electronic devices. They are interoperable with other ZigBee PRO feature set devices, including devices from other vendors.

Dependencies:   BufferedArray

Dependents:   MBEDminiproject

Core/BufferedArray.cpp

Committer:
yangcq88517
Date:
2015-10-22
Revision:
0:837e6c48e90d
Child:
1:3dc0ec2f9fd6

File content as of revision 0:837e6c48e90d:

#include "BufferedArray.h"

BufferedArray::BufferedArray()
{
    max = EXPANDSIZE;
    data = new char[EXPANDSIZE];
    index = 0;
}

BufferedArray::BufferedArray(int size)
{
    max = size;
    data = new char[size];
    index = 0;
}

BufferedArray::BufferedArray(BufferedArray * bufferedArray)
{
    if (bufferedArray != NULL) {
        this->data = bufferedArray->data;
        this->index = bufferedArray->index;
        this->max = bufferedArray->max;
    }
}

BufferedArray::~BufferedArray()
{
    if (data == NULL)
        return;

    delete[] data;
}

char * BufferedArray::gets()
{
    return data;
}

char * BufferedArray::gets(int position)
{
    return data + position;
}

char BufferedArray::get(int position)
{
    return *(data + position);
}

int BufferedArray::getPosition()
{
    return index;
}

void BufferedArray::setPosition(int position)
{
    if (this->index > max)
        this->index = max;
    else this->index = position;
}

void BufferedArray::allocate(int length)
{
    if (length <= 0)
        return;

    if (length > max) {
        delete[] data;
        data = new char[length];
    }

    rewind();
}

void BufferedArray::rewind()
{
    index = 0;
}

void BufferedArray::expandSpace(int length)
{
    max += EXPANDSIZE * (1 + length / EXPANDSIZE);
    char * temp = new char[max];
    memcpy(temp, data, index);
    delete[] data;
    data = temp;
}

void BufferedArray::set(int position, char value)
{
    if (position < 0)
        return;

    if (position >= max)
        expandSpace(1);

    data[position] = value;
}

void BufferedArray::set(char value)
{
    set(index, value);
    index++;
}

void BufferedArray::sets(const char * value, int offset, int length)
{
    if (length <= 0)
        return;

    if (offset < 0)
        return;

    sets(index, value, offset, length);
    index += length;
}

void BufferedArray::sets(int position, const char * value, int offset, int length)
{
    if (position < 0)
        return;

    if (length <= 0)
        return;

    if (offset < 0)
        return;

    if (position + length - offset > max)
        expandSpace(position + length - offset - max);

    memcpy(data + position, value + offset, length);
}