| 1 |
(************************************************************************) |
| 2 |
(* * The Rocq Prover / The Rocq Development Team *) |
| 3 |
(* v * Copyright INRIA, CNRS and contributors *) |
| 4 |
(* <O___,, * (see version control and CREDITS file for authors & dates) *) |
| 5 |
(* \VV/ **************************************************************) |
| 6 |
(* // * This file is distributed under the terms of the *) |
| 7 |
(* * GNU Lesser General Public License Version 2.1 *) |
| 8 |
(* * (see LICENSE file for the text of the license) *) |
| 9 |
(************************************************************************) |
| 10 |
|
| 11 |
let re_delim = Str.regexp (if Sys.win32 then "[/\\]+" else "/+" ) |
| 12 |
|
| 13 |
let to_relative_path : string -> string = fun full_path -> |
| 14 |
if Filename.is_relative full_path then full_path else |
| 15 |
let cwd = Str.split_delim re_delim (Sys.getcwd ()) in |
| 16 |
let path = Str.split_delim re_delim full_path in |
| 17 |
let rec remove_common_prefix l1 l2 = |
| 18 |
match (l1, l2) with |
| 19 |
| (x1 :: l1, x2 :: l2) when x1 = x2 -> remove_common_prefix l1 l2 |
| 20 |
| (_ , _ ) -> (l1, String.concat "/" l2) |
| 21 |
in |
| 22 |
let (cwd, path) = remove_common_prefix cwd path in |
| 23 |
let add_parent path _ = Filename.concat Filename.parent_dir_name path in |
| 24 |
List.fold_left add_parent path cwd |
| 25 |
|
| 26 |
let normalize_path : string -> string = fun path -> |
| 27 |
let path = Str.split_delim re_delim path in |
| 28 |
let rec normalize acc path = |
| 29 |
match (path, acc) with |
| 30 |
| ([] , _ ) -> List.rev acc |
| 31 |
| ("." :: path, _ ) -> normalize acc path |
| 32 |
| (".." :: path, [] ) -> normalize (".." :: []) path |
| 33 |
| (".." :: path, ".." :: _ ) -> normalize (".." :: acc) path |
| 34 |
| (".." :: path, _ :: acc) -> normalize acc path |
| 35 |
| (dir :: path, _ ) -> normalize (dir :: acc) path |
| 36 |
in |
| 37 |
match normalize [] path with |
| 38 |
| [] -> "." |
| 39 |
| path -> String.concat "/" path |