]> matita.cs.unibo.it Git - helm.git/blob - matita/components/binaries/probe/matitaList.ml
ee6cb417afeede5fe163439af7b57352e68c7aa7
[helm.git] / matita / components / binaries / probe / matitaList.ml
1 (*
2     ||M||  This file is part of HELM, an Hypertextual, Electronic        
3     ||A||  Library of Mathematics, developed at the Computer Science     
4     ||T||  Department, University of Bologna, Italy.                     
5     ||I||                                                                
6     ||T||  HELM is free software; you can redistribute it and/or         
7     ||A||  modify it under the terms of the GNU General Public License   
8     \   /  version 2 or (at your option) any later version.      
9      \ /   This software is distributed as is, NO WARRANTY.     
10       V_______________________________________________________________ *)
11
12 module A = Array
13 module F = Filename
14 module P = Printf
15 module S = String
16 module Y = Sys
17
18 module U  = NUri
19 module US = U.UriSet
20 module H  = HExtlib
21
22 module O = Options
23 module E = Engine
24
25 let chop_extension file =
26    try F.chop_extension file
27    with Invalid_argument("Filename.chop_extension") -> file
28
29 let script devel = chop_extension devel ^ ".ma"
30
31 let src_exists path = !O.no_devel || Y.file_exists path
32
33 let is_obj base path = 
34    if H.is_regular (F.concat base path) then
35       F.check_suffix path ".con.ng" ||
36       F.check_suffix path ".ind.ng" ||
37       F.check_suffix path ".var.ng"
38   else false
39
40 let is_src base path = 
41    H.is_regular (F.concat base path) &&
42    F.check_suffix path ".ng"
43
44 let is_dir base path =
45    H.is_dir (F.concat base path)
46
47 let is_script devel =
48    src_exists (script devel)
49
50 let mk_file path =
51    if F.check_suffix path "/" then S.sub path 0 (pred (S.length path))
52    else path ^ ".ng"
53
54 let add_obj path =
55    let path = F.chop_extension path in   
56    let str = F.concat "cic:" path in
57    O.objs := US.add (U.uri_of_string str) !O.objs
58
59 let add_src devel path =
60    let path = F.chop_extension path in   
61    let str = F.concat "cic:" path ^ "/" in
62    O.srcs := US.add (U.uri_of_string str) !O.srcs;
63    E.mac (script devel)
64
65 let add_remove base path =
66    O.remove := F.concat base path :: !O.remove
67
68 let rec scan_entry inner base devel path =
69 (*   Printf.eprintf "%b %s %s%!\n" inner devel path; *)
70    if is_obj base path && inner then add_obj path else
71    if is_src base path && is_script devel then add_src devel path else 
72    if is_dir base path && is_script devel then scan_dir true base devel path else
73    if is_dir base path && src_exists devel then scan_dir false base devel path else
74    add_remove base path
75
76 and scan_dir inner base devel path =
77    let files = Y.readdir (F.concat base path) in
78    let map base file = scan_entry inner base (F.concat devel file) (F.concat path file) in
79    A.iter (map base) files
80
81 let from_uri base devel uri =
82    O.no_devel := devel = "";
83    let str = U.string_of_uri uri in
84    let i = S.index str '/' in
85    let protocol = S.sub str 0 i in
86    if protocol = "cic:" then 
87       let path = S.sub str (succ i) (S.length str - succ i) in
88       let file = mk_file path in
89       if Y.file_exists (F.concat base file) then
90          scan_entry (is_script devel) base devel file 
91       else E.missing path
92    else E.unsupported protocol
93
94 let from_string base devel s =
95    from_uri base devel (U.uri_of_string s)