]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitaMisc.ml
added choose_uri method to console, used by the interpreter to implement the
[helm.git] / helm / matita / matitaMisc.ml
1 (* Copyright (C) 2004, HELM Team.
2  * 
3  * This file is part of HELM, an Hypertextual, Electronic
4  * Library of Mathematics, developed at the Computer Science
5  * Department, University of Bologna, Italy.
6  * 
7  * HELM is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * 
12  * HELM is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with HELM; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://helm.cs.unibo.it/
24  *)
25
26 open Printf
27
28 let is_dir fname = (Unix.stat fname).Unix.st_kind = Unix.S_DIR
29 let is_regular fname = (Unix.stat fname).Unix.st_kind = Unix.S_REG
30
31 let input_file fname =
32   let size = (Unix.stat fname).Unix.st_size in
33   let buf = Buffer.create size in
34   let ic = open_in fname in
35   Buffer.add_channel buf ic size;
36   close_in ic;
37   Buffer.contents buf
38
39 let is_proof_script fname = true  (** TODO Zack *)
40 let is_proof_object fname = true  (** TODO Zack *)
41
42 let append_phrase_sep s =
43   if not (Pcre.pmatch ~pat:(sprintf "%s$" BuildTimeConf.phrase_sep) s) then
44     s ^ BuildTimeConf.phrase_sep
45   else
46     s
47
48 let strip_trailing_blanks =
49   let rex = Pcre.regexp "\\s*$" in
50   fun s -> Pcre.replace ~rex s
51
52 let empty_mathml () =
53   Misc.domImpl#createDocument ~namespaceURI:(Some Misc.mathml_ns)
54     ~qualifiedName:(Gdome.domString "math") ~doctype:None
55
56 let empty_boxml () =
57   Misc.domImpl#createDocument ~namespaceURI:(Some Misc.boxml_ns) 
58     ~qualifiedName:(Gdome.domString "box") ~doctype:None
59
60 exception History_failure
61
62 type 'a memento = 'a array * int * int * int  (* data, hd, tl, cur *)
63
64 class type ['a] history =
65   object
66     method add : 'a -> unit
67     method next : 'a
68     method previous : 'a
69     method load: 'a memento -> unit
70     method save: 'a memento
71   end
72
73 class shell_history size =
74   let size = size + 1 in
75   let decr x = let x' = x - 1 in if x' < 0 then size + x' else x' in
76   let incr x = (x + 1) mod size in
77   object (self)
78     val data = Array.create size ""
79     val mutable hd = 0  (* insertion point *)
80     val mutable tl = -1 (* oldest inserted item *)
81     val mutable cur = -1  (* current item for the history *)
82     method add s =
83       data.(hd) <- s;
84       if tl = -1 then tl <- hd;
85       hd <- incr hd;
86       if hd = tl then tl <- incr tl;
87       cur <- hd
88     method previous =
89       if cur = tl then raise History_failure;
90       cur <- decr cur;
91       data.(cur)
92     method next =
93       if cur = hd then raise History_failure;
94       cur <- incr cur;
95       if cur = hd then "" else data.(cur)
96     method load (data', hd', tl', cur') =
97       assert (Array.length data = Array.length data');
98       hd <- hd'; tl <- tl'; cur <- cur';
99       Array.blit data' 0 data 0 (Array.length data')
100     method save = (Array.copy data, hd, tl, cur)
101   end
102
103 class ['a] browser_history ?memento size init =
104   object (self)
105     initializer match memento with Some m -> self#load m | _ -> ()
106     val data = Array.create size init
107     val mutable hd = 0
108     val mutable tl = 0
109     val mutable cur = 0
110     method previous =
111       if cur = tl then raise History_failure;
112       cur <- cur - 1;
113       if cur = ~-1 then cur <- size - 1;
114       data.(cur)
115     method next =
116       if cur = hd then raise History_failure;
117       cur <- cur + 1;
118       if cur = size then cur <- 0;
119       data.(cur)
120     method add (e:'a) =
121       cur <- cur + 1;
122       if cur = size then cur <- 0;
123       if cur = tl then tl <- tl + 1;
124       if tl = size then tl <- 0;
125       hd <- cur;
126       data.(cur) <- e
127     method load (data', hd', tl', cur') =
128       assert (Array.length data = Array.length data');
129       hd <- hd'; tl <- tl'; cur <- cur';
130       Array.blit data' 0 data 0 (Array.length data')
131     method save = (Array.copy data, hd, tl, cur)
132   end
133
134 let dbd_instance =
135   let dbd = lazy (
136     Mysql.quick_connect
137       ~host:(Helm_registry.get "db.host")
138       ~user:(Helm_registry.get "db.user")
139       ~database:(Helm_registry.get "db.database")
140       ())
141   in
142   fun () -> Lazy.force dbd
143
144 let singleton f =
145   let instance = lazy (f ()) in
146   fun () -> Lazy.force instance
147
148 let mkdirs = List.iter (fun d -> ignore (Unix.system ("mkdir -p " ^ d)))
149