]> matita.cs.unibo.it Git - helm.git/blob - helm/interface/uriManager.ml
Initial revision
[helm.git] / helm / interface / 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 (* "cic:/a/b/c.con" => [| "cic:/a" ; "cic:/a/b" ; "cic:/a/b/c.con" ; "c" |] *)
27 type uri = string array;;
28
29 let eq uri1 uri2 =
30  uri1 == uri2
31 ;;
32
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;;
37
38 (*CSC: ora e' diventato poco efficiente, migliorare *)
39 let relative_depth curi uri cookingsno =
40  let rec length_of_current_prefix l1 l2 =
41   match (l1, l2) with
42      (he1::tl1, he2::tl2) when he1 == he2 ->
43        1 + length_of_current_prefix tl1 tl2
44    | (_,_) -> 0
45  in
46   depth_of_uri uri -
47    length_of_current_prefix
48     (Array.to_list (Array.sub curi 0 (Array.length curi - (2 + cookingsno))))
49     (Array.to_list (Array.sub uri 0 (Array.length uri - 2)))
50   (*CSC: vecchio codice da eliminare
51   if eq curi uri then 0
52   else
53    depth_of_uri uri -
54     length_of_current_prefix (Array.to_list curi) (Array.to_list uri)
55   *)
56 ;;
57
58 module OrderedStrings =
59  struct
60   type t = string
61   let compare (s1 : t) (s2 : t) = compare s1 s2
62  end
63 ;;
64
65 module SetOfStrings = Map.Make(OrderedStrings);;
66
67 (*CSC: commento obsoleto ed errato *)
68 (* Invariant: the map is the identity function,      *)
69 (*  i.e. (SetOfStrings.find str !set_of_uri) == str  *)
70 let set_of_uri = ref SetOfStrings.empty;;
71 let set_of_prefixes = ref SetOfStrings.empty;;
72
73 (* similar to uri_of_string, but used for prefixes of uris *)
74 let normalize prefix =
75  try
76   SetOfStrings.find prefix !set_of_prefixes
77  with
78   Not_found ->
79    set_of_prefixes := SetOfStrings.add prefix prefix !set_of_prefixes ;
80    prefix
81 ;;
82
83 exception IllFormedUri of string;;
84
85 let mk_prefixes str =
86  let rec aux curi =
87   function
88      [he] ->
89       let prefix_uri = curi ^ "/" ^ he
90       and name = List.hd (Str.split (Str.regexp "\.") he) in
91        [ normalize prefix_uri ; name ]
92    | he::tl ->
93       let prefix_uri = curi ^ "/" ^ he in
94        (normalize prefix_uri)::(aux prefix_uri tl)
95    | _ -> raise (IllFormedUri str)
96  in
97   let tokens = (Str.split (Str.regexp "/") str) in
98    (* ty = "cic:" *)
99    let (ty, sp) = (List.hd tokens, List.tl tokens) in
100     aux ty sp
101 ;;
102
103 let uri_of_string str =
104  try
105   SetOfStrings.find str !set_of_uri
106  with
107   Not_found ->
108    let uri = Array.of_list (mk_prefixes str) in
109     set_of_uri := SetOfStrings.add str uri !set_of_uri ;
110     uri
111 ;;