[Libguestfs] [PATCH 1/3] daemon: add split_key_value_strings helper

Pino Toscano ptoscano at redhat.com
Mon Oct 16 14:45:49 UTC 2017


Add a simple helper to turn a list of strings into key/value pairs,
splitting by '='.
---
 daemon/utils.ml  | 15 +++++++++++++++
 daemon/utils.mli |  6 ++++++
 2 files changed, 21 insertions(+)

diff --git a/daemon/utils.ml b/daemon/utils.ml
index d87ad75db..fd1681a86 100644
--- a/daemon/utils.ml
+++ b/daemon/utils.ml
@@ -229,3 +229,18 @@ let unix_canonical_path path =
   let path = String.nsplit "/" path in
   let path = List.filter ((<>) "") path in
   (if is_absolute then "/" else "") ^ String.concat "/" path
+
+let split_key_value_strings lines =
+  let lines = List.filter ((<>) "") lines in
+  let lines = List.filter (fun s -> s.[0] <> '#') lines in
+  List.map (
+    fun line ->
+      let key, value = String.split "=" line in
+      let value =
+        let n = String.length value in
+        if n >= 2 && value.[0] = '"' && value.[n-1] = '"' then
+          String.sub value 1 (n-2)
+        else
+          value in
+      (key, value)
+  ) lines
diff --git a/daemon/utils.mli b/daemon/utils.mli
index f312bde41..9730d06b5 100644
--- a/daemon/utils.mli
+++ b/daemon/utils.mli
@@ -97,5 +97,11 @@ val unix_canonical_path : string -> string
     The path is modified in place because the result is always
     the same length or shorter than the argument passed. *)
 
+val split_key_value_strings : string list -> (string * string) list
+(** Split the lines by the [=] separator, removing the double quotes
+    in the value if present.  Empty lines, or that start with a comment
+    character [#], are ignored.
+*)
+
 (**/**)
 val get_verbose_flag : unit -> bool
-- 
2.13.6




More information about the Libguestfs mailing list