Initial commit.
[virt-resize-ui.git] / utils.ml
1 (* Virt-resize UI.
2  * Copyright (C) 2011 Red Hat Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  *)
18
19 open ExtString
20 open ExtList
21
22 open Printf
23
24 let (+^) = Int64.add
25 let (-^) = Int64.sub
26 let ( *^ ) = Int64.mul
27 let (/^) = Int64.div
28 let (&^) = Int64.logand
29
30 type ('a, 'b) either = Left of 'a | Right of 'b
31
32 let verbose = ref false
33 let set_verbose_flag () = verbose := true
34 let verbose () = !verbose
35
36 let debug fs =
37   let f str =
38     if verbose () then (
39       prerr_string Config.package;
40       prerr_string ": tid ";
41       prerr_string (string_of_int (Thread.id (Thread.self ())));
42       prerr_string ": ";
43       prerr_string str;
44       prerr_newline ()
45     )
46   in
47   ksprintf f fs
48
49 let failwith fs =
50   let f str =
51     if verbose () then (prerr_string str; prerr_newline ());
52     raise (Failure str)
53   in
54   ksprintf f fs
55
56 let trace = ref false
57 let set_trace_flag () = trace := true
58 let trace () = !trace
59
60 let connect_uri = ref None
61 let set_connect_uri conn = connect_uri := conn
62 let connect_uri () = !connect_uri
63
64 let utf8_copyright = "\194\169"
65 let utf8_rarrow = "\xe2\x86\x92"
66
67 let human_size i =
68   if i < 1024L then
69     sprintf "%Ld" i
70   else if i < 1024L *^ 1024L then
71     sprintf "%.1f KB" (Int64.to_float i /. 1024.)
72   else if i < 1024L *^ 1024L *^ 1024L then
73     sprintf "%.1f MB" (Int64.to_float i /. 1024. /. 1024.)
74   else if i < 1024L *^ 1024L *^ 1024L *^ 1024L then
75     sprintf "%.1f GB" (Int64.to_float i /. 1024. /. 1024. /. 1024.)
76   else
77     sprintf "%.1f TB" (Int64.to_float i /. 1024. /. 1024. /. 1024. /. 1024.)
78
79 let human_size_1k i =
80   if i < 1024L then
81     sprintf "%Ld KB" i
82   else if i < 1024L *^ 1024L then
83     sprintf "%.1f MB" (Int64.to_float i /. 1024.)
84   else
85     sprintf "%.1f GB" (Int64.to_float i /. 1024. /. 1024.)
86
87 let unique = let i = ref 0 in fun () -> incr i; !i
88
89 let rec find_map f = function
90   | [] -> raise Not_found
91   | x :: xs ->
92       match f x with
93       | Some y -> y
94       | None -> find_map f xs
95
96 (* g_markup_escape is not bound by lablgtk2, but we want to provide
97  * extra protection for \0 characters appearing in the string
98  * anyway.
99  *)
100 let markup_escape name =
101   let f = function
102     | '&' -> "&amp;" | '<' -> "&lt;" | '>' -> "&gt;"
103     | '\000' -> "\\0"
104     | c -> String.make 1 c
105   in
106   String.replace_chars f name
107
108 let libguestfs_version_string () =
109   let g = new Guestfs.guestfs () in
110   let v = g#version () in
111   let s =
112     sprintf "%Ld.%Ld.%Ld%s"
113       v.Guestfs.major v.Guestfs.minor v.Guestfs.release v.Guestfs.extra in
114   g#close ();
115   s
116
117 let libvirt_version_string () =
118   let v = fst (Libvirt.get_version ()) in
119   sprintf "%d.%d.%d" (v / 1_000_000) ((v / 1_000) mod 1_000) (v mod 1_000)