]> code.citadel.org Git - citadel.git/blobdiff - libcitadel/lib/tools.c
* add slash-stripper
[citadel.git] / libcitadel / lib / tools.c
index 15e346778aad74a2c3909c3425c5f39a9844667a..5662133be92854609a719e33d314a85281ad3646 100644 (file)
@@ -73,12 +73,14 @@ const byte dtable[256] = {
        128, 128, 0
 };
 
-/**
- * \brief copy a string into a buffer of a known size. abort if we exceed the limits
- * \param dest the targetbuffer
- * \param src the source string
- * \param n the size od dest
- * \returns the number of characters copied if dest is big enough, -n if not.
+/*
+ * copy a string into a buffer of a known size. abort if we exceed the limits
+ *
+ * dest        the targetbuffer
+ * src the source string
+ * n   the size od dest
+ *
+ * returns the number of characters copied if dest is big enough, -n if not.
  */
 int safestrncpy(char *dest, const char *src, size_t n)
 {
@@ -444,7 +446,7 @@ char *rfc2047encode(char *line, long length)
                return strdup(line);
        }
 
-       result = (char*) malloc(strlen(UTF8_HEADER) + 4 + length * 2);
+       result = (char*) malloc(sizeof(UTF8_HEADER) + 4 + length * 2);
        strncpy (result, UTF8_HEADER, strlen (UTF8_HEADER));
        CtdlEncodeBase64(result + strlen(UTF8_HEADER), line, length, 0);
        end = strlen (result);
@@ -454,30 +456,68 @@ char *rfc2047encode(char *line, long length)
        return result;
 }
 
-
 /*
- * Strip leading and trailing spaces from a string
+ * removes double slashes from pathnames
+ * allows / disallows trailing slashes
  */
-void striplt(char *buf)
+void StripSlashes(char *Dir, int TrailingSlash)
 {
-       size_t len;
-       int a;
+       char *a, *b;
+
+       a = b = Dir;
+
+       while (!IsEmptyStr(a)) {
+               if (*a == '/') {
+                       while (*a == '/')
+                               a++;
+                       *b = '/';
+                       b++;
+               }
+               else {
+                       *b = *a;
+                       b++; a++;
+               }
+       }
+       if ((TrailingSlash) && (*(b - 1) != '/')){
+               *b = '/';
+               b++;
+       }
+       *b = '\0';
 
-       if (buf==NULL) return;
-       if (IsEmptyStr(buf)) return;
-       len = strlen(buf);
-        while ((!IsEmptyStr(buf)) && (isspace(buf[len - 1])))
-                buf[--len] = 0;
-       if (IsEmptyStr(buf)) return;
-       a = 0;
-        while ((!IsEmptyStr(buf)) && (isspace(buf[a])))
-               a++;
-       if (a > 0)
-                memmove(buf, &buf[a], len - a + 1);
 }
 
+/*
+ * Strip leading and trailing spaces from a string
+ */
+size_t striplt(char *buf) {
+       char *first_nonspace = NULL;
+       char *last_nonspace = NULL;
+       char *ptr;
+       size_t new_len = 0;
+
+       if (!buf) {
+               return 0;
+       }
 
+       for (ptr=buf; *ptr!=0; ++ptr) {
+               if (!isspace(*ptr)) {
+                       if (!first_nonspace) {
+                               first_nonspace = ptr;
+                       }
+                       last_nonspace = ptr;
+               }
+       }
 
+       if ((!first_nonspace) || (!last_nonspace)) {
+               buf[0] = 0;
+               return 0;
+       }
+
+       new_len = last_nonspace - first_nonspace + 1;
+       memmove(buf, first_nonspace, new_len);
+       buf[new_len] = 0;
+       return new_len;
+}
 
 
 /**
@@ -553,7 +593,7 @@ void fmt_date(char *buf, size_t n, time_t thetime, int seconds) {
  * Determine whether the specified message number is contained within the
  * specified sequence set.
  */
-int is_msg_in_sequence_set(char *mset, long msgnum) {
+int is_msg_in_sequence_set(const char *mset, long msgnum) {
        int num_sets;
        int s;
        char setstr[128], lostr[128], histr[128];
@@ -646,7 +686,7 @@ char *memreadlinelen(char *start, char *buf, int maxlen, int *retlen)
  * Strip a boundarized substring out of a string (for example, remove
  * parentheses and anything inside them).
  */
-void stripout(char *str, char leftboundary, char rightboundary) {
+int stripout(char *str, char leftboundary, char rightboundary) {
        int a;
         int lb = (-1);
         int rb = (-1);
@@ -658,12 +698,14 @@ void stripout(char *str, char leftboundary, char rightboundary) {
 
         if ( (lb > 0) && (rb > lb) ) {
                 strcpy(&str[lb - 1], &str[rb + 1]);
+               return 1;
         }
 
         else if ( (lb == 0) && (rb > lb) ) {
                 strcpy(str, &str[rb + 1]);
+               return 1;
         }
-
+       return 0;
 }
 
 
@@ -763,21 +805,16 @@ void generate_uuid(char *buf) {
  * The code is roughly based on the strstr() replacement from 'tin' written
  * by Urs Jannsen.
  */
-char *bmstrcasestr(char *text, char *pattern) {
+inline char *_bmstrcasestr_len(char *text, size_t textlen, char *pattern, size_t patlen) {
 
        register unsigned char *p, *t;
        register int i, j, *delta;
        register size_t p1;
        int deltaspace[256];
-       size_t textlen;
-       size_t patlen;
 
        if (!text) return(NULL);
        if (!pattern) return(NULL);
 
-       textlen = strlen (text);
-       patlen = strlen (pattern);
-
        /* algorithm fails if pattern is empty */
        if ((p1 = patlen) == 0)
                return (text);
@@ -819,7 +856,29 @@ char *bmstrcasestr(char *text, char *pattern) {
        return (NULL);
 }
 
+/*
+ * bmstrcasestr() -- case-insensitive substring search
+ *
+ * This uses the Boyer-Moore search algorithm and is therefore quite fast.
+ * The code is roughly based on the strstr() replacement from 'tin' written
+ * by Urs Jannsen.
+ */
+char *bmstrcasestr(char *text, char *pattern) {
+       size_t textlen;
+       size_t patlen;
+
+       if (!text) return(NULL);
+       if (!pattern) return(NULL);
+
+       textlen = strlen (text);
+       patlen = strlen (pattern);
 
+       return _bmstrcasestr_len(text, textlen, pattern, patlen);
+}
+
+char *bmstrcasestr_len(char *text, size_t textlen, char *pattern, size_t patlen) {
+       return _bmstrcasestr_len(text, textlen, pattern, patlen);
+}
 
 /*
  * Local replacement for controversial C library function that generates
@@ -845,7 +904,7 @@ void CtdlMakeTempFileName(char *name, int len) {
  * Determine whether the specified message number is contained within the specified set.
  * Returns nonzero if the specified message number is in the specified message set string.
  */
-int is_msg_in_mset(char *mset, long msgnum) {
+int is_msg_in_mset(const char *mset, long msgnum) {
        int num_sets;
        int s;
        char setstr[SIZ], lostr[SIZ], histr[SIZ];       /* was 1024 */
@@ -879,10 +938,8 @@ int is_msg_in_mset(char *mset, long msgnum) {
 
 
 /*
- * \brief searches for a  paternn within asearch string
- * \param search the string to search 
- * \param patn the pattern to find in string
- * \returns position in string
+ * searches for a pattern within a search string
+ * returns position in string
  */
 int pattern2(char *search, char *patn)
 {
@@ -986,7 +1043,7 @@ int Ctdl_Utf8StrLen(char *str)
  */
 char *Ctdl_Utf8StrCut(char *str, int maxlen)
 {
-       int n, m = 0;
+       int n = 0, m = 0;
        char *aptr;
 
        if (str == NULL)
@@ -1029,3 +1086,4 @@ void convert_spaces_to_underscores(char *str)
        }
 }
 
+