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/.
26 (* "cic:/a/b/c.con" => [| "cic:/a" ; "cic:/a/b" ; "cic:/a/b/c.con" ; "c" |] *)
27 type uri = string array;;
33 let string_of_uri uri = uri.(Array.length uri - 2);;
34 let name_of_uri uri = uri.(Array.length uri - 1);;
35 let buri_of_uri uri = uri.(Array.length uri - 3);;
36 let depth_of_uri uri = Array.length uri - 2;;
38 module OrderedStrings =
41 let compare (s1 : t) (s2 : t) = compare s1 s2
45 module SetOfStrings = Map.Make(OrderedStrings);;
47 (*CSC: commento obsoleto ed errato *)
48 (* Invariant: the map is the identity function, *)
49 (* i.e. (SetOfStrings.find str !set_of_uri) == str *)
50 let set_of_uri = ref SetOfStrings.empty;;
51 let set_of_prefixes = ref SetOfStrings.empty;;
53 (* similar to uri_of_string, but used for prefixes of uris *)
54 let normalize prefix =
56 SetOfStrings.find prefix !set_of_prefixes
59 set_of_prefixes := SetOfStrings.add prefix prefix !set_of_prefixes ;
63 exception IllFormedUri of string;;
69 let prefix_uri = curi ^ "/" ^ he
70 and name = List.hd (Str.split (Str.regexp "\\.") he) in
71 [ normalize prefix_uri ; name ]
73 let prefix_uri = curi ^ "/" ^ he in
74 (normalize prefix_uri)::(aux prefix_uri tl)
75 | _ -> raise (IllFormedUri str)
77 let tokens = (Str.split (Str.regexp "/") str) in
80 (try (List.hd tokens, List.tl tokens)
81 with Failure "hd" | Failure "tl" ->
82 raise (IllFormedUri str))
87 let uri_of_string str =
89 SetOfStrings.find str !set_of_uri
92 let uri = Array.of_list (mk_prefixes str) in
93 set_of_uri := SetOfStrings.add str uri !set_of_uri ;
97 let cicuri_of_uri uri =
98 let completeuri = string_of_uri uri in
100 (Str.replace_first (Str.regexp "\\.types$") ""
101 (Str.replace_first (Str.regexp "\\.ann$") "" completeuri))
103 if completeuri = newcompleteuri then
106 let newuri = Array.copy uri in
107 newuri.(Array.length uri - 2) <- newcompleteuri ;
111 let annuri_of_uri uri =
112 let completeuri = string_of_uri uri in
113 if Str.string_match (Str.regexp ".*\\.ann$") completeuri 0 then
116 let newuri = Array.copy uri in
117 newuri.(Array.length uri - 2) <- completeuri ^ ".ann" ;
121 let uri_is_annuri uri =
122 Str.string_match (Str.regexp ".*\\.ann$") (string_of_uri uri) 0
125 let bodyuri_of_uri uri =
126 let struri = string_of_uri uri in
127 if Str.string_match (Str.regexp ".*\\.con$") (string_of_uri uri) 0 then
128 let newuri = Array.copy uri in
129 newuri.(Array.length uri - 2) <- struri ^ ".body" ;
135 let innertypesuri_of_uri uri =
136 let cicuri = cicuri_of_uri uri in
137 let newuri = Array.copy cicuri in
138 newuri.(Array.length cicuri - 2) <- (string_of_uri cicuri) ^ ".types" ;
142 type uriref = uri * (int list)
144 let string_of_uriref (uri, fi) =
145 let str = string_of_uri uri in
146 let xp t = "#xpointer(1/" ^ string_of_int (t + 1) in
149 | [t] -> str ^ xp t ^ ")"
150 | t :: c :: _ -> str ^ xp t ^ "/" ^ string_of_int c ^ ")"