{-# LANGUAGE Trustworthy #-}
module System.IO.Utils(
hCopy, hCopyProgress, hLineCopy, lineCopy,
copyFileLinesToFile,
hPutStrLns, hGetLines,
hInteract,
hLineInteract, lineInteract,
lazyMapM,
optimizeForBatch, optimizeForInteraction
) where
import Data.List (genericLength)
import System.IO (BufferMode (BlockBuffering, LineBuffering),
IOMode (ReadMode, WriteMode), hClose,
hSetBuffering, openFile, stdin, stdout)
import System.IO.HVIO (HVIO (vGetContents, vGetLine, vIsEOF, vPutStr, vPutStrLn))
import System.IO.Unsafe (unsafeInterleaveIO)
hPutStrLns :: HVIO a => a -> [String] -> IO ()
hPutStrLns :: forall a. HVIO a => a -> [String] -> IO ()
hPutStrLns a
h = forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ forall a b. (a -> b) -> a -> b
$ forall a. HVIO a => a -> String -> IO ()
vPutStrLn a
h
hGetLines :: HVIO a => a -> IO [String]
hGetLines :: forall a. HVIO a => a -> IO [String]
hGetLines a
h = forall a. IO a -> IO a
unsafeInterleaveIO (do
Bool
ieof <- forall a. HVIO a => a -> IO Bool
vIsEOF a
h
if (Bool
ieof)
then forall (m :: * -> *) a. Monad m => a -> m a
return []
else do
String
line <- forall a. HVIO a => a -> IO String
vGetLine a
h
[String]
remainder <- forall a. HVIO a => a -> IO [String]
hGetLines a
h
forall (m :: * -> *) a. Monad m => a -> m a
return (String
line forall a. a -> [a] -> [a]
: [String]
remainder))
hInteract :: (HVIO a, HVIO b) => a -> b -> (String -> String) -> IO ()
hInteract :: forall a b.
(HVIO a, HVIO b) =>
a -> b -> (String -> String) -> IO ()
hInteract a
finput b
foutput String -> String
func = do
String
content <- forall a. HVIO a => a -> IO String
vGetContents a
finput
forall a. HVIO a => a -> String -> IO ()
vPutStr b
foutput (String -> String
func String
content)
lineInteract :: ([String] -> [String]) -> IO ()
lineInteract :: ([String] -> [String]) -> IO ()
lineInteract = forall a b.
(HVIO a, HVIO b) =>
a -> b -> ([String] -> [String]) -> IO ()
hLineInteract Handle
stdin Handle
stdout
hLineInteract :: (HVIO a, HVIO b) => a -> b -> ([String] -> [String]) -> IO ()
hLineInteract :: forall a b.
(HVIO a, HVIO b) =>
a -> b -> ([String] -> [String]) -> IO ()
hLineInteract a
finput b
foutput [String] -> [String]
func =
do
[String]
ls <- forall a. HVIO a => a -> IO [String]
hGetLines a
finput
forall a. HVIO a => a -> [String] -> IO ()
hPutStrLns b
foutput ([String] -> [String]
func [String]
ls)
hCopy :: (HVIO a, HVIO b) => a -> b -> IO ()
hCopy :: forall a b. (HVIO a, HVIO b) => a -> b -> IO ()
hCopy a
hin b
hout = do
String
c <- forall a. HVIO a => a -> IO String
vGetContents a
hin
forall a. HVIO a => a -> String -> IO ()
vPutStr b
hout String
c
hCopyProgress :: (HVIO b, HVIO c, Integral a) =>
b
-> c
-> (Maybe a -> Integer -> Bool -> IO ())
-> Int
-> Maybe a
-> IO Integer
hCopyProgress :: forall b c a.
(HVIO b, HVIO c, Integral a) =>
b
-> c
-> (Maybe a -> Integer -> Bool -> IO ())
-> Int
-> Maybe a
-> IO Integer
hCopyProgress b
hin c
hout Maybe a -> Integer -> Bool -> IO ()
func Int
bsize Maybe a
estsize =
let copyFunc :: String -> Integer -> IO Integer
copyFunc :: String -> Integer -> IO Integer
copyFunc [] Integer
count = forall (m :: * -> *) a. Monad m => a -> m a
return Integer
count
copyFunc String
indata Integer
count =
let block :: String
block = forall a. Int -> [a] -> [a]
take Int
bsize String
indata
remainder :: String
remainder = forall a. Int -> [a] -> [a]
drop Int
bsize String
indata
newcount :: Integer
newcount = Integer
count forall a. Num a => a -> a -> a
+ (forall i a. Num i => [a] -> i
genericLength String
block)
in
do
forall a. HVIO a => a -> String -> IO ()
vPutStr c
hout String
block
Maybe a -> Integer -> Bool -> IO ()
func Maybe a
estsize Integer
count Bool
False
String -> Integer -> IO Integer
copyFunc String
remainder Integer
newcount
in
do
String
c <- forall a. HVIO a => a -> IO String
vGetContents b
hin
Integer
bytes <- String -> Integer -> IO Integer
copyFunc String
c Integer
0
Maybe a -> Integer -> Bool -> IO ()
func Maybe a
estsize Integer
bytes Bool
True
forall (m :: * -> *) a. Monad m => a -> m a
return Integer
bytes
hLineCopy :: (HVIO a, HVIO b) => a -> b -> IO()
hLineCopy :: forall a b. (HVIO a, HVIO b) => a -> b -> IO ()
hLineCopy a
hin b
hout = forall a b.
(HVIO a, HVIO b) =>
a -> b -> ([String] -> [String]) -> IO ()
hLineInteract a
hin b
hout forall a. a -> a
id
lineCopy :: IO ()
lineCopy :: IO ()
lineCopy = forall a b. (HVIO a, HVIO b) => a -> b -> IO ()
hLineCopy Handle
stdin Handle
stdout
copyFileLinesToFile :: FilePath -> FilePath -> IO ()
copyFileLinesToFile :: String -> String -> IO ()
copyFileLinesToFile String
infn String
outfn = do
Handle
hin <- String -> IOMode -> IO Handle
openFile String
infn IOMode
ReadMode
Handle
hout <- String -> IOMode -> IO Handle
openFile String
outfn IOMode
WriteMode
forall a b. (HVIO a, HVIO b) => a -> b -> IO ()
hLineCopy Handle
hin Handle
hout
Handle -> IO ()
hClose Handle
hin
Handle -> IO ()
hClose Handle
hout
forall (m :: * -> *) a. Monad m => a -> m a
return ()
optimizeForBatch :: IO ()
optimizeForBatch :: IO ()
optimizeForBatch = do
Handle -> BufferMode -> IO ()
hSetBuffering Handle
stdin (Maybe Int -> BufferMode
BlockBuffering (forall a. a -> Maybe a
Just Int
4096))
Handle -> BufferMode -> IO ()
hSetBuffering Handle
stdout (Maybe Int -> BufferMode
BlockBuffering (forall a. a -> Maybe a
Just Int
4096))
optimizeForInteraction :: IO ()
optimizeForInteraction :: IO ()
optimizeForInteraction = do
Handle -> BufferMode -> IO ()
hSetBuffering Handle
stdin BufferMode
LineBuffering
Handle -> BufferMode -> IO ()
hSetBuffering Handle
stdout BufferMode
LineBuffering
lazyMapM :: (a -> IO b) -> [a] -> IO [b]
lazyMapM :: forall a b. (a -> IO b) -> [a] -> IO [b]
lazyMapM a -> IO b
_ [] = forall (m :: * -> *) a. Monad m => a -> m a
return []
lazyMapM a -> IO b
conv (a
x:[a]
xs) =
do b
this <- a -> IO b
conv a
x
[b]
next <- forall a. IO a -> IO a
unsafeInterleaveIO forall a b. (a -> b) -> a -> b
$ forall a b. (a -> IO b) -> [a] -> IO [b]
lazyMapM a -> IO b
conv [a]
xs
forall (m :: * -> *) a. Monad m => a -> m a
return (b
thisforall a. a -> [a] -> [a]
:[b]
next)