https://github.com/EasyCrypt/easycrypt
Revision b93b1f80358aa3b56caca1ec0c562006636aea52 authored by Benjamin Gregoire on 06 February 2018, 15:04:56 UTC, committed by Benjamin Gregoire on 06 February 2018, 15:04:56 UTC
1 parent edc1072
Tip revision: b93b1f80358aa3b56caca1ec0c562006636aea52 authored by Benjamin Gregoire on 06 February 2018, 15:04:56 UTC
small progress
small progress
Tip revision: b93b1f8
ecFortune.ml
(* --------------------------------------------------------------------
* Copyright (c) - 2012--2016 - IMDEA Software Institute
* Copyright (c) - 2012--2017 - Inria
*
* Distributed under the terms of the CeCILL-C-V1 license
* -------------------------------------------------------------------- *)
(* --------------------------------------------------------------------- *)
open EcUtils
(* --------------------------------------------------------------------- *)
let thepick = ref (fun () -> failwith "fortune-uninit")
(* -------------------------------------------------------------------- *)
let pickfor values : unit -> string option =
fun () ->
match Array.length values with
| n when n <= 0 -> None
| n -> Some (values.(Random.int n))
(* --------------------------------------------------------------------- *)
let fortune_from_stream : in_channel -> string list =
let rec aux acc stream =
try
let line = input_line stream in
let line = String.strip line in
if String.length line = 0
then aux acc stream
else aux (line :: acc) stream
with End_of_file -> acc
in fun stream -> aux [] stream
(* --------------------------------------------------------------------- *)
let init () =
let conffiles =
XDG.Data.file
~exists:true ~appname:"easycrypt" ~mode:`All
"fortune.conf"
in
let values =
let for1 filename =
try
let stream = open_in filename in
EcUtils.try_finally
(fun () -> fortune_from_stream stream)
(fun () -> close_in stream)
with Sys_error _ ->
[]
in List.flatten (List.map for1 conffiles)
in thepick := pickfor (Array.of_list values)
(* --------------------------------------------------------------------- *)
let pick () : string option =
!thepick ()
Computing file changes ...