int umocktypes_copy()

in src/umocktypes.c [424:483]


int umocktypes_copy(const char* type, void* destination, const void* source)
{
    int result;

    if ((type == NULL) ||
        (destination == NULL) ||
        (source == NULL))
    {
        /* Codes_SRS_UMOCKTYPES_01_027: [ If any of the arguments is NULL, umocktypes_copy shall return -1. ]*/
        UMOCK_LOG("Could not copy type, bad arguments: type = %p, destination = %p, source = %p.\r\n", type, destination, source);
        result = -1;
    }
    else if (umocktypes_state != UMOCKTYPES_STATE_INITIALIZED)
    {
        /* Codes_SRS_UMOCKTYPES_01_047: [ If umocktypes_copy is called when the module is not initialized, umocktypes_copy shall fail and return a non zero value. ]*/
        UMOCK_LOG("Could not copy type, umock_c_types not initialized.\r\n");
        result = -1;
    }
    else
    {
        /* Codes_SRS_UMOCKTYPES_01_037: [ Before looking it up, the type string shall be normalized by calling umocktypename_normalize. ]*/
        char* normalized_type = umocktypename_normalize(type);
        if (normalized_type == NULL)
        {
            /* Codes_SRS_UMOCKTYPES_01_042: [ If normalizing the typename fails, umocktypes_copy shall fail and return a non-zero value. ]*/
            UMOCK_LOG("Could not copy type, normalizing type %s failed.\r\n", type);
            result = -1;
        }
        else
        {
            size_t normalized_type_length = strlen(normalized_type);
            UMOCK_VALUE_TYPE_HANDLERS* value_type_handlers = get_value_type_handlers(normalized_type);

            /* Codes_SRS_UMOCK_C_LIB_01_153: [ If no custom handler has beed registered for a pointer type, it shall be trated as void*. ] */
            if ((value_type_handlers == NULL) && (normalized_type[normalized_type_length - 1] == '*'))
            {
                /* Codes_SRS_UMOCKTYPES_01_065: [ If type is a pointer type and type was not registered then umocktypes_copy shall execute as if type is void*. ]*/
                value_type_handlers = get_value_type_handlers("void*");
            }

            if (value_type_handlers == NULL)
            {
                /* Codes_SRS_UMOCKTYPES_01_029: [ If type can not be found in the registered types list maintained by the module, umocktypes_copy shall fail and return -1. ]*/
                UMOCK_LOG("Could not copy type, type %s not registered.\r\n", normalized_type);
                result = __LINE__;
            }
            else
            {
                /* Codes_SRS_UMOCKTYPES_01_026: [ The copy shall be done by calling the underlying copy function (passed in umocktypes_register_type) for the type identified by the type argument. ]*/
                /* Codes_SRS_UMOCKTYPES_01_052: [ On success, umocktypes_copy shall return 0. ]*/
                /* Codes_SRS_UMOCKTYPES_01_028: [ If the underlying copy fails, umocktypes_copy shall return -1. ]*/
                result = value_type_handlers->copy_func(destination, source);
            }

            umockalloc_free(normalized_type);
        }
    }

    return result;
}