]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitaMisc.ml
* safe_remove exported and moved to MatitaMisc
[helm.git] / helm / matita / matitaMisc.ml
1 (* Copyright (C) 2004-2005, 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 open MatitaTypes 
28
29 let safe_remove fname = if Sys.file_exists fname then Sys.remove fname
30
31 let is_dir fname =
32   try
33     (Unix.stat fname).Unix.st_kind = Unix.S_DIR
34   with Unix.Unix_error _ -> false
35
36 let is_regular fname =
37   try
38     (Unix.stat fname).Unix.st_kind = Unix.S_REG
39   with Unix.Unix_error _ -> false
40
41 let input_file fname =
42   let size = (Unix.stat fname).Unix.st_size in
43   let buf = Buffer.create size in
44   let ic = open_in fname in
45   Buffer.add_channel buf ic size;
46   close_in ic;
47   Buffer.contents buf
48
49 let is_proof_script fname = true  (** TODO Zack *)
50 let is_proof_object fname = true  (** TODO Zack *)
51
52 let append_phrase_sep s =
53   if not (Pcre.pmatch ~pat:(sprintf "%s$" BuildTimeConf.phrase_sep) s) then
54     s ^ BuildTimeConf.phrase_sep
55   else
56     s
57
58 let strip_trailing_blanks =
59   let rex = Pcre.regexp "\\s*$" in
60   fun s -> Pcre.replace ~rex s
61
62 let strip_trailing_slash =
63   let rex = Pcre.regexp "/$" in
64   fun s -> Pcre.replace ~rex s
65
66 let empty_mathml () =
67   DomMisc.domImpl#createDocument ~namespaceURI:(Some DomMisc.mathml_ns)
68     ~qualifiedName:(Gdome.domString "math") ~doctype:None
69
70 let empty_boxml () =
71   DomMisc.domImpl#createDocument ~namespaceURI:(Some DomMisc.boxml_ns) 
72     ~qualifiedName:(Gdome.domString "box") ~doctype:None
73
74 exception History_failure
75
76 type 'a memento = 'a array * int * int * int  (* data, hd, tl, cur *)
77
78 class type ['a] history =
79   object
80     method add : 'a -> unit
81     method next : 'a
82     method previous : 'a
83     method load: 'a memento -> unit
84     method save: 'a memento
85     method is_begin: bool
86     method is_end: bool
87   end
88
89 class basic_history (head, tail, cur) =
90   object
91     val mutable hd = head  (* insertion point *)
92     val mutable tl = tail (* oldest inserted item *)
93     val mutable cur = cur  (* current item for the history *)
94     
95     method is_begin = cur <= tl
96     method is_end = cur >= hd
97   end
98   
99   
100 class shell_history size =
101   let size = size + 1 in
102   let decr x = let x' = x - 1 in if x' < 0 then size + x' else x' in
103   let incr x = (x + 1) mod size in
104   object (self)
105     val data = Array.create size ""
106
107     inherit basic_history (0, -1 , -1)
108     
109     method add s =
110       data.(hd) <- s;
111       if tl = -1 then tl <- hd;
112       hd <- incr hd;
113       if hd = tl then tl <- incr tl;
114       cur <- hd
115     method previous =
116       if cur = tl then raise History_failure;
117       cur <- decr cur;
118       data.(cur)
119     method next =
120       if cur = hd then raise History_failure;
121       cur <- incr cur;
122       if cur = hd then "" else data.(cur)
123     method load (data', hd', tl', cur') =
124       assert (Array.length data = Array.length data');
125       hd <- hd'; tl <- tl'; cur <- cur';
126       Array.blit data' 0 data 0 (Array.length data')
127     method save = (Array.copy data, hd, tl, cur)
128   end
129
130 class ['a] browser_history ?memento size init =
131   object (self)
132     initializer match memento with Some m -> self#load m | _ -> ()
133     val data = Array.create size init
134
135     inherit basic_history (0, 0, 0)
136     
137     method previous =
138       if cur = tl then raise History_failure;
139       cur <- cur - 1;
140       if cur = ~-1 then cur <- size - 1;
141       data.(cur)
142     method next =
143       if cur = hd then raise History_failure;
144       cur <- cur + 1;
145       if cur = size then cur <- 0;
146       data.(cur)
147     method add (e:'a) =
148       if e <> data.(cur) then
149         begin
150           cur <- cur + 1;
151           if cur = size then cur <- 0;
152           if cur = tl then tl <- tl + 1;
153           if tl = size then tl <- 0;
154           hd <- cur;
155           data.(cur) <- e
156         end
157     method load (data', hd', tl', cur') =
158       assert (Array.length data = Array.length data');
159       hd <- hd'; tl <- tl'; cur <- cur';
160       Array.blit data' 0 data 0 (Array.length data')
161     method save = (Array.copy data, hd, tl, cur)
162   end
163
164 let singleton f =
165   let instance = lazy (f ()) in
166   fun () -> Lazy.force instance
167
168 let mkdir d =
169   let errmsg = sprintf "Unable to create directory \"%s\"" d in
170   try
171     (match Unix.system ("mkdir -p " ^ d) with
172     | Unix.WEXITED 0 -> ()
173     | _ -> failwith errmsg)
174   with Unix.Unix_error _ -> failwith errmsg
175
176 let get_proof_status status =
177   match status.proof_status with
178   | Incomplete_proof s -> s
179   | _ -> statement_error "no ongoing proof"
180
181 let get_proof_metasenv status =
182   match status.proof_status with
183   | No_proof -> []
184   | Incomplete_proof ((_, metasenv, _, _), _) -> metasenv
185   | Proof (_, metasenv, _, _) -> metasenv
186   | Intermediate m -> m
187
188 let get_proof_context status =
189   match status.proof_status with
190   | Incomplete_proof ((_, metasenv, _, _), goal) ->
191       let (_, context, _) = CicUtil.lookup_meta goal metasenv in
192       context
193   | _ -> []
194  
195 let get_proof_aliases status = status.aliases
196
197 let qualify status name = get_string_option status "baseuri" ^ "/" ^ name
198
199 let unopt = function None -> failwith "unopt: None" | Some v -> v
200
201 let image_path n = sprintf "%s/%s" BuildTimeConf.images_dir n
202
203 let end_ma_RE = Pcre.regexp "\\.ma$"
204 let obj_file_of_script f = Pcre.replace ~rex:end_ma_RE ~templ:".moo" f
205
206 let rec list_uniq = function 
207   | [] -> []
208   | h::[] -> [h]
209   | h1::h2::tl when h1 = h2 -> list_uniq (h2 :: tl) 
210   | h1::tl (* when h1 <> h2 *) -> h1 :: list_uniq tl
211
212 let debug_wrap name f =
213   prerr_endline (sprintf "debug_wrap: ==>> %s" name);
214   let res = f () in
215   prerr_endline (sprintf "debug_wrap: <<== %s" name);
216   res
217