eina_value_blob: nicer convertion from string, array and blob.

SVN revision: 67154
This commit is contained in:
Gustavo Sverzut Barbieri 2012-01-12 19:39:16 +00:00
parent c486211f59
commit 32392d38f8
2 changed files with 121 additions and 11 deletions

View File

@ -3694,18 +3694,84 @@ _eina_value_type_blob_convert_from(const Eina_Value_Type *type, const Eina_Value
Eina_Value_Blob desc;
char *buf;
buf = malloc(convert->value_size);
if (!buf)
{
eina_error_set(EINA_ERROR_OUT_OF_MEMORY);
return EINA_FALSE;
}
if (!eina_value_type_pget(convert, convert_mem, buf))
return EINA_FALSE;
desc.ops = EINA_VALUE_BLOB_OPERATIONS_MALLOC;
desc.memory = buf;
desc.size = convert->value_size;
if ((convert == EINA_VALUE_TYPE_STRING) ||
(convert == EINA_VALUE_TYPE_STRINGSHARE))
{
const char *str = *(const char **)convert_mem;
if (!str)
{
desc.size = 0;
desc.memory = NULL;
}
else
{
desc.size = strlen(str) + 1;
desc.memory = buf = malloc(desc.size);
if (!desc.memory)
{
eina_error_set(EINA_ERROR_OUT_OF_MEMORY);
return EINA_FALSE;
}
memcpy(buf, str, desc.size);
}
}
else if (convert == EINA_VALUE_TYPE_ARRAY)
{
const Eina_Value_Array *a = convert_mem;
if ((!a->array) || (a->array->len == 0))
{
desc.size = 0;
desc.memory = NULL;
}
else
{
desc.size = a->array->len * a->array->member_size;
desc.memory = buf = malloc(desc.size);
if (!desc.memory)
{
eina_error_set(EINA_ERROR_OUT_OF_MEMORY);
return EINA_FALSE;
}
memcpy(buf, a->array->members, desc.size);
}
}
else if (convert == EINA_VALUE_TYPE_BLOB)
{
const Eina_Value_Blob *b = convert_mem;
if (b->size == 0)
{
desc.size = 0;
desc.memory = NULL;
}
else
{
desc.size = b->size;
desc.memory = buf = malloc(desc.size);
if (!desc.memory)
{
eina_error_set(EINA_ERROR_OUT_OF_MEMORY);
return EINA_FALSE;
}
memcpy(buf, b->memory, desc.size);
}
}
else
{
desc.size = convert->value_size;
desc.memory = buf = malloc(convert->value_size);
if (!desc.memory)
{
eina_error_set(EINA_ERROR_OUT_OF_MEMORY);
return EINA_FALSE;
}
if (!eina_value_type_pget(convert, convert_mem, buf))
{
free(buf);
return EINA_FALSE;
}
}
return eina_value_type_pset(type, type_mem, &desc);
}

View File

@ -1488,6 +1488,50 @@ START_TEST(eina_value_test_blob)
eina_value_flush(&other);
fail_unless(eina_value_setup(&other, EINA_VALUE_TYPE_STRING));
fail_unless(eina_value_set(&other, "hi there!"));
fail_unless(eina_value_convert(&other, value));
fail_unless(eina_value_get(value, &out));
fail_unless(out.memory != NULL);
fail_unless(out.size == sizeof("hi there!"));
fail_unless(strcmp(out.memory, "hi there!") == 0);
str = eina_value_to_string(value);
fail_unless(str != NULL);
fail_unless(strcmp(str, "BLOB(10, [68 69 20 74 68 65 72 65 21 00])") == 0);
free(str);
eina_value_flush(&other);
fail_unless(eina_value_array_setup(&other, EINA_VALUE_TYPE_CHAR, 0));
fail_unless(eina_value_array_append(&other, 0xa));
fail_unless(eina_value_array_append(&other, 0xb));
fail_unless(eina_value_array_append(&other, 0xc));
fail_unless(eina_value_convert(&other, value));
fail_unless(eina_value_get(value, &out));
fail_unless(out.memory != NULL);
fail_unless(out.size == 3);
str = eina_value_to_string(value);
fail_unless(str != NULL);
fail_unless(strcmp(str, "BLOB(3, [0a 0b 0c])") == 0);
free(str);
eina_value_flush(&other);
fail_unless(eina_value_setup(&other, EINA_VALUE_TYPE_BLOB));
fail_unless(eina_value_set(&other, in));
fail_unless(eina_value_convert(value, &other));
fail_unless(eina_value_get(&other, &out));
fail_unless(out.memory != NULL);
fail_unless(out.size == 3);
str = eina_value_to_string(&other);
fail_unless(str != NULL);
fail_unless(strcmp(str, "BLOB(3, [0a 0b 0c])") == 0);
free(str);
eina_value_flush(&other);
eina_value_free(value);
eina_shutdown();