Std.hs 792 B

1234567891011121314151617181920212223242526272829303132
  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. -- | UniformIO that reads from stdin and writes to stdout.
  10. instance UniformIO StdIO where
  11. uRead _ n = do
  12. 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 = do
  20. BS.useAsCStringLen t (
  21. \(str, n) -> do
  22. count <- c_sendStd str $ fromIntegral n
  23. if count < 0
  24. then throwErrno "could not write"
  25. else return ()
  26. )
  27. uClose _ = return ()
  28. startTls _ a = return a
  29. isSecure _ = True