Commits

Sébastien Ferré committed cc1f59c

Add of "string_of_file", and functions about external applications and Cygwin.

  • Participants
  • Parent commits 636b05a

Comments (0)

Files changed (1)

+(** Common utility functions. *)
+
 (* space *)
 
 let heap_size () : float = float_of_int (Gc.stat ()).Gc.heap_words *. float_of_int (Sys.word_size / 8)  (* in bytes *)
 (* List functionals *)
 (* ---------------- *)
 
+let rec filter : 'a option list -> 'a list =
+  function
+    | [] -> []
+    | None::l -> filter l
+    | Some x::l -> x::filter l
+
 let rec mapfilter : ('a -> 'b option) -> 'a list -> 'b list =
   fun f -> function
       [] -> []
 let tbl_prof : (string,(int * float * float)) Hashtbl.t = Hashtbl.create 100
 
 let prof : string -> (unit -> 'a) -> 'a =
-  fun s f ->
+  fun s f -> (* f () *)
 (* print_string ("<"^s^":"); flush stdout; *)
     let t1 = (Unix.times ()).Unix.tms_utime in
     let m1 = Gc.allocated_bytes () (* float_of_int (Gc.stat ()).Gc.live_words *) in
   Unix.close fd_in;
   Unix.close fd_out
 
+let string_of_file (filename : string) : string =
+  let ch = open_in filename in
+  let buf = Buffer.create 10000 in
+  fold_in_channel
+    (fun () line -> Buffer.add_string buf line)
+    () ch;
+  close_in ch;
+  Buffer.contents buf
+
 (* probabilities *)
 
 open Num
 
 let chance_ge (r,w) (k,n) = prof "chance_ge" (fun () ->
   float_of_num (chance_ge_num (r,w) (k,n)))
+
+(* external applications *)
+
+let xemacs filename pattern =
+  ignore (Sys.command ("xemacs -eval '(progn (find-file \""^filename^"\") (search-forward \"" ^ pattern ^ "\"))' &"))
+
+let mozilla url =
+  ignore (Sys.command ("mozilla -remote \"openurl(" ^ url ^ ")\""))
+    
+let gqview filename =
+  ignore (Sys.command ("gqview " ^ filename ^ " &"))
+
+let cyg2win path = (* convert a Cygwin path to a Windows path *)
+  let shift = 12 in (* prefix "/cygdrive/c/" *)
+  "C:\\" ^ String.concat "\\" (Str.split (Str.regexp "/") (String.sub path shift (String.length path - shift)))
+
+let acdsee_cygwin filename =
+  ignore (Sys.command ("/cygdrive/c/Program\\ Files/ACD\\ Systems/ACDSee\\ Trial\\ Version/ACDSee.exe /v \"" ^ cyg2win filename ^ "\" &"))
+
+let winamp_cygwin filename =
+   ignore (Sys.command ("/cygdrive/c/Program\\ Files/Winamp/winamp.exe \"" ^ cyg2win filename ^ "\" &"))
+