Skip to content

Commit

Permalink
Introduced utility function "with_file_in" and swapped arguments of
Browse files Browse the repository at this point in the history
"with_command_in".
  • Loading branch information
cfuehrmann committed Sep 4, 2011
1 parent 6499e4e commit c182aa7
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 39 deletions.
55 changes: 30 additions & 25 deletions sysUtil.ml
Original file line number Diff line number Diff line change
@@ -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));
Expand Down
1 change: 1 addition & 0 deletions sysUtil.mli
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions unixUtil.ml
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion unixUtil.mli
Original file line number Diff line number Diff line change
@@ -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
Expand Down
11 changes: 4 additions & 7 deletions unixUtilTest.ml
Original file line number Diff line number Diff line change
@@ -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))
Expand Down
4 changes: 2 additions & 2 deletions wmUtil.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down

0 comments on commit c182aa7

Please sign in to comment.