aboutsummaryrefslogtreecommitdiff
path: root/src/Haddock/InterfaceFile.hs
blob: becd40df6a37eae881236cadf40ba7bf1d67dc6a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
module Haddock.InterfaceFile (
  InterfaceFile(..),
  writeInterfaceFile,
  readInterfaceFile
) where

import HaddockTypes
import Haddock.Exception

import Binary
import System.IO
import Data.Word
import qualified Data.Map as Map
import Control.Monad

data InterfaceFile = InterfaceFile {
  ifDocEnv :: DocEnv
} 

instance Binary InterfaceFile where
  put_ bh (InterfaceFile docEnv) = put_ bh (Map.toList docEnv)
  get bh = do
    envList <- get bh
    return (InterfaceFile (Map.fromList envList))

packageFileMagic = 0xDA303001 :: Word32
packageFileVersion = 0 :: Word16

writeInterfaceFile :: FilePath -> InterfaceFile -> IO ()
writeInterfaceFile filename iface = do 
  h <- openBinaryFile filename WriteMode
  bh <- openBinIO h
  ud <- newWriteState
  bh <- return $ setUserData bh ud
  put_ bh packageFileMagic
  put_ bh packageFileVersion
  put_ bh iface
  hClose h
    
readInterfaceFile :: FilePath -> IO InterfaceFile
readInterfaceFile filename = do
  h <- openBinaryFile filename ReadMode
  bh <- openBinIO h
  ud <- newReadState undefined
  bh <- return (setUserData bh ud)
  magic <- get bh
  when (magic /= packageFileMagic) $ throwE $
    "Magic number mismatch: couldn't load interface file: " ++ filename
  (version :: Word16) <- get bh
  get bh