charbuf<N> - C++ class for built-in strings Template:NEW30
Synopsis
template<size_t size> class charbuf { private: char buffer[size]; public: charbuf<size>(void); charbuf<size>(const char *s); ~charbuf<size>(void); size_t get_size(void); size_t get_length(void); char *get_string(void); char* erase(void); char* copy_to(char *s); char* copy_from(const char *s); operator char*(void); bool operator ==(const char *s); bool operator <(const char *s); bool operator <(const char *s); bool operator <=(const char *s); bool operator >=(const char *s); char *find(const char c); char *find(const char *s); char *findrev(const char c); char *token(char *from, const char *delim, char **context); size_t format(char *fmt, ...); size_t vformat(char *fmt, va_list ptr); }; typedef charbuf<1025> char1024; typedef charbuf<257> char256; typedef charbuf<33> char32; typedef charbuf<9> char8;
Remarks
This template is used to implement the C++ classes for the string built-in types.
copy_from
Copies data from a string into the buffer.
copy_to
Copies data to a string from the buffer.
erase
Erases the buffer.
find
Finds a character or string in the buffer.
findrev
Find a character in the buffer starting from the end.
format
Formats the buffer using printf syntax.
get_size
Gets the size of the buffer.
get_length
Gets the length of the string in the buffer.
get_string
Gets a pointer to the string in the buffer.
operator char*
Casts to a char* type, which effectively gets a pointer to the string in the buffer.
operator ==
True is the string and the buffer are identical.
operator <
True if the buffer is alphabetically before the string.
operator >
True if the buffer is alphabetically after the string.
operator <=
True if the buffer is alphabetically before or the same as the string.
operator >=
True if the buffer is alphabetically after or the same as the string.
token
Find the first (or next) instance of a delimited string in the buffer.
vformat
Formats the string using vprintf syntax.
Caveat
You cannot rely on C++ compiler to automatically convert from the charbuf class to a string when using variable argument functions. This is a very insidious problem for certain compilers. In the absence of a cast or function that returns a char*, the compiler will either copy the entire buffer onto the stack (which is not what is usually expected) or reject the statement as an error due to the lack of a copy constructor. Therefore instead of
printf("%s",member);
you should use either
printf("%s",(const char*)member);
or
printf("%s",member.get_string());