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