]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitaMisc.ml
snapshot, notably:
[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   let doc =
54     Misc.domImpl#createDocument ~namespaceURI:(Some Misc.mathml_ns)
55       ~qualifiedName:(Gdome.domString "math") ~doctype:None
56   in
57   doc#get_documentElement
58
59 exception History_failure
60
61 type 'a memento = 'a array * int * int * int  (* data, hd, tl, cur *)
62
63 class type ['a] history =
64   object
65     method add : 'a -> unit
66     method next : 'a
67     method previous : 'a
68     method load: 'a memento -> unit
69     method save: 'a memento
70   end
71
72 class shell_history size =
73   let size = size + 1 in
74   let decr x = let x' = x - 1 in if x' < 0 then size + x' else x' in
75   let incr x = (x + 1) mod size in
76   object (self)
77     val data = Array.create size ""
78     val mutable hd = 0  (* insertion point *)
79     val mutable tl = -1 (* oldest inserted item *)
80     val mutable cur = -1  (* current item for the history *)
81     method add s =
82       data.(hd) <- s;
83       if tl = -1 then tl <- hd;
84       hd <- incr hd;
85       if hd = tl then tl <- incr tl;
86       cur <- hd
87     method previous =
88       if cur = tl then raise History_failure;
89       cur <- decr cur;
90       data.(cur)
91     method next =
92       if cur = hd then raise History_failure;
93       cur <- incr cur;
94       if cur = hd then "" else data.(cur)
95     method load (data', hd', tl', cur') =
96       assert (Array.length data = Array.length data');
97       hd <- hd'; tl <- tl'; cur <- cur';
98       Array.blit data' 0 data 0 (Array.length data')
99     method save = (Array.copy data, hd, tl, cur)
100   end
101
102 class ['a] browser_history ?memento size init =
103   object (self)
104     initializer match memento with Some m -> self#load m | _ -> ()
105     val data = Array.create size init
106     val mutable hd = 0
107     val mutable tl = 0
108     val mutable cur = 0
109     method previous =
110       if cur = tl then raise History_failure;
111       cur <- cur - 1;
112       if cur = ~-1 then cur <- size - 1;
113       data.(cur)
114     method next =
115       if cur = hd then raise History_failure;
116       cur <- cur + 1;
117       if cur = size then cur <- 0;
118       data.(cur)
119     method add (e:'a) =
120       cur <- cur + 1;
121       if cur = size then cur <- 0;
122       if cur = tl then tl <- tl + 1;
123       if tl = size then tl <- 0;
124       hd <- cur;
125       data.(cur) <- e
126     method load (data', hd', tl', cur') =
127       assert (Array.length data = Array.length data');
128       hd <- hd'; tl <- tl'; cur <- cur';
129       Array.blit data' 0 data 0 (Array.length data')
130     method save = (Array.copy data, hd, tl, cur)
131   end
132