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