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

[Libguestfs] [PATCH v2 2/7] Move xml and xpath_helpers OCAML code to mllib



To allow other pieces of code to process XML files easily, move the
xml.ml* and xpath_helpers.ml* from v2v to mllib.
---
 docs/C_SOURCE_FILES              |  2 +-
 mllib/Makefile.am                |  9 ++++++--
 {v2v => mllib}/xml-c.c           | 47 +++++++++++++++++++-------------------
 {v2v => mllib}/xml.ml            | 49 ++++++++++++++++++++++------------------
 {v2v => mllib}/xml.mli           |  3 +++
 {v2v => mllib}/xpath_helpers.ml  |  0
 {v2v => mllib}/xpath_helpers.mli |  0
 v2v/Makefile.am                  | 17 +++-----------
 8 files changed, 65 insertions(+), 62 deletions(-)
 rename {v2v => mllib}/xml-c.c (90%)
 rename {v2v => mllib}/xml.ml (79%)
 rename {v2v => mllib}/xml.mli (97%)
 rename {v2v => mllib}/xpath_helpers.ml (100%)
 rename {v2v => mllib}/xpath_helpers.mli (100%)

diff --git a/docs/C_SOURCE_FILES b/docs/C_SOURCE_FILES
index 286723b73..73039c927 100644
--- a/docs/C_SOURCE_FILES
+++ b/docs/C_SOURCE_FILES
@@ -335,6 +335,7 @@ mllib/progress-c.c
 mllib/statvfs-c.c
 mllib/uri-c.c
 mllib/visit-c.c
+mllib/xml-c.c
 ocaml/guestfs-c-actions.c
 ocaml/guestfs-c-errnos.c
 ocaml/guestfs-c.c
@@ -392,4 +393,3 @@ utils/qemu-boot/qemu-boot.c
 utils/qemu-speed-test/qemu-speed-test.c
 v2v/domainxml-c.c
 v2v/utils-c.c
-v2v/xml-c.c
diff --git a/mllib/Makefile.am b/mllib/Makefile.am
index 78fdf0211..aa5472ade 100644
--- a/mllib/Makefile.am
+++ b/mllib/Makefile.am
@@ -28,6 +28,8 @@ EXTRA_DIST = \
 	test-getopt.sh
 
 SOURCES_MLI = \
+	xml.mli \
+	xpath_helpers.mli \
 	checksums.mli \
 	common_utils.mli \
 	curl.mli \
@@ -67,7 +69,9 @@ SOURCES_ML = \
 	JSON.ml \
 	curl.ml \
 	exit.ml \
-	checksums.ml
+	checksums.ml \
+	xml.ml \
+	xpath_helpers.ml
 
 SOURCES_C = \
 	../common/visit/visit.c \
@@ -85,7 +89,8 @@ SOURCES_C = \
 	progress-c.c \
 	statvfs-c.c \
 	uri-c.c \
-	visit-c.c
+	visit-c.c \
+	xml-c.c
 
 if HAVE_OCAML
 
diff --git a/v2v/xml-c.c b/mllib/xml-c.c
similarity index 90%
rename from v2v/xml-c.c
rename to mllib/xml-c.c
index 0fcdbedec..d3e893076 100644
--- a/v2v/xml-c.c
+++ b/mllib/xml-c.c
@@ -1,5 +1,6 @@
-/* virt-v2v
+/* Bindings for libxml2
  * Copyright (C) 2009-2017 Red Hat Inc.
+ * Copyright (C) 2017 SUSE 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
@@ -52,7 +53,7 @@ static struct custom_operations docptr_custom_operations = {
 };
 
 value
-v2v_xml_free_docptr (value docv)
+mllib_xml_free_docptr (value docv)
 {
   CAMLparam1 (docv);
   xmlDocPtr doc = docptr_val (docv);
@@ -74,7 +75,7 @@ static struct custom_operations xpathctxptr_custom_operations = {
 };
 
 value
-v2v_xml_free_xpathctxptr (value xpathctxv)
+mllib_xml_free_xpathctxptr (value xpathctxv)
 {
   CAMLparam1 (xpathctxv);
   xmlXPathContextPtr xpathctx = xpathctxptr_val (xpathctxv);
@@ -96,7 +97,7 @@ static struct custom_operations xpathobjptr_custom_operations = {
 };
 
 value
-v2v_xml_free_xpathobjptr (value xpathobjv)
+mllib_xml_free_xpathobjptr (value xpathobjv)
 {
   CAMLparam1 (xpathobjv);
   xmlXPathObjectPtr xpathobj = xpathobjptr_val (xpathobjv);
@@ -106,7 +107,7 @@ v2v_xml_free_xpathobjptr (value xpathobjv)
 }
 
 value
-v2v_xml_parse_memory (value xmlv)
+mllib_xml_parse_memory (value xmlv)
 {
   CAMLparam1 (xmlv);
   CAMLlocal1 (docv);
@@ -128,7 +129,7 @@ v2v_xml_parse_memory (value xmlv)
 }
 
 value
-v2v_xml_copy_doc (value docv, value recursivev)
+mllib_xml_copy_doc (value docv, value recursivev)
 {
   CAMLparam2 (docv, recursivev);
   CAMLlocal1 (copyv);
@@ -147,7 +148,7 @@ v2v_xml_copy_doc (value docv, value recursivev)
 }
 
 value
-v2v_xml_to_string (value docv, value formatv)
+mllib_xml_to_string (value docv, value formatv)
 {
   CAMLparam2 (docv, formatv);
   CAMLlocal1 (strv);
@@ -166,7 +167,7 @@ v2v_xml_to_string (value docv, value formatv)
 }
 
 value
-v2v_xml_xpath_new_context (value docv)
+mllib_xml_xpath_new_context (value docv)
 {
   CAMLparam1 (docv);
   CAMLlocal1 (xpathctxv);
@@ -186,7 +187,7 @@ v2v_xml_xpath_new_context (value docv)
 }
 
 value
-v2v_xml_xpathctxptr_register_ns (value xpathctxv, value prefix, value uri)
+mllib_xml_xpathctxptr_register_ns (value xpathctxv, value prefix, value uri)
 {
   CAMLparam3 (xpathctxv, prefix, uri);
   xmlXPathContextPtr xpathctx;
@@ -203,7 +204,7 @@ v2v_xml_xpathctxptr_register_ns (value xpathctxv, value prefix, value uri)
 }
 
 value
-v2v_xml_xpathctxptr_eval_expression (value xpathctxv, value exprv)
+mllib_xml_xpathctxptr_eval_expression (value xpathctxv, value exprv)
 {
   CAMLparam2 (xpathctxv, exprv);
   CAMLlocal1 (xpathobjv);
@@ -223,7 +224,7 @@ v2v_xml_xpathctxptr_eval_expression (value xpathctxv, value exprv)
 }
 
 value
-v2v_xml_xpathobjptr_nr_nodes (value xpathobjv)
+mllib_xml_xpathobjptr_nr_nodes (value xpathobjv)
 {
   CAMLparam1 (xpathobjv);
   xmlXPathObjectPtr xpathobj = xpathobjptr_val (xpathobjv);
@@ -235,7 +236,7 @@ v2v_xml_xpathobjptr_nr_nodes (value xpathobjv)
 }
 
 value
-v2v_xml_xpathobjptr_get_nodeptr (value xpathobjv, value iv)
+mllib_xml_xpathobjptr_get_nodeptr (value xpathobjv, value iv)
 {
   CAMLparam2 (xpathobjv, iv);
   xmlXPathObjectPtr xpathobj = xpathobjptr_val (xpathobjv);
@@ -256,7 +257,7 @@ v2v_xml_xpathobjptr_get_nodeptr (value xpathobjv, value iv)
 }
 
 value
-v2v_xml_xpathctx_set_nodeptr (value xpathctxv, value nodev)
+mllib_xml_xpathctx_set_nodeptr (value xpathctxv, value nodev)
 {
   CAMLparam2 (xpathctxv, nodev);
   xmlXPathContextPtr xpathctx = xpathctxptr_val (xpathctxv);
@@ -268,7 +269,7 @@ v2v_xml_xpathctx_set_nodeptr (value xpathctxv, value nodev)
 }
 
 value
-v2v_xml_nodeptr_name (value nodev)
+mllib_xml_nodeptr_name (value nodev)
 {
   CAMLparam1 (nodev);
   xmlNodePtr node = (xmlNodePtr) nodev;
@@ -284,7 +285,7 @@ v2v_xml_nodeptr_name (value nodev)
 }
 
 value
-v2v_xml_nodeptr_as_string (value docv, value nodev)
+mllib_xml_nodeptr_as_string (value docv, value nodev)
 {
   CAMLparam2 (docv, nodev);
   CAMLlocal1 (strv);
@@ -316,7 +317,7 @@ v2v_xml_nodeptr_as_string (value docv, value nodev)
 }
 
 value
-v2v_xml_nodeptr_set_content (value nodev, value contentv)
+mllib_xml_nodeptr_set_content (value nodev, value contentv)
 {
   CAMLparam2 (nodev, contentv);
   xmlNodePtr node = (xmlNodePtr) nodev;
@@ -327,7 +328,7 @@ v2v_xml_nodeptr_set_content (value nodev, value contentv)
 }
 
 value
-v2v_xml_nodeptr_new_text_child (value nodev, value namev, value contentv)
+mllib_xml_nodeptr_new_text_child (value nodev, value namev, value contentv)
 {
   CAMLparam3 (nodev, namev, contentv);
   xmlNodePtr node = (xmlNodePtr) nodev;
@@ -339,14 +340,14 @@ v2v_xml_nodeptr_new_text_child (value nodev, value namev, value contentv)
   if (new_node == NULL)
     caml_invalid_argument ("nodeptr_new_text_child: failed to create new node");
 
-  /* See comment in v2v_xml_xpathobjptr_get_nodeptr about returning
+  /* See comment in mllib_xml_xpathobjptr_get_nodeptr about returning
    * named xmlNodePtr here.
    */
   CAMLreturn ((value) new_node);
 }
 
 value
-v2v_xml_nodeptr_set_prop (value nodev, value namev, value valv)
+mllib_xml_nodeptr_set_prop (value nodev, value namev, value valv)
 {
   CAMLparam3 (nodev, namev, valv);
   xmlNodePtr node = (xmlNodePtr) nodev;
@@ -360,7 +361,7 @@ v2v_xml_nodeptr_set_prop (value nodev, value namev, value valv)
 }
 
 value
-v2v_xml_nodeptr_unset_prop (value nodev, value namev)
+mllib_xml_nodeptr_unset_prop (value nodev, value namev)
 {
   CAMLparam2 (nodev, namev);
   xmlNodePtr node = (xmlNodePtr) nodev;
@@ -372,7 +373,7 @@ v2v_xml_nodeptr_unset_prop (value nodev, value namev)
 }
 
 value
-v2v_xml_nodeptr_unlink_node (value nodev)
+mllib_xml_nodeptr_unlink_node (value nodev)
 {
   CAMLparam1 (nodev);
   xmlNodePtr node = (xmlNodePtr) nodev;
@@ -384,7 +385,7 @@ v2v_xml_nodeptr_unlink_node (value nodev)
 }
 
 value
-v2v_xml_doc_get_root_element (value docv)
+mllib_xml_doc_get_root_element (value docv)
 {
   CAMLparam1 (docv);
   CAMLlocal1 (v);
@@ -402,7 +403,7 @@ v2v_xml_doc_get_root_element (value docv)
 }
 
 value
-v2v_xml_parse_uri (value strv)
+mllib_xml_parse_uri (value strv)
 {
   CAMLparam1 (strv);
   CAMLlocal3 (rv, sv, ov);
diff --git a/v2v/xml.ml b/mllib/xml.ml
similarity index 79%
rename from v2v/xml.ml
rename to mllib/xml.ml
index 7ed21cd30..78e75b8f2 100644
--- a/v2v/xml.ml
+++ b/mllib/xml.ml
@@ -1,5 +1,6 @@
-(* virt-v2v
+(* Bindings for libxml2
  * Copyright (C) 2009-2017 Red Hat Inc.
+ * Copyright (C) 2017 SUSE 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
@@ -56,87 +57,91 @@ type node = doc * nodeptr
 type xpathctx = doc * xpathctxptr
 type xpathobj = xpathctx * xpathobjptr
 
-external free_docptr : docptr -> unit = "v2v_xml_free_docptr"
-external free_xpathctxptr : xpathctxptr -> unit = "v2v_xml_free_xpathctxptr"
-external free_xpathobjptr : xpathobjptr -> unit = "v2v_xml_free_xpathobjptr"
+external free_docptr : docptr -> unit = "mllib_xml_free_docptr"
+external free_xpathctxptr : xpathctxptr -> unit = "mllib_xml_free_xpathctxptr"
+external free_xpathobjptr : xpathobjptr -> unit = "mllib_xml_free_xpathobjptr"
 
-external _parse_memory : string -> docptr = "v2v_xml_parse_memory"
+external _parse_memory : string -> docptr = "mllib_xml_parse_memory"
 let parse_memory xml =
   let docptr = _parse_memory xml in
   Gc.finalise free_docptr docptr;
   docptr
 
-external _copy_doc : docptr -> recursive:bool -> docptr = "v2v_xml_copy_doc"
+let parse_file file =
+  let xml = Common_utils.read_whole_file file in
+  parse_memory xml
+
+external _copy_doc : docptr -> recursive:bool -> docptr = "mllib_xml_copy_doc"
 let copy_doc docptr ~recursive =
   let copy = _copy_doc docptr ~recursive in
   Gc.finalise free_docptr copy;
   copy
 
-external to_string : docptr -> format:bool -> string = "v2v_xml_to_string"
+external to_string : docptr -> format:bool -> string = "mllib_xml_to_string"
 
 external _xpath_new_context : docptr -> xpathctxptr
-  = "v2v_xml_xpath_new_context"
+  = "mllib_xml_xpath_new_context"
 let xpath_new_context docptr =
   let xpathctxptr = _xpath_new_context docptr in
   Gc.finalise free_xpathctxptr xpathctxptr;
   docptr, xpathctxptr
 
 external xpathctxptr_register_ns : xpathctxptr -> string -> string -> unit
-  = "v2v_xml_xpathctxptr_register_ns"
+  = "mllib_xml_xpathctxptr_register_ns"
 let xpath_register_ns (_, xpathctxptr) prefix uri =
   xpathctxptr_register_ns xpathctxptr prefix uri
 
 external xpathctxptr_eval_expression : xpathctxptr -> string -> xpathobjptr
-  = "v2v_xml_xpathctxptr_eval_expression"
+  = "mllib_xml_xpathctxptr_eval_expression"
 let xpath_eval_expression ((_, xpathctxptr) as xpathctx) expr =
   let xpathobjptr = xpathctxptr_eval_expression xpathctxptr expr in
   Gc.finalise free_xpathobjptr xpathobjptr;
   xpathctx, xpathobjptr
 
 external xpathobjptr_nr_nodes : xpathobjptr -> int
-  = "v2v_xml_xpathobjptr_nr_nodes"
+  = "mllib_xml_xpathobjptr_nr_nodes"
 let xpathobj_nr_nodes (_, xpathobjptr) =
   xpathobjptr_nr_nodes xpathobjptr
 
 external xpathobjptr_get_nodeptr : xpathobjptr -> int -> nodeptr
-  = "v2v_xml_xpathobjptr_get_nodeptr"
+  = "mllib_xml_xpathobjptr_get_nodeptr"
 let xpathobj_node ((docptr, _), xpathobjptr) i =
   docptr, xpathobjptr_get_nodeptr xpathobjptr i
 
 external xpathctxptr_set_nodeptr : xpathctxptr -> nodeptr -> unit
-  = "v2v_xml_xpathctx_set_nodeptr"
+  = "mllib_xml_xpathctx_set_nodeptr"
 let xpathctx_set_current_context (_, xpathctxptr) (_, nodeptr) =
   xpathctxptr_set_nodeptr xpathctxptr nodeptr
 
-external nodeptr_name : nodeptr -> string = "v2v_xml_nodeptr_name"
+external nodeptr_name : nodeptr -> string = "mllib_xml_nodeptr_name"
 let node_name (_, nodeptr) = nodeptr_name nodeptr
 
 external nodeptr_as_string : docptr -> nodeptr -> string
-  = "v2v_xml_nodeptr_as_string"
+  = "mllib_xml_nodeptr_as_string"
 let node_as_string (docptr, nodeptr) = nodeptr_as_string docptr nodeptr
 
 external nodeptr_set_content : nodeptr -> string -> unit
-  = "v2v_xml_nodeptr_set_content"
+  = "mllib_xml_nodeptr_set_content"
 let node_set_content (_, nodeptr) = nodeptr_set_content nodeptr
 
 external nodeptr_new_text_child : nodeptr -> string -> string -> nodeptr
-  = "v2v_xml_nodeptr_new_text_child"
+  = "mllib_xml_nodeptr_new_text_child"
 let new_text_child (docptr, nodeptr) name content =
   docptr, nodeptr_new_text_child nodeptr name content
 
 external nodeptr_set_prop : nodeptr -> string -> string -> unit
-  = "v2v_xml_nodeptr_set_prop"
+  = "mllib_xml_nodeptr_set_prop"
 let set_prop (_, nodeptr) = nodeptr_set_prop nodeptr
 
 external nodeptr_unset_prop : nodeptr -> string -> bool
-  = "v2v_xml_nodeptr_unset_prop"
+  = "mllib_xml_nodeptr_unset_prop"
 let unset_prop (_, nodeptr) = nodeptr_unset_prop nodeptr
 
-external nodeptr_unlink_node : nodeptr -> unit = "v2v_xml_nodeptr_unlink_node"
+external nodeptr_unlink_node : nodeptr -> unit = "mllib_xml_nodeptr_unlink_node"
 let unlink_node (_, nodeptr) = nodeptr_unlink_node nodeptr
 
 external _doc_get_root_element : docptr -> nodeptr option
-  = "v2v_xml_doc_get_root_element"
+  = "mllib_xml_doc_get_root_element"
 let doc_get_root_element docptr =
   match _doc_get_root_element docptr with
   | None -> None
@@ -154,4 +159,4 @@ type uri = {
   uri_query_raw : string option;
 }
 
-external parse_uri : string -> uri = "v2v_xml_parse_uri"
+external parse_uri : string -> uri = "mllib_xml_parse_uri"
diff --git a/v2v/xml.mli b/mllib/xml.mli
similarity index 97%
rename from v2v/xml.mli
rename to mllib/xml.mli
index 9b2bc5c18..92d4977a2 100644
--- a/v2v/xml.mli
+++ b/mllib/xml.mli
@@ -33,6 +33,9 @@ val parse_memory : string -> doc
     For security reasons it actually calls xmlReadMemory with the
     [XML_PARSE_NONET] option set. *)
 
+val parse_file : string -> doc
+(** [parse_file path] parses the file pointed by [path].*)
+
 val copy_doc : doc -> recursive:bool -> doc
 (** http://xmlsoft.org/html/libxml-tree.html#xmlCopyDoc *)
 
diff --git a/v2v/xpath_helpers.ml b/mllib/xpath_helpers.ml
similarity index 100%
rename from v2v/xpath_helpers.ml
rename to mllib/xpath_helpers.ml
diff --git a/v2v/xpath_helpers.mli b/mllib/xpath_helpers.mli
similarity index 100%
rename from v2v/xpath_helpers.mli
rename to mllib/xpath_helpers.mli
diff --git a/v2v/Makefile.am b/v2v/Makefile.am
index d62ac477e..7b8d5d1ca 100644
--- a/v2v/Makefile.am
+++ b/v2v/Makefile.am
@@ -58,14 +58,10 @@ SOURCES_MLI = \
 	utils.mli \
 	vCenter.mli \
 	windows.mli \
-	windows_virtio.mli \
-	xml.mli \
-	xpath_helpers.mli
+	windows_virtio.mli
 
 SOURCES_ML = \
 	types.ml \
-	xml.ml \
-	xpath_helpers.ml \
 	uefi.ml \
 	utils.ml \
 	name_from_disk.ml \
@@ -103,8 +99,7 @@ SOURCES_ML = \
 
 SOURCES_C = \
 	domainxml-c.c \
-	utils-c.c \
-	xml-c.c
+	utils-c.c
 
 if HAVE_OCAML
 
@@ -119,7 +114,6 @@ virt_v2v_CPPFLAGS = \
 	-I$(top_srcdir)/lib
 virt_v2v_CFLAGS = \
 	$(WARN_CFLAGS) $(WERROR_CFLAGS) \
-	$(LIBXML2_CFLAGS) \
 	$(LIBVIRT_CFLAGS)
 
 BOBJECTS = \
@@ -168,8 +162,7 @@ virt_v2v_LINK = \
 
 virt_v2v_copy_to_local_SOURCES = \
 	domainxml-c.c \
-	utils-c.c \
-	xml-c.c
+	utils-c.c
 virt_v2v_copy_to_local_CPPFLAGS = \
 	-I. \
 	-I$(top_builddir) \
@@ -178,12 +171,9 @@ virt_v2v_copy_to_local_CPPFLAGS = \
 	-I$(top_srcdir)/lib
 virt_v2v_copy_to_local_CFLAGS = \
 	$(WARN_CFLAGS) $(WERROR_CFLAGS) \
-	$(LIBXML2_CFLAGS) \
 	$(LIBVIRT_CFLAGS)
 
 COPY_TO_LOCAL_BOBJECTS = \
-	xml.cmo \
-	xpath_helpers.cmo \
 	uefi.cmo \
 	utils.cmo \
 	vCenter.cmo \
@@ -398,7 +388,6 @@ endif
 
 v2v_unit_tests_BOBJECTS = \
 	types.cmo \
-	xml.cmo \
 	uefi.cmo \
 	utils.cmo \
 	DOM.cmo \
-- 
2.11.0


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