]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_kernel/nUriManager.ml
snapshot]
[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 (*                order, uri,      (which, constructor)        *)
27 type uri = Uri of int *  string *  (int  * int option ) option
28
29 let eq = (==);;
30
31 let string_of_uri = function
32   | Uri (_,s,None) -> s
33   | Uri (_,s,Some (i,None)) -> 
34       s ^ "#xpointer(1/" ^ string_of_int (i+1) ^ ")"
35   | Uri (_,s,Some (i, Some k)) -> 
36       s ^ "#xpointer(1/" ^ string_of_int (i+1) ^ "/" ^ string_of_int (k+1) ^ ")"
37 ;;
38
39 module OrderedStrings =
40  struct
41   type t = string
42   let compare (s1 : t) (s2 : t) = compare s1 s2
43  end
44 ;;
45
46 module MapStringsToUri = Map.Make(OrderedStrings);;
47
48 let set_of_uri = ref MapStringsToUri.empty;;
49
50 let uri_of_string_aux =
51   let counter = ref 0 in fun s ->
52   incr counter;
53   try
54     let sharp = String.rindex s '#' in
55     let pre = String.sub s 0 sharp in
56     let ind = String.sub s (sharp+12) (String.length s - (sharp+13)) in
57     try
58       let slash = String.rindex ind '/' in
59       let indno = String.sub ind 0 slash in   
60       let constr = String.sub ind (slash+1) (String.length ind - (slash+1)) in
61       Uri(!counter,pre,Some(int_of_string indno-1,Some(int_of_string constr-1)))
62     with Not_found -> Uri (!counter,pre,Some (int_of_string ind-1, None))
63   with Not_found -> Uri (!counter,s,None)
64 ;;
65
66 let uri_of_string s =
67   try
68     MapStringsToUri.find s !set_of_uri
69   with Not_found ->
70     let new_uri = uri_of_string_aux s in
71     set_of_uri := MapStringsToUri.add s new_uri !set_of_uri;
72     new_uri
73 ;;
74
75 let nuri_of_ouri u indinfo =
76   uri_of_string (string_of_uri (Uri(0,UriManager.string_of_uri u,indinfo)))
77 ;;
78
79 let ouri_of_nuri u =
80   UriManager.uri_of_string (string_of_uri u)
81 ;;