]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitaMisc.ml
test branch
[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 (* $Id$ *)
27
28 open Printf
29
30 (** Functions "imported" from Http_getter_misc *)
31
32 let normalize_dir = Http_getter_misc.normalize_dir
33 let strip_suffix = Http_getter_misc.strip_suffix
34
35 let absolute_path file =
36   if file.[0] = '/' then file else Unix.getcwd () ^ "/" ^ file
37   
38 let is_proof_script fname = true  (** TODO Zack *)
39 let is_proof_object fname = true  (** TODO Zack *)
40
41 let append_phrase_sep s =
42   if not (Pcre.pmatch ~pat:(sprintf "%s$" BuildTimeConf.phrase_sep) s) then
43     s ^ BuildTimeConf.phrase_sep
44   else
45     s
46
47 exception History_failure
48
49 type 'a memento = 'a array * int * int * int  (* data, hd, tl, cur *)
50
51 class type ['a] history =
52   object
53     method add : 'a -> unit
54     method next : 'a
55     method previous : 'a
56     method load: 'a memento -> unit
57     method save: 'a memento
58     method is_begin: bool
59     method is_end: bool
60   end
61
62 class basic_history (head, tail, cur) =
63   object
64     val mutable hd = head  (* insertion point *)
65     val mutable tl = tail (* oldest inserted item *)
66     val mutable cur = cur  (* current item for the history *)
67     
68     method is_begin = cur <= tl
69     method is_end = cur >= hd
70   end
71   
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
80     inherit basic_history (0, -1 , -1)
81     
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
108     inherit basic_history (0, 0, 0)
109     
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       if e <> data.(cur) then
122         begin
123           cur <- cur + 1;
124           if cur = size then cur <- 0;
125           if cur = tl then tl <- tl + 1;
126           if tl = size then tl <- 0;
127           hd <- cur;
128           data.(cur) <- e
129         end
130     method load (data', hd', tl', cur') =
131       assert (Array.length data = Array.length data');
132       hd <- hd'; tl <- tl'; cur <- cur';
133       Array.blit data' 0 data 0 (Array.length data')
134     method save = (Array.copy data, hd, tl, cur)
135   end
136
137 let singleton f =
138   let instance = lazy (f ()) in
139   fun () -> Lazy.force instance
140
141 let image_path n = sprintf "%s/%s" BuildTimeConf.images_dir n
142
143 let end_ma_RE = Pcre.regexp "\\.ma$"
144
145 let list_tl_at ?(equality=(==)) e l =
146   let rec aux =
147     function
148     | [] -> raise Not_found
149     | hd :: tl as l when equality hd e -> l
150     | hd :: tl -> aux tl
151   in
152   aux l