Implement bt_ctf_trace_get_byte_order()
[babeltrace.git] / formats / ctf / ir / trace.c
index a886b69afd6526b60e4e3be869219f0e7cbfd37f..fb897dabeea5e91ab804c9c1c4a06dcfde75dc21 100644 (file)
@@ -159,7 +159,7 @@ struct bt_ctf_stream *bt_ctf_trace_create_stream(struct bt_ctf_trace *trace,
 
                if (stream_id < 0) {
                        /* Try to assign a new stream id */
-                       if (bt_ctf_stream_class_set_id(stream->stream_class,
+                       if (_bt_ctf_stream_class_set_id(stream->stream_class,
                                trace->next_stream_id++)) {
                                goto error;
                        }
@@ -226,6 +226,7 @@ int bt_ctf_trace_add_environment_field(struct bt_ctf_trace *trace,
                goto error;
        }
 
+       var->type = BT_ENVIRONMENT_FIELD_TYPE_STRING;
        escaped_value = g_strescape(value, NULL);
        if (!escaped_value) {
                ret = -1;
@@ -233,9 +234,9 @@ int bt_ctf_trace_add_environment_field(struct bt_ctf_trace *trace,
        }
 
        var->name = g_string_new(name);
-       var->value = g_string_new(escaped_value);
+       var->value.string = g_string_new(escaped_value);
        g_free(escaped_value);
-       if (!var->name || !var->value) {
+       if (!var->name || !var->value.string) {
                ret = -1;
                goto error;
        }
@@ -248,14 +249,141 @@ error:
                g_string_free(var->name, TRUE);
        }
 
-       if (var && var->value) {
-               g_string_free(var->value, TRUE);
+       if (var && var->value.string) {
+               g_string_free(var->value.string, TRUE);
        }
 
        g_free(var);
        return ret;
 }
 
+int bt_ctf_trace_add_environment_field_integer(struct bt_ctf_trace *trace,
+               const char *name,
+               int64_t value)
+{
+       struct environment_variable *var = NULL;
+       int ret = 0;
+
+       if (!trace || !name) {
+               ret = -1;
+               goto error;
+       }
+
+       var = g_new0(struct environment_variable, 1);
+       if (!var) {
+               ret = -1;
+               goto error;
+       }
+
+       var->type = BT_ENVIRONMENT_FIELD_TYPE_INTEGER;
+       var->name = g_string_new(name);
+       var->value.integer = value;
+       if (!var->name) {
+               ret = -1;
+               goto error;
+       }
+
+       g_ptr_array_add(trace->environment, var);
+       return ret;
+
+error:
+       if (var && var->name) {
+               g_string_free(var->name, TRUE);
+       }
+
+       g_free(var);
+       return ret;
+}
+
+int bt_ctf_trace_get_environment_field_count(struct bt_ctf_trace *trace)
+{
+       int ret = 0;
+
+       if (!trace) {
+               ret = -1;
+               goto end;
+       }
+
+       ret = trace->environment->len;
+end:
+       return ret;
+}
+
+enum bt_environment_field_type
+bt_ctf_trace_get_environment_field_type(struct bt_ctf_trace *trace, int index)
+{
+       struct environment_variable *var;
+       enum bt_environment_field_type type = BT_ENVIRONMENT_FIELD_TYPE_UNKNOWN;
+
+       if (!trace || index < 0 || index >= trace->environment->len) {
+               goto end;
+       }
+
+       var = g_ptr_array_index(trace->environment, index);
+       type = var->type;
+end:
+       return type;
+}
+
+const char *
+bt_ctf_trace_get_environment_field_name(struct bt_ctf_trace *trace,
+               int index)
+{
+       struct environment_variable *var;
+       const char *ret = NULL;
+
+       if (!trace || index < 0 || index >= trace->environment->len) {
+               goto end;
+       }
+
+       var = g_ptr_array_index(trace->environment, index);
+       ret = var->name->str;
+end:
+       return ret;
+}
+
+const char *
+bt_ctf_trace_get_environment_field_value_string(struct bt_ctf_trace *trace,
+               int index)
+{
+       struct environment_variable *var;
+       const char *ret = NULL;
+
+       if (!trace || index < 0 || index >= trace->environment->len) {
+               goto end;
+       }
+
+       var = g_ptr_array_index(trace->environment, index);
+       if (var->type != BT_ENVIRONMENT_FIELD_TYPE_STRING) {
+               goto end;
+       }
+       ret = var->value.string->str;
+end:
+       return ret;
+}
+
+int
+bt_ctf_trace_get_environment_field_value_integer(struct bt_ctf_trace *trace,
+               int index, int64_t *value)
+{
+       struct environment_variable *var;
+       int ret = 0;
+
+       if (!trace || !value || index < 0 || index >= trace->environment->len) {
+               ret = -1;
+               goto end;
+       }
+
+       var = g_ptr_array_index(trace->environment, index);
+       if (var->type != BT_ENVIRONMENT_FIELD_TYPE_INTEGER) {
+               ret = -1;
+               goto end;
+       }
+       *value = var->value.integer;
+end:
+       return ret;
+}
+
 int bt_ctf_trace_add_clock(struct bt_ctf_trace *trace,
                struct bt_ctf_clock *clock)
 {
@@ -368,8 +496,18 @@ static
 void append_env_field_metadata(struct environment_variable *var,
                struct metadata_context *context)
 {
-       g_string_append_printf(context->string, "\t%s = \"%s\";\n",
-               var->name->str, var->value->str);
+       switch (var->type) {
+       case BT_ENVIRONMENT_FIELD_TYPE_STRING:
+               g_string_append_printf(context->string, "\t%s = \"%s\";\n",
+                       var->name->str, var->value.string->str);
+               break;
+       case BT_ENVIRONMENT_FIELD_TYPE_INTEGER:
+               g_string_append_printf(context->string, "\t%s = %" PRId64 ";\n",
+                       var->name->str, var->value.integer);
+               break;
+       default:
+               assert(0);
+       }
 }
 
 static
@@ -429,6 +567,28 @@ end:
        return metadata;
 }
 
+enum bt_ctf_byte_order bt_ctf_trace_get_byte_order(struct bt_ctf_trace *trace)
+{
+       enum bt_ctf_byte_order ret = BT_CTF_BYTE_ORDER_UNKNOWN;
+
+       if (!trace) {
+               goto end;
+       }
+
+       switch (trace->byte_order) {
+       case BIG_ENDIAN:
+               ret = BT_CTF_BYTE_ORDER_BIG_ENDIAN;
+               break;
+       case LITTLE_ENDIAN:
+               ret = BT_CTF_BYTE_ORDER_LITTLE_ENDIAN;
+               break;
+       default:
+               break;
+       }
+end:
+       return ret;
+}
+
 int bt_ctf_trace_set_byte_order(struct bt_ctf_trace *trace,
                enum bt_ctf_byte_order byte_order)
 {
@@ -467,9 +627,6 @@ int bt_ctf_trace_set_byte_order(struct bt_ctf_trace *trace,
        }
 
        trace->byte_order = internal_byte_order;
-       if (trace->packet_header_type) {
-               init_trace_packet_header(trace);
-       }
 end:
        return ret;
 }
@@ -585,17 +742,18 @@ int init_trace_packet_header(struct bt_ctf_trace *trace)
                goto end;
        }
 
-       bt_ctf_field_type_put(trace->packet_header_type);
-       trace->packet_header_type = trace_packet_header_type;
+       ret = bt_ctf_trace_set_packet_header_type(trace,
+               trace_packet_header_type);
+       if (ret) {
+               goto end;
+       }
 end:
        bt_ctf_field_type_put(uuid_array_type);
        bt_ctf_field_type_put(_uint32_t);
        bt_ctf_field_type_put(_uint8_t);
        bt_ctf_field_put(magic);
        bt_ctf_field_put(uuid_array);
-       if (ret) {
-               bt_ctf_field_type_put(trace_packet_header_type);
-       }
+       bt_ctf_field_type_put(trace_packet_header_type);
 
        return ret;
 }
@@ -604,6 +762,8 @@ static
 void environment_variable_destroy(struct environment_variable *var)
 {
        g_string_free(var->name, TRUE);
-       g_string_free(var->value, TRUE);
+       if (var->type == BT_ENVIRONMENT_FIELD_TYPE_STRING) {
+               g_string_free(var->value.string, TRUE);
+       }
        g_free(var);
 }
This page took 0.040468 seconds and 4 git commands to generate.