ATuin

PGM Parser - Not Complete

Jul 20th, 2013
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {-# LANGUAGE OverloadedStrings #-}
  2.  
  3. import Data.Image
  4. import Data.Int
  5. import System.Environment
  6. import System.FilePath.Posix
  7. import Control.Applicative
  8. import Control.Monad.State.Lazy
  9. import qualified Data.ByteString as B
  10. import qualified Data.ByteString.Lazy as LB
  11. import qualified Data.ByteString.Char8 as C
  12. import qualified Data.ByteString.Internal as I
  13. import Data.Word8
  14.  
  15. imgInPath :: FilePath -> IO FilePath
  16. imgInPath i = combine <$> getEnv "PWD" <*> return i
  17.  
  18. data Header = Header { format :: [Word8],
  19.               width :: Int64,
  20.               height :: Int64,
  21.               maxGrayValue :: Int64
  22.             }
  23.             deriving (Show)
  24.  
  25. data PGMFormat = PGMFormat { header :: Maybe Header,
  26.                              body :: Maybe LB.ByteString
  27.                            }
  28. data PGM = P1 PGMFormat
  29.          | P2 PGMFormat
  30.          | P3 PGMFormat
  31.          | P4 PGMFormat
  32.          | P5 PGMFormat
  33.          | P6 PGMFormat
  34.  
  35. -- | ParserState
  36. -- | State which stores info about the parser:
  37. -- |    parsed:    Value yet parsed
  38. -- |    toParse:   Value to parse
  39. -- |    offset:    Current offset in paring
  40. data ParserState = ParserState { parsed :: LB.ByteString
  41.                                , toParse :: LB.ByteString
  42.                                , offset :: Int64
  43.                                }
  44.                    deriving (Show)
  45.  
  46. parserState :: LB.ByteString -> ParserState
  47. parserState = (flip $ ParserState "") 0
  48.  
  49. type PGMState = State ParserState
  50.  
  51. parseWhile :: (Word8 -> Bool) -> PGMState LB.ByteString
  52. parseWhile f = do
  53.   parserSt <- get
  54.   let (Just (b, bss)) = LB.uncons $ toParse parserSt
  55.   put $ ParserState (LB.cons b $ parsed parserSt) bss ((+1) $ offset parserSt)
  56.   case f b of
  57.     True -> parseWhile f
  58.     False -> do
  59.       put $ parserSt { parsed = "" }
  60.       return $ LB.reverse $ parsed parserSt
  61.  
  62. parseWord :: PGMState LB.ByteString
  63. parseWord = parseWhile (not . notWhite) >> parseWhile notWhite
  64.  
  65. parseInt64 :: PGMState (Maybe Int64)
  66. parseInt64 = parseWord >>= f
  67.   where
  68.     f x = return $ (fromInteger <$> fst <$> C.readInteger (B.concat $ LB.toChunks x))
  69.  
  70. notWhite = not . I.isSpaceWord8
  71.  
  72. parsePGMHeader :: PGMState Header
  73. parsePGMHeader =  do
  74.     f <- parseWord
  75.     (Just w) <- parseInt64
  76.     (Just h) <- parseInt64
  77.     (Just g) <- parseInt64
  78.     return $ Header (LB.unpack f) w h g
  79.  
  80. -- parsePGM2 = do
  81. --   header <- parsePGMHeader
  82. --   (bs, o) <- get
  83. --   return o
Advertisement
Add Comment
Please, Sign In to add comment