diff options
Diffstat (limited to 'include/swbuf.h')
-rw-r--r-- | include/swbuf.h | 79 |
1 files changed, 53 insertions, 26 deletions
diff --git a/include/swbuf.h b/include/swbuf.h index eac0e70..0344299 100644 --- a/include/swbuf.h +++ b/include/swbuf.h @@ -1,23 +1,24 @@ /****************************************************************************** -* swbuf.h - code for SWBuf used as a transport and utility for data buffers -* -* $Id: swbuf.h 2378 2009-05-04 23:18:51Z scribe $ -* -* Copyright 2003 CrossWire Bible Society (http://www.crosswire.org) -* CrossWire Bible Society -* P. O. Box 2528 -* Tempe, AZ 85280-2528 -* -* This program is free software; you can redistribute it and/or modify it -* under the terms of the GNU General Public License as published by the -* Free Software Foundation version 2. -* -* This program is distributed in the hope that it will be useful, but -* WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -* General Public License for more details. -* -*/ + * + * swbuf.h - code for SWBuf used as a transport and utility for data buffers + * + * $Id: swbuf.h 2980 2013-09-14 21:51:47Z scribe $ + * + * Copyright 2003-2013 CrossWire Bible Society (http://www.crosswire.org) + * CrossWire Bible Society + * P. O. Box 2528 + * Tempe, AZ 85280-2528 + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation version 2. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + */ #ifndef SWBUF_H #define SWBUF_H @@ -47,8 +48,6 @@ class SWDLLEXPORT SWBuf { char *endAlloc; char fillByte; unsigned long allocSize; - static char *nullStr; - static char junkBuf[JUNKBUFSIZE]; inline void assureMore(size_t pastEnd) { if (size_t(endAlloc-end) < pastEnd) { @@ -81,6 +80,8 @@ class SWDLLEXPORT SWBuf { public: + static char *nullStr; + /****************************************************************************** * SWBuf Constructor - Creates an empty SWBuf object * @@ -143,7 +144,7 @@ public: * @param pos The position of the requested character. * @return The character at the specified position */ - inline char &charAt(unsigned long pos) { return ((pos <= (unsigned long)(end - buf)) ? buf[pos] : ((*junkBuf=0),*junkBuf)); } + inline char &charAt(unsigned long pos) { return ((pos <= (unsigned long)(end - buf)) ? buf[pos] : (*nullStr)); } /** * @return size() and length() return only the number of characters of the string. @@ -232,7 +233,7 @@ public: * @param str Append this. * @param max Append only max chars. */ - void append(const char *str, long max = -1); + SWBuf &append(const char *str, long max = -1); /** * SWBuf::append - appends a value to the current value of this SWBuf @@ -240,17 +241,38 @@ public: * @param str Append this. * @param max Append only max chars. */ - inline void append(const SWBuf &str, long max = -1) { append(str.c_str(), max); } + inline SWBuf &append(const SWBuf &str, long max = -1) { return append(str.c_str(), max); } /** * SWBuf::append - appends a value to the current value of this SWBuf * If the allocated memory is not enough, it will be resized accordingly. * @param ch Append this. */ - inline void append(char ch) { + inline SWBuf &append(char ch) { + assureMore(1); + *end++ = ch; + *end = 0; + return *this; + } + inline SWBuf &append(const unsigned char ch) { assureMore(1); *end++ = ch; *end = 0; + return *this; + } + + /** + * SWBuf::append - appends a wide charachter value to the current value of this SWBuf + * If the allocated memory is not enough, it will be resized accordingly. + * NOTE: This is dangerous, as wchar_t is currently different sizes on different + * platforms (stupid windoze; stupid c++ spec for not mandating 4byte). + * @param ch Append this. + */ + inline SWBuf &append(wchar_t wch) { + assureMore(sizeof(wchar_t)*2); + for (unsigned int i = 0; i < sizeof(wchar_t); i++) *end++ = ((char *)&wch)[i]; + for (unsigned int i = 0; i < sizeof(wchar_t); i++) end[i] = 0; + return *this; } /** @@ -269,7 +291,7 @@ public: * @param format The format string. Same syntax as printf, for example. * @param ... Add all arguments here. */ - void appendFormatted(const char *format, ...); + SWBuf &appendFormatted(const char *format, ...); /** * SWBuf::insert - inserts the given string at position into this string @@ -402,6 +424,11 @@ public: */ inline bool endsWith(const SWBuf &postfix) const { return (size() >= postfix.size())?!strncmp(end-postfix.size(), postfix.c_str(), postfix.size()):false; } + /** + * @return returns the index of a substring if it is found in this buffer; otherwise, returns < 0 + */ + inline long indexOf(const SWBuf &needle) const { const char *ch = strstr(buf, needle.c_str()); return (ch) ? ch - buf : -1; } + inline int compare(const SWBuf &other) const { return strcmp(c_str(), other.c_str()); } inline bool operator ==(const SWBuf &other) const { return compare(other) == 0; } inline bool operator !=(const SWBuf &other) const { return compare(other) != 0; } |