public
Last active

  • Download Gist
gistfile1.hs
Haskell
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
import System.Environment
import System.IO
 
import Control.Applicative hiding (many)
import Data.Attoparsec as A
import qualified Data.ByteString as BS
 
parseMarker = do
part1 <- word8 0xFF
part2 <- notWord8 0x0
return (part1, part2)
 
parseSection = do
A.skipWhile (\x -> x /= 0xFF) *> parseMarker
 
parseBody = do
many parseSection
 
parseJPEG jpeg = do
handleParseResult $ feed (parse parseBody jpeg) BS.empty
 
handleParseResult result = do
case result of
Fail _ _ msg -> msg
Done _ r -> show r
_ -> ""
 
main = do
(filename : _ ) <- getArgs
handle <- openFile filename ReadMode
contents <- BS.hGetContents handle
putStrLn $ parseJPEG contents
hClose handle

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.