This is an automated email from the git hooks/post-receive script.

git pushed a commit to branch master
in repository e16.

View the commit online.

commit 890173a5cfb2fd3ec326adfb40cd06c1f65a9da4
Author: Kim Woelders <k...@woelders.dk>
AuthorDate: Mon Jun 26 15:43:28 2023 +0200

    Move some string handling functions around
    
    To a maybe sligthly more logical location.
---
 src/memory.c | 292 +---------------------------------------------------------
 src/string.c | 295 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 src/util.h   |  22 ++---
 3 files changed, 306 insertions(+), 303 deletions(-)

diff --git a/src/memory.c b/src/memory.c
index ce4d6dfc..bee0915d 100644
--- a/src/memory.c
+++ b/src/memory.c
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2000-2007 Carsten Haitzler, Geoff Harrison and various contributors
- * Copyright (C) 2005-2022 Kim Woelders
+ * Copyright (C) 2005-2023 Kim Woelders
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to
@@ -66,24 +66,6 @@ EfreeDup(char **p, const char *s)
    *p = Estrdup(s);
 }
 
-char               *
-Estrtrim(char *s)
-{
-   int                 l;
-
-   while (*s == ' ')
-      s++;
-   if (!*s)
-      return s;
-
-   l = strlen(s);
-   while (isspace(s[l - 1]))
-      l--;
-   s[l] = '\0';
-
-   return s;
-}
-
 char               *
 Estrdup(const char *s)
 {
@@ -137,278 +119,6 @@ Estrdupcat2(char *ss, const char *s1, const char *s2)
    return s;
 }
 
-#if 0				/* Unused */
-char              **
-StrlistDup(char **lst, int num)
-{
-   char              **ss;
-   int                 i;
-
-   if (!lst || num <= 0)
-      return NULL;
-
-   ss = EMALLOC(char *, num + 1);
-   for (i = 0; i < num; i++)
-      ss[i] = Estrdup(lst[i]);
-   ss[i] = NULL;
-
-   return ss;
-}
-#endif
-
-void
-StrlistFree(char **lst, int num)
-{
-   if (!lst)
-      return;
-   while (num--)
-      Efree(lst[num]);
-   Efree(lst);
-}
-
-#if 0				/* FIXME - Remove? */
-char               *
-StrlistJoin(char **lst, int num)
-{
-   int                 i, size;
-   char               *s;
-
-   if (!lst || num <= 0)
-      return NULL;
-
-   s = NULL;
-
-   size = strlen(lst[0]) + 1;
-   s = EMALLOC(char, size);
-   strcpy(s, lst[0]);
-   for (i = 1; i < num; i++)
-     {
-	size += strlen(lst[i]) + 1;
-	s = EREALLOC(char, s, size);
-
-	strcat(s, " ");
-	strcat(s, lst[i]);
-     }
-
-   return s;
-}
-#endif
-
-char               *
-StrlistEncodeEscaped(char *buf, int len, char **lst, int num)
-{
-   int                 i, j, ch;
-   char               *s, *p;
-
-   if (!lst || num <= 0)
-      return NULL;
-
-   j = 0;
-   s = buf;
-   p = lst[0];
-   for (i = 0; i < len - 2; i++)
-     {
-	if (!p)			/* A string list should not contain NULL items */
-	   break;
-
-	ch = *p++;
-	switch (ch)
-	  {
-	  default:
-	     *s++ = ch;
-	     break;
-	  case '\0':
-	     if (++j >= num)
-		goto done;
-	     p = lst[j];
-	     if (!p || !p[0])
-		goto done;
-	     *s++ = ' ';
-	     break;
-	  case ' ':
-	     *s++ = '\\';
-	     *s++ = ' ';
-	     i++;
-	     break;
-	  }
-     }
-
- done:
-   *s = '\0';
-   return buf;
-}
-
-static int
-_StrlistDecodeArgLen(const char *str, const char **pnext)
-{
-   int                 len, ch, delim;
-   const char         *s;
-
-   len = 0;
-   delim = '\0';
-   for (s = str;; s++)
-     {
-	ch = *s;
-	switch (ch)
-	  {
-	  default:
-	     break;
-
-	  case '\0':
-	     len = s - str;
-	     goto done;
-
-	  case ' ':
-	     if (delim)
-		break;
-	     if (s > str && s[-1] == '\\')
-		break;
-	     len = s - str;
-	     s += 1;
-	     goto done;
-
-	  case '\'':
-	  case '"':
-	     if (ch == delim)
-		delim = '\0';
-	     else
-		delim = ch;
-	     break;
-	  }
-     }
-
- done:
-   *pnext = s;
-   return len;
-}
-
-static char        *
-_StrlistDecodeArgParse(const char *str, int len)
-{
-   char               *buf, *p;
-   int                 i, ch, ch_last, delim;
-
-   buf = EMALLOC(char, len + 1);
-
-   p = buf;
-   ch_last = '\0';
-   delim = '\0';
-   for (i = 0; i < len; i++)
-     {
-	ch = str[i];
-	switch (ch)
-	  {
-	  default:
-	     if (ch_last == '\\' && ch != ' ')
-		*p++ = '\\';	/* TBD!!! */
-	     *p++ = ch;
-	     break;
-
-	  case '\\':
-	     break;
-
-	  case '\'':
-	  case '"':
-	     if (!delim)
-		delim = ch;	/* Quote start */
-	     else if (delim == ch)
-		delim = '\0';	/* Quote end */
-	     else
-		*p++ = ch;
-	     break;
-	  }
-	ch_last = ch;
-     }
-
-   *p++ = '\0';
-   return buf;
-}
-
-char              **
-StrlistDecodeEscaped(const char *str, int *pnum)
-{
-   int                 num, len;
-   const char         *s, *p;
-   char              **lst;
-
-   if (!str)
-      return NULL;
-
-   lst = NULL;
-   num = 0;
-   s = str;
-   for (;; s = p)
-     {
-	/* Find next token */
-	while (*s == ' ')
-	   s++;
-	if (*s == '\0')
-	   break;
-
-	/* Find token extents (including quoting etc.) */
-	len = _StrlistDecodeArgLen(s, &p);
-
-	/* Add token */
-	lst = EREALLOC(char *, lst, num + 1);
-	lst[num++] = _StrlistDecodeArgParse(s, len);
-     }
-
-   /* Append NULL item */
-   lst = EREALLOC(char *, lst, num + 1);
-
-   lst[num] = NULL;
-
-   *pnum = num;
-   return lst;
-}
-
-char              **
-StrlistFromString(const char *str, int delim, int *num)
-{
-   const char         *s, *p;
-   char              **lst;
-   int                 n, len;
-
-   lst = NULL;
-   n = 0;
-   for (s = str; s; s = p)
-     {
-	p = strchr(s, delim);
-	if (p)
-	  {
-	     len = p - s;
-	     p++;
-	  }
-	else
-	  {
-	     len = strlen(s);
-	  }
-	if (len <= 0)
-	   continue;
-
-	lst = EREALLOC(char *, lst, n + 2);
-
-	lst[n++] = Estrndup(s, len);
-     }
-
-   if (lst)
-      lst[n] = NULL;
-   *num = n;
-   return lst;
-}
-
-static int
-_qsort_strcmp(const void *s1, const void *s2)
-{
-   return strcmp(*(const char **)s1, *(const char **)s2);
-}
-
-void
-StrlistSort(char **lst, int len)
-{
-   qsort(lst, (unsigned int)len, sizeof(char *), _qsort_strcmp);
-}
-
 void
 Esetenv(const char *name, const char *value)
 {
diff --git a/src/string.c b/src/string.c
index 8175f512..e479b3d3 100644
--- a/src/string.c
+++ b/src/string.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008-2022 Kim Woelders
+ * Copyright (C) 2008-2023 Kim Woelders
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to
@@ -120,3 +120,296 @@ EnvSubst(const char *str, char *bptr, unsigned int blen)
 	   nw += snprintf(bptr + nw, blen - nw, "%s", p1);
      }
 }
+
+/*
+ * Trim leading and trailing whitespace
+ */
+char               *
+Estrtrim(char *s)
+{
+   int                 l;
+
+   while (isspace(*s))
+      s++;
+   if (!*s)
+      return s;
+
+   l = strlen(s);
+   while (isspace(s[l - 1]))
+      l--;
+   s[l] = '\0';
+
+   return s;
+}
+
+#if 0				/* Unused */
+char              **
+StrlistDup(char **lst, int num)
+{
+   char              **ss;
+   int                 i;
+
+   if (!lst || num <= 0)
+      return NULL;
+
+   ss = EMALLOC(char *, num + 1);
+   for (i = 0; i < num; i++)
+      ss[i] = Estrdup(lst[i]);
+   ss[i] = NULL;
+
+   return ss;
+}
+#endif
+
+void
+StrlistFree(char **lst, int num)
+{
+   if (!lst)
+      return;
+   while (num--)
+      Efree(lst[num]);
+   Efree(lst);
+}
+
+#if 0				/* FIXME - Remove? */
+char               *
+StrlistJoin(char **lst, int num)
+{
+   int                 i, size;
+   char               *s;
+
+   if (!lst || num <= 0)
+      return NULL;
+
+   s = NULL;
+
+   size = strlen(lst[0]) + 1;
+   s = EMALLOC(char, size);
+   strcpy(s, lst[0]);
+   for (i = 1; i < num; i++)
+     {
+	size += strlen(lst[i]) + 1;
+	s = EREALLOC(char, s, size);
+
+	strcat(s, " ");
+	strcat(s, lst[i]);
+     }
+
+   return s;
+}
+#endif
+
+char               *
+StrlistEncodeEscaped(char *buf, int len, char **lst, int num)
+{
+   int                 i, j, ch;
+   char               *s, *p;
+
+   if (!lst || num <= 0)
+      return NULL;
+
+   j = 0;
+   s = buf;
+   p = lst[0];
+   for (i = 0; i < len - 2; i++)
+     {
+	if (!p)			/* A string list should not contain NULL items */
+	   break;
+
+	ch = *p++;
+	switch (ch)
+	  {
+	  default:
+	     *s++ = ch;
+	     break;
+	  case '\0':
+	     if (++j >= num)
+		goto done;
+	     p = lst[j];
+	     if (!p || !p[0])
+		goto done;
+	     *s++ = ' ';
+	     break;
+	  case ' ':
+	     *s++ = '\\';
+	     *s++ = ' ';
+	     i++;
+	     break;
+	  }
+     }
+
+ done:
+   *s = '\0';
+   return buf;
+}
+
+static int
+_StrlistDecodeArgLen(const char *str, const char **pnext)
+{
+   int                 len, ch, delim;
+   const char         *s;
+
+   len = 0;
+   delim = '\0';
+   for (s = str;; s++)
+     {
+	ch = *s;
+	switch (ch)
+	  {
+	  default:
+	     break;
+
+	  case '\0':
+	     len = s - str;
+	     goto done;
+
+	  case ' ':
+	     if (delim)
+		break;
+	     if (s > str && s[-1] == '\\')
+		break;
+	     len = s - str;
+	     s += 1;
+	     goto done;
+
+	  case '\'':
+	  case '"':
+	     if (ch == delim)
+		delim = '\0';
+	     else
+		delim = ch;
+	     break;
+	  }
+     }
+
+ done:
+   *pnext = s;
+   return len;
+}
+
+static char        *
+_StrlistDecodeArgParse(const char *str, int len)
+{
+   char               *buf, *p;
+   int                 i, ch, ch_last, delim;
+
+   buf = EMALLOC(char, len + 1);
+
+   p = buf;
+   ch_last = '\0';
+   delim = '\0';
+   for (i = 0; i < len; i++)
+     {
+	ch = str[i];
+	switch (ch)
+	  {
+	  default:
+	     if (ch_last == '\\' && ch != ' ')
+		*p++ = '\\';	/* TBD!!! */
+	     *p++ = ch;
+	     break;
+
+	  case '\\':
+	     break;
+
+	  case '\'':
+	  case '"':
+	     if (!delim)
+		delim = ch;	/* Quote start */
+	     else if (delim == ch)
+		delim = '\0';	/* Quote end */
+	     else
+		*p++ = ch;
+	     break;
+	  }
+	ch_last = ch;
+     }
+
+   *p++ = '\0';
+   return buf;
+}
+
+char              **
+StrlistDecodeEscaped(const char *str, int *pnum)
+{
+   int                 num, len;
+   const char         *s, *p;
+   char              **lst;
+
+   if (!str)
+      return NULL;
+
+   lst = NULL;
+   num = 0;
+   s = str;
+   for (;; s = p)
+     {
+	/* Find next token */
+	while (*s == ' ')
+	   s++;
+	if (*s == '\0')
+	   break;
+
+	/* Find token extents (including quoting etc.) */
+	len = _StrlistDecodeArgLen(s, &p);
+
+	/* Add token */
+	lst = EREALLOC(char *, lst, num + 1);
+	lst[num++] = _StrlistDecodeArgParse(s, len);
+     }
+
+   /* Append NULL item */
+   lst = EREALLOC(char *, lst, num + 1);
+
+   lst[num] = NULL;
+
+   *pnum = num;
+   return lst;
+}
+
+char              **
+StrlistFromString(const char *str, int delim, int *num)
+{
+   const char         *s, *p;
+   char              **lst;
+   int                 n, len;
+
+   lst = NULL;
+   n = 0;
+   for (s = str; s; s = p)
+     {
+	p = strchr(s, delim);
+	if (p)
+	  {
+	     len = p - s;
+	     p++;
+	  }
+	else
+	  {
+	     len = strlen(s);
+	  }
+	if (len <= 0)
+	   continue;
+
+	lst = EREALLOC(char *, lst, n + 2);
+
+	lst[n++] = Estrndup(s, len);
+     }
+
+   if (lst)
+      lst[n] = NULL;
+   *num = n;
+   return lst;
+}
+
+static int
+_qsort_strcmp(const void *s1, const void *s2)
+{
+   return strcmp(*(const char **)s1, *(const char **)s2);
+}
+
+void
+StrlistSort(char **lst, int len)
+{
+   qsort(lst, (unsigned int)len, sizeof(char *), _qsort_strcmp);
+}
diff --git a/src/util.h b/src/util.h
index 27fea8fb..6bbf4f43 100644
--- a/src/util.h
+++ b/src/util.h
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2000-2007 Carsten Haitzler, Geoff Harrison and various contributors
- * Copyright (C) 2004-2022 Kim Woelders
+ * Copyright (C) 2004-2023 Kim Woelders
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to
@@ -97,12 +97,20 @@ void                EfreeDup(char **p, const char *s);
 
 #define STRCPY(dst, src) do { src[sizeof(dst)-1] = '\0'; strcpy(dst, src); } while(0)
 
-char               *Estrtrim(char *s);
-
 char               *Estrdup(const char *s);
 char               *Estrndup(const char *s, size_t n);
 char               *Estrdupcat2(char *ss, const char *s1, const char *s2);
 
+void                Esetenv(const char *name, const char *value);
+
+/* misc.c */
+void __PRINTF__     Eprintf(const char *fmt, ...);
+
+/* string.c */
+void                EnvSubst(const char *str, char *bptr, unsigned int blen);
+
+char               *Estrtrim(char *s);
+
 char              **StrlistDup(char **lst, int num);
 void                StrlistFree(char **lst, int num);
 char               *StrlistJoin(char **lst, int num);
@@ -113,14 +121,6 @@ char              **StrlistFromString(const char *str, int delim, int *num);
 
 void                StrlistSort(char **lst, int num);
 
-void                Esetenv(const char *name, const char *value);
-
-/* misc.c */
-void __PRINTF__     Eprintf(const char *fmt, ...);
-
-/* string.c */
-void                EnvSubst(const char *str, char *bptr, unsigned int blen);
-
 #define Evsnprintf vsnprintf
 #define Esnprintf snprintf
 

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.

Reply via email to