factor out Utility.FileSystemEncoding

This commit is contained in:
Joey Hess 2012-03-09 19:08:10 -04:00
parent 789254747b
commit d6e77595ba
5 changed files with 26 additions and 16 deletions

View file

@ -1,4 +1,4 @@
{- File system encoding handling.
{- GHC File system encoding handling.
-
- Copyright 2012 Joey Hess <joey@kitenet.net>
-
@ -7,8 +7,17 @@
module Utility.FileSystemEncoding where
import GHC.IO.Encoding (getFileSystemEncoding)
import System.IO
import Foreign.C
import GHC.Foreign as GHC
import GHC.IO.Encoding
{- Sets a Handle to use the filesystem encoding. This causes data
- written or read from it to be encoded/decoded the same
- as ghc 7.4 does to filenames etc. This special encoding
- allows "arbitrary undecodable bytes to be round-tripped through it". -}
fileEncoding :: Handle -> IO ()
fileEncoding h = hSetEncoding h =<< getFileSystemEncoding
{- Marshal a Haskell FilePath into a NUL terminated C string using temporary
- storage. The FilePath is encoded using the filesystem encoding,
@ -16,3 +25,13 @@ import GHC.Foreign as GHC
- was obtained. -}
withFilePath :: FilePath -> (CString -> IO a) -> IO a
withFilePath fp f = getFileSystemEncoding >>= \enc -> GHC.withCString enc fp f
{- Encodes a FilePath into a String of encoded bytes, applying the
- filesystem encoding.
-
- This does not do any IO, beyond allocating a C buffer. GHC does not
- seem to provide a pure way to do this conversion. -}
encodeFilePath :: FilePath -> IO String
encodeFilePath fp = do
enc <- getFileSystemEncoding
GHC.withCString enc fp $ GHC.peekCString enc