diff options
Diffstat (limited to 'include/swbuf.h')
-rw-r--r-- | include/swbuf.h | 22 |
1 files changed, 14 insertions, 8 deletions
diff --git a/include/swbuf.h b/include/swbuf.h index 6ae6958..eac0e70 100644 --- a/include/swbuf.h +++ b/include/swbuf.h @@ -1,7 +1,7 @@ /****************************************************************************** * swbuf.h - code for SWBuf used as a transport and utility for data buffers * -* $Id: swbuf.h 2116 2007-10-17 00:12:27Z scribe $ +* $Id: swbuf.h 2378 2009-05-04 23:18:51Z scribe $ * * Copyright 2003 CrossWire Bible Society (http://www.crosswire.org) * CrossWire Bible Society @@ -213,7 +213,7 @@ public: * @param format The format string. Same syntax as printf, for example. * @param ... Add all arguments here. */ - void setFormatted(const char *format, ...); + SWBuf &setFormatted(const char *format, ...); /** * SWBuf::setSize - Size this buffer to a specific length. @@ -373,9 +373,14 @@ public: * Strip a prefix from this buffer up to a separator byte. * Returns the prefix and modifies this buffer, shifting left to remove prefix * @param separator to use (e.g. ':') + * @param endOfStringAsSeparator - also count end of string as separator. + * this is useful for tokenizing entire string like: + * x|y|z + * if true it will also include 'z'. + * * @return prefix if separator character found; otherwise, null and leaves buffer unmodified */ - inline const char *stripPrefix(char separator) { const char *m = strchr(buf, separator); if (m) { int len = m-buf; char *hold = new char[len]; memcpy(hold, buf, len); *this << (len+1); memcpy(end+1, hold, len); delete [] hold; end[len+1] = 0; } return (m) ? end+1 : 0; } // safe. we know we don't actually realloc and shrink buffer when shifting, so we can place our return val at end. + inline const char *stripPrefix(char separator, bool endOfStringAsSeparator = false) { const char *m = strchr(buf, separator); if (!m && endOfStringAsSeparator) { if (*buf) { operator >>(1); *buf=0; end = buf; return buf + 1;} else return buf; } if (m) { int len = m-buf; char *hold = new char[len]; memcpy(hold, buf, len); *this << (len+1); memcpy(end+1, hold, len); delete [] hold; end[len+1] = 0; } return (m) ? end+1 : 0; } // safe. we know we don't actually realloc and shrink buffer when shifting, so we can place our return val at end. // this could be nicer, like replacing a contiguous series of target bytes with single replacement; offering replacement const char * /** @@ -415,13 +420,14 @@ public: */ inline bool endsWith(const char *postfix) const { unsigned int psize = strlen(postfix); return (size() >= psize)?!strncmp(end-psize, postfix, psize):false; } - inline int compare(const char *other) const { return strcmp(c_str(), other); } + // be sure we've been given a valid pointer to compare. If not, we return !=; specifically less-than, for lack of better options + inline int compare(const char *other) const { return (other?strcmp(c_str(), other):-1); } inline bool operator ==(const char *other) const { return compare(other) == 0; } inline bool operator !=(const char *other) const { return compare(other) != 0; } - inline bool operator > (const char *other) const { return compare(other) > 0; } - inline bool operator < (const char *other) const { return compare(other) < 0; } - inline bool operator <=(const char *other) const { return compare(other) <= 0; } - inline bool operator >=(const char *other) const { return compare(other) >= 0; } + inline bool operator > (const char *other) const { return other && compare(other) > 0; } + inline bool operator < (const char *other) const { return other && compare(other) < 0; } + inline bool operator <=(const char *other) const { return other && compare(other) <= 0; } + inline bool operator >=(const char *other) const { return other && compare(other) >= 0; } }; |