---
src/utils.ml | 21 +++++++++++++++++++++
src/utils.mli | 3 +++
2 files changed, 24 insertions(+)
diff --git a/src/utils.ml b/src/utils.ml
index 3e81c21..7ae24bd 100644
--- a/src/utils.ml
+++ b/src/utils.ml
@@ -204,3 +204,24 @@ let compare_architecture a1 a2 =
exit 1
in
compare (index_of_architecture a1) (index_of_architecture a2)
+
+(* Parse a size field, eg. "10G". *)
+let parse_size =
+ let const_re = Str.regexp "^\\([.0-9]+\\)\\([bKMG]\\)$" in
+ fun field ->
+ let matches rex = Str.string_match rex field 0 in
+ let sub i = Str.matched_group i field in
+ let size_scaled f = function
+ | "b" -> Int64.of_float f
+ | "K" -> Int64.of_float (f *. 1024.)
+ | "M" -> Int64.of_float (f *. 1024. *. 1024.)
+ | "G" -> Int64.of_float (f *. 1024. *. 1024. *. 1024.)
+ | _ -> assert false
+ in
+
+ if matches const_re then (
+ size_scaled (float_of_string (sub 1)) (sub 2)
+ ) else (
+ eprintf "supermin: cannot parse size field '%s'\n" field;
+ exit 1
+ )
diff --git a/src/utils.mli b/src/utils.mli
index 5de940d..7896e34 100644
--- a/src/utils.mli
+++ b/src/utils.mli
@@ -90,3 +90,6 @@ val compare_version : string -> string -> int
val compare_architecture : string -> string -> int
(** Compare two architecture strings. *)
+
+val parse_size : string -> int64
+(** Parse a size field, eg. [10G] *)
--
2.1.0