/*
 * zSerializer.c
 * 
 * The Zen API 
 * Copyright (C) 2008-2009 by LMI Technologies Inc.
 * 
 * Licensed under The MIT License.
 * Redistributions of files must retain the above copyright notice.
 */
#include <z/zzSerializer.h>
#include <stdlib.h>
#include <memory.h>

ZAPI_EXPORT(zStatus) zSerializer_Construct(zSerializer* serializer, zSerializerReadCallback read, zSerializerWriteCallback write, zHandle context)
{
    zSerializerClass *obj = zNULL; 
    
    zCHECK(zCalloc(&obj, 1, sizeof(zSerializerClass))); 

    obj->read = read;
    obj->write = write;
    obj->context = context;

    zHANDLE(serializer, obj); 
       
    return zOK;
}

ZAPI_EXPORT(zStatus) zSerializer_Destroy(zSerializer serializer)
{
    zSerializerClass* obj = zHANDLE_POINTER(serializer);

    if (!zIS_NULL(obj))
    {
        zCHECK(zFree(obj)); 
    }
        
    return zOK;
}

#if defined(ZAPI_BIG_ENDIAN)

//not yet supported 

#else

ZAPI_EXPORT(zStatus) zSerializer_Read8u(zSerializer serializer, z8u* data)
{
    zSerializerClass* obj = zHANDLE_POINTER(serializer);
    return obj->read(obj->context, data, sizeof(z8u));
}

ZAPI_EXPORT(zStatus) zSerializer_Read8s(zSerializer serializer, z8s* data)
{
    zSerializerClass* obj = zHANDLE_POINTER(serializer);
    return obj->read(obj->context, data, sizeof(z8s));
}

ZAPI_EXPORT(zStatus) zSerializer_Read16u(zSerializer serializer, z16u* data)
{
    zSerializerClass* obj = zHANDLE_POINTER(serializer);
    return obj->read(obj->context, data, sizeof(z16u));
}

ZAPI_EXPORT(zStatus) zSerializer_Read16s(zSerializer serializer, z16s* data)
{
    zSerializerClass* obj = zHANDLE_POINTER(serializer);
    return obj->read(obj->context, data, sizeof(z16s));
}

ZAPI_EXPORT(zStatus) zSerializer_Read32u(zSerializer serializer, z32u* data)
{
    zSerializerClass* obj = zHANDLE_POINTER(serializer);
    return obj->read(obj->context, data, sizeof(z32u));
}

ZAPI_EXPORT(zStatus) zSerializer_Read32s(zSerializer serializer, z32s* data)
{
    zSerializerClass* obj = zHANDLE_POINTER(serializer);
    return obj->read(obj->context, data, sizeof(z32s));
}

ZAPI_EXPORT(zStatus) zSerializer_Read64u(zSerializer serializer, z64u* data)
{
    zSerializerClass* obj = zHANDLE_POINTER(serializer);
    return obj->read(obj->context, data, sizeof(z64u));
}

ZAPI_EXPORT(zStatus) zSerializer_Read64s(zSerializer serializer, z64s* data)
{
    zSerializerClass* obj = zHANDLE_POINTER(serializer);
    return obj->read(obj->context, data, sizeof(z64s));
}

ZAPI_EXPORT(zStatus) zSerializer_Read32f(zSerializer serializer, z32f* data)
{
    zSerializerClass* obj = zHANDLE_POINTER(serializer);
    return obj->read(obj->context, data, sizeof(z32f));
}

ZAPI_EXPORT(zStatus) zSerializer_Read64f(zSerializer serializer, z64f* data)
{
    zSerializerClass* obj = zHANDLE_POINTER(serializer);
    return obj->read(obj->context, data, sizeof(z64f));
}

ZAPI_EXPORT(zStatus) zSerializer_Read(zSerializer serializer, void* data, zUIntPtr count)
{
    zSerializerClass* obj = zHANDLE_POINTER(serializer);
    return obj->read(obj->context, data, count);
}

ZAPI_EXPORT(zStatus) zSerializer_ReadArray(zSerializer serializer, zType type, void* data, z32u count)
{
    zSerializerClass* obj = zHANDLE_POINTER(serializer);
    zUIntPtr size = zType_Size(type); 
    return obj->read(obj->context, data, size*count);
}

ZAPI_EXPORT(zStatus) zSerializer_Write8u(zSerializer serializer, z8u data)
{
    zSerializerClass* obj = zHANDLE_POINTER(serializer);
    return obj->write(obj->context, &data, sizeof(z8u));
}

ZAPI_EXPORT(zStatus) zSerializer_Write8s(zSerializer serializer, z8s data)
{
    zSerializerClass* obj = zHANDLE_POINTER(serializer);
    return obj->write(obj->context, &data, sizeof(z8s));
}

ZAPI_EXPORT(zStatus) zSerializer_Write16u(zSerializer serializer, z16u data)
{
    zSerializerClass* obj = zHANDLE_POINTER(serializer);
    return obj->write(obj->context, &data, sizeof(z16u));
}

ZAPI_EXPORT(zStatus) zSerializer_Write16s(zSerializer serializer, z16s data)
{
    zSerializerClass* obj = zHANDLE_POINTER(serializer);
    return obj->write(obj->context, &data, sizeof(z16s));
}
ZAPI_EXPORT(zStatus) zSerializer_Write32u(zSerializer serializer, z32u data)
{
    zSerializerClass* obj = zHANDLE_POINTER(serializer);
    return obj->write(obj->context, &data, sizeof(z32u));
}

ZAPI_EXPORT(zStatus) zSerializer_Write32s(zSerializer serializer, z32s data)
{
    zSerializerClass* obj = zHANDLE_POINTER(serializer);
    return obj->write(obj->context, &data, sizeof(z32s));
}

ZAPI_EXPORT(zStatus) zSerializer_Write64u(zSerializer serializer, z64u data)
{
    zSerializerClass* obj = zHANDLE_POINTER(serializer);
    return obj->write(obj->context, &data, sizeof(z64u));
}

ZAPI_EXPORT(zStatus) zSerializer_Write64s(zSerializer serializer, z64s data)
{
    zSerializerClass* obj = zHANDLE_POINTER(serializer);
    return obj->write(obj->context, &data, sizeof(z64s));
}

ZAPI_EXPORT(zStatus) zSerializer_Write32f(zSerializer serializer, z32f data)
{
    zSerializerClass* obj = zHANDLE_POINTER(serializer);
    return obj->write(obj->context, &data, sizeof(z32f));
}

ZAPI_EXPORT(zStatus) zSerializer_Write64f(zSerializer serializer, z64f data)
{
    zSerializerClass* obj = zHANDLE_POINTER(serializer);
    return obj->write(obj->context, &data, sizeof(z64f));
}

ZAPI_EXPORT(zStatus) zSerializer_Write(zSerializer serializer, const void* data, zUIntPtr count)
{
    zSerializerClass* obj = zHANDLE_POINTER(serializer);
    return obj->write(obj->context, data, count);
}

ZAPI_EXPORT(zStatus) zSerializer_WriteArray(zSerializer serializer, zType type, const void* data, z32u count)
{
    zSerializerClass* obj = zHANDLE_POINTER(serializer);
    zUIntPtr size = zType_Size(type); 
    return obj->write(obj->context, data, size*count);
}

#endif      /* #if defined(ZAPI_BIG_ENDIAN) */
