sword::SWBuf Class Reference

This class is used as a transport and utility for data buffers. More...

#include <swbuf.h>

List of all members.

Public Member Functions

 SWBuf ()
 SWBuf (const char *initVal, unsigned long initSize=0)
 SWBuf Constructor - Creates an SWBuf initialized to a value from a const char *.
 SWBuf (char initVal, unsigned long initSize=0)
 SWBuf Constructor - Creates an SWBuf initialized to a value from a char.
 SWBuf (const SWBuf &other, unsigned long initSize=0)
 SWBuf Constructor - Creates an SWBuf initialized to a value from another SWBuf.
 ~SWBuf ()
void setFillByte (char ch)
 SWBuf::setFillByte - Set the fillByte character.
char getFillByte ()
 SWBuf::getFillByte - Get the fillByte character.
const char * c_str () const
 
Returns:
a pointer to the buffer content (null-terminated string)

char & charAt (unsigned long pos)
 
Parameters:
pos The position of the requested character.

unsigned long size () const
 
Returns:
size() and length() return only the number of characters of the string.

void size (unsigned long newSize)
 set's the size of the buffer.
unsigned long length () const
 
Returns:
size() and length() return only the number of characters of the string.

void set (const SWBuf &newVal)
 SWBuf::set - sets this buf to a new value If the allocated memory is bigger than the new string, it will NOT be resized.
void set (const char *newVal)
 SWBuf::set - sets this buf to a new value.
void setFormatted (const char *format,...)
 SWBuf::setFormatted - sets this buf to a formatted string.
void setSize (unsigned long len)
 SWBuf::setSize - Size this buffer to a specific length.
void resize (unsigned long len)
 SWBuf::resize - Resize this buffer to a specific length.
void append (const char *str, long max=-1)
 SWBuf::append - appends a value to the current value of this SWBuf.
void append (const SWBuf &str, long max=-1)
 SWBuf::append - appends a value to the current value of this SWBuf If the allocated memory is not enough, it will be resized accordingly.
void append (char ch)
 SWBuf::append - appends a value to the current value of this SWBuf If the allocated memory is not enough, it will be resized accordingly.
void appendFormatted (const char *format,...)
 SWBuf::appendFormatted - appends formatted strings to the current value of this SWBuf.
void insert (unsigned long pos, const char *str, unsigned long start=0, signed long max=-1)
 SWBuf::insert - inserts the given string at position into this string.
void insert (unsigned long pos, const SWBuf &str, unsigned long start=0, signed long max=-1)
 SWBuf::insert - inserts the given string at position into this string.
void insert (unsigned long pos, char c)
 SWBuf::insert - inserts the given character at position into this string.
char * getRawData ()
 SWBuf::getRawData.
 operator const char * () const
char & operator[] (unsigned long pos)
char & operator[] (long pos)
char & operator[] (unsigned int pos)
char & operator[] (int pos)
SWBufoperator= (const char *newVal)
SWBufoperator= (const SWBuf &other)
SWBufoperator+= (const char *str)
SWBufoperator+= (char ch)
SWBufoperator-= (unsigned long len)
SWBufoperator-- (int)
SWBufoperator<< (unsigned long n)
SWBufoperator>> (unsigned long n)
SWBuf operator+ (const SWBuf &other) const
SWBuf operator+ (char ch) const
SWBuftrimStart ()
SWBuftrimEnd ()
SWBuftrim ()
SWBufreplaceBytes (const char *targets, char newByte)
bool startsWith (const SWBuf &prefix) const
bool endsWith (const SWBuf &postfix) const
int compare (const SWBuf &other) const
bool operator== (const SWBuf &other) const
bool operator!= (const SWBuf &other) const
bool operator> (const SWBuf &other) const
bool operator< (const SWBuf &other) const
bool operator<= (const SWBuf &other) const
bool operator>= (const SWBuf &other) const
bool startsWith (const char *prefix) const
bool endsWith (const char *postfix) const
int compare (const char *other) const
bool operator== (const char *other) const
bool operator!= (const char *other) const
bool operator> (const char *other) const
bool operator< (const char *other) const
bool operator<= (const char *other) const
bool operator>= (const char *other) const

Private Member Functions

void assureMore (size_t pastEnd)
void assureSize (size_t checkSize)
void init (size_t initSize)

Private Attributes

char * buf
char * end
char * endAlloc
char fillByte
unsigned long allocSize

Static Private Attributes

static char * nullStr
static char junkBuf [8191]


Detailed Description

This class is used as a transport and utility for data buffers.

Warning:
This class does not perform pointer validity checks (for speed reasons). Therefore, never try to pass an invalid string (const char* 0) as an argument- it will crash your program. You need to perform the checks yourself!

Definition at line 44 of file swbuf.h.


Member Function Documentation

void sword::SWBuf::setFillByte ( char  ch  )  [inline]

SWBuf::setFillByte - Set the fillByte character.

Parameters:
ch This character is used when the SWBuf is (re)sized. The memory will be filled with this character.
See also:
setSize()

resize()

Definition at line 128 of file swbuf.h.

char sword::SWBuf::getFillByte (  )  [inline]

SWBuf::getFillByte - Get the fillByte character.

Returns:
The character used for filling memory.
See also:
setFillByte.

Definition at line 135 of file swbuf.h.

char& sword::SWBuf::charAt ( unsigned long  pos  )  [inline]

Parameters:
pos The position of the requested character.

Returns:
The character at the specified position

Definition at line 146 of file swbuf.h.

unsigned long sword::SWBuf::size (  )  const [inline]

Returns:
size() and length() return only the number of characters of the string.

Add one for the following null and one for each char to be appended!

Definition at line 152 of file swbuf.h.

Referenced by endsWith(), and startsWith().

void sword::SWBuf::size ( unsigned long  newSize  )  [inline]

set's the size of the buffer.

This is a quick inline method which checks for changes before actually calling setSize().

Parameters:
newSize new size of the buffer

Definition at line 159 of file swbuf.h.

void sword::SWBuf::set ( const SWBuf newVal  )  [inline]

SWBuf::set - sets this buf to a new value If the allocated memory is bigger than the new string, it will NOT be resized.

Parameters:
newVal the value to set this buffer to.

Definition at line 171 of file swbuf.h.

References c_str(), and length().

void sword::SWBuf::set ( const char *  newVal  )  [inline]

SWBuf::set - sets this buf to a new value.

If the allocated memory is bigger than the new string, it will NOT be resized.

Parameters:
newVal the value to set this buffer to.

Definition at line 185 of file swbuf.h.

void sword::SWBuf::setFormatted ( const char *  format,
  ... 
)

SWBuf::setFormatted - sets this buf to a formatted string.

If the allocated memory is bigger than the new string, it will NOT be resized.

Warning:
This function can only write at most JUNKBUFSIZE to the string per call.

This function is not very fast. For loops with many iterations you might consider replacing it by other calls. Example:

 SWBuf buf.setFormatted("<%s>", stringVal); 
should be replaced by:
 buf.set("<"); buf.append(stringVal); buf.append(">"); 
This will produce much faster results.
Parameters:
format The format string. Same syntax as printf, for example.
... Add all arguments here.

void sword::SWBuf::setSize ( unsigned long  len  ) 

SWBuf::setSize - Size this buffer to a specific length.

Parameters:
len The new size of the buffer. One byte for the null will be added.

void sword::SWBuf::resize ( unsigned long  len  )  [inline]

SWBuf::resize - Resize this buffer to a specific length.

Parameters:
len The new size of the buffer. One byte for the null will be added.

Definition at line 227 of file swbuf.h.

void sword::SWBuf::append ( const char *  str,
long  max = -1 
)

SWBuf::append - appends a value to the current value of this SWBuf.

If the allocated memory is not enough, it will be resized accordingly.

Parameters:
str Append this.
max Append only max chars.

void sword::SWBuf::append ( const SWBuf str,
long  max = -1 
) [inline]

SWBuf::append - appends a value to the current value of this SWBuf If the allocated memory is not enough, it will be resized accordingly.

Parameters:
str Append this.
max Append only max chars.

Definition at line 243 of file swbuf.h.

References c_str().

void sword::SWBuf::append ( char  ch  )  [inline]

SWBuf::append - appends a value to the current value of this SWBuf If the allocated memory is not enough, it will be resized accordingly.

Parameters:
ch Append this.

Definition at line 250 of file swbuf.h.

void sword::SWBuf::appendFormatted ( const char *  format,
  ... 
)

SWBuf::appendFormatted - appends formatted strings to the current value of this SWBuf.

Warning:
This function can only write at most JUNKBUFSIZE to the string per call.

This function is not very fast. For loops with many iterations you might consider replacing it by other calls. Example:

 SWBuf buf.appendFormatted("<%s>", stringVal); 
should be replaced by:
 buf.append("<"); buf.append(stringVal); buf.append(">"); 
This will produce much faster results.
Parameters:
format The format string. Same syntax as printf, for example.
... Add all arguments here.

void sword::SWBuf::insert ( unsigned long  pos,
const char *  str,
unsigned long  start = 0,
signed long  max = -1 
)

SWBuf::insert - inserts the given string at position into this string.

Parameters:
pos The position where to insert. pos=0 inserts at the beginning, pos=1 after the first char, etc. Using pos=length() is the same as calling append(s)
str string to be inserted
start start from this position in the string to be inserted
max Insert only max chars.

void sword::SWBuf::insert ( unsigned long  pos,
const SWBuf str,
unsigned long  start = 0,
signed long  max = -1 
) [inline]

SWBuf::insert - inserts the given string at position into this string.

Parameters:
pos The position where to insert. pos=0 inserts at the beginning, pos=1 after the first char, etc. Using pos=length() is the same as calling append(s)
str string to be inserted
start start from this position in the string to be inserted
max Insert only max chars.

Definition at line 290 of file swbuf.h.

References c_str().

void sword::SWBuf::insert ( unsigned long  pos,
char  c 
) [inline]

SWBuf::insert - inserts the given character at position into this string.

Parameters:
pos The position where to insert. pos=0 inserts at the beginning, pos=1 after the first char, etc. Using pos=length() is the same as calling append(s)
c Insert this.

Definition at line 299 of file swbuf.h.

char* sword::SWBuf::getRawData (  )  [inline]

SWBuf::getRawData.

Warning:
be careful! Probably setSize needs to be called in conjunction before and maybe after
Returns:
Pointer to the allocated memory of the SWBuf.

Definition at line 309 of file swbuf.h.


The documentation for this class was generated from the following file: