int amqpvalue_set_list_item_count()

in src/amqpvalue.c [1255:1349]


int amqpvalue_set_list_item_count(AMQP_VALUE value, uint32_t list_size)
{
    int result;

    /* Codes_SRS_AMQPVALUE_01_155: [If the value argument is NULL, amqpvalue_set_list_item_count shall return a non-zero value.] */
    if (value == NULL)
    {
        LogError("NULL list value");
        result = MU_FAILURE;
    }
    else
    {
        AMQP_VALUE_DATA* value_data = (AMQP_VALUE_DATA*)value;
        if (value_data->type != AMQP_TYPE_LIST)
        {
            /* Codes_SRS_AMQPVALUE_01_156: [If the value is not of type list, then amqpvalue_set_list_item_count shall return a non-zero value.] */
            LogError("Value is not of type LIST");
            result = MU_FAILURE;
        }
        else
        {
            if (value_data->value.list_value.count < list_size)
            {
                AMQP_VALUE* new_list;

                /* Codes_SRS_AMQPVALUE_01_152: [amqpvalue_set_list_item_count shall resize an AMQP list.] */
                size_t realloc_size = safe_multiply_size_t(list_size, sizeof(AMQP_VALUE));
                if (realloc_size == SIZE_MAX ||
                    (new_list = (AMQP_VALUE*)realloc(value_data->value.list_value.items, realloc_size)) == NULL)
                {
                    /* Codes_SRS_AMQPVALUE_01_154: [If allocating memory for the list according to the new size fails, then amqpvalue_set_list_item_count shall return a non-zero value, while preserving the existing list contents.] */
                    LogError("Could not reallocate list memory, size:%zu", realloc_size);
                    result = MU_FAILURE;
                }
                else
                {
                    uint32_t i;
                    value_data->value.list_value.items = new_list;

                    /* Codes_SRS_AMQPVALUE_01_162: [When a list is grown a null AMQP_VALUE shall be inserted as new list items to fill the list up to the new size.] */
                    for (i = value_data->value.list_value.count; i < list_size; i++)
                    {
                        new_list[i] = amqpvalue_create_null();
                        if (new_list[i] == NULL)
                        {
                            LogError("Could not create NULL AMQP value to be inserted in list");
                            break;
                        }
                    }

                    if (i < list_size)
                    {
                        /* Codes_SRS_AMQPVALUE_01_154: [If allocating memory for the list according to the new size fails, then amqpvalue_set_list_item_count shall return a non-zero value, while preserving the existing list contents.] */
                        uint32_t j;
                        for (j = value_data->value.list_value.count; j < i; j++)
                        {
                            amqpvalue_destroy(new_list[j]);
                        }

                        result = MU_FAILURE;
                    }
                    else
                    {
                        value_data->value.list_value.count = list_size;

                        /* Codes_SRS_AMQPVALUE_01_153: [On success amqpvalue_set_list_item_count shall return 0.] */
                        result = 0;
                    }
                }
            }
            else if (value_data->value.list_value.count > list_size)
            {
                uint32_t i;

                /* Codes_SRS_AMQPVALUE_01_161: [When the list is shrunk, the extra items shall be freed by using amqp_value_destroy.] */
                for (i = list_size; i < value_data->value.list_value.count; i++)
                {
                    amqpvalue_destroy(value_data->value.list_value.items[i]);
                }

                value_data->value.list_value.count = list_size;

                /* Codes_SRS_AMQPVALUE_01_153: [On success amqpvalue_set_list_item_count shall return 0.] */
                result = 0;
            }
            else
            {
                /* Codes_SRS_AMQPVALUE_01_153: [On success amqpvalue_set_list_item_count shall return 0.] */
                result = 0;
            }
        }
    }

    return result;
}