Re: [Haskell-cafe] os.path.expanduser analogue

2011-11-20 Thread Ben Gamari
On Sun, 20 Nov 2011 21:02:30 -0500, Brandon Allbery  wrote:
> On Sun, Nov 20, 2011 at 20:36, Ben Gamari  wrote:
[Snip]
> 
> Although arguably there should be some error checking.
> 
Thanks for the improved implementation. I should have re-read my code
before sending as it wasn't even close to correct.

Cheers,

- Ben


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] os.path.expanduser analogue

2011-11-20 Thread Brandon Allbery
On Sun, Nov 20, 2011 at 20:36, Ben Gamari  wrote:

> expandUser :: FilePath -> IO FilePath
> expandUser p = if "~/" `isPrefixOf` p
>  then do u <- getLoginName
>  return $ u ++ drop 2 p
>  else return p
>

expandUser "~" = fmap homeDirectory getLoginName
expandUser ('~':'/':p) = getLoginName >>=
 fmap ((++ p) . homeDirectory)
  getUserEntryForName
expandUser ('~':up)= let (u,p) = break (== '/') up
  in fmap ((++ (drop 1 p)) . homeDirectory
  (getUserEntryForName u)
expandUser p   = p

Although arguably there should be some error checking.

-- 
brandon s allbery  allber...@gmail.com
wandering unix systems administrator (available) (412) 475-9364 vm/sms
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] os.path.expanduser analogue

2011-11-20 Thread Ben Gamari
On the whole, the filepath package does an excellent job of providing
basic path manipulation tools, one weakness is the inability to resolve
"~/..." style POSIX paths. Python implements this with
os.path.expanduser. Perhaps a similar function might be helpful in
filepath?

Cheers,

- Ben

Possible (but untested) implementation
expandUser :: FilePath -> IO FilePath
expandUser p = if "~/" `isPrefixOf` p
  then do u <- getLoginName
  return $ u ++ drop 2 p
  else return p

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe