Generate config.ml from Makefile, add iconsdir path.
[guestfs-browser.git] / utils.ml
index e0c2ff1..3afcf99 100644 (file)
--- a/utils.ml
+++ b/utils.ml
@@ -18,8 +18,6 @@
 
 open ExtString
 open ExtList
-open CamomileLibrary
-open Default.Camomile
 
 open Printf
 
@@ -66,6 +64,38 @@ let connect_uri () = !connect_uri
 
 let utf8_rarrow = "\xe2\x86\x92"
 
+let pretty_string_of_exn =
+  function
+  | Guestfs.Error str ->
+      "Libguestfs error",
+      sprintf "libguestfs reported an error:
+
+%s
+
+To get more information about libguestfs errors, run guestfs-browser
+with the -x flag on the command line."
+        str
+
+  | Libvirt.Virterror err ->
+      "Libvirt error",
+      sprintf "libvirt reported an error:
+
+%s
+
+To get more information about libvirt errors, run guestfs-browser
+from the command line like this:
+
+LIBVIRT_DEBUG=1 guestfs-browser"
+        (Libvirt.Virterror.to_string err)
+
+  (* Add more exception types here as we come across them.  Last
+   * case below is the catch-all.
+   *)
+  | exn ->
+      let str = Printexc.to_string exn in
+      debug "pretty_string_of_exn: unhandled exception %s" str;
+      "Error", str
+
 let human_size i =
   if i < 1024L then
     sprintf "%Ld" i
@@ -164,9 +194,12 @@ let tmpdir () =
        ignore (Sys.command cmd));
   tmpdir
 
-let utf16le = CharEncoding.utf16le
-let utf8 = CharEncoding.utf8
-let recode = CharEncoding.recode_string ~in_enc:utf16le ~out_enc:utf8
+module CE = CamomileLibraryDefault.Camomile.CharEncoding
+module UTF8 = CamomileLibraryDefault.Camomile.UTF8
+module UChar = CamomileLibraryDefault.Camomile.UChar
+let utf16le = CE.utf16le
+let utf8 = CE.utf8
+let recode = CE.recode_string ~in_enc:utf16le ~out_enc:utf8
 
 let windows_string_to_utf8 str =
   let str = recode str in
@@ -222,3 +255,21 @@ and reg_hex_of_string ?(split_long_lines=false) v =
 let local_file_exists filename =
   try Unix.access filename [Unix.F_OK]; true
   with Unix.Unix_error _ -> false
+
+let basename pathname =
+  let len = String.length pathname in
+  try
+    let i = String.rindex pathname '/' in
+    let r = String.sub pathname (i+1) (len-i-1) in
+    if r = "" then "root" else r
+  with
+    Not_found -> pathname
+
+let extension pathname =
+  let len = String.length pathname in
+  try
+    let i = String.rindex pathname '.' in
+    let r = String.sub pathname i (len-i) in
+    r
+  with
+    Not_found -> ""