Version française
Home     About     Download     Resources     Contact us    
Browse thread
absolute_name ?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Sylvain Le Gall <gildor@d...>
Subject: Re: absolute_name ?
On 20-07-2007, Christophe Raffalli <christophe.raffalli@univ-savoie.fr> wrote:
>
> Is this the best (and only) way to get the absolute name to a file in a way that would work on all 
> system (windonws, OS X, unix, ...) ?
>
>
> let absolute_name str =
>    let base = Filename.basename str in
>    let dir = Filename.dirname str in
>    let saved_dir = Sys.getcwd () in
>    Sys.chdir dir;
>    let res = Filename.concat (Sys.getcwd ()) base in
>    Sys.chdir saved_dir;
>    res
>
> I find this a bit complicated, so I may be missing some functions in Sys, Filename or some other lib ...
>

Maybe, you can have a look at ocaml-fileutils.

http://le-gall.net/sylvain+violaine/ocaml-fileutils.html

You can use either "readlink" if you want to solve the link and the
current working dir (the file need to exist)

(you have also FileUtil.Default.make_absolute which can apply to a non
existing file)

Here is an example:

$> ocaml

# #use "topfind";;
# #require "fileutils";;
# open FilePath.DefaultPath;;
# open FileUtil.StrUtil;;
# ls ".";;
- : FileUtil.filename list =
["./ocaml_3.10.0-5.dsc"; "./ocaml_3.10.0-5.dsc.asc";
 "./ocaml-base-nox_3.10.0-5_i386.deb"; "./ocaml_3.10.0-5_i386.deb";
 "./ocaml-source_3.10.0-5_all.deb"; "./camlp4_3.10.0-5_i386.deb";
 "./camlp4-extra_3.10.0-5_i386.deb"; "./ocaml_3.10.0-5.dsc.gpg";
 "./ocaml_3.10.0.orig.tar.gz"; "./ocaml-base_3.10.0-5_i386.deb";
 "./ocaml_3.10.0-5.diff.gz"; "./ocaml-interp_3.10.0-5_i386.deb";
 "./ocaml-mode_3.10.0-5_all.deb";
"./ocaml-compiler-libs_3.10.0-5_i386.deb";
 "./ocaml-nox_3.10.0-5_i386.deb";
 "./ocaml-native-compilers_3.10.0-5_i386.deb"]
# readlink "./ocaml_3.10.0-5.dsc";;
- : FileUtil.filename =
"/home/gildor/public_html/debstage/builder/queue/ocaml/ocaml_3.10.0-5.dsc"
# make_absolute (pwd ()) "./toto";;
- : FilePath.DefaultPath.filename =
"/home/gildor/public_html/debstage/builder/queue/ocaml/toto"

DefaultPath stands for the path for the current OS. You can use
UnixPath, MacOSPath, Win32Path or CygwinPath if you want to bypass OS
detection.

The documentation is a bit complicated, i need to rewrite it at some
point in the future.

Regards,
Sylvain Le Gall