]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/urimanager/uriManager.ml
added uri_is_ind
[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 xpointer_offset = 
59     try String.rindex uri '#' with Not_found -> String.length uri - 1
60   in
61   let index = String.rindex_from uri xpointer_offset '/' in
62   String.sub uri 0 index
63
64 module OrderedStrings =
65  struct
66   type t = string
67   let compare (s1 : t) (s2 : t) = compare s1 s2
68  end
69 ;;
70
71 module MapStringsToUri = Map.Make(OrderedStrings);;
72
73 (* Invariant: the map is the identity function,
74  *  i.e. 
75  *    let str' = (MapStringsToUri.find str !set_of_uri) in
76  *    str' == (MapStringsToUri.find str' !set_of_uri)
77  *)
78 let set_of_uri = ref MapStringsToUri.empty;;
79
80 exception IllFormedUri of string;;
81
82 let _dottypes = ".types"
83 let _types = "types",5
84 let _ann = "ann",3
85 let _dotann = ".ann"
86 let _var = "var",3
87 let _dotbody = ".body"
88 let _con = "con",3
89 let _ind = "ind",3
90 let _xpointer = "#xpointer(1/"
91 let _con3 = "con"
92 let _var3 = "var"
93 let _ind3 = "ind"
94 let _ann3 = "ann"
95 let _types5 = "types"
96 let _xpointer8 = "xpointer"
97 let _cic5 = "cic:/"
98
99 let is_malformed suri =
100   try 
101     if String.sub suri 0 5 <> _cic5 then true
102     else
103       let len = String.length suri - 5 in
104       let last5 = String.sub suri len 5 in
105       let last3 = String.sub last5 2 3 in
106       if last3 = _con3 || last3 = _var3 || last3 = _ind3 || 
107          last3 = _ann3 || last5 = _types5 || last5 = _dotbody then 
108            false
109       else 
110         try
111           let index = String.rindex suri '#' + 1 in
112           let xptr = String.sub suri index 8 in
113           if xptr = _xpointer8 then
114             false
115           else
116             true
117         with Not_found -> true
118   with Invalid_argument _ -> true
119     
120 (* hash conses an uri *)
121 let uri_of_string suri =
122   try
123     MapStringsToUri.find suri !set_of_uri
124   with Not_found -> 
125     if is_malformed suri then
126       raise (IllFormedUri suri) 
127     else
128       let new_uri = suri, fresh_id () in
129       set_of_uri := MapStringsToUri.add suri new_uri !set_of_uri;
130       new_uri
131
132
133 let strip_xpointer ((uri,_) as olduri) =
134   try 
135     let index = String.rindex uri '#' in
136     let no_xpointer = String.sub uri 0 index in
137     uri_of_string no_xpointer
138   with
139     Not_found -> olduri
140
141 let clear_suffix uri ?(pat2="",0) pat1 =
142   try
143     let index = String.rindex uri '.' in
144     let index' = index + 1 in 
145     let suffix = String.sub uri index' (String.length uri - index') in
146     if fst pat1 = suffix || fst pat2 = suffix then
147       String.sub uri 0 index
148     else
149       uri
150   with
151     Not_found -> assert false
152
153 let has_suffix uri (pat,n) =
154   try
155     let suffix = String.sub uri (String.length uri - n) n in
156     pat = suffix 
157   with
158     Not_found -> assert false
159
160  
161 let cicuri_of_uri (uri, _) = uri_of_string (clear_suffix uri ~pat2:_types _ann)
162
163 let annuri_of_uri (uri , _) = uri_of_string ((clear_suffix uri _ann) ^ _dotann)
164
165 let uri_is_annuri (uri, _) = has_suffix uri _ann
166
167 let uri_is_var (uri, _) = has_suffix uri _var
168
169 let uri_is_con (uri, _) = has_suffix uri _con
170
171 let uri_is_ind (uri, _) = has_suffix uri _ind
172
173 let bodyuri_of_uri (uri, _) =
174   if has_suffix uri _con then
175     Some (uri_of_string (uri ^ _dotbody))
176   else
177     None
178 ;;
179
180 let innertypesuri_of_uri (uri, _) =
181   uri_of_string ((clear_suffix uri _types) ^ _dottypes)
182 ;;
183
184 let uri_of_uriref (uri, _) typeno consno =
185   let typeno = typeno + 1 in
186   let suri =
187     match consno with
188     | None -> Printf.sprintf "%s%s%d)" uri _xpointer typeno
189     | Some n -> Printf.sprintf "%s%s%d/%d)" uri _xpointer typeno n
190   in
191   uri_of_string suri
192
193 let compare (_,id1) (_,id2) = id1 - id2
194
195 module OrderedUri =
196 struct
197   type t = uri
198   let compare = compare (* the one above, not Pervasives.compare *)
199 end
200
201 module UriSet = Set.Make (OrderedUri)
202