|
@@ -36,9 +36,33 @@
|
|
|
|
|
|
open Core.Std;;
|
|
open Core.Std;;
|
|
|
|
|
|
|
|
+(* XXX Using and keyword because each function can call each other *)
|
|
|
|
+(* Function to read the tmp file *)
|
|
|
|
+let rec read ~name =
|
|
|
|
+ (* Get the string corresponding to the file *)
|
|
|
|
+ let file_content = In_channel.read_all name in
|
|
|
|
+ try
|
|
|
|
+ Tmp_biniou_b.tmp_file_of_string file_content
|
|
|
|
+ (* In previous version, the JSON format was used, otherwise the file can
|
|
|
|
+ * have a bad format. In this case, the Ag_ob_run.Error("Read error (1)")
|
|
|
|
+ * exeption is throw. We catch it here *)
|
|
|
|
+ with Ag_ob_run.Error("Read error (1)") ->
|
|
|
|
+ (* If file is not in the right format, delete it and create a new one.
|
|
|
|
+ * Then, read it *)
|
|
|
|
+ Sys.remove name;
|
|
|
|
+ create_tmp_file ~name;
|
|
|
|
+ read ~name
|
|
|
|
+
|
|
|
|
+(* Function to write the tmp file *)
|
|
|
|
+and write ~name (tmp_file:Tmp_biniou_t.tmp_file) =
|
|
|
|
+ let biniou_tmp = Tmp_biniou_b.string_of_tmp_file tmp_file in
|
|
|
|
+ Out_channel.write_all name ~data:biniou_tmp
|
|
|
|
+
|
|
(* Function to create the tmp file *)
|
|
(* Function to create the tmp file *)
|
|
-let create_tmp_file ~name =
|
|
|
|
- Yojson.Basic.pretty_to_channel (Out_channel.create name) Const.tmp_file_template
|
|
|
|
|
|
+and create_tmp_file ~name =
|
|
|
|
+ Tmp_biniou_v.create_tmp_file ~command:[] ~number:0 ()
|
|
|
|
+ (* Convert it to biniou *)
|
|
|
|
+ |> write ~name
|
|
;;
|
|
;;
|
|
|
|
|
|
(* Function to open tmp file *)
|
|
(* Function to open tmp file *)
|
|
@@ -74,10 +98,11 @@ let rec is_prog_in_rc list_from_rc_file program =
|
|
(* Log when a program has been launched in a file in /tmp
|
|
(* Log when a program has been launched in a file in /tmp
|
|
~func is the function applied to the value *)
|
|
~func is the function applied to the value *)
|
|
let log ?(func= (+) 1 ) ~file_name =
|
|
let log ?(func= (+) 1 ) ~file_name =
|
|
- let file = Yojson.Basic.from_file file_name in
|
|
|
|
- match file with
|
|
|
|
- | `Assoc [( a, `List b ); ("num", `Int c)] -> let new_value = `Assoc [( a, `List b ); ("num", `Int (c |> func))] in Yojson.Basic.to_file file_name new_value
|
|
|
|
- | _ -> failwith "Incorrect format"
|
|
|
|
|
|
+ (* We will use tmp_file type *)
|
|
|
|
+ let open Tmp_biniou_t in
|
|
|
|
+ let file = read ~name:file_name in
|
|
|
|
+ (* Write the file with the new value *)
|
|
|
|
+ write ~name:file_name { file with number = (func file.number)}
|
|
;;
|
|
;;
|
|
|
|
|
|
(* Reset command number in two ways :
|
|
(* Reset command number in two ways :
|