Permalink
Browse files

Introduced utility function "with_file_in" and swapped arguments of

"with_command_in".
  • Loading branch information...
1 parent 6499e4e commit c182aa757c6403e9ac5b727c1842738900df4d9f @cfuehrmann cfuehrmann committed Sep 4, 2011
Showing with 42 additions and 39 deletions.
  1. +30 −25 sysUtil.ml
  2. +1 −0 sysUtil.mli
  3. +4 −4 unixUtil.ml
  4. +1 −1 unixUtil.mli
  5. +4 −7 unixUtilTest.ml
  6. +2 −2 wmUtil.ml
View
@@ -1,43 +1,48 @@
module String = ExtLib.String
module Hashtbl = ExtLib.Hashtbl
+
open ListUtil
+let with_file_in f name =
+ let ic = open_in name in
+ try
+ let result = f ic
+ and _ = close_in ic in
+ result
+ with
+ | e ->
+ let _ = close_in ic in
+ raise e
+
let line_frequencies file_name =
let result = Hashtbl.create 50 in
- begin
- let ic = open_in file_name in
- try
- let rec loop i =
- begin
- let line =
- let line = input_line ic in
- String.strip line in
- match Hashtbl.find_option result line with
- | Some (count, most_recent) ->
+ let f ic =
+ let rec loop i =
+ (let line =
+ let line = input_line ic in
+ String.strip line in
+ match Hashtbl.find_option result line with
+ | Some (count, most_recent) ->
Hashtbl.replace result line (count + 1, most_recent)
- | None -> Hashtbl.add result line (1, i)
- end;
- loop (i + 1)
- in loop 0
- with
- | End_of_file -> close_in ic
- | e -> close_in ic; raise e
- end;
- result
+ | None -> Hashtbl.add result line (1, i));
+ loop (i + 1)
+ in loop 0 in
+ try
+ with_file_in f file_name
+ with
+ | End_of_file -> result
let insert_at_beginning file_name line =
let lines =
- let ic = open_in file_name in
- try
+ let f ic =
let rec loop lines =
try
let line = input_line ic in
loop (line :: lines)
with
- | End_of_file -> close_in ic; lines
- in List.rev (loop [])
- with
- | e -> close_in ic; raise e in
+ | End_of_file -> lines
+ in List.rev (loop []) in
+ with_file_in f file_name in
let oc = open_out file_name in
try
output_string oc (string_of_list (fun s -> s) "\n" (line :: lines));
View
@@ -1,3 +1,4 @@
+val with_file_in : (in_channel -> 'a) -> string -> 'a
val line_frequencies : string -> (string, int * int) Hashtbl.t
val insert_at_beginning : string -> string -> unit
val time : ('a -> 'b) -> 'a -> 'b
View
@@ -1,4 +1,4 @@
-let with_command_in command f =
+let with_command_in f command =
let ic = Unix.open_process_in command in
try
let result = f ic
@@ -15,13 +15,13 @@ let command pid =
let line = input_line ic in
Str.replace_first (Str.regexp "^[^ ]*/") "" line in
try
- with_command_in c f
+ with_command_in f c
with
| End_of_file -> raise Not_found
-let home () = with_command_in "echo ~" input_line
+let home () = with_command_in input_line "echo ~"
-let date format = with_command_in ("date " ^ format) input_line
+let date format = with_command_in input_line ("date " ^ format)
let touch mod_mask file_name =
let fd = Unix.openfile file_name [ Unix.O_CREAT ] mod_mask in
View
@@ -1,4 +1,4 @@
-val with_command_in : string -> (in_channel -> 'a) -> 'a
+val with_command_in : (in_channel -> 'a) -> string -> 'a
val command : int -> string
val home : unit -> string
val date : string -> string
View
@@ -1,18 +1,15 @@
open OUnit
open UnixUtil
+open SysUtil
let max_pid () =
- let ic = open_in "/proc/sys/kernel/pid_max" in
- try
+ let f ic =
Scanf.fscanf ic "%d"
(fun pid ->
let result = pid in
close_in ic;
- result)
- with
- | e ->
- close_in ic;
- raise e
+ result) in
+ with_file_in f "/proc/sys/kernel/pid_max"
let pid_not_found () =
assert_raises Not_found (fun () -> UnixUtil.command (max_pid () + 1))
View
@@ -9,7 +9,7 @@ let current_desktop () =
(fun desktop marker rest ->
if marker = "*" then desktop else loop ()) in
loop () in
- with_command_in "wmctrl -d" f
+ with_command_in f "wmctrl -d"
let windows_per_desktop () =
let result = Hashtbl.create 5 in
@@ -21,7 +21,7 @@ let windows_per_desktop () =
loop () in
loop () in
try
- with_command_in "wmctrl -lp" f
+ with_command_in f "wmctrl -lp"
with
| End_of_file -> result

0 comments on commit c182aa7

Please sign in to comment.