
#include <stdlib.h>
#include <string.h>

#include "type.h"
#include "basictypes.h"

Type type_declaration_Int = { "Int", basictypes_serializeInt, basictypes_deserializeInt };
Type type_declaration_String = { "String", basictypes_serializeString, basictypes_deserializeString };

bool
basictypes_serializeInt (void *field, SerializationInfo *info)
{
    dynTstring *string = dynFinit();
    int *data = (int *) field;

    dynFappend_print (string, "%d", *data);

    serializationinfo_addValue (info, "value", string);

    return (true);
}

bool
basictypes_deserializeInt (void *field, SerializationInfo *info)
{
    int result;
    dynTstring *string;
    int *int_field = (int *) field;
    char *endptr;

    string = serializationinfo_getValue (info, "value");

    result = strtol (dynFgetstr (string), &endptr, 10);

    if (endptr == NULL)
    {
        return (false);
    }
    else
    {
        *int_field = result;

        return (true);
    }
}

bool
basictypes_serializeString (void *field, SerializationInfo *info)
{
    dynTstring *string = dynFinit();
    char **char_field = (char **) field;

    dynFsappend (string, *char_field);

    serializationinfo_addValue (info, "value", string);

    return (true);
}

bool
basictypes_deserializeString (void *field, SerializationInfo *info)
{
    char **char_field = (char **) field;
    dynTstring *string;

    string = serializationinfo_getValue (info, "value");

    *char_field = strdup (dynFgetstr (string));

    return (true);
}
