// Common, group Buffers
// Copyright Alexander Liss
#ifndef __STRBUF_H__
#define __STRBUF_H__
// StrBuffer class provides memory management and
// methods of placing data into an array of bytes
// data is directly accessible - no protection agains misuse
// can be interpreted as a string if needed:
// data==0 or data[data_size]==0
// allocated >= data_size+1
class StrBuffer
{
public:
int data_size, allocated;
char * data;
// if initial_allocation==0 does not allocate memory
StrBuffer(int initial_allocation=0,int Increment=8);
// sets zero terminated string
StrBuffer(const char * string):
increment(8),data_size(0),allocated(0),data(0)
{set(string);}
// sets size bytes from "from"
StrBuffer(const char * from,int size):
increment(8),data_size(0),allocated(0),data(0)
{set(from,size);}
StrBuffer(const StrBuffer& s):
increment(s.increment),data_size(0),allocated(0),data(0)
{ if(s.data){allocate(s.allocated); copy(s);}}
~StrBuffer();
StrBuffer& operator=(const StrBuffer& s)
{ if(&s != this){if(!data) allocate(s.allocated); copy(s);}; return *this;}
// checks that "new_size" is sufficient to hold old data
int reallocate(int new_size);
// set functions override old data
int set(const StrBuffer& s){return set(s.data,s.data_size);}
int set(const char * string);
int set(const char * from,int size);
int set(int number, char symbol);
// append functions add data to old data
int append(const StrBuffer& s){return append(s.data,s.data_size);}
int append(const char * string);
int append(const char * from,int size);
int append(int number, char symbol);
// a byte data[n] becomes data[0], drops first n bytes
int shift(int n);
protected:
int increment; // allocation and realoocation is done in multiples if "increment"
int copy(const StrBuffer& s);
int allocate(int size);
};
#endif