X-Git-Url: http://git.annexia.org/?a=blobdiff_plain;f=src%2Futils.ml;h=1f00803005549cb9c772b90e7c038cc509deba6f;hb=1aedabd0caf7a0562a3f314dddd13d49e0165c92;hp=4332ff7466c6ad6bd1eee347274e846c27333e3a;hpb=4c608eef2274bc267d45d62550aafe2151514013;p=virt-top.git diff --git a/src/utils.ml b/src/utils.ml index 4332ff7..1f00803 100644 --- a/src/utils.ml +++ b/src/utils.ml @@ -75,6 +75,14 @@ let trimr ?(test = isspace) str = let trim ?(test = isspace) str = trimr (triml str) +(* Split string on the first instance of 'sep' character. *) +let split str sep = + try + let i = String.index str sep in + String.sub str 0 i, String.sub str (i+1) (String.length str - 1) + with + Not_found -> str, "" + (* Read a configuration file as a list of (key, value) pairs. * If the config file is missing this returns an empty list. *) @@ -103,7 +111,7 @@ let read_config_file filename = (* Convert to key, value pairs. *) List.map ( fun (lineno, line) -> - let key, value = ExtString.String.split line " " in + let key, value = split line ' ' in lineno, trim key, trim value ) lines @@ -117,6 +125,19 @@ let pad width str = else (* if n < width then *) str ^ String.make (width-n) ' ' ) +(* Take up to n elements of xs, if available. *) +let rec list_take n xs = + if n <= 0 then [] + else ( + match xs with + | [] -> [] + | x :: xs -> x :: list_take (n-1) xs + ) + +let map_default f def = function + | None -> def + | Some v -> f v + module Show = struct (* Show a percentage in 4 chars. *) let percent percent =