1 (* 'top'-like tool for libvirt domains.
2 * $Id: virt_top.ml,v 1.5 2007/08/30 13:52:40 rjones Exp $
5 let (//) = Filename.concat
7 (* Input a whole file as a list of lines. *)
8 let input_all_lines chan =
12 lines := input_line chan :: !lines
18 (* Trim whitespace from the beginning and end of strings. *)
21 (* || c = '\f' *) || c = '\n' || c = '\r' || c = '\t' (* || c = '\v' *)
23 let triml ?(test = isspace) str =
25 let n = ref (String.length str) in
26 while !n > 0 && test str.[!i]; do
31 else String.sub str !i !n
33 let trimr ?(test = isspace) str =
34 let n = ref (String.length str) in
35 while !n > 0 && test str.[!n-1]; do
38 if !n = String.length str then str
39 else String.sub str 0 !n
41 let trim ?(test = isspace) str =
44 (* Read a configuration file as a list of (key, value) pairs.
45 * If the config file is missing this returns an empty list.
47 let blanks_and_comments = Str.regexp "^[ \t]*\\(#.*\\)?$"
49 let read_config_file filename =
52 let chan = open_in filename in
53 let lines = input_all_lines chan in
57 Sys_error _ -> [] in (* Ignore errors opening file. *)
61 let i = ref 0 in List.map (fun line -> (incr i; !i), line) lines in
63 (* Remove blank lines and comment lines. *)
66 (fun (lineno, line) ->
67 not (Str.string_match blanks_and_comments line 0)) lines in
69 (* Convert to key, value pairs. *)
72 let key, value = ExtString.String.split line " " in
73 lineno, trim key, trim value