From e16cfb5521f186388e56c47eab95140e81eb1ee5 Mon Sep 17 00:00:00 2001 From: sof Date: Sun, 19 Sep 1999 19:30:04 +0000 Subject: [PATCH] [project @ 1999-09-19 19:30:04 by sof] * Re-exported IO.HandlePosn, i.e., type HandlePosition = Integer data HandlePosn = HandlePosn Handle HandlePosition * Added hTell :: Handle -> HandlePosition (merely a wrapper for IO.hGetPosn ) * Added hSetBinaryMode :: Handle -> Bool -> IO Bool for dynamically changing the 'translation mode' of a Handle. This stuff is only useful on platforms that make a distinction between text and binary files (e.g., Win32) --- ghc/lib/exts/IOExts.lhs | 58 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 53 insertions(+), 5 deletions(-) diff --git a/ghc/lib/exts/IOExts.lhs b/ghc/lib/exts/IOExts.lhs index e83a78f..a0de1cc 100644 --- a/ghc/lib/exts/IOExts.lhs +++ b/ghc/lib/exts/IOExts.lhs @@ -60,6 +60,12 @@ module IOExts , unsafePtrEq , freeHaskellFunctionPtr + + , HandlePosition + , HandlePosn(..) + , hTell -- :: Handle -> IO HandlePosition + + , hSetBinaryMode -- :: Handle -> Bool -> IO Bool ) where @@ -182,9 +188,12 @@ trace string expr = unsafePerformIO $ do fd <- getHandleFd stderr hPutStr stderr string hPutChar stderr '\n' - _ccall_ PostTraceHook fd + postTraceHook fd return expr + +foreign import "PostTraceHook" postTraceHook :: Int -> IO () #endif + \end{code} Not something you want to call normally, but useful @@ -192,10 +201,8 @@ in the cases where you do want to flush stuff out of the heap or make sure you've got room enough \begin{code} -#ifdef __HUGS__ -#else -performGC :: IO () -performGC = _ccall_GC_ performGC +#ifndef __HUGS__ +foreign import "performGC" performGC :: IO () #endif \end{code} @@ -264,3 +271,44 @@ withStdin h a = withHandleFor h stdin a withStdout h a = withHandleFor h stdout a withStderr h a = withHandleFor h stderr a \end{code} + +@hTell@ is the lower-level version of @hGetPosn@ - return the +position, without bundling it together with the handle itself: + +\begin{code} +hTell :: Handle -> IO HandlePosition +hTell h = do + (HandlePosn _ x) <- hGetPosn h + return x +\end{code} + +@hSetBinaryMode@ lets you change the translation mode for a handle. +On some platforms (e.g., Win32) a distinction is made between being in +'text mode' or 'binary mode', with the former terminating lines +by \r\n rather than just \n. + +Debating the Winnitude or otherwise of such a scheme is less than +interesting -- it's there, so we have to cope. + +A side-effect of calling @hSetBinaryMode@ is that the output buffer +(if any) is flushed prior to changing the translation mode. + +\begin{code} +hSetBinaryMode :: Handle -> Bool -> IO Bool +hSetBinaryMode handle is_binary = do + -- is_binary = True => set translation mode to binary. + wantRWHandle "hSetBinaryMode" handle $ \ handle_ -> do + let fo = haFO__ handle_ + rc <- setBinaryMode fo flg + if rc >= 0 then + return (int2Bool rc) + else + constructErrorAndFail "hSetBinaryMode" + where + flg | is_binary = 1 + | otherwise = 0 + + int2Bool 0 = False + int2Bool _ = True + +\end{code} -- 1.7.10.4