#include <cstring>
using namespace std;
#include "StrArr.h"
void StrArr::Purge(){
for(unsigned int i=0;i<_length;i++){
if(_line[i]){
delete[] _line[i];
}
}
delete[] _line;
}
StrArr::StrArr(unsigned int length, unsigned int strLen){
_length = 0;
_lastIndex = 0;
_strLen = strLen;
Expand(length);
}
StrArr::StrArr(const StrArr& D){
if(&D != this){
unsigned int i;
_length = 0;
Expand(D.Length());
for(i=0;i<_length;i++){
if(D.Line(i)){
_line[i] = new char[_strLen];
strcpy(_line[i], D.Line(i));
}
}
_lastIndex = D._lastIndex;
}
}
StrArr& StrArr::operator=(const StrArr& D){
if(&D != this){
unsigned int i;
unsigned int Tlength = D.Length();
if(Tlength > _length){
Expand(Tlength - _length);
}
for(i=0;i<Tlength;i++){
if(D.Line(i)){
if(!this->Line(i)){
_line[i] = new char[_strLen];
}
strcpy(_line[i], D.Line(i));
}
}
for(;i<this->Length();i++){
if(this->Line(i)){
delete[] _line[i];
_line[i] = (char*)0;
}
}
_lastIndex = D._lastIndex;
}
return *this;
}
char* StrArr::operator[](unsigned int index){
if(index >= _length){
Expand(_FS_EXPAND_SIZE_);
}
if(!_line[index]){
_line[index] = new char[_strLen];
_line[index][0] = 0;
}
if(index > _lastIndex){
_lastIndex = index;
}
return _line[index];
}
const char* StrArr::Line(unsigned int index)const{
return _line[index % _length];
}
StrArr& StrArr::Expand(unsigned int num){
char** temp = new char*[_length + num];
unsigned int i;
for(i=0;i<_length;i++){
temp[i] = _line[i];
}
for(;i<_length+ num;i++){
temp[i] = (char*)0;
}
if(_length) delete[] _line;
_line = temp;
_length+=num;
return *this;
}
StrArr::~StrArr(){
Purge();
}
unsigned int StrArr::Size()const{
unsigned int _size = 0;
for(unsigned int i =0;i<_length;i++){
if(Line(i)){
_size += (strlen(Line(i))+1);
}
}
return _size;
}
unsigned int StrArr::Length()const{
return _length;
}
unsigned int StrArr::LastIndex()const{
return _lastIndex;
}
void StrArr::Insert(unsigned int index){
index = index % _length;
if(_lastIndex == _length - 1){
Expand(_FS_EXPAND_SIZE_);
}
for(unsigned int i = _lastIndex ; i>=index;i--){
_line[i+1] = _line[i];
}
_lastIndex++;
_line[index] = (char*)0;
}
void StrArr::Delete(unsigned int index){
if(_lastIndex){
index = index % _length;
if(_line[index]){
delete[] _line[index];
}
unsigned int i;
for(i=index; i<_lastIndex;i++){
_line[i] = _line[i+1];
}
_line[i] = (char*)0;
_lastIndex--;
}
}