I have a dynamically constructed file path in haskell that ends up something like this:
/abc/def/../ghi/./jkl
and I'd like to reduce it to the more readable
/abc/ghi/jkl
For printing. Is there a library function to do this in haskell? I've looked all over and can't find one. It's not too hard to write, but it's a bit messy because you have to "look ahead" for ".."s, and I'd rather use a baked-in function if I can.
Beware that this is not simply a string processing question when links are involved:
$ mkdir -p foo/bar
$ ln -s foo/bar baz
$ echo gotcha! >foo/quux
$ cat quux
cat: quux: No such file or directory
$ cat baz/../quux
gotcha!
So you need to do IO.
The nearest I can find to what you want is canonicalizePath from System.Directory. It returns a path starting from the root directory, so you may want to use it in conjunction with makeRelative, also from System.Directory. But it does run in IO.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With