patch #7303
[mldonkey.git] / src / utils / cdk / filepath.ml
blobfd3f18e9aea963c43fa4f73817d2f7d76b042603
1 (* Copyright 2001, 2002 b8_bavard, b8_fee_carabine, INRIA *)
2 (*
3 This file is part of mldonkey.
5 mldonkey is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 mldonkey is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with mldonkey; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 let find_in_path path name =
21 if not (Filename.is_implicit name) then
22 if Sys.file_exists name then name else raise Not_found
23 else begin
24 let rec try_dir = function
25 [] -> raise Not_found
26 | dir::rem ->
27 let fullname = Filename.concat dir name in
28 if Sys.file_exists fullname then fullname else try_dir rem
29 in try_dir path
30 end
32 let string_to_path sep str =
33 let len = String.length str in
34 let rec iter start pos =
35 if pos >= len then
36 if start >= len then [] else
37 [String.sub str start (len - start)]
38 else
39 if str.[pos] = sep then
40 if pos <= start then iter (pos+1) (pos+1) else
41 (String.sub str start (pos - start)) :: (iter (pos+1) (pos+1))
42 else
43 iter start (pos+1)
45 iter 0 0
47 let path_to_string sep path =
48 let s = List.fold_left (fun str dir ->
49 Printf.sprintf "%s%c%s" str sep dir) "" path in
50 if String.length s > 0 then
51 let len = String.length s in
52 String.sub s 1 (len-1)
53 else ""
55 let colonpath_to_string = path_to_string ':'
56 let string_to_colonpath = string_to_path ':'
58 let string_to_semipath = string_to_path ';'
59 let semipath_to_string = path_to_string ';'