/** * idmef_message_get_string: * @message: Pointer to an #idmef_message_t object. * @path: Path to retrieve the string from within @message. * @result: Pointer where the result should be stored. * * Retrieve the string stored within @path of @message and store it * in the user provided @result. * * The caller is responssible for freeing @result. * * Returns: A positive value in case @path was successfully retrieved * 0 if the path is empty, or a negative value if an error occured. */ int idmef_message_get_string(idmef_message_t *message, const char *path, char **result) { int ret; idmef_value_t *iv; prelude_string_t *str; ret = idmef_message_get_value(message, path, &iv); if ( ret <= 0 ) return ret; if ( idmef_value_get_type(iv) != IDMEF_VALUE_TYPE_STRING ) { ret = _idmef_value_cast(iv, IDMEF_VALUE_TYPE_STRING, 0); if ( ret < 0 ) goto err; } if ( ! (str = idmef_value_get_string(iv)) ) { ret = -1; goto err; } ret = prelude_string_get_string_released(str, result); err: idmef_value_destroy(iv); return ret; }
/** * idmef_message_get_string: * @message: Pointer to an #idmef_message_t object. * @path: Path to retrieve the string from within @message. * @result: Pointer where the result should be stored. * * Retrieve the string stored within @path of @message and store it * in the user provided @result. * * The caller is responssible for freeing @result. * * Returns: A positive value in case @path was successfully retrieved * 0 if the path is empty, or a negative value if an error occured. */ int idmef_message_get_string(idmef_message_t *message, const char *path, char **result) { int ret; idmef_value_t *iv; prelude_string_t *str; ret = idmef_message_get_value(message, path, &iv); if ( ret <= 0 ) return ret; if ( idmef_value_get_type(iv) != IDMEF_VALUE_TYPE_STRING ) { ret = _idmef_value_cast(iv, IDMEF_VALUE_TYPE_STRING, 0); if ( ret < 0 ) goto err; } if ( ! (str = idmef_value_get_string(iv)) ) { ret = -1; goto err; } if ( prelude_string_is_empty(str) ) { *result = NULL; return 0; } *result = strdup(prelude_string_get_string(str)); ret = prelude_string_get_len(str); err: idmef_value_destroy(iv); return ret; }
static void cast_int8(void) { idmef_value_t *value; assert(idmef_value_new_int8(&value, INT8_MIN) == 0); assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_UINT8, -1) < 0); assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_UINT16, -1) < 0); assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_UINT32, -1) < 0); assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_UINT64, -1) < 0); assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_INT16, -1) == 0); assert(idmef_value_get_type(value) == IDMEF_VALUE_TYPE_INT16); assert(idmef_value_get_int16(value) == INT8_MIN); idmef_value_destroy(value); assert(idmef_value_new_int8(&value, INT8_MIN) == 0); assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_INT32, -1) == 0); assert(idmef_value_get_type(value) == IDMEF_VALUE_TYPE_INT32); assert(idmef_value_get_int32(value) == INT8_MIN); idmef_value_destroy(value); assert(idmef_value_new_int8(&value, INT8_MIN) == 0); assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_INT64, -1) == 0); assert(idmef_value_get_type(value) == IDMEF_VALUE_TYPE_INT64); assert(idmef_value_get_int64(value) == INT8_MIN); idmef_value_destroy(value); assert(idmef_value_new_int8(&value, INT8_MIN) == 0); assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_FLOAT, -1) == 0); assert(idmef_value_get_type(value) == IDMEF_VALUE_TYPE_FLOAT); assert(idmef_value_get_float(value) == INT8_MIN); idmef_value_destroy(value); assert(idmef_value_new_int8(&value, INT8_MIN) == 0); assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_DOUBLE, -1) == 0); assert(idmef_value_get_type(value) == IDMEF_VALUE_TYPE_DOUBLE); assert(idmef_value_get_double(value) == INT8_MIN); idmef_value_destroy(value); assert(idmef_value_new_int8(&value, INT8_MIN) == 0); assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_DATA, -1) == 0); assert(idmef_value_get_type(value) == IDMEF_VALUE_TYPE_DATA); assert((int) idmef_data_get_uint32(idmef_value_get_data(value)) == INT8_MIN); idmef_value_destroy(value); }
/** * idmef_message_get_number: * @message: Pointer to an #idmef_message_t object. * @path: Path to retrieve the number from within @message. * @result: Pointer where the result should be stored. * * Retrieve the number stored within @path of @message and store it * in the user provided @result. * * Returns: A positive value in case @path was successfully retrieved * 0 if the path is empty, or a negative value if an error occured. */ int idmef_message_get_number(idmef_message_t *message, const char *path, double *result) { int ret; idmef_value_t *iv; ret = idmef_message_get_value(message, path, &iv); if ( ret <= 0 ) return ret; if ( idmef_value_get_type(iv) != IDMEF_VALUE_TYPE_DOUBLE ) { ret = _idmef_value_cast(iv, IDMEF_VALUE_TYPE_DOUBLE, 0); if ( ret < 0 ) goto err; } *result = idmef_value_get_double(iv); err: idmef_value_destroy(iv); return ret; }
static void cast_data(idmef_value_t *value) { assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_INT8, -1) < 0); assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_UINT8, -1) < 0); assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_INT16, -1) < 0); assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_UINT16, -1) < 0); assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_INT32, -1) < 0); assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_UINT32, -1) < 0); assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_INT64, -1) < 0); assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_UINT64, -1) < 0); assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_FLOAT, -1) < 0); assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_DOUBLE, -1) < 0); assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_TIME, -1) < 0); assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_ENUM, -1) < 0); assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_LIST, -1) < 0); assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_CLASS, -1) < 0); assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_STRING, -1) == 0); assert(idmef_value_get_type(value) == IDMEF_VALUE_TYPE_STRING); }
static void cast_string(void) { idmef_data_t *data; idmef_value_t *value; requiem_string_t *str; assert(requiem_string_new_ref(&str, "abcdefgh") == 0); assert(idmef_value_new_string(&value, str) == 0); idmef_value_dont_have_own_data(value); assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_INT8, -1) < 0); // assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_UINT8, -1) < 0); assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_INT16, -1) < 0); assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_UINT16, -1) < 0); assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_INT32, -1) < 0); assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_UINT32, -1) < 0); assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_INT64, -1) < 0); assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_UINT64, -1) < 0); assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_FLOAT, -1) < 0); assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_DOUBLE, -1) < 0); assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_TIME, -1) < 0); assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_ENUM, -1) < 0); assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_LIST, -1) < 0); assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_CLASS, -1) < 0); assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_DATA, -1) == 0); assert(idmef_value_get_type(value) == IDMEF_VALUE_TYPE_DATA); assert(data = idmef_value_get_data(value)); assert(idmef_data_get_len(data) == (requiem_string_get_len(str) + 1)); assert(memcmp(requiem_string_get_string(str), idmef_data_get_data(data), idmef_data_get_len(data)) == 0); requiem_string_destroy(str); cast_data(value); idmef_value_destroy(value); assert(requiem_string_new_ref(&str, "2008-01-01 20:42:31") == 0); assert(idmef_value_new_string(&value, str) == 0); assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_TIME, -1) == 0); idmef_value_destroy(value); }