Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell obscure file path operations (simplify away the dots)

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.

like image 244
So8res Avatar asked Dec 15 '11 04:12

So8res


1 Answers

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.

like image 97
dave4420 Avatar answered Nov 16 '22 00:11

dave4420