/
CIO.hs
158 lines (125 loc) · 4.4 KB
/
CIO.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
-- Compiler Toolkit: Compiler I/O
--
-- Author : Manuel M T Chakravarty
-- Created: 2 November 95
--
-- Copyright (c) [1995...2005] Manuel M T Chakravarty
--
-- This file is free software; you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation; either version 2 of the License, or
-- (at your option) any later version.
--
-- This file is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
--- DESCRIPTION ---------------------------------------------------------------
--
-- This module lifts the Haskell I/O facilities into `STB' and provides some
-- useful extensions.
--
--- DOCU ----------------------------------------------------------------------
--
-- language: Haskell 98
--
-- * the usage of the `...CIO' functions is exactly as that of the
-- corresponding `...' functions from the Haskell 98 prelude and library
--
-- * error handling can be found in the module `StateTrans' and `State'
--
-- * Also reexports constants, such as `stderr', and data types of `IO' to
-- avoid explicit imports of `IO' in the rest of the compiler.
--
--- TODO ----------------------------------------------------------------------
--
module CIO (-- (verbatim) re-exports
--
Handle, HandlePosn, IOMode(..), BufferMode(..), SeekMode(..),
stdin, stdout, stderr,
isAlreadyExistsError, isDoesNotExistError, isAlreadyInUseError,
isFullError, isEOFError, isIllegalOperation, isPermissionError,
isUserError,
ioeGetErrorString, ioeGetHandle, ioeGetFileName,
--
-- file handling
--
openFileCIO, hCloseCIO,
--
-- text I/O
--
putCharCIO, putStrCIO, putStrLnCIO, hPutStrCIO, hPutStrLnCIO,
writeFileCIO, readFileCIO, printCIO, getCharCIO, hFlushCIO,
hPutCharCIO, hSetBufferingCIO, hGetBufferingCIO, newlineCIO,
--
-- `Directory'
--
doesFileExistCIO, removeFileCIO,
--
-- `System'
--
ExitCode(..), exitWithCIO, getArgsCIO, getProgNameCIO, systemCIO,
)
where
import System.IO
import System.IO.Error
import System.Directory (doesFileExist, removeFile)
import System.Environment (getArgs, getProgName)
import System.Cmd (system)
import System.Exit (ExitCode(..), exitWith)
import StateBase (PreCST, liftIO)
-- file handling
-- -------------
openFileCIO :: FilePath -> IOMode -> PreCST e s Handle
openFileCIO p m = liftIO (openFile p m)
hCloseCIO :: Handle -> PreCST e s ()
hCloseCIO h = liftIO (hClose h)
-- text I/O
-- --------
putCharCIO :: Char -> PreCST e s ()
putCharCIO c = liftIO (putChar c)
putStrCIO :: String -> PreCST e s ()
putStrCIO s = liftIO (putStr s)
putStrLnCIO :: String -> PreCST e s ()
putStrLnCIO s = liftIO (putStrLn s)
hPutStrCIO :: Handle -> String -> PreCST e s ()
hPutStrCIO h s = liftIO (hPutStr h s)
hPutStrLnCIO :: Handle -> String -> PreCST e s ()
hPutStrLnCIO h s = liftIO (hPutStrLn h s)
writeFileCIO :: FilePath -> String -> PreCST e s ()
writeFileCIO fname contents = liftIO (writeFile fname contents)
readFileCIO :: FilePath -> PreCST e s String
readFileCIO fname = liftIO (readFile fname)
printCIO :: Show a => a -> PreCST e s ()
printCIO a = liftIO (print a)
getCharCIO :: PreCST e s Char
getCharCIO = liftIO getChar
hFlushCIO :: Handle -> PreCST e s ()
hFlushCIO h = liftIO (hFlush h)
hPutCharCIO :: Handle -> Char -> PreCST e s ()
hPutCharCIO h ch = liftIO (hPutChar h ch)
hSetBufferingCIO :: Handle -> BufferMode -> PreCST e s ()
hSetBufferingCIO h m = liftIO (hSetBuffering h m)
hGetBufferingCIO :: Handle -> PreCST e s BufferMode
hGetBufferingCIO h = liftIO (hGetBuffering h)
-- derived functions
--
newlineCIO :: PreCST e s ()
newlineCIO = putCharCIO '\n'
-- `Directory'
-- -----------
doesFileExistCIO :: FilePath -> PreCST e s Bool
doesFileExistCIO = liftIO . doesFileExist
removeFileCIO :: FilePath -> PreCST e s ()
removeFileCIO = liftIO . removeFile
-- `System'
-- --------
exitWithCIO :: ExitCode -> PreCST e s a
exitWithCIO = liftIO . exitWith
getArgsCIO :: PreCST e s [String]
getArgsCIO = liftIO getArgs
getProgNameCIO :: PreCST e s String
getProgNameCIO = liftIO getProgName
systemCIO :: String -> PreCST e s ExitCode
systemCIO = liftIO . system