Tny
A simple data serializer in C
Data Structures | Defines | Typedefs | Enumerations | Functions | Variables
/home/nop/Eclipse-CDT/workspace/Tny/src/tny/tny.h File Reference
#include <stddef.h>
#include <stdint.h>

Go to the source code of this file.

Data Structures

union  tnyHostOrder
struct  _Tny
 Tny is the main type. Every Tny-document consists of chained Tny-elements. More...

Defines

#define HOST_ORDER   (tnyHostOrder.value)

Typedefs

typedef struct _Tny Tny
 Tny is the main type. Every Tny-document consists of chained Tny-elements.

Enumerations

enum  TnyEndianness { ORDER_LITTLE_ENDIAN = 0x03020100ul, ORDER_BIG_ENDIAN = 0x00010203ul }
enum  TnyType {
  TNY_NULL, TNY_ARRAY, TNY_DICT, TNY_OBJ,
  TNY_BIN, TNY_CHAR, TNY_INT32, TNY_INT64,
  TNY_DOUBLE
}
 TnyType contains every supported type. More...

Functions

TnyTny_add (Tny *prev, TnyType type, char *key, void *value, uint64_t size)
 Adds a new element after the prev element.
TnyTny_copy (size_t *docSizePtr, const Tny *src)
 Performs a deep copy of the src object.
void Tny_remove (Tny *tny)
 Removes an element or a complete document.
TnyTny_at (const Tny *tny, size_t index)
 Returns the element at position index.
TnyTny_get (const Tny *tny, const char *key)
 Returns the element with the specified key.
size_t Tny_dumps (const Tny *tny, void **data)
 Serializes a document.
TnyTny_loads (void *data, size_t length)
 Deserializes a serialized document.
int Tny_hasNext (const Tny *tny)
 Checks if there are more elements to fetch.
TnyTny_next (const Tny *tny)
 Returns the next element.
void Tny_free (Tny *tny)
 Frees the document.

Variables

union tnyHostOrder tnyHostOrder

Detailed Description

The Tny binary format looks like this (ABNF):

 {.txt}
        Document            =  (ArrayHeader *ArrayElement) / (DictionaryHeader *DictionaryElement)
        ArrayHeader         =  ArrayType NumberOfElements
        DictionaryHeader    =  DictionaryType NumberOfElements
        NumberOfElements    =  int32
        ; Dictionary element
        DictionaryElement   =  NullType   Key
        DictionaryElement   =/ ObjectType Key ObjectValue
        DictionaryElement   =/ BinaryType Key BinaryValue
        DictionaryElement   =/ CharType   Key CharValue
        DictionaryElement   =/ Int32Type  Key Int32Value
        DictionaryElement   =/ Int64Type  Key Int64Value
        DictionaryElement   =/ DoubleType Key DoubleValue
        Key                 =  int32 1*(%x01-FF) %x00
        ; Array element
        ArrayElement        =  NullType
        ArrayElement        =/ ObjectType       ObjectValue
        ArrayElement        =/ BinaryType       BinaryValue
        ArrayElement        =/ CharType         CharValue
        ArrayElement        =/ Int32Type        Int32Value
        ArrayElement        =/ Int64Type        Int64Value
        ArrayElement        =/ DoubleType       DoubleValue
        ; Types
        NullType            =  %x00
        ArrayType           =  %x01
        DictionaryType      =  %x02
        ObjectType          =  %x03
        BinaryType          =  %x04
        CharType            =  %x05
        Int32Type           =  %x06
        Int64Type           =  %x07
        DoubleType          =  %x08
        ; Values
        ObjectValue         =  Document
        BinaryValue         =  int32 *CharValue
        CharValue           =  %x00-FF
        Int32Value          =  int32
        Int64Value          =  int64
        DoubleValue         =  int64
        int32               =  4(%x00-FF)
        int64               =  8(%x00-FF)

Enumeration Type Documentation

enum TnyType

TnyType contains every supported type.

Enumerator:
TNY_NULL 

NULL type

TNY_ARRAY 

Document type: Array

TNY_DICT 

Document type: Dictionary

TNY_OBJ 

Object type: Used for sub documents.

TNY_BIN 

Binary type: Used for binary data (the size has to be specified).

TNY_CHAR 

Character type: Used for storing a single character.

TNY_INT32 

32 bit integer type.

TNY_INT64 

64 bit integer type.

TNY_DOUBLE 

Double-precision floating-point number.


Function Documentation

Tny* Tny_add ( Tny prev,
TnyType  type,
char *  key,
void *  value,
uint64_t  size 
)

Adds a new element after the prev element.

Parameters:
[in]previs the previous element. If prev is NULL, a new document will be created.
[in]typeis the type of the new element. If this is the first element you can either use TNY_ARRAY for an array or TNY_DICT for a dictionary.
[in]keyIf the document is of type TNY_DICT, the key must be set in every element. Otherwise key can be NULL.
[in]valueis the value of the new element. If the type is TNY_OBJ a deep copy of value will be performed.
[in]sizeneeds only to be set if the element is of type TNY_BIN. Otherwise it can be 0.
Returns:
If the function succeeds it returns the new created element, otherwise NULL.
Tny* Tny_at ( const Tny tny,
size_t  index 
)

Returns the element at position index.

Works in a TNY_ARRAY and a TNY_DICT.

Parameters:
[in]tnyis the document or an element somewhere in the document.
[in]indexis the position of the element in the document tny.
Returns:
the element if the function succeeds, otherwise NULL.
Tny* Tny_copy ( size_t *  docSizePtr,
const Tny src 
)

Performs a deep copy of the src object.

Parameters:
[in]docSizePtris only needed for internal use of Tny and can be NULL.
[in]srcis the source document which will be copied.
Returns:
the newly copied element. If any error occurs, NULL is returned.
size_t Tny_dumps ( const Tny tny,
void **  data 
)

Serializes a document.

Serializes the document and stores the result in data. The memory needed for the serialization will get allocated.

Parameters:
[in]tnyis the document which shall be serialized.
[out]datais the position where the serialized document is copied to.
Returns:
the size in bytes of the serialized document. If the function fails, 0 is returned.
void Tny_free ( Tny tny)

Frees the document.

Parameters:
[in]tnyis the document which shall be free'd.
Tny* Tny_get ( const Tny tny,
const char *  key 
)

Returns the element with the specified key.

Works in a TNY_ARRAY and a TNY_DICT.

Parameters:
[in]tnyis the document or an element somewhere in the document.
[in]keyis the key of the element in the document tny.
Returns:
the element if the function succeeds, otherwise NULL.
int Tny_hasNext ( const Tny tny)

Checks if there are more elements to fetch.

Simple iterator function which makes it easy to walk through an Tny document.

Parameters:
[in]tnyis the current element.
Returns:
1 if there are more elements, otherwise 0.
Tny* Tny_loads ( void *  data,
size_t  length 
)

Deserializes a serialized document.

Parameters:
[in]datacontains the serialized document.
[in]lengthis the size in bytes of the serialized document.
Returns:
the deserialized document. If the function fails, NULL is returned.
Tny* Tny_next ( const Tny tny)

Returns the next element.

Parameters:
[in]tnyis the last element fetched by Tny_next.
Returns:
the next element in the list, otherwise NULL.
void Tny_remove ( Tny tny)

Removes an element or a complete document.

Parameters:
[in]tnyis the element which shall be deleted. If an element somewhere in the document is passed, the elements get deleted. But if you pass the beginning of a document (the element with the type TNY_ARRAY or TNY_DICT) then to entire document will be deleted.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator