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/.
27 * "cic:/a/b/c.con" => [| "cic:/a" ; "cic:/a/b" ; "cic:/a/b/c.con" ; "c"; "" |]
28 * "cic:/a/b/c.ind#xpointer(1/1)" =>
29 * [| "cic:/a" ; "cic:/a/b" ; "cic:/a/b/c.con" ; "c"; "#xpointer(1/1)" |]
30 * "cic:/a/b/c.ind#xpointer(1/1/1)" =>
31 * [| "cic:/a" ; "cic:/a/b" ; "cic:/a/b/c.con" ; "c"; "#xpointer(1/1/1)" |]
33 type uri = string array;;
39 let string_of_uri uri =
40 match uri.(Array.length uri - 1) with
42 uri.(Array.length uri - 3)
45 [ uri.(Array.length uri - 3); uri.(Array.length uri - 1) ]
47 let name_of_uri uri = uri.(Array.length uri - 2);;
48 let buri_of_uri uri = uri.(Array.length uri - 4);;
49 let depth_of_uri uri = Array.length uri - 3;;
51 module OrderedStrings =
54 let compare (s1 : t) (s2 : t) = compare s1 s2
58 module SetOfStrings = Map.Make(OrderedStrings);;
60 (*CSC: commento obsoleto ed errato *)
61 (* Invariant: the map is the identity function, *)
62 (* i.e. (SetOfStrings.find str !set_of_uri) == str *)
63 let set_of_uri = ref SetOfStrings.empty;;
64 let set_of_prefixes = ref SetOfStrings.empty;;
67 (* hash conses an uri *)
68 let add_to_uriset ?suri uri =
69 let lookup_suri suri =
71 SetOfStrings.find suri !set_of_uri
72 with Not_found -> assert false
76 | None -> string_of_uri uri
79 if not(SetOfStrings.mem suri !set_of_uri) then
80 set_of_uri := SetOfStrings.add suri uri !set_of_uri;
84 (* similar to uri_of_string, but used for prefixes of uris *)
85 let normalize prefix =
87 SetOfStrings.find prefix !set_of_prefixes
90 set_of_prefixes := SetOfStrings.add prefix prefix !set_of_prefixes ;
94 exception IllFormedUri of string;;
96 let mk_prefixes str xpointer =
100 let prefix_uri = curi ^ "/" ^ he
101 and name = List.hd (Str.split (Str.regexp "\\.") he) in
102 [ normalize prefix_uri ; name ]
104 let prefix_uri = curi ^ "/" ^ he in
105 (normalize prefix_uri)::(aux prefix_uri tl)
106 | _ -> raise (IllFormedUri str)
108 let tokens = (Str.split (Str.regexp "/") str) in
111 (try (List.hd tokens, List.tl tokens)
112 with Failure "hd" | Failure "tl" ->
113 raise (IllFormedUri str))
115 (aux ty sp) @ [xpointer]
123 let uri_of_string str =
125 match Str.split sharp_rex str with
127 | [base; xpointer] -> base,xpointer
128 | _ -> raise (IllFormedUri str)
131 SetOfStrings.find str !set_of_uri
134 let uri = Array.of_list (mk_prefixes base xpointer) in
135 add_to_uriset ~suri:str uri
138 let strip_xpointer uri =
139 let stripped_uri = Array.copy uri in
140 stripped_uri.(Array.length uri - 1) <- ""; (* reset xpointer field *)
141 let suri = string_of_uri stripped_uri in
142 add_to_uriset ~suri stripped_uri
145 let cicuri_of_uri uri =
146 let completeuri = string_of_uri uri in
148 (Str.replace_first (Str.regexp "\\.types$") ""
149 (Str.replace_first (Str.regexp "\\.ann$") "" completeuri))
151 if completeuri = newcompleteuri then
154 let newuri = Array.copy uri in
155 newuri.(Array.length uri - 3) <- newcompleteuri ;
159 let annuri_of_uri uri =
160 let completeuri = string_of_uri uri in
161 if Str.string_match (Str.regexp ".*\\.ann$") completeuri 0 then
164 let newuri = Array.copy uri in
165 newuri.(Array.length uri - 3) <- completeuri ^ ".ann" ;
169 let uri_is_annuri uri =
170 Str.string_match (Str.regexp ".*\\.ann$") (string_of_uri uri) 0
173 let bodyuri_of_uri uri =
174 let struri = string_of_uri uri in
175 if Str.string_match (Str.regexp ".*\\.con$") (string_of_uri uri) 0 then
176 let newuri = Array.copy uri in
177 newuri.(Array.length uri - 3) <- struri ^ ".body" ;
178 Some (add_to_uriset newuri)
183 let innertypesuri_of_uri uri =
184 let cicuri = cicuri_of_uri uri in
185 let newuri = Array.copy cicuri in
186 newuri.(Array.length cicuri - 3) <- (string_of_uri cicuri) ^ ".types" ;
190 type uriref = uri * (int list)
192 let string_of_uriref (uri, fi) =
193 let str = string_of_uri uri in
194 let xp t = "#xpointer(1/" ^ string_of_int (t + 1) in
197 | [t] -> str ^ xp t ^ ")"
198 | t :: c :: _ -> str ^ xp t ^ "/" ^ string_of_int c ^ ")"
201 let su1 = string_of_uri u1 in
202 let su2 = string_of_uri u2 in
203 Pervasives.compare su1 su2
208 let compare = compare (* the one above, not Pervasives.compare *)
211 module UriSet = Set.Make (OrderedUri)