From f98c6554b0e1dd37e0f992f7b5e67c4431f911a6 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Wed, 15 Apr 2015 17:20:25 -0400 Subject: [PATCH] ir: add bt_ctf_field_string_append_len() MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Philippe Proulx Signed-off-by: Jérémie Galarneau --- formats/ctf/ir/event-fields.c | 39 ++++++++++++++++++++++++ include/babeltrace/ctf-ir/event-fields.h | 22 +++++++++++++ 2 files changed, 61 insertions(+) diff --git a/formats/ctf/ir/event-fields.c b/formats/ctf/ir/event-fields.c index 92c06da8..c80c12fe 100644 --- a/formats/ctf/ir/event-fields.c +++ b/formats/ctf/ir/event-fields.c @@ -946,6 +946,45 @@ end: return ret; } +int bt_ctf_field_string_append_len(struct bt_ctf_field *field, + const char *value, unsigned int length) +{ + int i; + int ret = 0; + unsigned int effective_length = length; + struct bt_ctf_field_string *string_field; + + if (!field || !value || + bt_ctf_field_type_get_type_id(field->type) != + CTF_TYPE_STRING) { + ret = -1; + goto end; + } + + string_field = container_of(field, struct bt_ctf_field_string, parent); + + /* make sure no null bytes are appended */ + for (i = 0; i < length; ++i) { + if (value[i] == '\0') { + effective_length = i; + break; + } + } + + if (string_field->payload) { + g_string_insert_len(string_field->payload, -1, value, + effective_length); + } else { + string_field->payload = g_string_new_len(value, + effective_length); + } + + string_field->parent.payload_set = 1; + +end: + return ret; +} + BT_HIDDEN int bt_ctf_field_validate(struct bt_ctf_field *field) { diff --git a/include/babeltrace/ctf-ir/event-fields.h b/include/babeltrace/ctf-ir/event-fields.h index 489c2bf8..b102d632 100644 --- a/include/babeltrace/ctf-ir/event-fields.h +++ b/include/babeltrace/ctf-ir/event-fields.h @@ -304,6 +304,28 @@ extern int bt_ctf_field_string_set_value(struct bt_ctf_field *string_field, extern int bt_ctf_field_string_append(struct bt_ctf_field *string_field, const char *value); +/* + * bt_ctf_field_string_append_len: append a string of a given length to + * a string field's current value. + * + * Append a string of a given length to the current value of a string + * field. If the string field was never set using + * bt_ctf_field_string_set_value(), it is first set to an empty string, + * and then the concatenation happens. + * + * If a null byte is encountered before the given length, only the + * substring before the first null byte is appended. + * + * @param string_field String field instance. + * @param value String to append to the current string field's value. + * @param length Length of string value to append. + * + * Returns 0 on success, a negative value on error. + */ +extern int bt_ctf_field_string_append_len( + struct bt_ctf_field *string_field, const char *value, + unsigned int length); + /* * bt_ctf_field_get_type: get a field's type * -- 2.34.1