* add function to replace a sub snippet of a string
authorWilfried Göesgens <willi@citadel.org>
Tue, 9 Mar 2010 09:49:48 +0000 (09:49 +0000)
committerWilfried Göesgens <willi@citadel.org>
Tue, 9 Mar 2010 09:49:48 +0000 (09:49 +0000)
libcitadel/lib/libcitadel.h
libcitadel/lib/stringbuf.c

index f841657413e69406a79fea4a2ed5ddb4cbdac877..464edc34bb249e5cde95917542cb762a1ae87dfc 100644 (file)
@@ -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);
 
index 8048aeb663dff97665a414b7ec0b26b819ffadd9..1e27edb45401313f0d02f39c5a7bbd24e67ac279 100644 (file)
@@ -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++;