1 (* 'top'-like tool for libvirt domains.
4 let (//) = Filename.concat
6 (* Input a whole file as a list of lines. *)
7 let input_all_lines chan =
11 lines := input_line chan :: !lines
17 (* Trim whitespace from the beginning and end of strings. *)
20 (* || c = '\f' *) || c = '\n' || c = '\r' || c = '\t' (* || c = '\v' *)
22 let triml ?(test = isspace) str =
24 let n = ref (String.length str) in
25 while !n > 0 && test str.[!i]; do
30 else String.sub str !i !n
32 let trimr ?(test = isspace) str =
33 let n = ref (String.length str) in
34 while !n > 0 && test str.[!n-1]; do
37 if !n = String.length str then str
38 else String.sub str 0 !n
40 let trim ?(test = isspace) str =
43 (* Read a configuration file as a list of (key, value) pairs.
44 * If the config file is missing this returns an empty list.
46 let blanks_and_comments = Str.regexp "^[ \t]*\\(#.*\\)?$"
48 let read_config_file filename =
51 let chan = open_in filename in
52 let lines = input_all_lines chan in
56 Sys_error _ -> [] in (* Ignore errors opening file. *)
60 let i = ref 0 in List.map (fun line -> (incr i; !i), line) lines in
62 (* Remove blank lines and comment lines. *)
65 (fun (lineno, line) ->
66 not (Str.string_match blanks_and_comments line 0)) lines in
68 (* Convert to key, value pairs. *)
71 let key, value = ExtString.String.split line " " in
72 lineno, trim key, trim value