Skip to content

Commit 1b7694a

Browse files
Add asCString for Path
1 parent a26c49b commit 1b7694a

2 files changed

Lines changed: 19 additions & 7 deletions

File tree

core/src/Streamly/Internal/FileSystem/Posix/File.hs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import GHC.IO.Handle.FD (fdToHandle)
3333
import System.Posix.Types (Fd(..), CMode(..), FileMode)
3434
import Streamly.Internal.FileSystem.Posix.Errno (throwErrnoPathIfMinus1Retry)
3535

36-
import qualified Streamly.Internal.Data.Array as Array
3736
import qualified Streamly.Internal.FileSystem.PosixPath as Path
3837
-- import qualified GHC.IO.FD as FD
3938

@@ -124,9 +123,6 @@ openFdAtWith_ OpenFlags{..} fdMay path how =
124123

125124
all_flags = creat_f .|. flags .|. open_mode
126125

127-
withFilePath :: PosixPath -> (CString -> IO a) -> IO a
128-
withFilePath p = Array.asCStringUnsafe (Path.toChunk p)
129-
130126
-- | Open a file relative to an optional directory file descriptor.
131127
--
132128
-- {-# INLINE openFdAtWith #-}
@@ -137,7 +133,7 @@ openFdAtWith ::
137133
-> OpenMode -- ^ Read-only, read-write or write-only
138134
-> IO Fd
139135
openFdAtWith flags fdMay name how =
140-
withFilePath name $ \str -> do
136+
Path.asCString name $ \str -> do
141137
throwErrnoPathIfMinus1Retry "openFdAt" name
142138
$ openFdAtWith_ flags fdMay str how
143139

core/src/Streamly/Internal/FileSystem/PosixPath.hs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,11 @@ module Streamly.Internal.FileSystem.OS_PATH
7676
, toChars
7777
, toChars_
7878
, toString
79-
-- , toCString
80-
-- , toW16CString
79+
#ifndef IS_WINDOWS
80+
, asCString
81+
#else
82+
, asCWString
83+
#endif
8184
, toString_
8285
, showRaw
8386

@@ -128,12 +131,15 @@ import Data.Word (Word16)
128131
#endif
129132
#ifndef IS_WINDOWS
130133
import Foreign.C (CString)
134+
#else
135+
import Foreign.C (CWString)
131136
#endif
132137
import Language.Haskell.TH.Syntax (lift)
133138
import Streamly.Internal.Data.Array (Array(..))
134139
import Streamly.Internal.Data.Stream (Stream)
135140
import Streamly.Internal.FileSystem.Path.Common (mkQ, EqCfg(..), eqCfg)
136141

142+
import qualified Streamly.Internal.Data.Array as Array
137143
import qualified Streamly.Internal.Data.Stream as Stream
138144
import qualified Streamly.Internal.FileSystem.Path.Common as Common
139145
import qualified Streamly.Internal.Unicode.Stream as Unicode
@@ -382,6 +388,16 @@ instance Show OS_PATH where
382388
show (OS_PATH x) = show x
383389
-}
384390

391+
#ifndef IS_WINDOWS
392+
{-# INLINE asCString #-}
393+
asCString :: OS_PATH -> (CString -> IO a) -> IO a
394+
asCString p = Array.asCStringUnsafe (toChunk p)
395+
#else
396+
{-# INLINE asCWString #-}
397+
asCWString :: OS_PATH -> (CWString -> IO a) -> IO a
398+
asCWString p = Array.asCWString (toChunk p)
399+
#endif
400+
385401
------------------------------------------------------------------------------
386402
-- Operations on Path
387403
------------------------------------------------------------------------------

0 commit comments

Comments
 (0)