]> code.citadel.org Git - citadel.git/blobdiff - libcitadel/lib/tools.c
Awesome sauce, read description below for details.
[citadel.git] / libcitadel / lib / tools.c
index 7523b17e3647da79ff9d8d2f60f311d67684f39c..ffd4a5dc1d7a1ebbe6e57cb147830ce36bf69e5e 100644 (file)
@@ -1,7 +1,7 @@
 // A basic toolset containing miscellaneous functions for string manipluation,
 // encoding/decoding, and a bunch of other stuff.
 //
-// Copyright (c) 1987-2022 by the citadel.org team
+// Copyright (c) 1987-2023 by the citadel.org team
 //
 // This program is open source software.  Use, duplication, or disclosure
 // is subject to the terms of the GNU General Public License, version 3.
 #include <sys/stat.h>
 #include <errno.h>
 #include <limits.h>
-
-#if TIME_WITH_SYS_TIME
-# include <sys/time.h>
-# include <time.h>
-#else
-# if HAVE_SYS_TIME_H
-#  include <sys/time.h>
-# else
-#  include <time.h>
-# endif
-#endif
-
+#include <time.h>
 #include "libcitadel.h"
 
-
 #define TRUE  1
 #define FALSE 0
 
@@ -46,8 +34,7 @@ typedef unsigned char byte;         // Byte type
 int safestrncpy(char *dest, const char *src, size_t n) {
        int i = 0;
 
-       if (dest == NULL || src == NULL)
-       {
+       if (dest == NULL || src == NULL) {
                fprintf(stderr, "safestrncpy: NULL argument\n");
                abort();
        }
@@ -431,53 +418,60 @@ const char *cmemreadlinelen(const char *start, char *buf, int maxlen, int *retle
 
 // Strip a boundarized substring out of a string (for example, remove parentheses and anything inside them).
 int stripout(char *str, char leftboundary, char rightboundary) {
-       int a;
-        int lb = (-1);
-        int rb = (-1);
-
-        for (a = 0; a < strlen(str); ++a) {
-                if (str[a] == leftboundary) lb = a;
-                if (str[a] == rightboundary) rb = a;
-        }
-
-        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;
-}
+       long lb = (-1);
+       long rb = (-1);
+
+       if (!str) {
+               return 0;
+       }
+
+       for (int a = 0; str[a]; ++a) {
+               if ((lb==-1) && (str[a] == leftboundary)) {
+                       lb = a;
+               } else if (str[a] == rightboundary) {
+                       rb = a;
+               }
+       }
 
+       if ((lb==-1) || (rb <= lb)) {
+               return 0;
+       }
+
+       strcpy(str + lb, str + rb + 1);
+       return 1;
+}
 
 // Reduce a string down to a boundarized substring (for example, remove
 // parentheses and anything outside them).
 long stripallbut(char *str, char leftboundary, char rightboundary) {
-       long len = 0;
-
-       char *lb = NULL;
-       char *rb = NULL;
-
-       lb = strrchr(str, leftboundary);
-       if (lb != NULL) {
-               ++lb;
-               rb = strchr(str, rightboundary);
-               if ((rb != NULL) && (rb >= lb))  {
-                       *rb = 0;
-                       fflush(stderr);
-                       len = (long)rb - (long)lb;
-                       memmove(str, lb, len);
-                       str[len] = 0;
-                       return(len);
+       long lb = (-1);
+       long rb = (-1);
+       long orig_len = 0;
+
+       if (!str) {
+               return 0;
+       }
+
+       while (str[orig_len]) {
+               if ((lb==-1) && (str[orig_len] == leftboundary)) {
+                       lb = orig_len;
+               } else if (str[orig_len] == rightboundary) {
+                       rb = orig_len;
                }
+               orig_len++;
        }
 
-       return (long)strlen(str);
-}
+       if ((lb==-1) || (rb <= lb)) {
+               return orig_len;
+       }
+
+       fflush(stderr);
 
+       long new_len = rb - lb - 1;
+       memmove(str, str + lb + 1, new_len);
+       str[new_len] = 0;
+       return new_len;
+}
 
 char *myfgets(char *s, int size, FILE *stream) {
        char *ret = fgets(s, size, stream);
@@ -808,27 +802,6 @@ int pattern2(char *search, char *patn) {
 }
 
 
-/*
- * Strip leading and trailing spaces from a string; with premeasured and adjusted length.
- * buf - the string to modify
- * len - length of the string. 
- */
-void string_trimlen(char *buf, int *len) {
-       int delta = 0;
-       if (*len == 0) return;
-       while ((*len > delta) && (isspace(buf[delta]))){
-               delta ++;
-       }
-       memmove (buf, &buf[delta], *len - delta + 1);
-       (*len) -=delta;
-
-       if (*len == 0) return;
-       while (isspace(buf[(*len) - 1])){
-               buf[--(*len)] = '\0';
-       }
-}
-
-
 /*
  * Convert all whitespace characters in a supplied string to underscores
  */