From: Wilfried Göesgens Date: Tue, 9 Mar 2010 09:49:48 +0000 (+0000) Subject: * add function to replace a sub snippet of a string X-Git-Tag: v7.86~330 X-Git-Url: https://code.citadel.org/?p=citadel.git;a=commitdiff_plain;h=d071894ceddc5d81693c70da424066c3db1628e8 * add function to replace a sub snippet of a string --- diff --git a/libcitadel/lib/libcitadel.h b/libcitadel/lib/libcitadel.h index f84165741..464edc34b 100644 --- a/libcitadel/lib/libcitadel.h +++ b/libcitadel/lib/libcitadel.h @@ -306,6 +306,7 @@ int StrBufTCP_read_buffered_line_fast(StrBuf *Line, const char **Error); int StrBufSipLine(StrBuf *LineBuf, StrBuf *Buf, const char **Ptr); +int StrBufReplaceToken(StrBuf *Buf, long where, long HowLong, const char *Repl, long ReplLen); int StrBufExtract_token(StrBuf *dest, const StrBuf *Source, int parmnum, char separator); int StrBufSub(StrBuf *dest, const StrBuf *Source, unsigned long Offset, size_t nChars); diff --git a/libcitadel/lib/stringbuf.c b/libcitadel/lib/stringbuf.c index 8048aeb66..1e27edb45 100644 --- a/libcitadel/lib/stringbuf.c +++ b/libcitadel/lib/stringbuf.c @@ -991,6 +991,41 @@ void StrBufLowerCase(StrBuf *Buf) * a tokenizer that kills, maims, and destroys * *******************************************************************************/ +/** + * @ingroup StrBuf_Tokenizer + * @brief Replace a token at a given place with a given length by another token with given length + * @param Buf String where to work on + * @param where where inside of the Buf is the search-token + * @param HowLong How long is the token to be replaced + * @param Repl Token to insert at 'where' + * @param ReplLen Length of repl + * @returns -1 if fail else length of resulting Buf + */ +int StrBufReplaceToken(StrBuf *Buf, long where, long HowLong, + const char *Repl, long ReplLen) +{ + + if ((Buf == NULL) || + (where > Buf->BufUsed) || + (where + HowLong > Buf->BufUsed)) + return -1; + + if (where + ReplLen - HowLong > Buf->BufSize) + if (IncreaseBuf(Buf, 1, Buf->BufUsed + ReplLen) < 0) + return -1; + + memmove(Buf->buf + where + HowLong, + Buf->buf + where + ReplLen, + Buf->BufUsed - where - HowLong); + + memcpy(Buf->buf + where, + Repl, ReplLen); + + Buf->BufUsed += ReplLen - HowLong; + + return Buf->BufUsed; +} + /** * @ingroup StrBuf_Tokenizer * @brief Counts the numbmer of tokens in a buffer @@ -1549,8 +1584,7 @@ void StrBufUrlescAppend(StrBuf *OutBuf, const StrBuf *In, const char *PlainIn) (*pch >= '@' && *pch <= 'Z') || /* @ A-Z */ (*pch >= '0' && *pch <= ':') || /* 0-9 : */ (*pch == '!') || (*pch == '_') || - (*pch == ',') || (*pch == '.') || - (*pch == ',')) + (*pch == ',') || (*pch == '.')) { *(pt++) = *(pch++); OutBuf->BufUsed++;