
2008/1/20 Bryan Green
Does anyone know of a library that will handle bytea encodings from postgres? The bytea field that I need to access contains a jpg file. I want to retrieve it from the database and write it out for an image display program.
I'd love to see Don give the correct ByteString solution, but here's a slow version I knocked up (probably buggy): import qualified Data.ByteString as B import Data.ByteString.Internal (c2w) import Data.Maybe (catMaybes) import Data.List (mapAccumL) byteaDecode :: B.ByteString -> B.ByteString byteaDecode = B.pack . catMaybes . snd . mapAccumL f initState . B.unpack where initState = (0, 0) f (0, _) 92 = ((1, 0), Nothing) f (0, _) x = ((0, 0), Just x) f (1, _) 92 = ((0, 0), Just 92) f (3, n) x = ((0, 0), Just (n * 8 + (x - 48))) f (c, n) x = ((c + 1, n * 8 + (x - 48)), Nothing) AGL -- Adam Langley agl@imperialviolet.org http://www.imperialviolet.org 650-283-9641