[libvirt] [PATCHv3 2/3] util: add virTrimSpaces

Eric Blake eblake at redhat.com
Wed Jun 29 17:52:43 UTC 2011


The next patch wants to adjust an end pointer to trim trailing
spaces but without modifying the underlying string, but a more
generally useful ability to trim trailing spaces in place is
also worth providing.

* src/util/util.h (virTrimSpaces, virSkipSpacesBackwards): New
prototypes.
* src/util/util.c (virTrimSpaces, virSkipSpacesBackwards): New
functions.
* src/libvirt_private.syms (util.h): Export new functions.
Inspired by a patch by Minoru Usui.
---
 src/libvirt_private.syms |    2 +
 src/util/util.c          |   53 ++++++++++++++++++++++++++++++++++++++++++++++
 src/util/util.h          |    4 +++
 3 files changed, 59 insertions(+), 0 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 024b3f1..a444a40 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1031,6 +1031,7 @@ virSetNonBlock;
 virSetUIDGID;
 virSkipSpaces;
 virSkipSpacesAndBackslash;
+virSkipSpacesBackwards;
 virStrToDouble;
 virStrToLong_i;
 virStrToLong_l;
@@ -1042,6 +1043,7 @@ virStrcpy;
 virStrncpy;
 virTimeMs;
 virTimestamp;
+virTrimSpaces;
 virVasprintf;


diff --git a/src/util/util.c b/src/util/util.c
index 27eefb2..f6601b9 100644
--- a/src/util/util.c
+++ b/src/util/util.c
@@ -1565,6 +1565,59 @@ virSkipSpacesAndBackslash(const char **str)
 }

 /**
+ * virTrimSpaces:
+ * @str: string to modify to remove all trailing spaces
+ * @endp: track the end of the string
+ *
+ * If @endp is NULL on entry, then all spaces prior to the trailing
+ * NUL in @str are removed, by writing NUL into the appropriate
+ * location.  If @endp is non-NULL but points to a NULL pointer,
+ * then all spaces prior to the trailing NUL in @str are removed,
+ * NUL is written to the new string end, and endp is set to the
+ * location of the (new) string end.  If @endp is non-NULL and
+ * points to a non-NULL pointer, then that pointer is used as
+ * the end of the string, endp is set to the (new) location, but
+ * no NUL pointer is written into the string.
+ */
+void
+virTrimSpaces(char *str, char **endp)
+{
+    char *end;
+
+    if (!endp || !*endp)
+        end = str + strlen(str);
+    else
+        end = *endp;
+    while (end > str && c_isspace(end[-1]))
+        end--;
+    if (endp) {
+        if (!*endp)
+            *end = '\0';
+        *endp = end;
+    } else {
+        *end = '\0';
+    }
+}
+
+/**
+ * virSkipSpacesBackwards:
+ * @str: start of string
+ * @endp: on entry, *endp must be NULL or a location within @str, on exit,
+ * will be adjusted to skip trailing spaces
+ */
+void
+virSkipSpacesBackwards(const char *str, char **endp)
+{
+    /* Casting away const is safe, since virTrimSpaces does not
+     * modify string with this particular usage.  */
+    char *s = (char*) str;
+
+    if (!*endp)
+        *endp = s + strlen(s);
+    virTrimSpaces(s, endp);
+}
+
+/**
  * virParseNumber:
  * @str: pointer to the char pointer used
  *
diff --git a/src/util/util.h b/src/util/util.h
index 8dec78a..2ca592b 100644
--- a/src/util/util.h
+++ b/src/util/util.h
@@ -168,6 +168,10 @@ int virMacAddrCompare (const char *mac1, const char *mac2);

 void virSkipSpaces(const char **str);
 void virSkipSpacesAndBackslash(const char **str);
+void virTrimSpaces(char *str, char **endp) ATTRIBUTE_NONNULL(1);
+void virSkipSpacesBackwards(const char *str, char **endp)
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
+
 int virParseNumber(const char **str);
 int virParseVersionString(const char *str, unsigned long *version);
 int virAsprintf(char **strp, const char *fmt, ...)
-- 
1.7.4.4




More information about the libvir-list mailing list