]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/urimanager/uriManager.ml
test branch
[helm.git] / helm / ocaml / 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 buri_of_uri (uri,_) = 
60   let xpointer_offset = 
61     try String.rindex uri '#' with Not_found -> String.length uri - 1
62   in
63   let index = String.rindex_from uri xpointer_offset '/' in
64   String.sub uri 0 index
65
66 module OrderedStrings =
67  struct
68   type t = string
69   let compare (s1 : t) (s2 : t) = compare s1 s2
70  end
71 ;;
72
73 module MapStringsToUri = Map.Make(OrderedStrings);;
74
75 (* Invariant: the map is the identity function,
76  *  i.e. 
77  *    let str' = (MapStringsToUri.find str !set_of_uri) in
78  *    str' == (MapStringsToUri.find str' !set_of_uri)
79  *)
80 let set_of_uri = ref MapStringsToUri.empty;;
81
82 exception IllFormedUri of string;;
83
84 let _dottypes = ".types"
85 let _types = "types",5
86 let _dotuniv = ".univ"
87 let _univ = "univ",4
88 let _dotann = ".ann"
89 let _ann = "ann",3
90 let _var = "var",3
91 let _dotbody = ".body"
92 let _con = "con",3
93 let _ind = "ind",3
94 let _xpointer = "#xpointer(1/"
95 let _con3 = "con"
96 let _var3 = "var"
97 let _ind3 = "ind"
98 let _ann3 = "ann"
99 let _univ4 = "univ"
100 let _types5 = "types"
101 let _xpointer8 = "xpointer"
102 let _cic5 = "cic:/"
103
104 let is_malformed suri =
105   try 
106     if String.sub suri 0 5 <> _cic5 then true
107     else
108       let len = String.length suri - 5 in
109       let last5 = String.sub suri len 5 in
110       let last4 = String.sub last5 1 4 in
111       let last3 = String.sub last5 2 3 in
112       if last3 = _con3 || last3 = _var3 || last3 = _ind3 || 
113          last3 = _ann3 || last5 = _types5 || last5 = _dotbody ||
114          last4 = _univ4 then 
115            false
116       else 
117         try
118           let index = String.rindex suri '#' + 1 in
119           let xptr = String.sub suri index 8 in
120           if xptr = _xpointer8 then
121             false
122           else
123             true
124         with Not_found -> true
125   with Invalid_argument _ -> true
126     
127 (* hash conses an uri *)
128 let uri_of_string suri =
129   try
130     MapStringsToUri.find suri !set_of_uri
131   with Not_found -> 
132     if is_malformed suri then
133       raise (IllFormedUri suri) 
134     else
135       let new_uri = suri, fresh_id () in
136       set_of_uri := MapStringsToUri.add suri new_uri !set_of_uri;
137       new_uri
138
139
140 let strip_xpointer ((uri,_) as olduri) =
141   try 
142     let index = String.rindex uri '#' in
143     let no_xpointer = String.sub uri 0 index in
144     uri_of_string no_xpointer
145   with
146     Not_found -> olduri
147
148 let clear_suffix uri ?(pat2="",0) pat1 =
149   try
150     let index = String.rindex uri '.' in
151     let index' = index + 1 in 
152     let suffix = String.sub uri index' (String.length uri - index') in
153     if fst pat1 = suffix || fst pat2 = suffix then
154       String.sub uri 0 index
155     else
156       uri
157   with
158     Not_found -> assert false
159
160 let has_suffix uri (pat,n) =
161   try
162     let suffix = String.sub uri (String.length uri - n) n in
163     pat = suffix 
164   with
165     Not_found -> assert false
166
167  
168 let cicuri_of_uri (uri, _) = uri_of_string (clear_suffix uri ~pat2:_types _ann)
169
170 let annuri_of_uri (uri , _) = uri_of_string ((clear_suffix uri _ann) ^ _dotann)
171
172 let uri_is_annuri (uri, _) = has_suffix uri _ann
173
174 let uri_is_var (uri, _) = has_suffix uri _var
175
176 let uri_is_con (uri, _) = has_suffix uri _con
177
178 let uri_is_ind (uri, _) = has_suffix uri _ind
179
180 let bodyuri_of_uri (uri, _) =
181   if has_suffix uri _con then
182     Some (uri_of_string (uri ^ _dotbody))
183   else
184     None
185 ;;
186
187 (* these are bugged!
188  * we should remove _types, _univ, _ann all toghether *)
189 let innertypesuri_of_uri (uri, _) =
190   uri_of_string ((clear_suffix uri _types) ^ _dottypes)
191 ;;
192 let univgraphuri_of_uri (uri,_) = 
193   uri_of_string ((clear_suffix uri _univ) ^ _dotuniv)
194 ;;
195   
196
197 let uri_of_uriref (uri, _) typeno consno =
198   let typeno = typeno + 1 in
199   let suri =
200     match consno with
201     | None -> Printf.sprintf "%s%s%d)" uri _xpointer typeno
202     | Some n -> Printf.sprintf "%s%s%d/%d)" uri _xpointer typeno n
203   in
204   uri_of_string suri
205
206 let compare (_,id1) (_,id2) = id1 - id2
207
208 module OrderedUri =
209 struct
210   type t = uri
211   let compare = compare (* the one above, not Pervasives.compare *)
212 end
213
214 module UriSet = Set.Make (OrderedUri)
215
216 module HashedUri =
217 struct
218   type t = uri
219   let equal = eq
220   let hash = snd
221 end
222
223 module UriHashtbl = Hashtbl.Make (HashedUri)
224
225