Safe Haskell | None |
---|---|
Language | Haskell98 |
Convert tuples of Haskell values to and from ASCII or packed binary representations.
This package is intended for cheap and cheerful serialisation and deserialisation of flat tables, where each row has a fixed format. If you have a table consisting of a couple hundred megs of Pipe-Separated-Variables issued by some filthy enterprise system, then this package is for you.
If you want to parse context-free, or context-sensitive
languages then try the parsec
or attoparsec
packages.
If you have binary data that does not have a fixed format then
try the binary
or cereal
packages.
For testing purposes, use packToString
which takes a format,
a record, and returns a list of bytes.
> import Data.Repa.Convert > let format = Sep '|' (VarAsc :*: IntAsc :*: DoubleAsc :*: ()) > let Just str = packToString format ("foo" :*: 66 :*: 93.42 :*: ()) > str "foo|66|93.42"
We can then unpack the raw bytes back to Haskell values with unpackFromString
.
> unpackFromString format str Just ("foo" :*: (66 :*: (93.42 :*: ())))
In production code use unsafeRunPacker
and unsafeRunUnpacker
to work directly
with a buffer in foreign memory.
- NOTE that in the current version the separating character is un-escapable.
- The above means that the format
(Sep ',')
does NOT parse a CSV file according to the CSV specification: http://tools.ietf.org/html/rfc4180.
- module Data.Repa.Convert.Formats
- class Format f where
- forFormat :: format -> Value format -> Value format
- listFormat :: format -> [Value format] -> [Value format]
- class Format format => Packable format where
- data Packer = Packer {}
- unsafeRunPacker :: Packer -> Ptr Word8 -> IO (Maybe (Ptr Word8))
- data Unpacker a = Unpacker {}
- unsafeRunUnpacker :: Unpacker a -> Ptr Word8 -> Int -> (Word8 -> Bool) -> IO (Maybe (a, Ptr Word8))
- packToAscii :: (FormatAscii a, Value (FormatAscii' a) ~ a, Packable (FormatAscii' a)) => a -> Maybe String
- packToList8 :: Packable format => format -> Value format -> Maybe [Word8]
- unpackFromList8 :: Packable format => format -> [Word8] -> Maybe (Value format)
- packToString :: Packable format => format -> Value format -> Maybe String
- unpackFromString :: Packable format => format -> String -> Maybe (Value format)
Documentation
The Formats
module contains the pre-defined data formats.
module Data.Repa.Convert.Formats
Data formats
Relates a storage format to the Haskell type of the value that is stored in that format.
fieldCount :: f -> Int Source
Yield the number of separate fields in this format.
Yield the minumum number of bytes that a value of this format will take up.
Packing a value into this format is guaranteed to use at least this many bytes. This is exact for fixed-size formats.
fixedSize :: f -> Maybe Int Source
For fixed size formats, yield their size (length) in bytes.
Yields Nothing
if this is not a fixed size format.
packedSize :: f -> Value f -> Maybe Int Source
Yield the size of a value in the given format.
Yields Nothing
when a collection of values is to be packed into a
fixed length format, but the size of the collection does not match
the format.
If fixedSize
returns a size then packedSize
returns the same size.
Format Float64be Source | |
Format Float32be Source | |
Format Int64be Source | |
Format Word64be Source | |
Format Int32be Source | |
Format Word32be Source | |
Format Int16be Source | |
Format Word16be Source | |
Format Int8be Source | |
Format Word8be Source | |
Format VarString Source | |
Format VarAsc Source | |
Format FixAsc Source | |
Format DoubleAsc Source | |
Format IntAsc0 Source | |
Format IntAsc Source | |
Format DDsMMsYYYY Source | |
Format YYYYsMMsDD Source | |
Format (App ()) Source | |
(Format f1, Format (App fs), (~) * (Value (App fs)) (Value fs)) => Format (App ((:*:) f1 fs)) Source | |
Format (Sep ()) Source | |
(Format f1, Format (Sep fs), (~) * (Value (Sep fs)) (Value fs)) => Format (Sep ((:*:) f1 fs)) Source | |
Format (Tup ()) Source | |
(Format f1, Format (Tup fs), Format (Sep fs), (~) * (Value (Sep fs)) (Value fs)) => Format (Tup ((:*:) f1 fs)) Source |
Type constraints
forFormat :: format -> Value format -> Value format Source
Constrain the type of a value to match the given format.
The value itself is not used.
listFormat :: format -> [Value format] -> [Value format] Source
Constrain the type of some values to match the given format.
The value itself is not used.
Packing data
class Format format => Packable format where Source
Class of storage formats that can have values packed and unpacked from foreign bufferes.
The methods are written using continuations to make it easier for GHC to optimise its core code when packing/unpacking many fields.
Pack a value into a buffer using the given format.
Unpack a value from a buffer using the given format.
Packable Float64be Source | |
Packable Float32be Source | |
Packable Int64be Source | |
Packable Word64be Source | |
Packable Int32be Source | |
Packable Word32be Source | |
Packable Int16be Source | |
Packable Word16be Source | |
Packable Int8be Source | |
Packable Word8be Source | |
Packable VarString Source | |
Packable VarAsc Source | |
Packable FixAsc Source | |
Packable DoubleAsc Source | |
Packable IntAsc0 Source | |
Packable IntAsc Source | |
Packable DDsMMsYYYY Source | |
Packable YYYYsMMsDD Source | |
Packable (App ()) Source | |
(Packable f1, Packable (App fs), (~) * (Value (App fs)) (Value fs)) => Packable (App ((:*:) f1 fs)) Source | |
Packable (Sep ()) Source | |
(Packable f1, Packable (Sep fs), (~) * (Value (Sep fs)) (Value fs)) => Packable (Sep ((:*:) f1 fs)) Source | |
Packable (Tup ()) Source | |
(Packable f1, Packable (Sep fs), Format (Tup fs), (~) * (Value (Sep fs)) (Value fs)) => Packable (Tup ((:*:) f1 fs)) Source |
Packer monoid
Packer wraps a function that can write to a buffer.
:: Packer | Packer to run. |
-> Ptr Word8 | Start of buffer. |
-> IO (Maybe (Ptr Word8)) | Pointer to the byte after the last one written. |
Pack data into the given buffer.
PRECONDITION: The buffer needs to be big enough to hold the packed data,
otherwise you'll corrupt the heap (bad). Use packedSize
to work out
how big it needs to be.
Unpacker monad
Unpacker | |
|
:: Unpacker a | Unpacker to run. |
-> Ptr Word8 | Source buffer. |
-> Int | Length of source buffer. |
-> (Word8 -> Bool) | Detect a field terminator. |
-> IO (Maybe (a, Ptr Word8)) | Unpacked result, and pointer to the byte after the last one read. |
Unpack data from the given buffer.
PRECONDITION: The buffer must be at least the minimum size of the format (minSize). This allows us to avoid repeatedly checking for buffer overrun when unpacking fixed size format. If the buffer is not long enough then you'll get an indeterminate result (bad).
Interfaces
Default Ascii
packToAscii :: (FormatAscii a, Value (FormatAscii' a) ~ a, Packable (FormatAscii' a)) => a -> Maybe String Source
Pack a value to a list of Word8
using the default Ascii format.
List Interface
packToList8 :: Packable format => format -> Value format -> Maybe [Word8] Source
Pack a value to a list of Word8
.
unpackFromList8 :: Packable format => format -> [Word8] -> Maybe (Value format) Source
Unpack a value from a list of Word8
.