2016-02-05 80 views
3

在OCaml中,我如何获得canonical file path,相当于Java的File.getCanonicalPath()(即规范化的绝对​​路径)?获取规范文件路径

模块Filename具有dirname函数,但它并未标准化文件路径,例如/etc/../usr/usr,例如。

回答

3

JaneStreet Ocaml Core拥有Filename.realpath

此外,谷歌为ocaml realpath(请见C realpath(3);你可能会为它编写一些包装)

2

我们phat库定义normalized相当精确。这个库对于大多数用例来说都是过度的,但是我们将它写入了与文件系统密切相关的应用程序。我们很快就会发布opam,但现在你必须做几个针脚。这是一个完整的工作示例:

$ opam pin add solvuu_build https://github.com/solvuu/solvuu_build 
$ opam pin add phat https://github.com/solvuu/phat.git 
$ utop 
# #require "phat.pure";; 
# module Phat = Phat_pure.Std;; 
# Phat.(abs_dir "https://stackoverflow.com/a/../b/./c/d/" |> ok_exn |> normalize |> to_string);; 
- : string = "/b/c/d"