unsigned char array

Dependents:   MGC3130 SmartLabXBeeCore

Committer:
yangcq88517
Date:
Thu Nov 12 02:41:54 2015 +0000
Revision:
2:765da30c4d9b
Parent:
1:77c1ea04eb5a
bug fix

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yangcq88517 0:b35da77c40ca 1 #include "BufferedArray.h"
yangcq88517 0:b35da77c40ca 2
yangcq88517 2:765da30c4d9b 3 BufferedArray::BufferedArray(unsigned int initialLength, unsigned int expandSize)
yangcq88517 0:b35da77c40ca 4 {
yangcq88517 0:b35da77c40ca 5 this->expandSize = expandSize;
yangcq88517 0:b35da77c40ca 6 max = initialLength;
yangcq88517 0:b35da77c40ca 7 data = new unsigned char[initialLength];
yangcq88517 0:b35da77c40ca 8 index = 0;
yangcq88517 0:b35da77c40ca 9 }
yangcq88517 0:b35da77c40ca 10
yangcq88517 0:b35da77c40ca 11 BufferedArray::BufferedArray(BufferedArray * bufferedArray)
yangcq88517 0:b35da77c40ca 12 {
yangcq88517 0:b35da77c40ca 13 if (bufferedArray != NULL) {
yangcq88517 0:b35da77c40ca 14 this->data = bufferedArray->data;
yangcq88517 0:b35da77c40ca 15 this->index = bufferedArray->index;
yangcq88517 0:b35da77c40ca 16 this->max = bufferedArray->max;
yangcq88517 0:b35da77c40ca 17 this->expandSize = bufferedArray->expandSize;
yangcq88517 0:b35da77c40ca 18 }
yangcq88517 0:b35da77c40ca 19 }
yangcq88517 0:b35da77c40ca 20
yangcq88517 0:b35da77c40ca 21 BufferedArray::~BufferedArray()
yangcq88517 0:b35da77c40ca 22 {
yangcq88517 0:b35da77c40ca 23 if (data == NULL)
yangcq88517 0:b35da77c40ca 24 return;
yangcq88517 0:b35da77c40ca 25
yangcq88517 0:b35da77c40ca 26 delete[] data;
yangcq88517 0:b35da77c40ca 27 }
yangcq88517 0:b35da77c40ca 28
yangcq88517 0:b35da77c40ca 29 unsigned char * BufferedArray::gets()
yangcq88517 0:b35da77c40ca 30 {
yangcq88517 0:b35da77c40ca 31 return data;
yangcq88517 0:b35da77c40ca 32 }
yangcq88517 0:b35da77c40ca 33
yangcq88517 2:765da30c4d9b 34 unsigned char * BufferedArray::gets(unsigned long position)
yangcq88517 0:b35da77c40ca 35 {
yangcq88517 0:b35da77c40ca 36 return data + position;
yangcq88517 0:b35da77c40ca 37 }
yangcq88517 0:b35da77c40ca 38
yangcq88517 2:765da30c4d9b 39 unsigned char BufferedArray::get(unsigned long position)
yangcq88517 0:b35da77c40ca 40 {
yangcq88517 0:b35da77c40ca 41 return *(data + position);
yangcq88517 0:b35da77c40ca 42 }
yangcq88517 0:b35da77c40ca 43
yangcq88517 0:b35da77c40ca 44 int BufferedArray::getPosition()
yangcq88517 0:b35da77c40ca 45 {
yangcq88517 0:b35da77c40ca 46 return index;
yangcq88517 0:b35da77c40ca 47 }
yangcq88517 0:b35da77c40ca 48
yangcq88517 2:765da30c4d9b 49 void BufferedArray::setPosition(unsigned long position)
yangcq88517 0:b35da77c40ca 50 {
yangcq88517 0:b35da77c40ca 51 if (this->index > max)
yangcq88517 0:b35da77c40ca 52 this->index = max;
yangcq88517 0:b35da77c40ca 53 else this->index = position;
yangcq88517 0:b35da77c40ca 54 }
yangcq88517 0:b35da77c40ca 55
yangcq88517 2:765da30c4d9b 56 void BufferedArray::allocate(unsigned long length)
yangcq88517 0:b35da77c40ca 57 {
yangcq88517 0:b35da77c40ca 58 if (length <= 0)
yangcq88517 0:b35da77c40ca 59 return;
yangcq88517 0:b35da77c40ca 60
yangcq88517 0:b35da77c40ca 61 if (length > max) {
yangcq88517 0:b35da77c40ca 62 if (data != NULL)
yangcq88517 0:b35da77c40ca 63 delete[] data;
yangcq88517 0:b35da77c40ca 64 data = new unsigned char[length];
yangcq88517 0:b35da77c40ca 65 }
yangcq88517 0:b35da77c40ca 66
yangcq88517 0:b35da77c40ca 67 rewind();
yangcq88517 0:b35da77c40ca 68 }
yangcq88517 0:b35da77c40ca 69
yangcq88517 0:b35da77c40ca 70 void BufferedArray::rewind()
yangcq88517 0:b35da77c40ca 71 {
yangcq88517 0:b35da77c40ca 72 index = 0;
yangcq88517 0:b35da77c40ca 73 }
yangcq88517 0:b35da77c40ca 74
yangcq88517 2:765da30c4d9b 75 void BufferedArray::expandSpace(unsigned long length)
yangcq88517 0:b35da77c40ca 76 {
yangcq88517 0:b35da77c40ca 77 max += expandSize * (1 + length / expandSize);
yangcq88517 0:b35da77c40ca 78 unsigned char * temp = new unsigned char[max];
yangcq88517 0:b35da77c40ca 79 memcpy(temp, data, index);
yangcq88517 0:b35da77c40ca 80 delete[] data;
yangcq88517 0:b35da77c40ca 81 data = temp;
yangcq88517 0:b35da77c40ca 82 }
yangcq88517 0:b35da77c40ca 83
yangcq88517 2:765da30c4d9b 84 void BufferedArray::set(unsigned long position, unsigned char value)
yangcq88517 0:b35da77c40ca 85 {
yangcq88517 0:b35da77c40ca 86 if (position >= max)
yangcq88517 1:77c1ea04eb5a 87 expandSpace(position - max + 1);
yangcq88517 0:b35da77c40ca 88
yangcq88517 0:b35da77c40ca 89 data[position] = value;
yangcq88517 0:b35da77c40ca 90 }
yangcq88517 0:b35da77c40ca 91
yangcq88517 0:b35da77c40ca 92 void BufferedArray::set(unsigned char value)
yangcq88517 0:b35da77c40ca 93 {
yangcq88517 0:b35da77c40ca 94 set(index, value);
yangcq88517 0:b35da77c40ca 95 index++;
yangcq88517 0:b35da77c40ca 96 }
yangcq88517 0:b35da77c40ca 97
yangcq88517 2:765da30c4d9b 98 void BufferedArray::sets(const unsigned char * value, unsigned long offset, unsigned long length)
yangcq88517 0:b35da77c40ca 99 {
yangcq88517 0:b35da77c40ca 100 if (length <= 0)
yangcq88517 0:b35da77c40ca 101 return;
yangcq88517 0:b35da77c40ca 102
yangcq88517 1:77c1ea04eb5a 103 sets(index, value, offset, length);
yangcq88517 0:b35da77c40ca 104 index += length;
yangcq88517 0:b35da77c40ca 105 }
yangcq88517 0:b35da77c40ca 106
yangcq88517 2:765da30c4d9b 107 void BufferedArray::sets(unsigned long position, const unsigned char * value, unsigned long offset, unsigned long length)
yangcq88517 0:b35da77c40ca 108 {
yangcq88517 0:b35da77c40ca 109 if (length <= 0)
yangcq88517 0:b35da77c40ca 110 return;
yangcq88517 0:b35da77c40ca 111
yangcq88517 0:b35da77c40ca 112 if (position + length > max)
yangcq88517 0:b35da77c40ca 113 expandSpace(position + length - max);
yangcq88517 0:b35da77c40ca 114
yangcq88517 1:77c1ea04eb5a 115 memcpy(data + position, value, length);
yangcq88517 0:b35da77c40ca 116 }