From 7b54ec9006d5a9dfe61e50eeee7555ca845549eb Mon Sep 17 00:00:00 2001 From: Stefan Dresselhaus Date: Tue, 15 Apr 2014 07:17:45 +0200 Subject: [PATCH] improved Parser and added documentation --- src/Importer/IQM/Parser.hs | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/Importer/IQM/Parser.hs b/src/Importer/IQM/Parser.hs index 932d0a2..29fc148 100644 --- a/src/Importer/IQM/Parser.hs +++ b/src/Importer/IQM/Parser.hs @@ -135,16 +135,23 @@ readMeshes n = do infix 5 .- +skipToCounter :: Integral a => a -> CParser () +skipToCounter a = do + let d = fromIntegral a + c <- get + when (d < c) $ fail "wanting to skip to counter already passed" + _ <- lift $ take $ d .- c + put d + parseIQM :: CParser IQM parseIQM = do - put 0 - h <- readHeader - soFar <- get - _ <- lift $ take $ ofs_text h .- soFar - text <- lift $ take $ fromIntegral $ num_text h - soFar <- get - _ <- lift $ take $ ofs_meshes h .- soFar - meshes' <- readMeshes (fromIntegral (num_meshes h)) + put 0 --start at offset 0 + h <- readHeader --read header + skipToCounter $ ofs_text h --skip 0-n bytes to get to text + text <- lift . take . fromIntegral $ num_text h --read texts + modify . (+) . fromIntegral $ num_text h --put offset forward + skipToCounter $ ofs_meshes h --skip 0-n bytes to get to meshes + meshes' <- readMeshes (fromIntegral (num_meshes h)) --read meshes return IQM { header = h , texts = filter (not.null) (split (unsafeCoerce '\0') text)