Fix: ir: bt_ctf_field_copy(): copy payload_set
[babeltrace.git] / formats / ctf / ir / event-fields.c
index 8bc0557b5f6b2e799bd6eb72e33cda14215b057a..f524d5dbb8ce2fef86b935a5d4e69226e4c1999d 100644 (file)
@@ -150,7 +150,7 @@ static
 int increase_packet_size(struct ctf_stream_pos *pos);
 
 static
-struct bt_ctf_field *(*field_create_funcs[])(
+struct bt_ctf_field *(* const field_create_funcs[])(
                struct bt_ctf_field_type *) = {
        [CTF_TYPE_INTEGER] = bt_ctf_field_integer_create,
        [CTF_TYPE_ENUM] = bt_ctf_field_enumeration_create,
@@ -164,7 +164,7 @@ struct bt_ctf_field *(*field_create_funcs[])(
 };
 
 static
-void (*field_destroy_funcs[])(struct bt_ctf_field *) = {
+void (* const field_destroy_funcs[])(struct bt_ctf_field *) = {
        [CTF_TYPE_INTEGER] = bt_ctf_field_integer_destroy,
        [CTF_TYPE_ENUM] = bt_ctf_field_enumeration_destroy,
        [CTF_TYPE_FLOAT] =
@@ -177,7 +177,7 @@ void (*field_destroy_funcs[])(struct bt_ctf_field *) = {
 };
 
 static
-int (*field_validate_funcs[])(struct bt_ctf_field *) = {
+int (* const field_validate_funcs[])(struct bt_ctf_field *) = {
        [CTF_TYPE_INTEGER] = bt_ctf_field_generic_validate,
        [CTF_TYPE_ENUM] = bt_ctf_field_enumeration_validate,
        [CTF_TYPE_FLOAT] = bt_ctf_field_generic_validate,
@@ -189,7 +189,7 @@ int (*field_validate_funcs[])(struct bt_ctf_field *) = {
 };
 
 static
-int (*field_reset_funcs[])(struct bt_ctf_field *) = {
+int (* const field_reset_funcs[])(struct bt_ctf_field *) = {
        [CTF_TYPE_INTEGER] = bt_ctf_field_generic_reset,
        [CTF_TYPE_ENUM] = bt_ctf_field_enumeration_reset,
        [CTF_TYPE_FLOAT] = bt_ctf_field_generic_reset,
@@ -201,7 +201,7 @@ int (*field_reset_funcs[])(struct bt_ctf_field *) = {
 };
 
 static
-int (*field_serialize_funcs[])(struct bt_ctf_field *,
+int (* const field_serialize_funcs[])(struct bt_ctf_field *,
                struct ctf_stream_pos *) = {
        [CTF_TYPE_INTEGER] = bt_ctf_field_integer_serialize,
        [CTF_TYPE_ENUM] = bt_ctf_field_enumeration_serialize,
@@ -215,7 +215,8 @@ int (*field_serialize_funcs[])(struct bt_ctf_field *,
 };
 
 static
-int (*field_copy_funcs[])(struct bt_ctf_field *, struct bt_ctf_field *) = {
+int (* const field_copy_funcs[])(struct bt_ctf_field *,
+               struct bt_ctf_field *) = {
        [CTF_TYPE_INTEGER] = bt_ctf_field_integer_copy,
        [CTF_TYPE_ENUM] = bt_ctf_field_enumeration_copy,
        [CTF_TYPE_FLOAT] = bt_ctf_field_floating_point_copy,
@@ -918,6 +919,72 @@ end:
        return ret;
 }
 
+int bt_ctf_field_string_append(struct bt_ctf_field *field,
+               const char *value)
+{
+       int ret = 0;
+       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);
+
+       if (string_field->payload) {
+               g_string_append(string_field->payload, value);
+       } else {
+               string_field->payload = g_string_new(value);
+       }
+
+       string_field->parent.payload_set = 1;
+
+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_append_len(string_field->payload, 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)
 {
@@ -1006,6 +1073,7 @@ struct bt_ctf_field *bt_ctf_field_copy(struct bt_ctf_field *field)
                goto end;
        }
 
+       copy->payload_set = field->payload_set;
        ret = field_copy_funcs[type_id](field, copy);
        if (ret) {
                bt_ctf_field_put(copy);
@@ -1287,7 +1355,9 @@ void bt_ctf_field_string_destroy(struct bt_ctf_field *field)
        }
 
        string = container_of(field, struct bt_ctf_field_string, parent);
-       g_string_free(string->payload, TRUE);
+       if (string->payload) {
+               g_string_free(string->payload, TRUE);
+       }
        g_free(string);
 }
 
This page took 0.024668 seconds and 4 git commands to generate.