]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_kernel/nUriManager.ml
40b66984571166ae7dafeaba609c12219499f2f0
[helm.git] / helm / software / components / ng_kernel / nUriManager.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 exception IllFormedUri of string Lazy.t
27
28 type spec = 
29  | Decl 
30  | Def
31  | Fix of int * int (* fixno, recparamno *)
32  | CoFix of int
33  | Ind of int
34  | Con of int * int (* indtyno, constrno *)
35
36 type uri = Uri of int * string * spec
37
38 let eq = (==);;
39
40 let string_of_uri (Uri (_,s,_)) = s;;
41
42 module OrderedStrings =
43  struct
44   type t = string
45   let compare (s1 : t) (s2 : t) = compare s1 s2
46  end
47 ;;
48
49 module MapStringsToUri = Map.Make(OrderedStrings);;
50
51 let set_of_uri = ref MapStringsToUri.empty;;
52
53 (* '.' not allowed in path and foo
54  *
55  * Decl                   cic:/path/foo.dec
56  * Def                    cic:/path/foo.def
57  * Fix of int * int       cic:/path/foo.fix(i,j)
58  * CoFix of int           cic:/path/foo.cfx(i)
59  * Ind of int             cic:/path/foo.ind(i)
60  * Con of int * int       cic:/path/foo.con(i,j)
61  *)
62
63 let uri_of_string =
64   let counter = ref 0 in 
65   let c () = incr counter; !counter in 
66   let get2 s dot =
67     let comma = String.rindex s ',' in
68     let i = int_of_string (String.sub s (dot+5) (comma-dot-5)) in
69     let j = int_of_string (String.sub s (comma+1) (String.length s-comma-2)) in
70     i,j
71   in
72   let get1 s dot =
73     let i = int_of_string (String.sub s (dot+5) (String.length s-1)) in
74     i
75   in
76 fun s ->
77   try MapStringsToUri.find s !set_of_uri
78   with Not_found ->
79     let new_uri =
80       try
81         let dot = String.rindex s '.' in
82         let suffix = String.sub s (dot+1) 3 in
83         match suffix with
84         | "dec" -> Uri (c (), s, Decl)
85         | "def" -> Uri (c (), s, Def)
86         | "fix" -> let i,j = get2 s dot in Uri (c (), s, Fix (i,j))
87         | "cfx" -> let i = get1 s dot in Uri (c (), s, CoFix (i))
88         | "ind" -> let i = get1 s dot in Uri (c (), s, Ind (i))
89         | "con" -> let i,j = get2 s dot in Uri (c (), s, Con (i,j))
90         | _ -> raise Not_found
91       with Not_found -> raise (IllFormedUri (lazy s))
92     in
93     set_of_uri := MapStringsToUri.add s new_uri !set_of_uri;
94     new_uri
95 ;;
96
97 let nuri_of_ouri u indinfo =
98   uri_of_string (string_of_uri (Uri(0,UriManager.string_of_uri u,indinfo)))
99 ;;
100
101 let ouri_of_nuri u =
102   UriManager.uri_of_string (string_of_uri u)
103 ;;