]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/urimanager/uriManager.ml
Much simpler (and slightly more performant) implementation of the UriManager.
[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/b/c.con", id )
28  * "cic:/a/b/c.ind#xpointer(1/1)" => ("cic:/a/b/c.con#xpointer(1/1)", id)
29  * "cic:/a/b/c.ind#xpointer(1/1/1)" => ("cic:/a/b/c.con#xpointer(1/1/1)", id)
30  *)
31
32 let fresh_id =
33  let id = ref 0 in
34   function () ->
35     incr id;
36     !id
37
38 (* (uriwithxpointer, uniqueid) 
39  * where uniqueid is used to build a set of uri *)
40 type uri = string * int;;
41
42 let eq uri1 uri2 =
43   uri1 == uri2 
44 ;;
45
46 let string_of_uri (uri,_) =
47  uri
48
49 let name_of_uri (uri, _) = 
50   let xpointer_offset = 
51     try String.rindex uri '#' with Not_found -> String.length uri - 1
52   in
53   let index1 = String.rindex_from uri xpointer_offset '/' + 1 in
54   let index2 = String.rindex uri '.' in
55   String.sub uri index1 (index2 - index1)
56   
57 let buri_of_uri (uri,_) = 
58   let index = String.rindex uri '/' in
59   String.sub uri 0 index
60
61 module OrderedStrings =
62  struct
63   type t = string
64   let compare (s1 : t) (s2 : t) = compare s1 s2
65  end
66 ;;
67
68 module MapStringsToUri = Map.Make(OrderedStrings);;
69
70 (* Invariant: the map is the identity function,
71  *  i.e. 
72  *    let str' = (MapStringsToUri.find str !set_of_uri) in
73  *    str' == (MapStringsToUri.find str' !set_of_uri)
74  *)
75 let set_of_uri = ref MapStringsToUri.empty;;
76
77 (* hash conses an uri *)
78 let uri_of_string suri =
79   try
80     MapStringsToUri.find suri !set_of_uri
81   with Not_found -> 
82 prerr_endline ("@@@ " ^ suri);
83     let new_uri = suri, fresh_id () in
84     set_of_uri := MapStringsToUri.add suri new_uri !set_of_uri;
85     new_uri
86
87 exception IllFormedUri of string;;
88
89 let strip_xpointer ((uri,_) as olduri) =
90   try 
91     let index = String.rindex uri '#' in
92     let no_xpointer = String.sub uri 0 index in
93     uri_of_string no_xpointer
94   with
95     Not_found -> olduri
96
97 let clear_suffix uri ?(pat2="") pat1 =
98   try
99     let index = String.rindex uri '.' in
100     let index' = index + 1 in 
101     let suffix = String.sub uri index' (String.length uri - index') in
102     if pat1 = suffix || pat2 = suffix then
103       String.sub uri 0 index
104     else
105       uri
106   with
107     Not_found -> assert false
108
109 let has_suffix uri pat =
110   try
111     let index = String.rindex uri '.' + 1 in
112     let suffix = String.sub uri index (String.length uri - index) in
113     pat = suffix 
114   with
115     Not_found -> assert false
116
117 let _types = "types"
118 let _dottypes = ".types"
119 let _ann = "ann"
120 let _dotann = ".ann"
121 let _var = "var"
122 let _dotbody = ".body"
123 let _con = "con"
124 let _xpointer = "#xpointer(1/"
125  
126 let cicuri_of_uri (uri, _) =
127   uri_of_string (clear_suffix uri ~pat2:_types _ann)
128 ;;
129
130 let annuri_of_uri (uri , _) =
131   uri_of_string ((clear_suffix uri _ann) ^ _dotann)
132 ;;
133
134 let uri_is_annuri (uri, _) =
135   has_suffix uri _ann
136 ;;
137
138 let uri_is_var (uri, _) =
139   has_suffix uri _var
140 ;;
141
142 let bodyuri_of_uri (uri, _) =
143   if has_suffix uri _con then
144     Some (uri_of_string (uri ^ _dotbody))
145   else
146     None
147 ;;
148
149 let innertypesuri_of_uri (uri, _) =
150   uri_of_string ((clear_suffix uri _types) ^ _dottypes)
151 ;;
152
153 let uri_of_uriref (uri, _) typeno consno =
154   let typeno = typeno + 1 in
155   let suri =
156     match consno with
157     | None -> Printf.sprintf "%s%s%d)" uri _xpointer typeno
158     | Some n -> Printf.sprintf "%s%s%d/%d)" uri _xpointer typeno n
159   in
160   uri_of_string suri
161
162 let compare (_,id1) (_,id2) = id1 - id2
163
164 module OrderedUri =
165 struct
166   type t = uri
167   let compare = compare (* the one above, not Pervasives.compare *)
168 end
169
170 module UriSet = Set.Make (OrderedUri)
171