]> code.citadel.org Git - citadel.git/blobdiff - libcitadel/lib/tools.c
* move utf8 handling stuff into strbuf, so we can be more exact about our buffer...
[citadel.git] / libcitadel / lib / tools.c
index 6cb0646ea17133bcdd42937eb4c2dd3f1a796908..03d3213de62542568426f793b5cbac89fc4321c0 100644 (file)
@@ -456,48 +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
  */
-long striplt(char *buf)
+void StripSlashes(char *Dir, int TrailingSlash)
 {
-       int CountTrail = 0;
-       int FromStart = 1;
-       char *aptr, *bptr;
-
-       if ((buf==NULL) || (IsEmptyStr(buf)))
-               return 0;
+       char *a, *b;
 
-       bptr = aptr = buf;
+       a = b = Dir;
 
-       while (!IsEmptyStr(aptr)) {
-               if (isspace(*aptr)) {
-                       if (FromStart)
-                               aptr ++;
-                       else {
-                               CountTrail ++;
-                               *bptr = *aptr;
-                               aptr++; bptr++;
-                       }
+       while (!IsEmptyStr(a)) {
+               if (*a == '/') {
+                       while (*a == '/')
+                               a++;
+                       *b = '/';
+                       b++;
                }
                else {
-                       CountTrail = 0;
-                       *bptr = *aptr;
-                       aptr++; bptr++;
+                       *b = *a;
+                       b++; a++;
                }
        }
-
-       if (CountTrail > 0) {
-               bptr -= CountTrail;
+       if ((TrailingSlash) && (*(b - 1) != '/')){
+               *b = '/';
+               b++;
        }
+       *b = '\0';
 
-       *bptr = '\0';
-       return bptr - buf;
 }
 
+/*
+ * 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;
+}
 
 
 /**
@@ -956,97 +976,6 @@ void stripltlen(char *buf, int *len)
        }
 }
 
-/**
- * \brief detect whether this char starts an utf-8 encoded char
- * \param Char character to inspect
- * \returns yes or no
- */
-inline int Ctdl_IsUtf8SequenceStart(char Char)
-{
-/** 11??.???? indicates an UTF8 Sequence. */
-       return ((Char & 0xC0) != 0);
-}
-
-/**
- * \brief evaluate the length of an utf8 special character sequence
- * \param Char the character to examine
- * \returns width of utf8 chars in bytes
- */
-inline int Ctdl_GetUtf8SequenceLength(char Char)
-{
-       int n = 1;
-        char test = (1<<7);
-       
-       while ((n < 8) && ((test & Char) != 0)) {
-               test = test << 1;
-               n ++;
-       }
-       if (n > 6)
-               n = 1;
-       return n;
-}
-
-/**
- * \brief measure the number of glyphs in an UTF8 string...
- * \param str string to measure
- * \returns the length of str
- */
-int Ctdl_Utf8StrLen(char *str)
-{
-       int n = 0;
-       int m = 0;
-       char *aptr;
-
-       if (str == NULL)
-               return n;
-       aptr = str;
-       while (*aptr != '\0') {
-               if (Ctdl_IsUtf8SequenceStart(*aptr)){
-                       m = Ctdl_GetUtf8SequenceLength(*aptr);
-                       while ((m-- > 0) && (*aptr++ != '\0'))
-                               n ++;
-               }
-               else {
-                       n++;
-                       aptr++;
-               }
-                       
-       }
-       return n;
-}
-
-/**
- * \brief cuts a string after maxlen glyphs
- * \param str string to cut to maxlen glyphs
- * \param maxlen how long may the string become?
- * \returns pointer to maxlen or the end of the string
- */
-char *Ctdl_Utf8StrCut(char *str, int maxlen)
-{
-       int n = 0, m = 0;
-       char *aptr;
-
-       if (str == NULL)
-               return NULL;
-       aptr = str;
-       while (*aptr != '\0') {
-               if (Ctdl_IsUtf8SequenceStart(*aptr)){
-                       m = Ctdl_GetUtf8SequenceLength(*aptr);
-                       while ((m-- > 0) && (*aptr++ != '\0'))
-                               n ++;
-               }
-               else {
-                       n++;
-                       aptr++;
-               }
-               if (n > maxlen) {
-                       *aptr = '\0';
-                       return aptr;
-               }                       
-       }
-       return aptr;
-}
-
 
 /*
  * Convert all whitespace characters in a supplied string to underscores