[PATCHv3 3/4] util: Add helper to assign typed params from string

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

 



This patch adds a helper to deal with assigning values to
virTypedParameter structures from strings. The helper parses the value
from the string and assigns it to the corresponding union value.
---
New in series.
---
 src/libvirt_private.syms |  1 +
 src/util/virtypedparam.c | 97 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/util/virtypedparam.h |  6 +++
 3 files changed, 104 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 65067d6..c8dcece 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1734,6 +1734,7 @@ virTimeStringThenRaw;
 virTypedParameterArrayClear;
 virTypedParameterArrayValidate;
 virTypedParameterAssign;
+virTypedParameterAssignFromStr;


 # viruri.h
diff --git a/src/util/virtypedparam.c b/src/util/virtypedparam.c
index d68d79f..89a0caa 100644
--- a/src/util/virtypedparam.c
+++ b/src/util/virtypedparam.c
@@ -181,3 +181,100 @@ cleanup:
     va_end(ap);
     return ret;
 }
+
+/* Assign name, type, and convert the argument from a const string.
+ * In case of a string, the string is copied.
+ * Return 0 on success, -1 after an error message on failure.  */
+int
+virTypedParameterAssignFromStr(virTypedParameterPtr param, const char *name,
+                               int type, const char *val)
+{
+    int ret = -1;
+
+    if (!val) {
+        virReportError(VIR_ERR_INVALID_ARG, _("NULL value for field '%s'"),
+                       name);
+        goto cleanup;
+    }
+
+    if (virStrcpyStatic(param->field, name) == NULL) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, _("Field name '%s' too long"),
+                       name);
+        goto cleanup;
+    }
+
+    param->type = type;
+    switch (type) {
+    case VIR_TYPED_PARAM_INT:
+        if (virStrToLong_i(val, NULL, 10, &param->value.i) < 0) {
+            virReportError(VIR_ERR_INVALID_ARG,
+                           _("Invalid value for field '%s': expected int"),
+                           name);
+            goto cleanup;
+        }
+        break;
+    case VIR_TYPED_PARAM_UINT:
+        if (virStrToLong_ui(val, NULL, 10, &param->value.ui) < 0) {
+            virReportError(VIR_ERR_INVALID_ARG,
+                           _("Invalid value for field '%s': "
+                             "expected unsigned int"),
+                           name);
+            goto cleanup;
+        }
+        break;
+    case VIR_TYPED_PARAM_LLONG:
+        if (virStrToLong_ll(val, NULL, 10, &param->value.l) < 0) {
+            virReportError(VIR_ERR_INVALID_ARG,
+                           _("Invalid value for field '%s': "
+                             "expected long long"),
+                           name);
+            goto cleanup;
+        }
+        break;
+    case VIR_TYPED_PARAM_ULLONG:
+        if (virStrToLong_ull(val, NULL, 10, &param->value.ul) < 0) {
+            virReportError(VIR_ERR_INVALID_ARG,
+                           _("Invalid value for field '%s': "
+                             "expected long long"),
+                           name);
+            goto cleanup;
+        }
+        break;
+    case VIR_TYPED_PARAM_DOUBLE:
+        if (virStrToDouble(val, NULL, &param->value.d) < 0) {
+            virReportError(VIR_ERR_INVALID_ARG,
+                           _("Invalid value for field '%s': "
+                             "expected double"),
+                           name);
+            goto cleanup;
+        }
+        break;
+    case VIR_TYPED_PARAM_BOOLEAN:
+        if (STRCASEEQ(val, "true") ||
+            STREQ(val, "1")) {
+            param->value.b = true;
+        } else if (STRCASEEQ(val, "false") ||
+                 STREQ(val, "0")) {
+            param->value.b = false;
+        } else {
+            virReportError(VIR_ERR_INVALID_ARG,
+                           _("Invalid boolean value for field '%s'"), name);
+            goto cleanup;
+        }
+        break;
+    case VIR_TYPED_PARAM_STRING:
+        if (!(param->value.s = strdup(val))) {
+            virReportOOMError();
+            goto cleanup;
+        }
+        break;
+    default:
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("unexpected type %d for field %s"), type, name);
+        goto cleanup;
+    }
+
+    ret = 0;
+cleanup:
+    return ret;
+}
diff --git a/src/util/virtypedparam.h b/src/util/virtypedparam.h
index f117b84..581201c 100644
--- a/src/util/virtypedparam.h
+++ b/src/util/virtypedparam.h
@@ -35,4 +35,10 @@ int virTypedParameterAssign(virTypedParameterPtr param, const char *name,
                             int type, /* TYPE arg */ ...)
     ATTRIBUTE_RETURN_CHECK;

+int virTypedParameterAssignFromStr(virTypedParameterPtr param,
+                                   const char *name,
+                                   int type,
+                                   const char *val)
+    ATTRIBUTE_RETURN_CHECK;
+
 #endif /* __VIR_TYPED_PARAM_H */
-- 
1.7.12

--
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]