Std.hs 741 B

1234567891011121314151617181920212223242526272829
  1. module System.IO.Uniform.Std (
  2. StdIO(StdIO)
  3. ) where
  4. import System.IO.Uniform
  5. import System.IO.Uniform.External
  6. import Foreign
  7. import Foreign.C.Error
  8. import qualified Data.ByteString as BS
  9. import Control.Monad
  10. -- | UniformIO that reads from stdin and writes to stdout.
  11. instance UniformIO StdIO where
  12. uRead _ n = allocaArray n (
  13. \b -> do
  14. count <- c_recvStd b (fromIntegral n)
  15. if count < 0
  16. then throwErrno "could not read"
  17. else BS.packCStringLen (b, fromIntegral count)
  18. )
  19. uPut _ t = BS.useAsCStringLen t (
  20. \(str, n) -> do
  21. count <- c_sendStd str $ fromIntegral n
  22. when (count < 0) $ throwErrno "could not write"
  23. )
  24. uClose _ = return ()
  25. startTls _ = return
  26. isSecure _ = True