* substituded two hot functions by ones that behave more like the cpu beats.
authorWilfried Göesgens <willi@citadel.org>
Fri, 10 Aug 2007 14:19:11 +0000 (14:19 +0000)
committerWilfried Göesgens <willi@citadel.org>
Fri, 10 Aug 2007 14:19:11 +0000 (14:19 +0000)
citadel/tools.c
citadel/tools.h

index 7650baede5f2a19d1f8ada2bfd02a51db700ed9f..6d62916b8cf79d159fa2fba09d814d5b13e5e9e1 100644 (file)
@@ -27,6 +27,7 @@
 
 #include "tools.h"
 #include "citadel.h"
+#include "sysdep_decls.h"
 
 #define TRUE  1
 #define FALSE 0
@@ -103,35 +104,90 @@ int num_tokens(const char *source, char tok)
 
 /*
  * extract_token() - a string tokenizer
+ * returns -1 if not found, or length of token.
  */
-void extract_token(char *dest, const char *source, int parmnum, char separator, int maxlen)
+long extract_token(char *dest, const char *source, int parmnum, char separator, int maxlen)
 {
-       char *d;                /* dest */
        const char *s;          /* source */
+       const char *start;
+       const char *end;
        int count = 0;
        int len = 0;
+       /* Locate desired parameter */
+       s = source;
+       while (1) {
+               /* End of string, bail! */
+               if (IsEmptyStr(s)) {
+                       *dest = '\0';
+                       return -1;              /* Parameter not found */
+               }
+               if (*s == separator) {
+                       count++;
+                       if (count == parmnum)
+                               start = s + 1; // we found da start border!
+                       else if (count == parmnum + 1)
+                       {
+                               end = s; // we found da end border!
+                               break; // so we're done.
+                       }
+               }
+               s++;
+       }
 
-       strcpy(dest, "");
+       len = end - start;
+       if (len > maxlen){
+               /** TODO: do find an alternative for this if we're not inside of the server
+               lprintf (ERROR, 
+                        "Warning: Tokenizer failed due to space. better use grab_token here? "
+                        "(Token: %d N: %d Extractstring at: %s FullString: %s", 
+                        (char)separator, parmnum, start, source);
+               */
+               *dest = '\0';
+               return -1;              /* Parameter exceeds space */
+       }               
+       memcpy(dest, start, len);
+       dest[len]='\0';
+       return len;
+}
+
+/*
+ * extract_token() - a string tokenizer
+ * returns -1 if not found, or length of token.
+ */
+long grab_token(char **dest, const char *source, int parmnum, char separator)
+{
+       const char *s;          /* source */
+       const char *start;
+       const char *end;
+       int count = 0;
+       int len = 0;
 
        /* Locate desired parameter */
        s = source;
-       while (count < parmnum) {
+       while (1) {
                /* End of string, bail! */
-               if (!*s) {
-                       s = NULL;
-                       break;
+               if (IsEmptyStr(s)) {
+                       *dest = '\0';
+                       return -1;              /* Parameter not found */
                }
                if (*s == separator) {
                        count++;
+                       if (count == parmnum)
+                               start = s + 1; // we found da start border!
+                       else if (count == parmnum + 1)
+                       {
+                               end = s; // we found da end border!
+                               break; // so we're done.
+                       }
                }
                s++;
        }
-       if (!s) return;         /* Parameter not found */
 
-       for (d = dest; *s && *s != separator && ++len<maxlen; s++, d++) {
-               *d = *s;
-       }
-       *d = 0;
+       len = end - start;
+       *dest = (char*)malloc (len + 1);
+       memcpy(*dest, start, len);
+       (*dest)[len]='\0';
+       return len;
 }
 
 
@@ -389,15 +445,20 @@ void striplt(char *buf)
 
 
 
-/* 
- * Return the number of occurances of character ch in string st
- */ 
-int haschar(const char *st, int ch)
+/**
+ * \brief check for the presence of a character within a string (returns count)
+ * \param st the string to examine
+ * \param ch the char to search
+ * \return the position inside of st
+ */
+int haschar(const char *st,int ch)
 {
-       int a, b;
+       const char *ptr;
+       int b;
        b = 0;
-       for (a = 0; a < strlen(st); ++a)
-               if (st[a] == ch)
+       ptr = st;
+       while (!IsEmptyStr(ptr))
+               if (*ptr == ch)
                        ++b;
        return (b);
 }
index d11c86105ac3d78cbd432dc0da6a597b5c41a861..247a91bac67d9ef29460b844085ee84dde03714e 100644 (file)
@@ -1,7 +1,8 @@
 /* $Id$ */
 char *safestrncpy(char *dest, const char *src, size_t n);
 int num_tokens (const char *source, char tok);
-void extract_token(char *dest, const char *source, int parmnum, char separator, int maxlen);
+long extract_token(char *dest, const char *source, int parmnum, char separator, int maxlen);
+long grab_token(char **dest, const char *source, int parmnum, char separator);
 int extract_int (const char *source, int parmnum);
 long extract_long (const char *source, int parmnum);
 unsigned long extract_unsigned_long(const char *source, int parmnum);