1 (* Copyright (C) 2000, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://cs.unibo.it/helm/.
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)
40 (* (uriwithxpointer, uniqueid)
41 * where uniqueid is used to build a set of uri *)
42 type uri = string * int;;
48 let string_of_uri (uri,_) =
51 let name_of_uri (uri, _) =
53 try String.rindex uri '#' with Not_found -> String.length uri - 1
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)
59 let buri_of_uri (uri,_) =
61 try String.rindex uri '#' with Not_found -> String.length uri - 1
63 let index = String.rindex_from uri xpointer_offset '/' in
64 String.sub uri 0 index
66 module OrderedStrings =
69 let compare (s1 : t) (s2 : t) = compare s1 s2
73 module MapStringsToUri = Map.Make(OrderedStrings);;
75 (* Invariant: the map is the identity function,
77 * let str' = (MapStringsToUri.find str !set_of_uri) in
78 * str' == (MapStringsToUri.find str' !set_of_uri)
80 let set_of_uri = ref MapStringsToUri.empty;;
82 exception IllFormedUri of string;;
84 let _dottypes = ".types"
85 let _types = "types",5
86 let _dotuniv = ".univ"
91 let _dotbody = ".body"
94 let _xpointer = "#xpointer(1/"
100 let _types5 = "types"
101 let _xpointer8 = "xpointer"
104 let is_malformed suri =
106 if String.sub suri 0 5 <> _cic5 then true
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 ||
118 let index = String.rindex suri '#' + 1 in
119 let xptr = String.sub suri index 8 in
120 if xptr = _xpointer8 then
124 with Not_found -> true
125 with Invalid_argument _ -> true
127 (* hash conses an uri *)
128 let uri_of_string suri =
130 MapStringsToUri.find suri !set_of_uri
132 if is_malformed suri then
133 raise (IllFormedUri suri)
135 let new_uri = suri, fresh_id () in
136 set_of_uri := MapStringsToUri.add suri new_uri !set_of_uri;
140 let strip_xpointer ((uri,_) as olduri) =
142 let index = String.rindex uri '#' in
143 let no_xpointer = String.sub uri 0 index in
144 uri_of_string no_xpointer
148 let clear_suffix uri ?(pat2="",0) pat1 =
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
158 Not_found -> assert false
160 let has_suffix uri (pat,n) =
162 let suffix = String.sub uri (String.length uri - n) n in
165 Not_found -> assert false
168 let cicuri_of_uri (uri, _) = uri_of_string (clear_suffix uri ~pat2:_types _ann)
170 let annuri_of_uri (uri , _) = uri_of_string ((clear_suffix uri _ann) ^ _dotann)
172 let uri_is_annuri (uri, _) = has_suffix uri _ann
174 let uri_is_var (uri, _) = has_suffix uri _var
176 let uri_is_con (uri, _) = has_suffix uri _con
178 let uri_is_ind (uri, _) = has_suffix uri _ind
180 let bodyuri_of_uri (uri, _) =
181 if has_suffix uri _con then
182 Some (uri_of_string (uri ^ _dotbody))
188 * we should remove _types, _univ, _ann all toghether *)
189 let innertypesuri_of_uri (uri, _) =
190 uri_of_string ((clear_suffix uri _types) ^ _dottypes)
192 let univgraphuri_of_uri (uri,_) =
193 uri_of_string ((clear_suffix uri _univ) ^ _dotuniv)
197 let uri_of_uriref (uri, _) typeno consno =
198 let typeno = typeno + 1 in
201 | None -> Printf.sprintf "%s%s%d)" uri _xpointer typeno
202 | Some n -> Printf.sprintf "%s%s%d/%d)" uri _xpointer typeno n
206 let compare (_,id1) (_,id2) = id1 - id2
211 let compare = compare (* the one above, not Pervasives.compare *)
214 module UriSet = Set.Make (OrderedUri)
223 module UriHashtbl = Hashtbl.Make (HashedUri)