]> matita.cs.unibo.it Git - helm.git/blob - components/urimanager/uriManager.ml
matita 0.5.1 tagged
[helm.git] / components / urimanager / uriManager.ml
1 (* Copyright (C) 2000, 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://cs.unibo.it/helm/.
24  *)
25
26 (* $Id$ *)
27
28 (*
29  * "cic:/a/b/c.con" => ("cic:/a/b/c.con", id )
30  * "cic:/a/b/c.ind#xpointer(1/1)" => ("cic:/a/b/c.con#xpointer(1/1)", id)
31  * "cic:/a/b/c.ind#xpointer(1/1/1)" => ("cic:/a/b/c.con#xpointer(1/1/1)", id)
32  *)
33
34 let fresh_id =
35  let id = ref 0 in
36   function () ->
37     incr id;
38     !id
39
40 (* (uriwithxpointer, uniqueid) 
41  * where uniqueid is used to build a set of uri *)
42 type uri = string * int;;
43
44 let eq uri1 uri2 =
45   uri1 == uri2 
46 ;;
47
48 let string_of_uri (uri,_) =
49  uri
50
51 let name_of_uri (uri, _) = 
52   let xpointer_offset = 
53     try String.rindex uri '#' with Not_found -> String.length uri - 1
54   in
55   let index1 = String.rindex_from uri xpointer_offset '/' + 1 in
56   let index2 = String.rindex uri '.' in
57   String.sub uri index1 (index2 - index1)
58
59 let nameext_of_uri (uri, _) = 
60   let xpointer_offset, mah = 
61     try String.rindex uri '#', 0 with Not_found -> String.length uri - 1, 1
62   in
63   let index1 = String.rindex_from uri xpointer_offset '/' + 1 in
64   String.sub uri index1 (xpointer_offset - index1 + mah)
65   
66 let buri_of_uri (uri,_) = 
67   let xpointer_offset = 
68     try String.rindex uri '#' with Not_found -> String.length uri - 1
69   in
70   let index = String.rindex_from uri xpointer_offset '/' in
71   String.sub uri 0 index
72
73 module OrderedStrings =
74  struct
75   type t = string
76   let compare (s1 : t) (s2 : t) = compare s1 s2
77  end
78 ;;
79
80 module MapStringsToUri = Map.Make(OrderedStrings);;
81
82 (* Invariant: the map is the identity function,
83  *  i.e. 
84  *    let str' = (MapStringsToUri.find str !set_of_uri) in
85  *    str' == (MapStringsToUri.find str' !set_of_uri)
86  *)
87 let set_of_uri = ref MapStringsToUri.empty;;
88
89 exception IllFormedUri of string;;
90
91 let _dottypes = ".types"
92 let _types = "types",5
93 let _dotuniv = ".univ"
94 let _univ = "univ",4
95 let _dotann = ".ann"
96 let _ann = "ann",3
97 let _var = "var",3
98 let _dotbody = ".body"
99 let _con = "con",3
100 let _ind = "ind",3
101 let _xpointer = "#xpointer(1/"
102 let _con3 = "con"
103 let _var3 = "var"
104 let _ind3 = "ind"
105 let _ann3 = "ann"
106 let _univ4 = "univ"
107 let _types5 = "types"
108 let _xpointer8 = "xpointer"
109 let _cic5 = "cic:/"
110
111 let is_malformed suri =
112   try 
113     if String.sub suri 0 5 <> _cic5 then true
114     else
115       let len = String.length suri - 5 in
116       let last5 = String.sub suri len 5 in
117       let last4 = String.sub last5 1 4 in
118       let last3 = String.sub last5 2 3 in
119       if last3 = _con3 || last3 = _var3 || last3 = _ind3 || 
120          last3 = _ann3 || last5 = _types5 || last5 = _dotbody ||
121          last4 = _univ4 then 
122            false
123       else 
124         try
125           let index = String.rindex suri '#' + 1 in
126           let xptr = String.sub suri index 8 in
127           if xptr = _xpointer8 then
128             false
129           else
130             true
131         with Not_found -> true
132   with Invalid_argument _ -> true
133     
134 (* hash conses an uri *)
135 let uri_of_string suri =
136   try
137     MapStringsToUri.find suri !set_of_uri
138   with Not_found -> 
139     if is_malformed suri then
140       raise (IllFormedUri suri) 
141     else
142       let new_uri = suri, fresh_id () in
143       set_of_uri := MapStringsToUri.add suri new_uri !set_of_uri;
144       new_uri
145
146
147 let strip_xpointer ((uri,_) as olduri) =
148   try 
149     let index = String.rindex uri '#' in
150     let no_xpointer = String.sub uri 0 index in
151     uri_of_string no_xpointer
152   with
153     Not_found -> olduri
154
155 let clear_suffix uri ?(pat2="",0) pat1 =
156   try
157     let index = String.rindex uri '.' in
158     let index' = index + 1 in 
159     let suffix = String.sub uri index' (String.length uri - index') in
160     if fst pat1 = suffix || fst pat2 = suffix then
161       String.sub uri 0 index
162     else
163       uri
164   with
165     Not_found -> assert false
166
167 let has_suffix uri (pat,n) =
168   try
169     let suffix = String.sub uri (String.length uri - n) n in
170     pat = suffix 
171   with
172     Not_found -> assert false
173
174  
175 let cicuri_of_uri (uri, _) = uri_of_string (clear_suffix uri ~pat2:_types _ann)
176
177 let annuri_of_uri (uri , _) = uri_of_string ((clear_suffix uri _ann) ^ _dotann)
178
179 let uri_is_annuri (uri, _) = has_suffix uri _ann
180
181 let uri_is_var (uri, _) = has_suffix uri _var
182
183 let uri_is_con (uri, _) = has_suffix uri _con
184
185 let uri_is_ind (uri, _) = has_suffix uri _ind
186
187 let bodyuri_of_uri (uri, _) =
188   if has_suffix uri _con then
189     Some (uri_of_string (uri ^ _dotbody))
190   else
191     None
192 ;;
193
194 (* these are bugged!
195  * we should remove _types, _univ, _ann all toghether *)
196 let innertypesuri_of_uri (uri, _) =
197   uri_of_string ((clear_suffix uri _types) ^ _dottypes)
198 ;;
199 let univgraphuri_of_uri (uri,_) = 
200   uri_of_string ((clear_suffix uri _univ) ^ _dotuniv)
201 ;;
202   
203
204 let uri_of_uriref (uri, _) typeno consno =
205   let typeno = typeno + 1 in
206   let suri =
207     match consno with
208     | None -> Printf.sprintf "%s%s%d)" uri _xpointer typeno
209     | Some n -> Printf.sprintf "%s%s%d/%d)" uri _xpointer typeno n
210   in
211   uri_of_string suri
212
213 let compare (_,id1) (_,id2) = id1 - id2
214
215 module OrderedUri =
216 struct
217   type t = uri
218   let compare = compare (* the one above, not Pervasives.compare *)
219 end
220
221 module UriSet = Set.Make (OrderedUri)
222
223 (*
224 module OrderedUriPair =
225 struct
226   type t = uri * uri
227   let compare (u11, u12) (u21, u22) =
228     match compare u11 u21 with
229     | 0 -> compare u12 u22
230     | x -> x
231 end
232
233 module UriPairSet = Set.Make (OrderedUriPair)
234 *)
235
236 module HashedUri =
237 struct
238   type t = uri
239   let equal = eq
240   let hash = snd
241 end
242
243 module UriHashtbl = Hashtbl.Make (HashedUri)
244
245