* add define to easy make a hashkey from a stringbuffer
authorWilfried Göesgens <willi@citadel.org>
Sun, 14 Sep 2008 15:43:47 +0000 (15:43 +0000)
committerWilfried Göesgens <willi@citadel.org>
Sun, 14 Sep 2008 15:43:47 +0000 (15:43 +0000)
* add StrBufRemoveToken
* add StrBufUpcase

libcitadel/debian/files
libcitadel/lib/libcitadel.h
libcitadel/lib/stringbuf.c

index 73fc3064e0412a561172b5a5c47e4c6ec0596e3b..d2ac0561c0b0a03d7eab551e7db9c29484f59207 100644 (file)
@@ -1,3 +1,3 @@
-libcitadel1_7.38-8_amd64.deb libs optional
-libcitadel1-dbg_7.38-8_amd64.deb libdevel optional
-libcitadel-dev_7.38-8_amd64.deb libdevel optional
+libcitadel1_7.38-8_i386.deb libs optional
+libcitadel1-dbg_7.38-8_i386.deb libdevel optional
+libcitadel-dev_7.38-8_i386.deb libdevel optional
index b629d0dd4392d3f0a9413ae52345ec2a3ca6b0d3..f67c6f147af4076606d42cfcc26a794bf7c4a97a 100644 (file)
@@ -214,6 +214,7 @@ int FlushStrBuf(StrBuf *buf);
 
 inline const char *ChrPtr(const StrBuf *Str);
 inline int StrLength(const StrBuf *Str);
+#define SKEY(a) ChrPtr(a), StrLength(a)
 long StrBufPeek(StrBuf *Buf, const char* ptr, long nThChar, char PeekValue);
 
 int StrBufTCP_read_line(StrBuf *buf, int *fd, int append, const char **Error);
@@ -231,6 +232,7 @@ unsigned long StrBufExtract_unsigned_long(const StrBuf* Source, int parmnum, cha
 long StrBufExtract_long(const StrBuf* Source, int parmnum, char separator);
 int StrBufExtract_int(const StrBuf* Source, int parmnum, char separator);
 inline int StrBufNum_tokens(const StrBuf *source, char tok);
+int StrBufRemove_token(StrBuf *Source, int parmnum, char separator);
 
 void StrBufAppendBufPlain(StrBuf *Buf, const char *AppendBuf, long AppendSize, size_t Offset);
 void StrBufAppendBuf(StrBuf *Buf, const StrBuf *AppendBuf, size_t Offset);
@@ -242,6 +244,7 @@ void StrBufVAppendPrintf(StrBuf *Buf, const char *format, va_list ap);
 void StrBufPrintf(StrBuf *Buf, const char *format, ...) __attribute__((__format__(__printf__,2,3)));
 void StrBufCutLeft(StrBuf *Buf, int nChars);
 void StrBufCutRight(StrBuf *Buf, int nChars);
+void StrBufUpCase(StrBuf *Buf);
 void StrBufEUid_unescapize(StrBuf *target, const StrBuf *source);
 void StrBufEUid_escapize(StrBuf *target, const StrBuf *source);
 
index 6d429a6898f84e0eb8ada8eadacdf5f41d40340a..e2dae77306fc523734fb38282cc505de2b34f8aa 100644 (file)
@@ -24,6 +24,10 @@ struct StrBuf {
 
 /** 
  * \Brief Cast operator to Plain String 
+ * Note: if the buffer is altered by StrBuf operations, this pointer may become 
+ *  invalid. So don't lean on it after altering the buffer!
+ *  Since this operation is considered cheap, rather call it often than risking
+ *  your pointer to become invalid!
  * \param Str the string we want to get the c-string representation for
  * \returns the Pointer to the Content. Don't mess with it!
  */
@@ -700,9 +704,73 @@ void StrBufPrintf(StrBuf *Buf, const char *format, ...)
  */
 inline int StrBufNum_tokens(const StrBuf *source, char tok)
 {
+       if (source == NULL)
+               return 0;
        return num_tokens(source->buf, tok);
 }
 
+/*
+ * remove_token() - a tokenizer that kills, maims, and destroys
+ */
+/**
+ * \brief a string tokenizer
+ * \param Source StringBuffer to read into
+ * \param parmnum n'th parameter to remove
+ * \param separator tokenizer param
+ * \returns -1 if not found, else length of token.
+ */
+int StrBufRemove_token(StrBuf *Source, int parmnum, char separator)
+{
+       int ReducedBy;
+       char *d, *s;            /* dest, source */
+       int count = 0;
+
+       /* Find desired parameter */
+       d = Source->buf;
+       while (count < parmnum) {
+               /* End of string, bail! */
+               if (!*d) {
+                       d = NULL;
+                       break;
+               }
+               if (*d == separator) {
+                       count++;
+               }
+               d++;
+       }
+       if (!d) return 0;               /* Parameter not found */
+
+       /* Find next parameter */
+       s = d;
+       while (*s && *s != separator) {
+               s++;
+       }
+
+       ReducedBy = d - s;
+
+       /* Hack and slash */
+       if (*s) {
+               memmove(d, s, Source->BufUsed - (s - Source->buf));
+               Source->BufUsed -= (ReducedBy + 1);
+       }
+       else if (d == Source->buf) {
+               *d = 0;
+               Source->BufUsed = 0;
+       }
+       else {
+               *--d = 0;
+               Source->BufUsed -= (ReducedBy + 1);
+       }
+       /*
+       while (*s) {
+               *d++ = *s++;
+       }
+       *d = 0;
+       */
+       return ReducedBy;
+}
+
+
 /**
  * \brief a string tokenizer
  * \param dest Destination StringBuffer
@@ -912,7 +980,7 @@ int StrBufTCP_read_buffered_line(StrBuf *Line,
                        return len - rlen;
                }
        }
-
+       
        if (buf->BufSize - buf->BufUsed < 10)
                IncreaseBuf(buf, 1, -1);
 
@@ -1057,6 +1125,19 @@ void StrBufCutRight(StrBuf *Buf, int nChars)
 }
 
 
+void StrBufUpCase(StrBuf *Buf) 
+{
+       char *pch, *pche;
+
+       pch = Buf->buf;
+       pche = pch + Buf->BufUsed;
+       while (pch < pche) {
+               *pch = toupper(*pch);
+               pch ++;
+       }
+}
+
+
 /**
  * \brief unhide special chars hidden to the HTML escaper
  * \param target buffer to put the unescaped string in