[Libguestfs] [PATCH] builder: split INI C <-> OCaml glue code in own module

Pino Toscano ptoscano at redhat.com
Mon Feb 24 14:12:13 UTC 2014


Move in an own module the code which calls the C
virt_builder_parse_index and does the array -> list conversion of the
result. This way this code can be easily called also in places different
than Index_parser without the need to copy the types mapping, etc.

Just code motion, no actual behaviour changes.
---
 builder/Makefile.am     |  3 +++
 builder/index_parser.ml | 17 +----------------
 builder/ini_reader.ml   | 38 ++++++++++++++++++++++++++++++++++++++
 builder/ini_reader.mli  | 24 ++++++++++++++++++++++++
 po/POTFILES-ml          |  1 +
 5 files changed, 67 insertions(+), 16 deletions(-)
 create mode 100644 builder/ini_reader.ml
 create mode 100644 builder/ini_reader.mli

diff --git a/builder/Makefile.am b/builder/Makefile.am
index 8c48be5..a72b7ac 100644
--- a/builder/Makefile.am
+++ b/builder/Makefile.am
@@ -46,6 +46,8 @@ SOURCES = \
 	index_parser.mli \
 	index_parser.ml \
 	index-parser-c.c \
+	ini_reader.mli \
+	ini_reader.ml \
 	list_entries.mli \
 	list_entries.ml \
 	paths.ml \
@@ -94,6 +96,7 @@ OBJECTS = \
 	pxzcat.cmx \
 	setlocale-c.o \
 	setlocale.cmx \
+	ini_reader.cmx \
 	paths.cmx \
 	get_kernel.cmx \
 	downloader.cmx \
diff --git a/builder/index_parser.ml b/builder/index_parser.ml
index de4d72e..9fd9cda 100644
--- a/builder/index_parser.ml
+++ b/builder/index_parser.ml
@@ -102,15 +102,6 @@ let print_entry chan (name, { printable_name = printable_name;
   ) notes;
   if hidden then fp "hidden=true\n"
 
-(* Types returned by the C index parser. *)
-type sections = section array
-and section = string * fields           (* [name] + fields *)
-and fields = field array
-and field = string * string option * string    (* key + subkey + value *)
-
-(* Calls yyparse in the C code. *)
-external parse_index : string -> sections = "virt_builder_parse_index"
-
 let get_index ~prog ~debug ~downloader ~sigchecker source =
   let corrupt_file () =
     eprintf (f_"\nThe index file downloaded from '%s' is corrupt.\nYou need to ask the supplier of this file to fix it and upload a fixed version.\n")
@@ -128,16 +119,10 @@ let get_index ~prog ~debug ~downloader ~sigchecker source =
     Sigchecker.verify sigchecker tmpfile;
 
     (* Try parsing the file. *)
-    let sections = parse_index tmpfile in
+    let sections = Ini_reader.read_ini tmpfile in
     if delete_tmpfile then
       (try Unix.unlink tmpfile with _ -> ());
 
-    let sections = Array.to_list sections in
-    let sections = List.map (
-      fun (n, fields) ->
-        n, Array.to_list fields
-    ) sections in
-
     (* Check for repeated os-version names. *)
     let nseen = Hashtbl.create 13 in
     List.iter (
diff --git a/builder/ini_reader.ml b/builder/ini_reader.ml
new file mode 100644
index 0000000..fbd4d2f
--- /dev/null
+++ b/builder/ini_reader.ml
@@ -0,0 +1,38 @@
+(* virt-builder
+ * Copyright (C) 2013-2014 Red Hat Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *)
+
+type sections = section list
+and section = string * fields                (* [name] + fields *)
+and fields = field list
+and field = string * string option * string  (* key + subkey + value *)
+
+(* Types returned by the C index parser. *)
+type c_sections = c_section array
+and c_section = string * c_fields             (* [name] + fields *)
+and c_fields = field array
+
+(* Calls yyparse in the C code. *)
+external parse_index : string -> c_sections = "virt_builder_parse_index"
+
+let read_ini file =
+  let sections = parse_index file in
+  let sections = Array.to_list sections in
+  List.map (
+    fun (n, fields) ->
+      n, Array.to_list fields
+  ) sections
diff --git a/builder/ini_reader.mli b/builder/ini_reader.mli
new file mode 100644
index 0000000..992a1cb
--- /dev/null
+++ b/builder/ini_reader.mli
@@ -0,0 +1,24 @@
+(* virt-builder
+ * Copyright (C) 2013-2014 Red Hat Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *)
+
+type sections = section list
+and section = string * fields                (* [name] + fields *)
+and fields = field list
+and field = string * string option * string  (* key + subkey + value *)
+
+val read_ini : string -> sections
diff --git a/po/POTFILES-ml b/po/POTFILES-ml
index d3c8918..2c9f5dc 100644
--- a/po/POTFILES-ml
+++ b/po/POTFILES-ml
@@ -3,6 +3,7 @@ builder/cmdline.ml
 builder/downloader.ml
 builder/get_kernel.ml
 builder/index_parser.ml
+builder/ini_reader.ml
 builder/list_entries.ml
 builder/paths.ml
 builder/pxzcat.ml
-- 
1.8.3.1




More information about the Libguestfs mailing list