[libvirt] [PATCH 06/22] util: typedparam: Separate code to assign value to typed parameter

Peter Krempa posted 22 patches 6 years, 4 months ago
[libvirt] [PATCH 06/22] util: typedparam: Separate code to assign value to typed parameter
Posted by Peter Krempa 6 years, 4 months ago
The code will be reused in other function.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
---
 src/util/virtypedparam.c | 55 ++++++++++++++++++++++++----------------
 1 file changed, 33 insertions(+), 22 deletions(-)

diff --git a/src/util/virtypedparam.c b/src/util/virtypedparam.c
index d9f8203796..9f86166707 100644
--- a/src/util/virtypedparam.c
+++ b/src/util/virtypedparam.c
@@ -205,24 +205,12 @@ virTypedParameterToString(virTypedParameterPtr param)
     return value;
 }

-/* Assign name, type, and the appropriately typed arg to param; in the
- * case of a string, the caller is assumed to have malloc'd a string,
- * or can pass NULL to have this function malloc an empty string.
- * Return 0 on success, -1 after an error message on failure.  */
-int
-virTypedParameterAssign(virTypedParameterPtr param, const char *name,
-                        int type, ...)
-{
-    va_list ap;
-    int ret = -1;
-
-    va_start(ap, type);

-    if (virStrcpyStatic(param->field, name) < 0) {
-        virReportError(VIR_ERR_INTERNAL_ERROR, _("Field name '%s' too long"),
-                       name);
-        goto cleanup;
-    }
+static int
+virTypedParameterAssignValueAP(virTypedParameterPtr param,
+                               int type,
+                               va_list ap)
+{
     param->type = type;
     switch (type) {
     case VIR_TYPED_PARAM_INT:
@@ -246,17 +234,40 @@ virTypedParameterAssign(virTypedParameterPtr param, const char *name,
     case VIR_TYPED_PARAM_STRING:
         param->value.s = va_arg(ap, char *);
         if (!param->value.s && VIR_STRDUP(param->value.s, "") < 0)
-            goto cleanup;
+            return -1;
         break;
     default:
         virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("unexpected type %d for field %s"), type, name);
-        goto cleanup;
+                       _("unexpected type %d for field %s"), type,
+                       NULLSTR(param->field));
+        return -1;
     }

-    ret = 0;
- cleanup:
+    return 0;
+}
+
+
+/* Assign name, type, and the appropriately typed arg to param; in the
+ * case of a string, the caller is assumed to have malloc'd a string,
+ * or can pass NULL to have this function malloc an empty string.
+ * Return 0 on success, -1 after an error message on failure.  */
+int
+virTypedParameterAssign(virTypedParameterPtr param, const char *name,
+                        int type, ...)
+{
+    va_list ap;
+    int ret = -1;
+
+    if (virStrcpyStatic(param->field, name) < 0) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, _("Field name '%s' too long"),
+                       name);
+        return -1;
+    }
+
+    va_start(ap, type);
+    ret = virTypedParameterAssignValueAP(param, type, ap);
     va_end(ap);
+
     return ret;
 }

-- 
2.21.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH 06/22] util: typedparam: Separate code to assign value to typed parameter
Posted by Ján Tomko 6 years, 4 months ago
On Thu, Sep 19, 2019 at 07:13:09PM +0200, Peter Krempa wrote:
>The code will be reused in other function.
>
>Signed-off-by: Peter Krempa <pkrempa@redhat.com>
>---
> src/util/virtypedparam.c | 55 ++++++++++++++++++++++++----------------
> 1 file changed, 33 insertions(+), 22 deletions(-)
>
>diff --git a/src/util/virtypedparam.c b/src/util/virtypedparam.c
>index d9f8203796..9f86166707 100644
>--- a/src/util/virtypedparam.c
>+++ b/src/util/virtypedparam.c
>@@ -205,24 +205,12 @@ virTypedParameterToString(virTypedParameterPtr param)
>     return value;
> }
>
>-/* Assign name, type, and the appropriately typed arg to param; in the
>- * case of a string, the caller is assumed to have malloc'd a string,
>- * or can pass NULL to have this function malloc an empty string.
>- * Return 0 on success, -1 after an error message on failure.  */
>-int
>-virTypedParameterAssign(virTypedParameterPtr param, const char *name,
>-                        int type, ...)
>-{
>-    va_list ap;
>-    int ret = -1;
>-
>-    va_start(ap, type);
>
>-    if (virStrcpyStatic(param->field, name) < 0) {
>-        virReportError(VIR_ERR_INTERNAL_ERROR, _("Field name '%s' too long"),
>-                       name);
>-        goto cleanup;
>-    }
>+static int
>+virTypedParameterAssignValueAP(virTypedParameterPtr param,

Over the libvirt codebase, we use mostly VArgs, VAList in one case.
Please use one of those unless you strongly want to people think of
access points instead of the Commonwealth of Virginia.

>+                               int type,
>+                               va_list ap)
>+{
>     param->type = type;
>     switch (type) {
>     case VIR_TYPED_PARAM_INT:

Reviewed-by: Ján Tomko <jtomko@redhat.com>

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