ir: stream: add bt_ctf_stream_is_writer()
[babeltrace.git] / formats / ctf / ir / stream.c
index b3081de487749d101906d92e33438720b305f14e..5e9e133794232aeba637540998dd6afd7c84c4bd 100644 (file)
@@ -61,7 +61,7 @@ int set_packet_header_magic(struct bt_ctf_stream *stream)
                goto end;
        }
 
-       if (!bt_ctf_field_validate(magic_field)) {
+       if (bt_ctf_field_is_set(magic_field)) {
                /* Value already set. Not an error, skip. */
                goto end;
        }
@@ -113,7 +113,7 @@ int set_packet_header_uuid(struct bt_ctf_stream *stream)
                goto end;
        }
 
-       if (!bt_ctf_field_validate(uuid_field)) {
+       if (bt_ctf_field_is_set(uuid_field)) {
                /* Value already set. Not an error, skip. */
                goto end;
        }
@@ -186,7 +186,7 @@ int set_packet_header_stream_id(struct bt_ctf_stream *stream)
                goto end;
        }
 
-       if (!bt_ctf_field_validate(stream_id_field)) {
+       if (bt_ctf_field_is_set(stream_id_field)) {
                /* Value already set. Not an error, skip. */
                goto end;
        }
@@ -299,7 +299,8 @@ end:
 }
 
 struct bt_ctf_stream *bt_ctf_stream_create(
-               struct bt_ctf_stream_class *stream_class)
+               struct bt_ctf_stream_class *stream_class,
+               const char *name)
 {
        int ret;
        struct bt_ctf_stream *stream = NULL;
@@ -307,17 +308,17 @@ struct bt_ctf_stream *bt_ctf_stream_create(
        struct bt_ctf_writer *writer = NULL;
 
        if (!stream_class) {
-               goto end;
+               goto error;
        }
 
        trace = bt_ctf_stream_class_get_trace(stream_class);
        if (!trace) {
-               goto end;
+               goto error;
        }
 
        stream = g_new0(struct bt_ctf_stream, 1);
        if (!stream) {
-               goto end;
+               goto error;
        }
 
        bt_object_init(stream, bt_ctf_stream_destroy);
@@ -330,6 +331,13 @@ struct bt_ctf_stream *bt_ctf_stream_create(
        stream->stream_class = stream_class;
        stream->pos.fd = -1;
 
+       if (name) {
+               stream->name = g_string_new(name);
+               if (!stream->name) {
+                       goto error;
+               }
+       }
+
        if (trace->is_created_by_writer) {
                int fd;
                writer = (struct bt_ctf_writer *)
@@ -400,7 +408,6 @@ struct bt_ctf_stream *bt_ctf_stream_create(
        /* Add this stream to the trace's streams */
        g_ptr_array_add(trace->streams, stream);
 
-end:
        BT_PUT(trace);
        BT_PUT(writer);
        return stream;
@@ -556,16 +563,29 @@ int bt_ctf_stream_append_event(struct bt_ctf_stream *stream,
                goto end;
        }
 
+       /*
+        * The event is not supposed to have a parent stream at this
+        * point. The only other way an event can have a parent stream
+        * is if it was assigned when setting a packet to the event,
+        * in which case the packet's stream is not a writer stream,
+        * and thus the user is trying to append an event which belongs
+        * to another stream.
+        */
+       if (event->base.parent) {
+               ret = -1;
+               goto end;
+       }
+
        bt_object_set_parent(event, stream);
        ret = bt_ctf_event_populate_event_header(event);
        if (ret) {
-               goto end;
+               goto error;
        }
 
        /* Make sure the various scopes of the event are set */
        ret = bt_ctf_event_validate(event);
        if (ret) {
-               goto end;
+               goto error;
        }
 
        /* Save the new event and freeze it */
@@ -579,14 +599,17 @@ int bt_ctf_stream_append_event(struct bt_ctf_stream *stream,
         * longer needed.
         */
        bt_put(event->event_class);
+
 end:
-       if (ret) {
-               /*
-                * Orphan the event; we were not succesful in associating it to
-                * a stream.
-                */
-               bt_object_set_parent(event, NULL);
-       }
+       return ret;
+
+error:
+       /*
+        * Orphan the event; we were not successful in associating it to
+        * a stream.
+        */
+       bt_object_set_parent(event, NULL);
+
        return ret;
 }
 
@@ -613,7 +636,7 @@ int bt_ctf_stream_set_packet_context(struct bt_ctf_stream *stream,
        int ret = 0;
        struct bt_ctf_field_type *field_type;
 
-       if (!stream || !field || stream->pos.fd < 0) {
+       if (!stream || stream->pos.fd < 0) {
                ret = -1;
                goto end;
        }
@@ -626,9 +649,8 @@ int bt_ctf_stream_set_packet_context(struct bt_ctf_stream *stream,
        }
 
        bt_put(field_type);
-       bt_get(field);
        bt_put(stream->packet_context);
-       stream->packet_context = field;
+       stream->packet_context = bt_get(field);
 end:
        return ret;
 }
@@ -657,7 +679,7 @@ int bt_ctf_stream_set_packet_header(struct bt_ctf_stream *stream,
        struct bt_ctf_trace *trace = NULL;
        struct bt_ctf_field_type *field_type = NULL;
 
-       if (!stream || !field || stream->pos.fd < 0) {
+       if (!stream || stream->pos.fd < 0) {
                ret = -1;
                goto end;
        }
@@ -669,9 +691,8 @@ int bt_ctf_stream_set_packet_header(struct bt_ctf_stream *stream,
                goto end;
        }
 
-       bt_get(field);
        bt_put(stream->packet_header);
-       stream->packet_header = field;
+       stream->packet_header = bt_get(field);
 end:
        BT_PUT(trace);
        bt_put(field_type);
@@ -906,9 +927,13 @@ void bt_ctf_stream_destroy(struct bt_object *obj)
        if (stream->events) {
                g_ptr_array_free(stream->events, TRUE);
        }
+
+       if (stream->name) {
+               g_string_free(stream->name, TRUE);
+       }
+
        bt_put(stream->packet_header);
        bt_put(stream->packet_context);
-       bt_put(stream->event_context);
        g_free(stream);
 }
 
@@ -932,7 +957,7 @@ int set_structure_field_integer(struct bt_ctf_field *structure, char *name,
        }
 
        /* Make sure the payload has not already been set. */
-       if (!bt_ctf_field_validate(integer)) {
+       if (bt_ctf_field_is_set(integer)) {
                /* Payload already set, not an error */
                goto end;
        }
@@ -961,3 +986,31 @@ end:
        bt_put(field_type);
        return ret;
 }
+
+const char *bt_ctf_stream_get_name(struct bt_ctf_stream *stream)
+{
+       const char *name = NULL;
+
+       if (!stream) {
+               goto end;
+       }
+
+       name = stream->name ? stream->name->str : NULL;
+
+end:
+       return name;
+}
+
+int bt_ctf_stream_is_writer(struct bt_ctf_stream *stream)
+{
+       int ret = -1;
+
+       if (!stream) {
+               goto end;
+       }
+
+       ret = (stream->pos.fd >= 0);
+
+end:
+       return ret;
+}
This page took 0.039616 seconds and 4 git commands to generate.