ips/main/ips.hs

30 lines
822 B
Haskell

module Main (main) where
import IPS (Bytes, makeBytes)
import qualified IPS
import System.Environment (getArgs)
import System.Exit (exitFailure)
import System.IO (hPutBuf, withFile, IOMode(WriteMode))
import qualified Data.ByteString as ByteString
import qualified Data.Vector.Storable as Vector
main :: IO ()
main = do
args <- getArgs
case args of
[inf, ipsf, outf] -> do
buf <- readBytes inf
ips <- either error id <$> IPS.parseFile ipsf
writeBytes outf $ IPS.apply ips buf
_ -> do
putStrLn "usage: ips <in> <ips> <out>"
exitFailure
readBytes :: FilePath -> IO Bytes
readBytes f = makeBytes <$> ByteString.readFile f
writeBytes :: FilePath -> Bytes -> IO ()
writeBytes f buf =
withFile f WriteMode \h ->
Vector.unsafeWith buf \ptr ->
hPutBuf h ptr (Vector.length buf)