]> code.citadel.org Git - citadel.git/blobdiff - libcitadel/lib/tools.c
* Commented out the 'PrintFlat' and 'PrintFile' functions
[citadel.git] / libcitadel / lib / tools.c
index 606449da1d6ae270244b6b005025b6498e4a5c21..fac3cb5ad6644f08cd52efc4dca0659f633b1155 100644 (file)
@@ -73,8 +73,16 @@ const byte dtable[256] = {
        128, 128, 0
 };
 
-
-char *safestrncpy(char *dest, const char *src, size_t n)
+/*
+ * 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)
 {
        int i = 0;
 
@@ -85,11 +93,11 @@ char *safestrncpy(char *dest, const char *src, size_t n)
 
        do {
                dest[i] = src[i];
-               if (dest[i] == 0) return(dest);
+               if (dest[i] == 0) return i;
                ++i;
        } while (i<n);
        dest[n - 1] = 0;
-       return dest;
+       return -i;
 }
 
 
@@ -438,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);
@@ -766,6 +774,9 @@ 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);
 
@@ -820,7 +831,7 @@ void CtdlMakeTempFileName(char *name, int len) {
        int i = 0;
 
        while (i++, i < 100) {
-               snprintf(name, len, "/tmp/ctdl.%4lx.%04x",
+               snprintf(name, len, "/tmp/ctdl.%04lx.%04x",
                        (long)getpid(),
                        rand()
                );
@@ -889,10 +900,10 @@ int pattern2(char *search, char *patn)
 }
 
 
-/**
- * \brief Strip leading and trailing spaces from a string; with premeasured and adjusted length.
- * \param buf the string to modify
- * \param len length of the string. 
+/*
+ * Strip leading and trailing spaces from a string; with premeasured and adjusted length.
+ * buf - the string to modify
+ * len - length of the string. 
  */
 void stripltlen(char *buf, int *len)
 {
@@ -910,3 +921,114 @@ 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
+ */
+void convert_spaces_to_underscores(char *str)
+{
+       int len;
+       int i;
+
+       if (!str) return;
+
+       len = strlen(str);
+       for (i=0; i<len; ++i) {
+               if (isspace(str[i])) {
+                       str[i] = '_';
+               }
+       }
+}
+
+