]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitaMisc.ml
ocaml 3.09 transition
[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 (** Functions "imported" from Http_getter_misc *)
30
31 let strip_trailing_slash = Http_getter_misc.strip_trailing_slash
32 let normalize_dir = Http_getter_misc.normalize_dir
33 let strip_suffix = Http_getter_misc.strip_suffix
34
35 let baseuri_of_baseuri_decl st =
36   match st with
37   | GrafiteAst.Executable (_, GrafiteAst.Command (_, GrafiteAst.Set (_, "baseuri", buri))) ->
38       Some buri
39   | _ -> None
40
41 let is_empty buri =
42  List.for_all
43   (function
44       Http_getter_types.Ls_section _ -> true
45     | Http_getter_types.Ls_object _ -> false)
46   (Http_getter.ls (Http_getter_misc.strip_trailing_slash buri ^ "/"))
47
48 let safe_remove fname = if Sys.file_exists fname then Sys.remove fname
49
50 let is_dir_empty d =
51   try 
52     let od = Unix.opendir d in
53     try 
54       ignore (Unix.readdir od);
55       ignore (Unix.readdir od);
56       ignore (Unix.readdir od);
57       Unix.closedir od;
58       false
59     with End_of_file -> 
60       Unix.closedir od;
61       true
62   with Unix.Unix_error _ -> true
63
64 let safe_rmdir d = try Unix.rmdir d with Unix.Unix_error _ -> ()
65
66 let rec rmdir_descend d = 
67   if is_dir_empty d then
68     begin
69       safe_rmdir d;
70       rmdir_descend (Filename.dirname d)
71     end
72
73 let absolute_path file =
74   if file.[0] = '/' then file else Unix.getcwd () ^ "/" ^ file
75   
76 let is_proof_script fname = true  (** TODO Zack *)
77 let is_proof_object fname = true  (** TODO Zack *)
78
79 let append_phrase_sep s =
80   if not (Pcre.pmatch ~pat:(sprintf "%s$" BuildTimeConf.phrase_sep) s) then
81     s ^ BuildTimeConf.phrase_sep
82   else
83     s
84
85 exception History_failure
86
87 type 'a memento = 'a array * int * int * int  (* data, hd, tl, cur *)
88
89 class type ['a] history =
90   object
91     method add : 'a -> unit
92     method next : 'a
93     method previous : 'a
94     method load: 'a memento -> unit
95     method save: 'a memento
96     method is_begin: bool
97     method is_end: bool
98   end
99
100 class basic_history (head, tail, cur) =
101   object
102     val mutable hd = head  (* insertion point *)
103     val mutable tl = tail (* oldest inserted item *)
104     val mutable cur = cur  (* current item for the history *)
105     
106     method is_begin = cur <= tl
107     method is_end = cur >= hd
108   end
109   
110   
111 class shell_history size =
112   let size = size + 1 in
113   let decr x = let x' = x - 1 in if x' < 0 then size + x' else x' in
114   let incr x = (x + 1) mod size in
115   object (self)
116     val data = Array.create size ""
117
118     inherit basic_history (0, -1 , -1)
119     
120     method add s =
121       data.(hd) <- s;
122       if tl = -1 then tl <- hd;
123       hd <- incr hd;
124       if hd = tl then tl <- incr tl;
125       cur <- hd
126     method previous =
127       if cur = tl then raise History_failure;
128       cur <- decr cur;
129       data.(cur)
130     method next =
131       if cur = hd then raise History_failure;
132       cur <- incr cur;
133       if cur = hd then "" else data.(cur)
134     method load (data', hd', tl', cur') =
135       assert (Array.length data = Array.length data');
136       hd <- hd'; tl <- tl'; cur <- cur';
137       Array.blit data' 0 data 0 (Array.length data')
138     method save = (Array.copy data, hd, tl, cur)
139   end
140
141 class ['a] browser_history ?memento size init =
142   object (self)
143     initializer match memento with Some m -> self#load m | _ -> ()
144     val data = Array.create size init
145
146     inherit basic_history (0, 0, 0)
147     
148     method previous =
149       if cur = tl then raise History_failure;
150       cur <- cur - 1;
151       if cur = ~-1 then cur <- size - 1;
152       data.(cur)
153     method next =
154       if cur = hd then raise History_failure;
155       cur <- cur + 1;
156       if cur = size then cur <- 0;
157       data.(cur)
158     method add (e:'a) =
159       if e <> data.(cur) then
160         begin
161           cur <- cur + 1;
162           if cur = size then cur <- 0;
163           if cur = tl then tl <- tl + 1;
164           if tl = size then tl <- 0;
165           hd <- cur;
166           data.(cur) <- e
167         end
168     method load (data', hd', tl', cur') =
169       assert (Array.length data = Array.length data');
170       hd <- hd'; tl <- tl'; cur <- cur';
171       Array.blit data' 0 data 0 (Array.length data')
172     method save = (Array.copy data, hd, tl, cur)
173   end
174
175 let singleton f =
176   let instance = lazy (f ()) in
177   fun () -> Lazy.force instance
178
179 let image_path n = sprintf "%s/%s" BuildTimeConf.images_dir n
180
181 let end_ma_RE = Pcre.regexp "\\.ma$"
182
183 let obj_file_of_baseuri baseuri =
184  let path =
185   Helm_registry.get "matita.basedir" ^ "/xml" ^
186    Pcre.replace ~pat:"^cic:" ~templ:"" baseuri
187  in
188   path ^ ".moo"
189
190 let list_tl_at ?(equality=(==)) e l =
191   let rec aux =
192     function
193     | [] -> raise Not_found
194     | hd :: tl as l when equality hd e -> l
195     | hd :: tl -> aux tl
196   in
197   aux l
198