]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/urimanager/uriManager.ml
fixed classical non-C programmer misunderstooding of pointers
[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" ; "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)" |]
32 *)
33 type uri = string array;;
34
35 let eq uri1 uri2 =
36   uri1 == uri2 
37 ;;
38
39 let string_of_uri uri =
40   match  uri.(Array.length uri - 1) with
41   | "" -> 
42       uri.(Array.length uri - 3)
43   | _ -> 
44       String.concat "#" 
45         [ uri.(Array.length uri - 3); uri.(Array.length uri - 1) ]
46
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;;
50
51 module OrderedStrings =
52  struct
53   type t = string
54   let compare (s1 : t) (s2 : t) = compare s1 s2
55  end
56 ;;
57
58 module SetOfStrings = Map.Make(OrderedStrings);;
59
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;;
65
66   
67 (* hash conses an uri *)
68 let add_to_uriset ?suri uri =
69   let lookup_suri suri =
70     try
71       SetOfStrings.find suri !set_of_uri
72     with Not_found -> assert false
73   in
74   let suri = 
75     match suri with 
76     | None -> string_of_uri uri
77     | Some suri -> suri 
78   in
79   if not(SetOfStrings.mem suri !set_of_uri) then
80     set_of_uri := SetOfStrings.add suri uri !set_of_uri;
81   lookup_suri suri  
82     
83
84 (* similar to uri_of_string, but used for prefixes of uris *)
85 let normalize prefix =
86  try
87   SetOfStrings.find prefix !set_of_prefixes
88  with
89   Not_found ->
90    set_of_prefixes := SetOfStrings.add prefix prefix !set_of_prefixes ;
91    prefix
92 ;;
93
94 exception IllFormedUri of string;;
95
96 let mk_prefixes str xpointer =
97  let rec aux curi =
98   function
99      [he] ->
100       let prefix_uri = curi ^ "/" ^ he
101       and name = List.hd (Str.split (Str.regexp "\\.") he) in
102        [ normalize prefix_uri ; name ]
103    | he::tl ->
104       let prefix_uri = curi ^ "/" ^ he in
105        (normalize prefix_uri)::(aux prefix_uri tl)
106    | _ -> raise (IllFormedUri str)
107  in
108   let tokens = (Str.split (Str.regexp "/") str) in
109    (* ty = "cic:" *)
110    let (ty, sp) =
111     (try (List.hd tokens, List.tl tokens)
112      with Failure "hd" | Failure "tl" ->
113       raise (IllFormedUri str))
114     in
115     (aux ty sp) @ [xpointer]
116 ;;
117
118
119 let sharp_rex = 
120   Str.regexp "#"
121 ;;
122
123 let uri_of_string str =
124   let base, xpointer =
125     match Str.split sharp_rex str with
126     | [base] -> base,""
127     | [base; xpointer] -> base,xpointer 
128     | _ -> raise (IllFormedUri str)
129   in
130    try
131     SetOfStrings.find str !set_of_uri
132    with
133     Not_found ->
134       let uri = Array.of_list (mk_prefixes base xpointer) in
135       add_to_uriset ~suri:str uri
136 ;;
137
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
143
144   
145 let cicuri_of_uri uri =
146  let completeuri = string_of_uri uri in
147   let newcompleteuri = 
148    (Str.replace_first (Str.regexp "\\.types$") ""
149     (Str.replace_first (Str.regexp "\\.ann$") "" completeuri))
150   in
151    if completeuri = newcompleteuri then
152     uri
153    else
154     let newuri = Array.copy uri in
155      newuri.(Array.length uri - 3) <- newcompleteuri ;
156      add_to_uriset newuri
157 ;;
158
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
162    uri
163   else
164    let newuri = Array.copy uri in
165     newuri.(Array.length uri - 3) <- completeuri ^ ".ann" ;
166     add_to_uriset newuri
167 ;;
168
169 let uri_is_annuri uri =
170  Str.string_match (Str.regexp ".*\\.ann$") (string_of_uri uri) 0
171 ;;
172
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)
179   else
180    None
181 ;;
182
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" ;
187    add_to_uriset newuri
188 ;;
189
190 type uriref = uri * (int list)
191
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
195    match fi with
196       | []          -> str 
197       | [t]         -> str ^ xp t ^ ")" 
198       | t :: c :: _ -> str ^ xp t ^ "/" ^ string_of_int c ^ ")" 
199
200 let compare u1 u2 =
201   let su1 = string_of_uri u1 in
202   let su2 = string_of_uri u2 in
203   Pervasives.compare su1 su2
204
205 module OrderedUri =
206 struct
207   type t = uri
208   let compare = compare (* the one above, not Pervasives.compare *)
209 end
210
211 module UriSet = Set.Make (OrderedUri)
212