Skip to content

Commit

Permalink
New function: Filename.abspath (from John Goerzen's MissingLib)
Browse files Browse the repository at this point in the history
  • Loading branch information
thelema committed Mar 23, 2008
1 parent fe003be commit 36679ec
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
27 changes: 27 additions & 0 deletions stdlib/filename.ml
Expand Up @@ -9,6 +9,7 @@
(* under the terms of the GNU Library General Public License, with *)
(* the special exception on linking described in file ../LICENSE. *)
(* *)
(* portions (C) 2004 John Goerzen *)
(***********************************************************************)

(* $Id$ *)
Expand Down Expand Up @@ -215,3 +216,29 @@ let open_temp_file ?(mode = [Open_text]) prefix suffix =
with Sys_error _ as e ->
if counter >= 1000 then raise e else try_name (counter + 1)
in try_name 0

(* from missinglib (John Goerzen) - modified by Eric Norige *)
let abspath ?(startdir=Sys.getcwd ()) filename =
(* if not (Filename.is_relative filename) then
filename
else *)
let rec simplify acc = function
[] -> List.rev acc
| "" :: xs -> simplify acc xs (* OK to drop empty path entries? *)
| cdn :: xs when cdn = current_dir_name ->
simplify acc xs (* drop '.' entries *)
| pdn :: xs when pdn = parent_dir_name ->
( try simplify (List.tl acc) xs (* drop the most recent dir *)
with Failure "tl" -> failwith "Filename.abspath: .. past root" )
| x :: xs -> simplify xs (x :: acc)
in
if String.length filename < 1 then
startdir
else
let fn =
if String.starts_with filename dir_sep
then filename
else concat startdir filename
in
let components = String.nsplit dir_sep fn in
List.fold_left concat "/" (simplify [] components)
5 changes: 5 additions & 0 deletions stdlib/filename.mli
Expand Up @@ -105,3 +105,8 @@ val quote : string -> string
with programs that follow the standard Windows quoting
conventions.
*)

val abspath : ?startdir:string -> string -> string
(** [abspath start filename] takes a starting directory (defaults to
[Sys.getcwd()]) and a filename (including path) and returns a
simplified absolute filename to that same filename. *)

0 comments on commit 36679ec

Please sign in to comment.