[PATCH 04/12] utils: Add virStringUpdate

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



If @src is an empty string then @dst will be freed otherwise @dst will
be a duplicate of @src.

Signed-off-by: Marc Hartmayer <mhartmay@xxxxxxxxxxxxxxxxxx>
Reviewed-by: Boris Fiuczynski <fiuczy@xxxxxxxxxxxxx>
Reviewed-by: Stefan Zimmermann <stzi@xxxxxxxxxxxxx>
---
 src/libvirt_private.syms |  1 +
 src/util/virstring.c     | 27 +++++++++++++++++++++++++++
 src/util/virstring.h     |  2 ++
 3 files changed, 30 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index f8883dc50da2..c684f21905af 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2871,6 +2871,7 @@ virStringStripControlChars;
 virStringStripIPv6Brackets;
 virStringToUpper;
 virStringTrimOptionalNewline;
+virStringUpdate;
 virStrncpy;
 virStrndup;
 virStrToDouble;
diff --git a/src/util/virstring.c b/src/util/virstring.c
index 15f367af7c18..1e9bf1f561b5 100644
--- a/src/util/virstring.c
+++ b/src/util/virstring.c
@@ -1248,6 +1248,33 @@ virStringReplace(const char *haystack,
 
 
 /**
+ * virStringUpdate:
+ * @dest: where to update string
+ * @src: string to duplicate
+ *
+ * If @src is an empty string then @dest will be freed otherwise @dest
+ * will be a duplicate of @src.
+ *
+ * Returns -1 on failure (with OOM error reported), 0 if @dest was
+ * freed only, 1 if @src was copied
+ */
+int
+virStringUpdate(char **dest, const char *src)
+{
+    if (virStringIsEmpty(src)) {
+        VIR_FREE(*dest);
+        return 0;
+    } else {
+        char *temp = *dest;
+        if (VIR_STRDUP(*dest, src) < 0)
+            return -1;
+        VIR_FREE(temp);
+        return 1;
+    }
+}
+
+
+/**
  * virStringStripIPv6Brackets:
  * @str: the string to strip
  *
diff --git a/src/util/virstring.h b/src/util/virstring.h
index fa2ec1df4def..69f2cb69037d 100644
--- a/src/util/virstring.h
+++ b/src/util/virstring.h
@@ -290,6 +290,8 @@ char *virStringReplace(const char *haystack,
                        const char *oldneedle,
                        const char *newneedle)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3);
+int virStringUpdate(char **dest, const char *src)
+    ATTRIBUTE_NONNULL(1);
 
 void virStringStripIPv6Brackets(char *str);
 bool virStringHasChars(const char *str,
-- 
2.13.4

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux