-
Notifications
You must be signed in to change notification settings - Fork 18
/
common.ml
48 lines (44 loc) · 1.75 KB
/
common.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
(*
* Copyright (C) 2011-2013 Citrix Systems Inc
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*)
type t = {
debug: bool;
verb: bool;
unbuffered: bool;
}
let make debug verb unbuffered =
{ debug; verb; unbuffered; }
let ( |> ) a b = b a
let parse_size x =
let kib = 1024L in
let mib = Int64.mul kib kib in
let gib = Int64.mul mib kib in
let tib = Int64.mul gib kib in
let endswith suffix x =
let suffix' = String.length suffix in
let x' = String.length x in
x' >= suffix' && (String.sub x (x' - suffix') suffix' = suffix) in
let remove suffix x =
let suffix' = String.length suffix in
let x' = String.length x in
String.sub x 0 (x' - suffix') in
try
if endswith "KiB" x then Int64.(mul kib (of_string (remove "KiB" x)))
else if endswith "MiB" x then Int64.(mul mib (of_string (remove "MiB" x)))
else if endswith "GiB" x then Int64.(mul gib (of_string (remove "GiB" x)))
else if endswith "TiB" x then Int64.(mul tib (of_string (remove "TiB" x)))
else Int64.of_string x
with _ ->
failwith (Printf.sprintf "Cannot parse size: %s" x)