* add new buffer class which handles concatenation of strings in dynamic buffers...
authorWilfried Göesgens <willi@citadel.org>
Wed, 9 Jul 2008 00:47:39 +0000 (00:47 +0000)
committerWilfried Göesgens <willi@citadel.org>
Wed, 9 Jul 2008 00:47:39 +0000 (00:47 +0000)
* adopt our algorithms to these new structures

libcitadel/Makefile.in
libcitadel/debian/changelog
libcitadel/debian/files
libcitadel/lib/hash.c
libcitadel/lib/libcitadel.h
libcitadel/lib/stringbuf.c [new file with mode: 0644]

index 7dfeaa6da91005a3e6de958c3d6fc073e20bc556..f18a1e43ea0937f2450684889c86a8fc8284257d 100755 (executable)
@@ -108,6 +108,7 @@ LIB_OBJS = lib/libcitadel.lo \
        lib/vnote.lo \
        lib/hash.lo \
        lib/lookup3.lo \
+       lib/stringbuf.lo \
        lib/xdgmime/xdgmime.lo \
        lib/xdgmime/xdgmimeglob.lo \
        lib/xdgmime/xdgmimeint.lo \
index 8bf8c3c0543be1458f20e5968e83c09f52c3391a..0af783fe99710a8f801f802968f92dae17c8e710 100644 (file)
@@ -1,3 +1,20 @@
+libcitadel (1.09-5) stable; urgency=low
+
+  * new upstream version
+
+ -- Wilfried Goesgens <w.goesgens@outgesourced.org>  Tue, 22 Apr 2008 19:00:00 +0002
+
+libcitadel (1.08-5) stable; urgency=low
+  
+   * minor upstream bugfixes
+  
+ -- Wilfried Goesgens <w.goesgens@outgesourced.org>  Mon, 17 Mar 2008 22:00:00 +0001
+
+libcitadel (1.08-4) stable; urgency=high 
+
+   * release 1.08; hashing / sorting implemented
+
+ -- Wilfried Goesgens <w.goesgens@outgesourced.org>  Mo, 3 Mar 2008 22:00:00 +0001 
 libcitadel (1.07-8) stable; urgency=high
 
    * new upstream version
index 6b5384a9193b8fdb76608ab85aa769b0ccb9e0d7..addf18a45c84dd274d79bb943bf37365c4e7c29e 100644 (file)
@@ -1,3 +1,3 @@
-libcitadel1_1.07-8_i386.deb libs optional
-libcitadel1-dbg_1.07-8_i386.deb libdevel optional
-libcitadel-dev_1.07-8_i386.deb libdevel optional
+libcitadel1_1.09-5_i386.deb libs optional
+libcitadel1-dbg_1.09-5_i386.deb libdevel optional
+libcitadel-dev_1.09-5_i386.deb libdevel optional
index 63c80e357b4ade63ed3c773319fa395f4dbb0dee..1de53624da09f57355213fb1b6100bdb1263db85 100644 (file)
@@ -132,8 +132,12 @@ int dbg_PrintHash(HashList *Hash, PrintHashContent First, PrintHashContent Secon
                        foo = Hash->LookupTable[i]->HashKey;
                        if (First != NULL)
                                bar = First(Hash->Members[Hash->LookupTable[i]->Position]->Data);
+                       else 
+                               bar = "";
                        if (Second != NULL)
                                bla = Second(Hash->Members[Hash->LookupTable[i]->Position]->Data);
+                       else
+                               bla = "";
                }
 #ifdef DEBUG
                printf (" ---- Hashkey[%ld][%ld]: '%s' Value: '%s' ; %s\n", i, key, foo, bar, bla);
index 32b3034dbdee4baff211c20c18a84b67cc824b85..ff9590a343e89f82df177d975283a39960255a37 100644 (file)
@@ -14,6 +14,7 @@
  */
 #include <time.h>
 #include <stdlib.h>
+#include <stdarg.h>
 #define LIBCITADEL_VERSION_NUMBER      737
 
 /*
@@ -199,6 +200,36 @@ void the_mime_parser(char *partnum,
                      int dont_decode
 );
 
+typedef struct StrBuf StrBuf;
+
+StrBuf* NewStrBuf(void);
+StrBuf* NewStrBufPlain(const char* ptr, int nChars);
+StrBuf* _NewConstStrBuf(const char* StringConstant, size_t SizeOfStrConstant);
+#define NewConstStrBuf(a) _NewConstStrBuf(a, sizeof(a))
+void FreeStrBuf (StrBuf **FreeMe);
+void HFreeStrBuf (void *VFreeMe);
+int FlushStrBuf(StrBuf *buf);
+
+inline const char *ChrPtr(StrBuf *Str);
+inline int StrLength(StrBuf *Str);
+
+int StrBufTCP_read_line(StrBuf *buf, int fd, int append, const char **Error);
+
+int StrBufExtract_token(StrBuf *dest, const StrBuf *Source, int parmnum, char separator);
+int StrBufSub(StrBuf *dest, const StrBuf *Source, size_t Offset, size_t nChars);
+unsigned long StrBufExtract_unsigned_long(const StrBuf* Source, int parmnum, char separator);
+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);
+
+void StrBufAppendBuf(StrBuf *Buf, StrBuf *AppendBuf, size_t Offset);
+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 StrBufEUid_unescapize(StrBuf *target, StrBuf *source);
+
+long StrTol(StrBuf *Buf);
+
 const char *GuessMimeType(char *data, size_t dlen);
 const char* GuessMimeByFilename(const char *what, size_t len);
 
@@ -253,6 +284,7 @@ int is_msg_in_mset(char *mset, long msgnum);
 int pattern2(char *search, char *patn);
 void stripltlen(char *, int *);
 char *html_to_ascii(char *inputmsg, int msglen, int screenwidth, int do_citaformat);
+void LoadEntityList(char *FileName);
 
 
 
diff --git a/libcitadel/lib/stringbuf.c b/libcitadel/lib/stringbuf.c
new file mode 100644 (file)
index 0000000..a94e1d5
--- /dev/null
@@ -0,0 +1,416 @@
+
+#include <errno.h>
+#include <string.h>
+#include <unistd.h>
+#include <string.h>
+#include <stdio.h>
+#include "libcitadel.h"
+
+#include <stdarg.h>
+
+struct StrBuf {
+       char *buf;
+       long BufSize;
+       long BufUsed;
+       int ConstBuf;
+};
+
+
+inline const char *ChrPtr(StrBuf *Str)
+{
+       if (Str == NULL)
+               return "";
+       return Str->buf;
+}
+
+inline int StrLength(StrBuf *Str)
+{
+       return Str->BufUsed;
+}
+
+StrBuf* NewStrBuf(void)
+{
+       StrBuf *NewBuf;
+
+       NewBuf = (StrBuf*) malloc(sizeof(StrBuf));
+       NewBuf->buf = (char*) malloc(SIZ);
+       NewBuf->buf[0] = '\0';
+       NewBuf->BufSize = SIZ;
+       NewBuf->BufUsed = 0;
+       NewBuf->ConstBuf = 0;
+       return NewBuf;
+}
+
+
+StrBuf* NewStrBufPlain(const char* ptr, int nChars)
+{
+       StrBuf *NewBuf;
+       size_t Siz = SIZ;
+       size_t CopySize;
+
+       NewBuf = (StrBuf*) malloc(sizeof(StrBuf));
+       if (nChars < 0)
+               CopySize = strlen(ptr);
+       else
+               CopySize = nChars;
+
+       while (Siz <= CopySize)
+               Siz *= 2;
+
+       NewBuf->buf = (char*) malloc(Siz);
+       memcpy(NewBuf->buf, ptr, CopySize);
+       NewBuf->buf[CopySize] = '\0';
+       NewBuf->BufSize = Siz;
+       NewBuf->BufUsed = CopySize;
+       NewBuf->ConstBuf = 0;
+       return NewBuf;
+}
+
+StrBuf* _NewConstStrBuf(const char* StringConstant, size_t SizeOfStrConstant)
+{
+       StrBuf *NewBuf;
+
+       NewBuf = (StrBuf*) malloc(sizeof(StrBuf));
+       NewBuf->buf = (char*) StringConstant;
+       NewBuf->BufSize = SizeOfStrConstant;
+       NewBuf->BufUsed = SizeOfStrConstant;
+       NewBuf->ConstBuf = 1;
+       return NewBuf;
+}
+
+
+static int IncreaseBuf(StrBuf *Buf, int KeepOriginal, int DestSize)
+{
+       char *NewBuf;
+       size_t NewSize = Buf->BufSize * 2;
+
+       if (Buf->ConstBuf)
+               return -1;
+               
+       if (DestSize > 0)
+               while (NewSize < DestSize)
+                       NewSize *= 2;
+
+       NewBuf= (char*) malloc(NewSize);
+       if (KeepOriginal)
+       {
+               memcpy(NewBuf, Buf->buf, Buf->BufUsed);
+       }
+       else
+       {
+               NewBuf[0] = '\0';
+               Buf->BufUsed = 0;
+       }
+       free (Buf->buf);
+       Buf->buf = NewBuf;
+       Buf->BufSize *= 2;
+       return Buf->BufSize;
+}
+
+int FlushStrBuf(StrBuf *buf)
+{
+       if (buf->ConstBuf)
+               return -1;       
+       buf->buf[0] ='\0';
+       buf->BufUsed = 0;
+       return 0;
+}
+
+void FreeStrBuf (StrBuf **FreeMe)
+{
+       if (!(*FreeMe)->ConstBuf) 
+               free((*FreeMe)->buf);
+       free(*FreeMe);
+       *FreeMe = NULL;
+}
+
+void HFreeStrBuf (void *VFreeMe)
+{
+       StrBuf *FreeMe = (StrBuf*)VFreeMe;
+       if (!FreeMe->ConstBuf) 
+               free(FreeMe->buf);
+       free(FreeMe);
+}
+
+long StrTol(StrBuf *Buf)
+{
+       if(Buf->BufUsed > 0)
+               return atol(Buf->buf);
+       else
+               return 0;
+}
+
+
+void StrBufAppendBuf(StrBuf *Buf, StrBuf *AppendBuf, size_t Offset)
+{
+       if ((AppendBuf == NULL) || (Buf == NULL))
+               return;
+       if (Buf->BufSize - Offset < AppendBuf->BufUsed)
+               IncreaseBuf(Buf, (Buf->BufUsed > 0), AppendBuf->BufUsed);
+       memcpy(Buf->buf + Buf->BufUsed - 1, 
+              AppendBuf->buf + Offset, 
+              AppendBuf->BufUsed - Offset);
+       Buf->BufUsed += AppendBuf->BufUsed - Offset;
+       Buf->buf[Buf->BufUsed] = '\0';
+}
+
+
+inline int StrBufNum_tokens(const StrBuf *source, char tok)
+{
+       return num_tokens(source->buf, tok);
+}
+
+
+int StrBufSub(StrBuf *dest, const StrBuf *Source, size_t Offset, size_t nChars)
+{
+       size_t NCharsRemain;
+       if (Offset > Source->BufUsed)
+       {
+               FlushStrBuf(dest);
+               return 0;
+       }
+       if (Offset + nChars < Source->BufUsed)
+       {
+               if (nChars < dest->BufSize)
+                       IncreaseBuf(dest, 0, nChars + 1);
+               memcpy(dest->buf, Source->buf + Offset, nChars);
+               dest->BufUsed = nChars + 1;
+               dest->buf[dest->BufUsed] = '\0';
+               return nChars;
+       }
+       NCharsRemain = Source->BufUsed - Offset;
+       if (NCharsRemain < dest->BufSize)
+               IncreaseBuf(dest, 0, NCharsRemain + 1);
+       memcpy(dest->buf, Source->buf + Offset, NCharsRemain);
+       dest->BufUsed = NCharsRemain + 1;
+       dest->buf[dest->BufUsed] = '\0';
+       return NCharsRemain;
+}
+
+void StrBufPrintf(StrBuf *Buf, const char *format, ...)
+{
+       size_t nWritten = Buf->BufSize + 1;
+       va_list arg_ptr;
+       
+       while (nWritten >= Buf->BufSize) {
+               va_start(arg_ptr, format);
+               nWritten = vsnprintf(Buf->buf, Buf->BufSize, format, arg_ptr);
+               va_end(arg_ptr);
+               Buf->BufUsed = nWritten ;
+               if (nWritten >= Buf->BufSize)
+                       IncreaseBuf(Buf, 0, 0);
+       }
+}
+
+
+/**
+ * \brief a string tokenizer
+ * \param dest Destination StringBuffer
+ * \param Source StringBuffer to read into
+ * \param separator tokenizer param
+ * \returns -1 if not found, else length of token.
+ */
+int StrBufExtract_token(StrBuf *dest, const StrBuf *Source, int parmnum, char separator)
+{
+       const char *s;                  //* source * /
+       int len = 0;                    //* running total length of extracted string * /
+       int current_token = 0;          //* token currently being processed * /
+
+       if ((Source == NULL) || (Source->BufUsed ==0)) {
+               return(-1);
+       }
+       s = Source->buf;
+
+       if (dest == NULL) {
+               return(-1);
+       }
+
+       //cit_backtrace();
+       //lprintf (CTDL_DEBUG, "test >: n: %d sep: %c source: %s \n willi \n", parmnum, separator, source);
+       dest->buf[0] = '\0';
+       dest->BufUsed = 0;
+
+       while (*s) {
+               if (*s == separator) {
+                       ++current_token;
+               }
+               if (len >= dest->BufSize)
+                       if (!IncreaseBuf(dest, 1, -1))
+                               break;
+               if ( (current_token == parmnum) && 
+                    (*s != separator)) {
+                       dest->buf[len] = *s;
+                       ++len;
+               }
+               else if (current_token > parmnum) {
+                       break;
+               }
+               ++s;
+       }
+       
+       dest->buf[len] = '\0';
+       dest->BufUsed = len;
+               
+       if (current_token < parmnum) {
+               //lprintf (CTDL_DEBUG,"test <!: %s\n", dest);
+               return(-1);
+       }
+       //lprintf (CTDL_DEBUG,"test <: %d; %s\n", len, dest);
+       return(len);
+}
+
+
+/*
+ * extract_int()  -  extract an int parm w/o supplying a buffer
+ */
+int StrBufExtract_int(const StrBuf* Source, int parmnum, char separator)
+{
+       StrBuf tmp;
+       char buf[64];
+       
+       tmp.buf = buf;
+       buf[0] = '\0';
+       tmp.BufSize = 64;
+       tmp.BufUsed = 0;
+       if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0)
+               return(atoi(buf));
+       else
+               return 0;
+}
+
+/*
+ * extract_long()  -  extract an long parm w/o supplying a buffer
+ */
+long StrBufExtract_long(const StrBuf* Source, int parmnum, char separator)
+{
+       StrBuf tmp;
+       char buf[64];
+       
+       tmp.buf = buf;
+       buf[0] = '\0';
+       tmp.BufSize = 64;
+       tmp.BufUsed = 0;
+       if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0)
+               return(atoi(buf));
+       else
+               return 0;
+}
+
+
+/*
+ * extract_unsigned_long() - extract an unsigned long parm
+ */
+unsigned long StrBufExtract_unsigned_long(const StrBuf* Source, int parmnum, char separator)
+{
+       StrBuf tmp;
+       char buf[64];
+       
+       tmp.buf = buf;
+       buf[0] = '\0';
+       tmp.BufSize = 64;
+       tmp.BufUsed = 0;
+       if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0)
+               return(atoi(buf));
+       else 
+               return 0;
+}
+
+
+
+/**
+ * \brief Input binary data from socket
+ * \param buf the buffer to get the input to
+ * \param bytes the maximal number of bytes to read
+ */
+int StrBufTCP_read_line(StrBuf *buf, int fd, int append, const char **Error)
+{
+       int len, rlen, slen;
+
+       if (!append)
+               FlushStrBuf(buf);
+
+       slen = len = buf->BufUsed;
+       while (buf->buf[len] != '\n') {
+               rlen = read(fd, &buf->buf[len], 1);
+               if (rlen < 1) {
+                       *Error = strerror(errno);
+                       
+                       close(fd);
+                       
+                       return -1;
+               }
+               if (buf->buf[len] == '\n')
+                       break;
+               if (buf->buf[len] != '\r')
+                       len ++;
+               if (!(len < buf->BufSize)) {
+                       buf->BufUsed = len;
+                       buf->buf[len+1] = '\0';
+                       IncreaseBuf(buf, 1, -1);
+               }
+       }
+       buf->BufUsed = len;
+       buf->buf[len] = '\0';
+       return len - slen;
+}
+
+void StrBufCutLeft(StrBuf *Buf, int nChars)
+{
+       if (nChars >= Buf->BufUsed) {
+               FlushStrBuf(Buf);
+               return;
+       }
+       memmove(Buf->buf, Buf->buf + nChars, Buf->BufUsed - nChars);
+       Buf->BufUsed -= nChars;
+}
+
+void StrBufCutRight(StrBuf *Buf, int nChars)
+{
+       if (nChars >= Buf->BufUsed) {
+               FlushStrBuf(Buf);
+               return;
+       }
+       Buf->BufUsed -= nChars;
+       Buf->buf[Buf->BufUsed] = '\0';
+}
+
+
+/*
+ * string conversion function
+ */
+void StrBufEUid_unescapize(StrBuf *target, StrBuf *source) 
+{
+       int a, b, len;
+       char hex[3];
+       int target_length = 0;
+
+       if (target != NULL)
+               FlushStrBuf(target);
+
+       if (source == NULL ||target == NULL)
+       {
+               return;
+       }
+
+       len = source->BufUsed;
+       for (a = 0; a < len; ++a) {
+               if (target_length >= target->BufSize)
+                       IncreaseBuf(target, 1, -1);
+
+               if (source->buf[a] == '=') {
+                       hex[0] = source->buf[a + 1];
+                       hex[1] = source->buf[a + 2];
+                       hex[2] = 0;
+                       b = 0;
+                       sscanf(hex, "%02x", &b);
+                       target->buf[target_length] = b;
+                       target->buf[++target_length] = 0;
+                       a += 2;
+               }
+               else {
+                       target->buf[target_length] = source->buf[a];
+                       target->buf[++target_length] = 0;
+               }
+       }
+}
+