cedict-0.2.2: Data/Char/CEDICT/GenerateCode.hs
module Data.Char.CEDICT.GenerateCode
( readAndWrite
) where
import Data.Char.CEDICT.Reader.Parser
import Data.Char.CEDICT.Reader.ListRebuild
import Data.Char.CEDICT.C.Declarations
import System.IO.UTF8
import System.IO hiding
( appendFile
, getContents
, getLine
, print
, putStr
, putStrLn
, readFile
, readLn
, writeFile
, hGetContents
, hGetLine
, hPutStr
, hPutStrLn
)
import Prelude hiding
( appendFile
, getContents
, getLine
, print
, putStr
, putStrLn
, readFile
, readLn
, writeFile
)
import qualified Data.ByteString
import Control.Monad
import System.Time
import System.Locale
readAndWrite :: FilePath -> FilePath -> IO ()
readAndWrite dF cF = do
parseHappening <- parseFile dF
case parseHappening of
Left err -> print err
Right stuff -> do
time <- liftM toUTCTime getClockTime
Data.ByteString.writeFile cF $ handleResults time stuff
handleResults time (tag, list) = Data.ByteString.concat
[ toBytes . cHeader $ message time tag
, toBytes "\n\n"
, toCDecl ts "ts"
, toBytes "\n\n"
, toCDecl st "st"
, toBytes "\n\n"
, toCDecl d "d"
]
where
(d, ts, st) = majorLists list
cHeader note = unlines
[ " /* /"
, note
, " / */"
, ""
, "#include <stddef.h>"
, ""
]
message time tag = unlines
[ " Generated on " ++ (fmt time) ++ " from:"
, " " ++ tag
, " Changes will be overwritten the next time the generation script is run."
]
where
fmt = formatCalendarTime defaultTimeLocale "%Y-%m-%dT%H:%M:%SZ"