From: Martin J. <mar...@em...> - 2003-11-14 12:57:07
|
On Fri, 14 Nov 2003, Nicolas Cannasse wrote: > Hi list, > I came up very recently with a typical problem. > I had a file A and its relative path, I had a file B and its absolute path, > I wanted to get the file B path relative to A.... not so easy actually. I > wrote this small module Path : > > Path.make "my/../path/./" build a relative path > Path.absolute give the absolute path > Path.relative a b give the path of a relative to b > Path.up goes up one directory > > I did some tests under windows , it looks like it works well. If people can > test it under *nix... > > Note : this does not actually do any system checking ( the path may not > exists or contains invalid characters ). Only special paths . .. and / ( and > windows drive letters ) are recognized as special. Hi, Such a module seems very important to me too. I've got some suggestions too. It seems important to me to be able to choose input/output style: Unix, Windows, MacOS, and others. Maybe as an option passed to functions "make" and "to_string". type style = Unix | Windows | Cygwin | ... let default_style = match Sys.os_type with "Win32" -> Windows | "Unix" -> Unix | ... let of_string ?(style = default_style) s = ... let to_string ?(style = default_style) x = ... This is less clear to me, but I feel that it would be convenient to split the type t into a root part and a virtual path part: type root = Windows of string | Unix | Current_dir | Path of t and t = { root : root; virtual_path : string list } ... and implement functions that change either the root or the virtual path. Some problems concerning the current implementation under Linux: 1. //tmp///toto is valid and equivalent to /tmp/toto (because any filename must have at least one character) 2. /../../.. is equivalent to / (which is dangerous anyway) 3. toto/ is more restrictive than toto (trailing slash indicates that toto must be a directory), so don't output trailing slashes except for the root directory, or introduce a distinction between files and directories. For solving points 1 and 2, maybe it would be nice to introduce 2 input styles: Unix and Abstract, where Unix conforms to Unix (Linux?) standards and Abstract is something more abstract, that makes "/.." invalid and allows 0-length filenames (would it be useful?). Martin |