From 82c71da129f568054d4796f35ef6bfa7200030bc Mon Sep 17 00:00:00 2001 From: =?utf8?q?Wilfried=20G=C3=B6esgens?= Date: Sat, 13 Mar 2010 09:56:00 +0000 Subject: [PATCH] * add StrBufPook which blobs out a sub-part of the string * add StrBufStripAllBut, which aims to replace stripallbut, whose functionality is not yet all clear to me. * add NULL protection to StrBufSub --- libcitadel/debian/changelog | 6 ++++ libcitadel/lib/libcitadel.h | 2 ++ libcitadel/lib/stringbuf.c | 56 ++++++++++++++++++++++++++++++++++++- 3 files changed, 63 insertions(+), 1 deletion(-) diff --git a/libcitadel/debian/changelog b/libcitadel/debian/changelog index 89a20b553..9fce5cd2d 100644 --- a/libcitadel/debian/changelog +++ b/libcitadel/debian/changelog @@ -1,3 +1,9 @@ +libcitadel (7.79-87) unstable; urgency=low + + * no new release yet. + + -- Wilfried Goesgens Tue, 16 Dec 2009 22:00:00 +0002 + libcitadel (7.70-86) unstable; urgency=low * new release diff --git a/libcitadel/lib/libcitadel.h b/libcitadel/lib/libcitadel.h index 464edc34b..cc9893182 100644 --- a/libcitadel/lib/libcitadel.h +++ b/libcitadel/lib/libcitadel.h @@ -278,6 +278,7 @@ const char *ChrPtr(const StrBuf *Str); int StrLength(const StrBuf *Str); #define SKEY(a) ChrPtr(a), StrLength(a) long StrBufPeek(StrBuf *Buf, const char* ptr, long nThChar, char PeekValue); +long StrBufPook(StrBuf *Buf, const char* ptr, long nThChar, long nChars, char PookValue); int StrBufTCP_read_line(StrBuf *buf, int *fd, int append, const char **Error); int StrBufReadBLOB(StrBuf *Buf, int *fd, int append, long nBytes, const char **Error); @@ -337,6 +338,7 @@ void StrBufCutLeft(StrBuf *Buf, int nChars); void StrBufCutRight(StrBuf *Buf, int nChars); void StrBufCutAt(StrBuf *Buf, int AfternChars, const char *At); void StrBufTrim(StrBuf *Buf); +void StrBufStripAllBut(StrBuf *Buf, char leftboundary, char rightboundary); void StrBufUpCase(StrBuf *Buf); void StrBufLowerCase(StrBuf *Buf); void StrBufStripSlashes(StrBuf *Dir, int RemoveTrailingSlash); diff --git a/libcitadel/lib/stringbuf.c b/libcitadel/lib/stringbuf.c index 1e27edb45..f0506886a 100644 --- a/libcitadel/lib/stringbuf.c +++ b/libcitadel/lib/stringbuf.c @@ -618,6 +618,7 @@ int StrBufIsNumber(const StrBuf *Buf) { return 0; return 0; } + /** * @ingroup StrBuf_Filler * @brief modifies a Single char of the Buf @@ -639,6 +640,33 @@ long StrBufPeek(StrBuf *Buf, const char* ptr, long nThChar, char PeekValue) return nThChar; } +/** + * @ingroup StrBuf_Filler + * @brief modifies a range of chars of the Buf + * You can point to it via char* or a zero-based integer + * @param Buf The buffer to manipulate + * @param ptr char* to zero; use NULL if unused + * @param nThChar zero based pointer into the string; use -1 if unused + * @param nChars how many chars are to be flushed? + * @param PookValue The Character to place into that area + */ +long StrBufPook(StrBuf *Buf, const char* ptr, long nThChar, long nChars, char PookValue) +{ + if (Buf == NULL) + return -1; + if (ptr != NULL) + nThChar = ptr - Buf->buf; + if ((nThChar < 0) || (nThChar > Buf->BufUsed)) + return -1; + if (nThChar + nChars > Buf->BufUsed) + nChars = Buf->BufUsed - nThChar; + + memset(Buf->buf + nThChar, PookValue, nChars); + /* just to be shure... */ + Buf->buf[Buf->BufUsed] = 0; + return nChars; +} + /** * @ingroup StrBuf_Filler * @brief Append a StringBuffer to the buffer @@ -844,7 +872,8 @@ int StrBufSub(StrBuf *dest, const StrBuf *Source, unsigned long Offset, size_t n size_t NCharsRemain; if (Offset > Source->BufUsed) { - FlushStrBuf(dest); + if (dest != NULL) + FlushStrBuf(dest); return 0; } if (Offset + nChars < Source->BufUsed) @@ -944,9 +973,34 @@ void StrBufTrim(StrBuf *Buf) delta ++; } if (delta > 0) StrBufCutLeft(Buf, delta); +} + +void StrBufStripAllBut(StrBuf *Buf, char leftboundary, char rightboundary) +{ + const char *pBuff; + const char *pLeft; + const char *pRight; + if (Buf == NULL) + return; + pLeft = pBuff = Buf->buf; + while (pBuff != NULL) { + pLeft = pBuff; + pBuff = strchr(pBuff, leftboundary); + } + + if (pLeft != NULL) + pBuff = pLeft; + else + pBuff = Buf->buf; + pRight = strchr(pBuff, rightboundary); + if (pRight != NULL) + StrBufCutAt(Buf, 0, pRight - 1); + if (pLeft != NULL) + StrBufCutLeft(Buf, pLeft - Buf->buf + 1); } + /** * @ingroup StrBuf_Filler * @brief uppercase the contents of a buffer -- 2.30.2