bini 0.1.3 → 0.1.4
raw patch · 4 files changed
+34/−34 lines, 4 filesbinary-added
Files
- bini.cabal +2/−2
- src/Data/Bini.hi binary
- src/Data/Bini.hs +32/−32
- src/Data/Bini.o binary
bini.cabal view
@@ -1,5 +1,5 @@ Name: bini-Version: 0.1.3+Version: 0.1.4 Description: A manipulation library for b(inary)ini files used in windows programms like the game Freelancer. Synopsis: A collection of various methods for reading and writing bini files. License: BSD3@@ -15,7 +15,7 @@ Library ghc-options: -Wall- build-depends: base <4.8, binary, bytestring+ build-depends: base <100.0, binary, bytestring exposed-modules: Data.Bini default-language: Haskell2010 hs-source-dirs: src/
+ src/Data/Bini.hi view
binary file changed (absent → 2860 bytes)
src/Data/Bini.hs view
@@ -1,5 +1,3 @@-- -- | Compressed binary configuration files in the (B)INI format. -- --@@ -24,7 +22,6 @@ -- ... -- @ - module Data.Bini (-- * Reading readBiniFromFile,@@ -36,21 +33,22 @@ import qualified Data.ByteString.Lazy as BL import Data.Binary.Get-import Data.Word---import Data.Binary-import Data.Int+import Data.Word(Word32) import Data.List(intercalate)+import qualified Data.ByteString.Lazy.Char8 as BLC+import Data.Binary.IEEE754(getFloat32le) ---helper +--helper bstr2str :: BL.ByteString -> String bstr2str = tail.init.show -- |Get a String from the stringtable getOffset :: BL.ByteString -> Int -> BL.ByteString-getOffset bstr offset = BL.takeWhile (\c-> not $ c==0) s where- (_,s) = BL.splitAt (fromIntegral offset) bstr+getOffset bstr offset = BL.takeWhile (/=0) s where+ s = snd $ BL.splitAt (fromIntegral offset) bstr + --datatypes -- |The abstract representation of the Bini@@ -60,8 +58,9 @@ data Section = Section{ name :: String, entries :: [Entry] } -- |The abstract representation of an entry-data Entry = Entry String [BiniVal]+data Entry = Entry{ varname :: String, values :: [BiniVal] } + -- |The abstract representation of a bini-value data BiniVal = BiniInt Int | BiniFloat Float | BiniString String @@ -71,10 +70,10 @@ show (Bini v secs) = intercalate "\n" (map show secs) instance Show Section where- show (Section name entries) = "["++name++"]\n"++(intercalate "\n" (map show entries))++"\n"+ show (Section name entries) = "["++name++"]\n"++intercalate "\n" (map show entries)++"\n" instance Show Entry where- show (Entry name vals) = name++" = "++(intercalate ", " (map show vals))+ show (Entry name vals) = name++" = "++intercalate ", " (map show vals) instance Show BiniVal where show (BiniInt i) = show i@@ -83,28 +82,27 @@ -- |Parse a Binifile--- |Parse a Binifile readBiniFromFile :: String -> IO (Either String Bini) readBiniFromFile path = do content <- BL.readFile path- let (isBini, version, strtableoff) = runGet parseHeader content if "BINI"== bstr2str (BL.take 4 content) then do+ let (isBini, version, strtableoff) = runGet parseHeader content let (header_body, table) = BL.splitAt (fromIntegral strtableoff) content let body = BL.drop 12 header_body let secs = runGet (parseSections table) body return $ Right $ Bini version secs- else do- return $ Left $ path++"is not a Bini-file!"-+ else return $ Left $ path++"is not a Bini-file!" ---Parsing the Bini-file:+--------------------------------------------------------------------------------+---------------------- Parsing the Bini-file: ----------------------------------+-------------------------------------------------------------------------------- -- |Parse an entry parseEntry :: BL.ByteString -> Get Entry parseEntry ls = do stroffset <- getWord16le n_vals <- getWord8- let entryname = bstr2str $ ls `getOffset` (fromIntegral stroffset)+ let entryname = bstr2str $ ls `getOffset` fromIntegral stroffset vals <- sequence [parseVal ls | x<-[1..(fromIntegral n_vals)]] return (Entry entryname vals) @@ -112,18 +110,23 @@ parseVal :: BL.ByteString -> Get BiniVal parseVal ls = do typ <- getWord8- dat <- getWord32le- let val = case (fromIntegral typ) of- 1 -> BiniInt $ fromIntegral dat- 2 -> BiniFloat $ fromIntegral dat- 3 -> BiniString $ bstr2str $ ls `getOffset` (fromIntegral dat)- return val+ case fromIntegral typ of+ 1 -> do+ dat <- getWord32le+ return $ BiniInt $ fromIntegral dat+ 2 -> do+ dat <- getFloat32le+ return $ BiniFloat dat+ 3 -> do+ dat <- getWord32le+ return $ BiniString $ bstr2str $ ls `getOffset` fromIntegral dat + parseSection :: BL.ByteString -> Get Section parseSection table = do stroffset <- getWord16le num_e <- getWord16le- let secname = bstr2str $ table `getOffset` (fromIntegral stroffset)+ let secname = bstr2str $ table `getOffset` fromIntegral stroffset let num_entries = fromIntegral num_e entries <- sequence [parseEntry table | x<-[1..num_entries]] return Section{ name = secname, entries = entries }@@ -131,16 +134,14 @@ parseSections :: BL.ByteString -> Get [Section] parseSections table = do b <- isEmpty- if b then do- return []+ if b then return [] else do sec <- parseSection table b2 <- isEmpty- if b2 then do- return [sec]+ if b2 then return [sec] else do secs <- parseSections table- return $ [sec]++secs+ return $ sec : secs parseHeader :: Get (Word32, Int, Int) parseHeader = do@@ -148,7 +149,6 @@ version <- getWord32le str_table_offset <- getWord32le return (isBini, fromIntegral version, fromIntegral str_table_offset)-
+ src/Data/Bini.o view
binary file changed (absent → 53376 bytes)