00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "precomp.h"
00030 #include "bytearray.h"
00031 #include <string.h>
00032
00034
00035
00036 CL_ByteArray::CL_ByteArray() : data(0), size(0), allocated_size(0)
00037 {
00038 }
00039
00040 CL_ByteArray::CL_ByteArray(const CL_ByteArray ©) : data(0), size(0), allocated_size(0)
00041 {
00042 set_size(copy.size);
00043 memcpy(data, copy.data, size);
00044 }
00045
00046 CL_ByteArray::CL_ByteArray(int size) : data(0), size(0), allocated_size(0)
00047 {
00048 set_size(size);
00049 }
00050
00051 CL_ByteArray::~CL_ByteArray()
00052 {
00053 delete[] data;
00054 }
00055
00057
00058
00059 char *CL_ByteArray::get_data()
00060 {
00061 return data;
00062 }
00063
00064 const char *CL_ByteArray::get_data() const
00065 {
00066 return data;
00067 }
00068
00069 int CL_ByteArray::get_size() const
00070 {
00071 return size;
00072 }
00073
00074 int CL_ByteArray::get_capacity() const
00075 {
00076 return allocated_size;
00077 }
00078
00079 char &CL_ByteArray::operator[](int i)
00080 {
00081 return data[i];
00082 }
00083
00084 const char &CL_ByteArray::operator[](int i) const
00085 {
00086 return data[i];
00087 }
00088
00089 char &CL_ByteArray::operator[](unsigned int i)
00090 {
00091 return data[i];
00092 }
00093
00094 const char &CL_ByteArray::operator[](unsigned int i) const
00095 {
00096 return data[i];
00097 }
00098
00100
00101
00102 CL_ByteArray &CL_ByteArray::operator =(const CL_ByteArray ©)
00103 {
00104 if (data != copy.data)
00105 {
00106 set_size(copy.size);
00107 memcpy(data, copy.data, size);
00108 }
00109 return *this;
00110 }
00111
00112 void CL_ByteArray::set_size(int new_size)
00113 {
00114 if (new_size > allocated_size)
00115 {
00116 char *old_data = data;
00117 data = new char[new_size];
00118 memcpy(data, old_data, size);
00119 delete[] old_data;
00120 memset(data+size, 0, new_size-size);
00121 size = new_size;
00122 allocated_size = new_size;
00123 }
00124 else if (new_size <= allocated_size)
00125 {
00126 size = new_size;
00127 }
00128 }
00129
00130 void CL_ByteArray::set_capacity(int new_capacity)
00131 {
00132 if (new_capacity > allocated_size)
00133 {
00134 char *old_data = data;
00135 data = new char[new_capacity];
00136 memcpy(data, old_data, size);
00137 delete[] old_data;
00138 memset(data+size, 0, new_capacity-size);
00139 allocated_size = new_capacity;
00140 }
00141 }
00142
00144