[make Support.CFF work with bytestrings more rather than handles
John Meacham <john@repetae.net>**20080211093236] hunk ./Support/CFF.hs 8
+    FileType(),
hunk ./Support/CFF.hs 19
-    lazyReadCFF,
-    lazyGetCFF,
+    mkCFFfile,
hunk ./Support/CFF.hs 54
+type FileType = ChunkType
hunk ./Support/CFF.hs 85
-lbsCFF :: Monad m => LBS.ByteString -> m (ChunkType,[(ChunkType,LBS.ByteString)])
+lbsCFF :: Monad m => LBS.ByteString -> m (FileType,[(ChunkType,LBS.ByteString)])
hunk ./Support/CFF.hs 87
-    ans bs = do
+    ans bs' = do
hunk ./Support/CFF.hs 90
+            bs = LBS.take 8 bs'
hunk ./Support/CFF.hs 119
-bsCFF :: Monad m => BS.ByteString -> m (ChunkType,[(ChunkType,BS.ByteString)])
+bsCFF :: Monad m => BS.ByteString -> m (FileType,[(ChunkType,BS.ByteString)])
hunk ./Support/CFF.hs 151
+mkCFFHeader :: FileType -> BS.ByteString
+mkCFFHeader (ChunkType ft) = BS.pack [0x89,b1,b2,b3,0x0d,0x0a,0x1a,0x0a] where
+    (b1,b2,b3,_) = word32ToBytes ft
hunk ./Support/CFF.hs 170
-writeCFFHeader :: Handle -> ChunkType -> IO ()
-writeCFFHeader h (ChunkType ft) = do
-    writeByte h 0x89
-    let (b1,b2,b3,_) = word32ToBytes ft
-    writeByte h b1
-    writeByte h b2
-    writeByte h b3
-    writeByte h 0x0d
-    writeByte h 0x0a
-    writeByte h 0x1a
-    writeByte h 0x0a
+writeCFFHeader :: Handle -> FileType -> IO ()
+writeCFFHeader h ft = BS.hPut h (mkCFFHeader ft)
hunk ./Support/CFF.hs 222
+mkCFFfile :: FileType -> [(ChunkType,LBS.ByteString)] -> LBS.ByteString
+mkCFFfile ft cs = LBS.fromChunks [mkCFFHeader ft] `LBS.append` LBS.concat (concatMap f cs) where
+    f (ChunkType ct,bs) = [hl,bs,zero]  where
+        (b1,b2,b3,b4) = word32ToBytes ct
+        (l1,l2,l3,l4) = word32ToBytes (fromIntegral $ LBS.length bs)
+        hl = LBS.pack [l1,l2,l3,l4,b1,b2,b3,b4]
+zero = LBS.pack [0,0,0,0]
+
hunk ./Support/CFF.hs 251
-lazyReadCFF :: Handle -> IO (ChunkType,Map.Map ChunkType [BS.ByteString])
-lazyReadCFF h = do
-    mv <- newEmptyMVar
-    let getMap = do
-            xs <- readChunk
-            let xs' = sortBy (\ (x,y) (a,b) -> compare x a) xs
-                xs'' = groupBy  (\ (x,y) (a,b) -> x == a) xs'
-                xs''' = [ (ct,map snd xs) | xs@((ct,_):_) <- xs'' ]
-            return (Map.fromList xs''')
-        readChunk = do
-            b <- hIsEOF h
-            if b then return [] else do
-            len <- fromIntegral `fmap` readWord32 h
-            ct <- readChunkType h
-            off <- hTell h
-            hSeek h RelativeSeek (fromIntegral len)
-            hSeek h RelativeSeek 4
-            bs <- unsafeInterleaveIO $ do
-                takeMVar mv
-                hSeek h AbsoluteSeek off
-                res <- BS.hGet h len
-                putMVar mv ()
-                return res
-            xs <- readChunk
-            return ((ct,bs):xs)
-
-    cffType <- readCFFHeader h
-    map <-  getMap
-    putMVar mv ()
-    return (cffType,map)
-
-
-lazyGetCFF fn = do openBinaryFile fn ReadMode >>= lazyReadCFF
-
hunk ./Support/CFF.hs 295
---main = do
---    --xs <- getArgs
---    --mapM_ print ([ (x,isCritical x) | x <- map readChunk xs])
---    --mapM_ print (sort $ map readChunk xs)
---    xs <- getArgs
---    flip mapM xs $ \fn -> do
---        h <- openBinaryFile fn ReadMode
---        cf <- readCFF h
---        hClose h
---        print cf
---        nh <- openBinaryFile "out.cff" WriteMode
---        writeCFF nh cf
---        hClose nh
---    return ()
-