]> xenbits.xensource.com Git - people/dariof/libvirt.git/commitdiff
Introduce virTypedParamsReplaceString internal API
authorJiri Denemark <jdenemar@redhat.com>
Tue, 21 May 2013 13:11:56 +0000 (15:11 +0200)
committerJiri Denemark <jdenemar@redhat.com>
Mon, 24 Jun 2013 22:38:24 +0000 (00:38 +0200)
src/libvirt_private.syms
src/util/virtypedparam.c
src/util/virtypedparam.h

index af32b3e36fb85294a44829c14892fb3f5929dd3e..9570a94d01a04cb8dd80d3b05fc0559c148af70e 100644 (file)
@@ -1944,6 +1944,7 @@ virTPMCreateCancelPath;
 virTypedParameterAssign;
 virTypedParameterAssignFromStr;
 virTypedParamsCheck;
+virTypedParamsReplaceString;
 virTypedParamsValidate;
 
 
index 825148b00c233bb938726e997859e96d7e691089..129777d948b1d387caa3a32750262fd50f7b1eb2 100644 (file)
@@ -285,6 +285,71 @@ cleanup:
 }
 
 
+/**
+ * virTypedParamsReplaceString:
+ * @params: pointer to the array of typed parameters
+ * @nparams: number of parameters in the @params array
+ * @name: name of the parameter to set
+ * @value: the value to store into the parameter
+ *
+ * Sets new value @value to parameter called @name with char * type. If the
+ * parameter does not exist yet in @params, it is automatically created and
+ * @naprams is incremented by one. Otherwise current value of the parameter
+ * is freed on success. The function creates its own copy of @value string,
+ * which needs to be freed using virTypedParamsFree or virTypedParamsClear.
+ *
+ * Returns 0 on success, -1 on error.
+ */
+int
+virTypedParamsReplaceString(virTypedParameterPtr *params,
+                            int *nparams,
+                            const char *name,
+                            const char *value)
+{
+    char *str = NULL;
+    char *old = NULL;
+    size_t n = *nparams;
+    virTypedParameterPtr param;
+
+    virResetLastError();
+
+    param = virTypedParamsGet(*params, n, name);
+    if (param) {
+        if (param->type != VIR_TYPED_PARAM_STRING) {
+            virReportError(VIR_ERR_INVALID_ARG,
+                           _("Parameter '%s' is not a string"),
+                           param->field);
+            goto error;
+        }
+        old = param->value.s;
+    } else {
+        if (VIR_EXPAND_N(*params, n, 1) < 0) {
+            virReportOOMError();
+            goto error;
+        }
+        param = *params + n - 1;
+    }
+
+    if (VIR_STRDUP(str, value) < 0)
+        goto error;
+
+    if (virTypedParameterAssign(param, name,
+                                VIR_TYPED_PARAM_STRING, str) < 0) {
+        param->value.s = old;
+        VIR_FREE(str);
+        goto error;
+    }
+    VIR_FREE(old);
+
+    *nparams = n;
+    return 0;
+
+error:
+    virDispatchError(NULL);
+    return -1;
+}
+
+
 /* The following APIs are public and their signature may never change. */
 
 /**
index b0f852202bc34b7797d65bca383600e0320490d0..6eb61c4d3b8756f6085e97029f71b736f8b02f06 100644 (file)
@@ -44,4 +44,9 @@ int virTypedParameterAssignFromStr(virTypedParameterPtr param,
                                    const char *val)
     ATTRIBUTE_RETURN_CHECK;
 
+int virTypedParamsReplaceString(virTypedParameterPtr *params,
+                                int *nparams,
+                                const char *name,
+                                const char *value);
+
 #endif /* __VIR_TYPED_PARAM_H */