From: Wilfried Goesgens Date: Sun, 12 Sep 2010 21:23:35 +0000 (+0200) Subject: * NewStrBufDupAppendFlush(): add way to feed in char*, fix several bugs X-Git-Tag: v8.01~719^2~2 X-Git-Url: https://code.citadel.org/?p=citadel.git;a=commitdiff_plain;h=593fc491c243bb47de4bd809b9095f8e948dd029 * NewStrBufDupAppendFlush(): add way to feed in char*, fix several bugs * StrBufSpaceToBlank(): add a function to convert random blanks to 0x20 --- diff --git a/libcitadel/lib/libcitadel.h b/libcitadel/lib/libcitadel.h index d297385bc..10fb22f79 100644 --- a/libcitadel/lib/libcitadel.h +++ b/libcitadel/lib/libcitadel.h @@ -189,7 +189,7 @@ StrBuf* NewStrBufDup(const StrBuf *CopyMe); StrBuf* NewStrBufPlain(const char* ptr, int nChars); long StrBufShrinkToFit(StrBuf *Buf, int Force); void ReAdjustEmptyBuf(StrBuf *Buf, long ThreshHold, long NewSize); -void NewStrBufDupAppendFlush(StrBuf **CreateRelpaceMe, StrBuf *CopyFlushMe, int KeepOriginal); +void NewStrBufDupAppendFlush(StrBuf **CreateRelpaceMe, StrBuf *CopyFlushMe, const char *NoMe, int KeepOriginal); int StrBufPlain(StrBuf *Buf, const char* ptr, int nChars); StrBuf* _NewConstStrBuf(const char* StringConstant, size_t SizeOfStrConstant); @@ -264,6 +264,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 StrBufSpaceToBlank(StrBuf *Buf); void StrBufStripAllBut(StrBuf *Buf, char leftboundary, char rightboundary); void StrBufUpCase(StrBuf *Buf); void StrBufLowerCase(StrBuf *Buf); diff --git a/libcitadel/lib/stringbuf.c b/libcitadel/lib/stringbuf.c index 61a688c13..d6d960dab 100644 --- a/libcitadel/lib/stringbuf.c +++ b/libcitadel/lib/stringbuf.c @@ -392,17 +392,28 @@ StrBuf* NewStrBufDup(const StrBuf *CopyMe) /** * @ingroup StrBuf_DeConstructors * @brief Copy Constructor; CreateRelpaceMe will contain CopyFlushMe afterwards. + * @param NoMe if non-NULL, we will use that buffer as value; KeepOriginal will abused as len. * @param CopyFlushMe Buffer to faxmilate if KeepOriginal, or to move into CreateRelpaceMe if !KeepOriginal. * @param CreateRelpaceMe If NULL, will be created, else Flushed and filled CopyFlushMe * @param KeepOriginal should CopyFlushMe remain intact? or may we Steal its buffer? * @returns the new stringbuffer */ -void NewStrBufDupAppendFlush(StrBuf **CreateRelpaceMe, StrBuf *CopyFlushMe, int KeepOriginal) +void NewStrBufDupAppendFlush(StrBuf **CreateRelpaceMe, StrBuf *CopyFlushMe, const char *NoMe, int KeepOriginal) { StrBuf *NewBuf; if (CreateRelpaceMe == NULL) return; + + if (NoMe != NULL) + { + if (*CreateRelpaceMe != NULL) + StrBufPlain(*CreateRelpaceMe, NoMe, KeepOriginal); + else + *CreateRelpaceMe = NewStrBufPlain(NoMe, KeepOriginal); + return; + } + if (CopyFlushMe == NULL) { if (*CreateRelpaceMe != NULL) @@ -417,11 +428,11 @@ void NewStrBufDupAppendFlush(StrBuf **CreateRelpaceMe, StrBuf *CopyFlushMe, int * else *CreateRelpaceMe may use more memory than needed in a longer term, CopyFlushMe might * be a big IO-Buffer... */ - if (KeepOriginal || (StrLength(CopyFlushMe) > 64)) + if (KeepOriginal || (StrLength(CopyFlushMe) < 256)) { if (*CreateRelpaceMe == NULL) { - *CreateRelpaceMe = NewBuf = NewStrBufPlain(NULL, CopyFlushMe->BufUsed + 1); + *CreateRelpaceMe = NewBuf = NewStrBufPlain(NULL, CopyFlushMe->BufUsed); dbg_Init(NewBuf); } else @@ -435,7 +446,7 @@ void NewStrBufDupAppendFlush(StrBuf **CreateRelpaceMe, StrBuf *CopyFlushMe, int { if (*CreateRelpaceMe == NULL) { - *CreateRelpaceMe = NewBuf = NewStrBufPlain(NULL, CopyFlushMe->BufSize); + *CreateRelpaceMe = NewBuf = NewStrBufPlain(NULL, CopyFlushMe->BufUsed); dbg_Init(NewBuf); } else @@ -472,6 +483,11 @@ StrBuf* NewStrBufPlain(const char* ptr, int nChars) Siz *= 2; NewBuf->buf = (char*) malloc(Siz); + if (NewBuf->buf == NULL) + { + free(NewBuf); + return NULL; + } NewBuf->BufSize = Siz; if (ptr != NULL) { memcpy(NewBuf->buf, ptr, CopySize); @@ -484,9 +500,9 @@ StrBuf* NewStrBufPlain(const char* ptr, int nChars) } NewBuf->ConstBuf = 0; - dbg_Init(NewBuf) + dbg_Init(NewBuf); - return NewBuf; + return NewBuf; } /** @@ -1060,6 +1076,26 @@ void StrBufTrim(StrBuf *Buf) } if (delta > 0) StrBufCutLeft(Buf, delta); } +/** + * @ingroup StrBuf + * @brief changes all spaces in the string (tab, linefeed...) to Blank (0x20) + * @param Buf the string to modify + */ +void StrBufSpaceToBlank(StrBuf *Buf) +{ + char *pche, *pch; + + if ((Buf == NULL) || (Buf->BufUsed == 0)) return; + + pch = Buf->buf; + pche = pch + Buf->BufUsed; + while (pch < pche) + { + if (isspace(*pch)) + *pch = ' '; + pch ++; + } +} void StrBufStripAllBut(StrBuf *Buf, char leftboundary, char rightboundary) {