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)
34 import PreludeMonadicIO
40 hPutChar :: Handle -> Char -> IO ()
42 takeMVar handle >>= \ htype ->
44 _ErrorHandle ioError ->
45 putMVar handle htype >>
48 putMVar handle htype >>
49 failWith (IllegalOperation "handle is closed")
50 _SemiClosedHandle _ _ ->
51 putMVar handle htype >>
52 failWith (IllegalOperation "handle is closed")
54 putMVar handle htype >>
55 failWith (IllegalOperation "handle is not open for writing")
57 _ccall_ filePutc (_filePtr other) (ord c) `thenPrimIO` \ rc ->
58 putMVar handle (_markHandle htype) >>
62 _constructError `thenPrimIO` \ ioError ->
65 putChar :: Char -> IO ()
66 putChar = hPutChar stdout13
70 Computation $hPutChar hdl c$ writes the character {\em c} to the file
71 or channel managed by {\em hdl}. Characters may be buffered if
72 buffering is enabled for {\em hdl}.
75 hPutStr :: Handle -> String -> IO ()
77 takeMVar handle >>= \ htype ->
79 _ErrorHandle ioError ->
80 putMVar handle htype >>
83 putMVar handle htype >>
84 failWith (IllegalOperation "handle is closed")
85 _SemiClosedHandle _ _ ->
86 putMVar handle htype >>
87 failWith (IllegalOperation "handle is closed")
89 putMVar handle htype >>
90 failWith (IllegalOperation "handle is not open for writing")
92 _getBufferMode other `thenPrimIO` \ other ->
93 (case _bufferMode other of
95 writeLines (_filePtr other) str
96 Just (BlockBuffering (Just size)) ->
97 writeBlocks (_filePtr other) size str
98 Just (BlockBuffering Nothing) ->
99 writeBlocks (_filePtr other) ``BUFSIZ'' str
100 _ -> -- Nothing is treated pessimistically as NoBuffering
101 writeChars (_filePtr other) str
102 ) `thenPrimIO` \ success ->
103 putMVar handle (_markHandle other) `seqPrimIO`
107 _constructError `thenPrimIO` \ ioError ->
112 writeBlocks :: _Addr -> Int -> String -> PrimIO Bool
113 writeBlocks fp size "" = returnPrimIO True
114 writeBlocks fp size s =
116 (some, more) = splitAt size s
118 _packBytesForCST some `thenPrimIO`
119 \ bytes@(_ByteArray (0, count) _) ->
120 _ccall_ writeFile bytes fp (count+1) `thenPrimIO` \ rc ->
122 writeBlocks fp size more
126 writeLines :: _Addr -> String -> PrimIO Bool
127 writeLines fp "" = returnPrimIO True
130 (some, more) = breakLine s
132 _packBytesForCST some `thenPrimIO`
133 \ bytes@(_ByteArray (0, count) _) ->
134 _ccall_ writeFile bytes fp (count+1) `thenPrimIO` \ rc ->
140 breakLine "" = ("","")
142 | x == '\n' = ([x],xs)
143 | otherwise = let (ys,zs) = breakLine xs in (x:ys,zs)
145 writeChars :: _Addr -> String -> PrimIO Bool
146 writeChars fp "" = returnPrimIO True
147 writeChars fp (c:cs) =
148 _ccall_ filePutc fp (ord c) `thenPrimIO` \ rc ->
154 putStr :: String -> IO ()
155 putStr = hPutStr stdout13
157 hPutText :: Text a => Handle -> a -> IO ()
158 hPutText hdl = hPutStr hdl . show
160 putText :: Text a => a -> IO ()
161 putText = hPutText stdout13
163 print13 :: Text a => a -> IO ()
164 print13 x = putText x >> putChar '\n'
168 Computation $hPutStr hdl s$ writes the string {\em s} to the file or
169 channel managed by {\em hdl}.
171 Computation $putStr s$ writes the string {\em s} to $stdout$.
173 Computation $hPutText hdl t$ writes the string representation of {\em
174 t} given by the $shows$ function to the file or channel managed by
179 writeFile13 :: FilePath -> String -> IO ()
180 writeFile13 name str =
181 openFile name WriteMode >>= \hdl -> hPutStr hdl str >> hClose hdl
183 appendFile13 :: FilePath -> String -> IO ()
184 appendFile13 name str =
185 openFile name AppendMode >>= \hdl -> hPutStr hdl str >> hClose hdl
189 $writeFile file s$ replaces the contents of {\em file} by the string
190 {\em s}. $appendFile file s$ appends string {\em s} to {\em file}.