[Libguestfs] [PATCH v2 API PROPOSAL 2/5] common/mlutils: Add wrapper around guestfs_int_is_true.

Richard W.M. Jones rjones at redhat.com
Mon Oct 1 19:16:46 UTC 2018


Plus two small helper functions.
---
 common/mlutils/c_utils-c.c | 13 +++++++++++++
 common/mlutils/c_utils.ml  |  4 ++++
 common/mlutils/c_utils.mli | 11 +++++++++++
 3 files changed, 28 insertions(+)

diff --git a/common/mlutils/c_utils-c.c b/common/mlutils/c_utils-c.c
index 83cf76398..a25bf3d16 100644
--- a/common/mlutils/c_utils-c.c
+++ b/common/mlutils/c_utils-c.c
@@ -60,6 +60,19 @@ guestfs_int_mlutils_drive_index (value strv)
   CAMLreturn (Val_int (r));
 }
 
+value
+guestfs_int_mlutils_is_true (value strv)
+{
+  CAMLparam1 (strv);
+  ssize_t r;
+
+  r = guestfs_int_is_true (String_val (strv));
+  if (r == -1)
+    caml_invalid_argument ("is_true");
+
+  CAMLreturn (Val_bool (r));
+}
+
 value
 guestfs_int_mlutils_shell_unquote (value strv)
 {
diff --git a/common/mlutils/c_utils.ml b/common/mlutils/c_utils.ml
index 0f9d1943d..33a1db313 100644
--- a/common/mlutils/c_utils.ml
+++ b/common/mlutils/c_utils.ml
@@ -23,6 +23,10 @@ open Printf
 external drive_name : int -> string = "guestfs_int_mlutils_drive_name"
 external drive_index : string -> int = "guestfs_int_mlutils_drive_index"
 
+external is_true : string -> bool = "guestfs_int_mlutils_is_true"
+let is_true_noraise s = try is_true s with Invalid_argument _ -> false
+let is_false_noraise s = try not (is_true s) with Invalid_argument _ -> false
+
 external shell_unquote : string -> string = "guestfs_int_mlutils_shell_unquote"
 
 external is_reg : int64 -> bool = "guestfs_int_mlutils_is_reg" "noalloc"
diff --git a/common/mlutils/c_utils.mli b/common/mlutils/c_utils.mli
index d2ad6fc1d..bb2f43017 100644
--- a/common/mlutils/c_utils.mli
+++ b/common/mlutils/c_utils.mli
@@ -21,6 +21,17 @@
 val drive_name : int -> string
 val drive_index : string -> int
 
+val is_true : string -> bool
+(** Converts strings like ["true"], ["yes"] etc into [true], and
+    strings like ["false"], ["no"] etc into [false].  Other strings
+    will raise [Invalid_argument "is_true"]. *)
+
+val is_true_noraise : string -> bool
+val is_false_noraise : string -> bool
+(** Wrappers around {!is_true} which do not raise an error.  They
+    simply return a boolean indicating if the string is a true-like
+    or false-like string. *)
+
 val shell_unquote : string -> string
 (** If the string looks like a shell quoted string, then attempt to
     unquote it.
-- 
2.19.0.rc0




More information about the Libguestfs mailing list