summaryrefslogtreecommitdiff
path: root/Utility/FileSystemEncoding.hs
diff options
context:
space:
mode:
authorGravatar Joey Hess <joey@kitenet.net>2012-03-09 19:08:10 -0400
committerGravatar Joey Hess <joey@kitenet.net>2012-03-09 19:08:10 -0400
commitd6e77595ba45762b3c2dfdcd47a2d6b5b70154ae (patch)
tree896615a59c8f67f3bfec97c55616b7e59017927b /Utility/FileSystemEncoding.hs
parent789254747bceeaac004236275a6c1906f859945a (diff)
factor out Utility.FileSystemEncoding
Diffstat (limited to 'Utility/FileSystemEncoding.hs')
-rw-r--r--Utility/FileSystemEncoding.hs23
1 files changed, 21 insertions, 2 deletions
diff --git a/Utility/FileSystemEncoding.hs b/Utility/FileSystemEncoding.hs
index 6970a10de..048323ee3 100644
--- a/Utility/FileSystemEncoding.hs
+++ b/Utility/FileSystemEncoding.hs
@@ -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