Dynamic Array Of Strings - OOP344

From CDOT Wiki
Jump to: navigation, search

strarr.h

// A dynamic array of strings V0.9


#ifndef __FS_STRARR_H__
#define __FS_STRARR_H__


#ifndef _FS_INIT_ARR_SIZE_
# define _FS_INIT_ARR_SIZE_ (50u)
#endif
#ifndef _FS_EXPAND_SIZE_
#define _FS_EXPAND_SIZE_ (50u)
#endif
#ifndef _FS_MAX_CHARS_PER_STR
#define _FS_MAX_CHARS_PER_STR (1024u)
#endif


class StrArr{
  char** _line;
  unsigned int _length;
  unsigned int _lastIndex;
  unsigned int _strLen;
  void Purge();
  // Expands the string array by num strings
  StrArr& Expand(unsigned int num);
public:                 
  // Creates an array of Strings
  StrArr(unsigned int size=_FS_INIT_ARR_SIZE_,unsigned int strLen = _FS_MAX_CHARS_PER_STR );
  StrArr(const StrArr& D);
  StrArr& operator=(const StrArr& D);
  ~StrArr();
  char* operator[](unsigned int index);
  const char* Line(unsigned int index)const;

  unsigned int Size()const;
  unsigned int Length()const;
  unsigned int LastIndex()const;
  void Insert(unsigned int index);
  void Delete(unsigned int index);
};

#endif

strarr.cpp

#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--;
  }
}