2 % (c) The GRASP/AQUA Project, Glasgow University, 1994
4 \section[PrelWriteTextIO]{Haskell 1.3 Text Output}
6 This module defines the standard set of output operations for writing
7 characters and strings to text files, using
11 module PreludeWriteTextIO (
28 import List ( splitAt, (++) )
29 import Prel ( ord, (.), otherwise )
31 import TyArray -- instance _CCallable (_ByteArray a)
35 import PreludeMonadicIO
41 hPutChar :: Handle -> Char -> IO ()
43 takeMVar handle >>= \ htype ->
45 _ErrorHandle ioError ->
46 putMVar handle htype >>
49 putMVar handle htype >>
50 failWith (IllegalOperation "handle is closed")
51 _SemiClosedHandle _ _ ->
52 putMVar handle htype >>
53 failWith (IllegalOperation "handle is closed")
55 putMVar handle htype >>
56 failWith (IllegalOperation "handle is not open for writing")
58 _ccall_ filePutc (_filePtr other) (ord c) `thenPrimIO` \ rc ->
59 putMVar handle (_markHandle htype) >>
63 _constructError `thenPrimIO` \ ioError ->
66 putChar :: Char -> IO ()
67 putChar = hPutChar stdout13
71 Computation $hPutChar hdl c$ writes the character {\em c} to the file
72 or channel managed by {\em hdl}. Characters may be buffered if
73 buffering is enabled for {\em hdl}.
76 hPutStr :: Handle -> String -> IO ()
78 takeMVar handle >>= \ htype ->
80 _ErrorHandle ioError ->
81 putMVar handle htype >>
84 putMVar handle htype >>
85 failWith (IllegalOperation "handle is closed")
86 _SemiClosedHandle _ _ ->
87 putMVar handle htype >>
88 failWith (IllegalOperation "handle is closed")
90 putMVar handle htype >>
91 failWith (IllegalOperation "handle is not open for writing")
93 _getBufferMode other `thenPrimIO` \ other ->
94 (case _bufferMode other of
96 writeLines (_filePtr other) str
97 Just (BlockBuffering (Just size)) ->
98 writeBlocks (_filePtr other) size str
99 Just (BlockBuffering Nothing) ->
100 writeBlocks (_filePtr other) ``BUFSIZ'' str
101 _ -> -- Nothing is treated pessimistically as NoBuffering
102 writeChars (_filePtr other) str
103 ) `thenPrimIO` \ success ->
104 putMVar handle (_markHandle other) `seqPrimIO`
108 _constructError `thenPrimIO` \ ioError ->
113 writeBlocks :: _Addr -> Int -> String -> PrimIO Bool
114 writeBlocks fp size "" = returnPrimIO True
115 writeBlocks fp size s =
117 (some, more) = splitAt size s
119 _packBytesForCST some `thenPrimIO`
120 \ bytes@(_ByteArray (0, count) _) ->
121 _ccall_ writeFile bytes fp (count+1) `thenPrimIO` \ rc ->
123 writeBlocks fp size more
127 writeLines :: _Addr -> String -> PrimIO Bool
128 writeLines fp "" = returnPrimIO True
131 (some, more) = breakLine s
133 _packBytesForCST some `thenPrimIO`
134 \ bytes@(_ByteArray (0, count) _) ->
135 _ccall_ writeFile bytes fp (count+1) `thenPrimIO` \ rc ->
141 breakLine "" = ("","")
143 | x == '\n' = ([x],xs)
144 | otherwise = let (ys,zs) = breakLine xs in (x:ys,zs)
146 writeChars :: _Addr -> String -> PrimIO Bool
147 writeChars fp "" = returnPrimIO True
148 writeChars fp (c:cs) =
149 _ccall_ filePutc fp (ord c) `thenPrimIO` \ rc ->
155 putStr :: String -> IO ()
156 putStr = hPutStr stdout13
158 hPutText :: Text a => Handle -> a -> IO ()
159 hPutText hdl = hPutStr hdl . show
161 putText :: Text a => a -> IO ()
162 putText = hPutText stdout13
164 print13 :: Text a => a -> IO ()
165 print13 x = putText x >> putChar '\n'
169 Computation $hPutStr hdl s$ writes the string {\em s} to the file or
170 channel managed by {\em hdl}.
172 Computation $putStr s$ writes the string {\em s} to $stdout$.
174 Computation $hPutText hdl t$ writes the string representation of {\em
175 t} given by the $shows$ function to the file or channel managed by
180 writeFile13 :: FilePath -> String -> IO ()
181 writeFile13 name str =
182 openFile name WriteMode >>= \hdl -> hPutStr hdl str >> hClose hdl
184 appendFile13 :: FilePath -> String -> IO ()
185 appendFile13 name str =
186 openFile name AppendMode >>= \hdl -> hPutStr hdl str >> hClose hdl
190 $writeFile file s$ replaces the contents of {\em file} by the string
191 {\em s}. $appendFile file s$ appends string {\em s} to {\em file}.