shithub: MicroHs

ref: f962f4a8fa5d9de0354263ee3fabfafdd9759291
dir: /Tools/Addcombs.hs/

View raw version
module Addcombs(main) where
import Prelude
import Data.Char
import System.Environment
import System.IO

chunkify :: Int -> [Char] -> [[Char]]
chunkify n [] = []
chunkify n xs =
  let (as, bs) = splitAt n xs
  in  as : chunkify n bs

showChunk :: [Char] -> String
showChunk = concatMap (\ c -> show (ord c) ++ ",")

main :: IO ()
main = do
  args <- getArgs
  let (ifn, ofn) = case args of { [a,b] -> (a,b); _ -> error "Usage: Addcombs in-file out-file" }
  ifile <- openBinaryFile ifn ReadMode
  ofile <- openFile ofn WriteMode

  file <- hGetContents ifile
  let size = length file
      chunks = chunkify 20 file
  hPutStrLn ofile $ "unsigned char combexprdata[] = {"
  mapM_ (hPutStrLn ofile . showChunk) chunks
  hPutStrLn ofile "0 };"
  hPutStrLn ofile "unsigned char *combexpr = combexprdata;"
  hPutStrLn ofile $ "int combexprlen = " ++ show size ++ ";"
  hClose ifile
  hClose ofile