int umocktypes_register_alias_type()

in src/umocktypes.c [191:276]


int umocktypes_register_alias_type(const char* type, const char* is_type)
{
    int result;

    /* Codes_SRS_UMOCKTYPES_01_055: [ If any of the arguments is NULL, umocktypes_register_alias_type shall fail and return a non-zero value. ]*/
    if ((type == NULL) || (is_type == NULL))
    {
        UMOCK_LOG("Could not register alias type, bad arguments: type = %p, is_type = %p.\r\n", type, is_type);
        result = __LINE__;
    }
    else if (umocktypes_state != UMOCKTYPES_STATE_INITIALIZED)
    {
        /* Codes_SRS_UMOCKTYPES_01_061: [ If umocktypes_register_alias_type is called when the module is not initialized, umocktypes_register_type shall fail and return a non zero value. ]*/
        UMOCK_LOG("Could not register alias type, umock_c_types not initialized.\r\n");
        result = __LINE__;
    }
    else
    {
        /* Codes_SRS_UMOCKTYPES_01_058: [ Before looking it up, is_type shall be normalized by using umocktypename_normalize. ] */
        char* normalized_is_type = umocktypename_normalize(is_type);
        if (normalized_is_type == NULL)
        {
            /* Codes_SRS_UMOCKTYPES_01_060: [ If umocktypename_normalize fails, umocktypes_register_alias_type shall fail and return a non-zero value. ]*/
            UMOCK_LOG("Could not register alias type, normalizing type %s failed.\r\n", is_type);
            result = __LINE__;
        }
        else
        {
            UMOCK_VALUE_TYPE_HANDLERS* new_type_handlers = (UMOCK_VALUE_TYPE_HANDLERS*)umockalloc_realloc(type_handlers, sizeof(UMOCK_VALUE_TYPE_HANDLERS) * (type_handler_count + 1));
            if (new_type_handlers == NULL)
            {
                UMOCK_LOG("Could not register alias type, failed allocating memory.\r\n");
                result = __LINE__;
            }
            else
            {
                UMOCK_VALUE_TYPE_HANDLERS* value_type_handlers;
                type_handlers = new_type_handlers;
                value_type_handlers = get_value_type_handlers(normalized_is_type);
                if (value_type_handlers == NULL)
                {
                    /* Codes_SRS_UMOCKTYPES_01_057: [ If is_type was not already registered, umocktypes_register_alias_type shall fail and return a non-zero value. ]*/
                    UMOCK_LOG("Could not register alias type, type %s was not previously registered.\r\n", normalized_is_type);
                    result = __LINE__;
                }
                else
                {
                    /* Codes_SRS_UMOCKTYPES_01_059: [ Before adding it as alias, type shall be normalized by using umocktypename_normalize. ]*/
                    char* normalized_type = umocktypename_normalize(type);
                    if (normalized_type == NULL)
                    {
                        /* Codes_SRS_UMOCKTYPES_01_060: [ If umocktypename_normalize fails, umocktypes_register_alias_type shall fail and return a non-zero value. ]*/
                        UMOCK_LOG("Could not register alias type, normalizing type %s failed.\r\n", type);
                        result = __LINE__;
                    }
                    else
                    {
                        if (strcmp(normalized_type, normalized_is_type) == 0)
                        {
                            umockalloc_free(normalized_type);

                            /* Codes_SRS_UMOCKTYPES_01_062: [ If type and is_type are the same, umocktypes_register_alias_type shall succeed and return 0. ]*/
                            result = 0;
                        }
                        else
                        {
                            type_handlers[type_handler_count].type = normalized_type;
                            type_handlers[type_handler_count].stringify_func = value_type_handlers->stringify_func;
                            type_handlers[type_handler_count].copy_func = value_type_handlers->copy_func;
                            type_handlers[type_handler_count].free_func = value_type_handlers->free_func;
                            type_handlers[type_handler_count].are_equal_func = value_type_handlers->are_equal_func;
                            type_handler_count++;

                            /* Codes_SRS_UMOCKTYPES_01_054: [ On success, umocktypes_register_alias_type shall return 0. ]*/
                            result = 0;
                        }
                    }
                }
            }

            umockalloc_free(normalized_is_type);
        }
    }

    return result;
}