lib: rename plural file names to singular
[babeltrace.git] / plugins / utils / trimmer / trimmer.c
index b938bd51f5932d9582092921a9818a78b6db7c84..21dc871a9eba14c638703a26ea52aad6da09dd8f 100644 (file)
  * SOFTWARE.
  */
 
-#include <babeltrace/plugin/plugin-dev.h>
-#include <babeltrace/graph/component.h>
-#include <babeltrace/graph/private-component.h>
-#include <babeltrace/graph/component-filter.h>
-#include <babeltrace/graph/notification.h>
-#include <babeltrace/graph/notification-iterator.h>
-#include <babeltrace/graph/private-notification-iterator.h>
-#include <babeltrace/graph/private-component-filter.h>
-#include <babeltrace/graph/notification-event.h>
+#define BT_LOG_TAG "PLUGIN-UTILS-TRIMMER-FLT"
+#include "logging.h"
+
+#include <babeltrace/compat/utc-internal.h>
+#include <babeltrace/babeltrace.h>
 #include <plugins-common.h>
 #include "trimmer.h"
 #include "iterator.h"
-#include <assert.h>
+#include <babeltrace/assert-internal.h>
 
 static
 void destroy_trimmer_data(struct trimmer *trimmer)
@@ -49,19 +45,12 @@ void destroy_trimmer_data(struct trimmer *trimmer)
 static
 struct trimmer *create_trimmer_data(void)
 {
-       struct trimmer *trimmer;
-
-       trimmer = g_new0(struct trimmer, 1);
-       if (!trimmer) {
-               goto end;
-       }
-end:
-       return trimmer;
+       return g_new0(struct trimmer, 1);
 }
 
-void finalize_trimmer(struct bt_private_component *component)
+void finalize_trimmer(struct bt_self_component *component)
 {
-       void *data = bt_private_component_get_user_data(component);
+       void *data = bt_self_component_get_user_data(component);
 
        destroy_trimmer_data(data);
 }
@@ -83,12 +72,25 @@ void finalize_trimmer(struct bt_private_component *component)
  *   ss
  */
 static
-int timestamp_from_arg(const char *arg, struct trimmer *trimmer,
-               struct trimmer_bound *result_bound, bt_bool gmt)
+int timestamp_from_param(const char *param_name, struct bt_value *param,
+               struct trimmer *trimmer, struct trimmer_bound *result_bound,
+               bt_bool gmt)
 {
        int ret;
        int64_t value;
        unsigned int year, month, day, hh, mm, ss, ns;
+       const char *arg;
+
+       if (bt_value_is_integer(param)) {
+               (void) bt_value_integer_get(param, &value);
+               goto set;
+       } else if (!bt_value_is_string(param)) {
+               BT_LOGE("`%s` parameter must be an integer or a string value.",
+                       param_name);
+               goto error;
+       }
+
+       (void) bt_value_string_get(param, &arg);
 
        /* YYYY-MM-DD hh:mm:ss.ns */
        ret = sscanf(arg, "%u-%u-%u %u:%u:%u.%u",
@@ -106,7 +108,7 @@ int timestamp_from_arg(const char *arg, struct trimmer *trimmer,
                time_t result;
 
                if (gmt) {
-                       result = timegm(&tm);
+                       result = bt_timegm(&tm);
                        if (result < 0) {
                                return -1;
                        }
@@ -133,7 +135,7 @@ int timestamp_from_arg(const char *arg, struct trimmer *trimmer,
        if (ret == 4) {
                if (!trimmer->date) {
                        /* We don't know which day until we get an event. */
-                       result_bound->lazy_values.hh = hh;
+                       result_bound->lazy_value.hh = hh;
                        result_bound->lazy_values.mm = mm;
                        result_bound->lazy_values.ss = ss;
                        result_bound->lazy_values.ns = ns;
@@ -152,7 +154,7 @@ int timestamp_from_arg(const char *arg, struct trimmer *trimmer,
                        time_t result;
 
                        if (gmt) {
-                               result = timegm(&tm);
+                               result = bt_timegm(&tm);
                                if (result < 0) {
                                        return -1;
                                }
@@ -200,7 +202,7 @@ int timestamp_from_arg(const char *arg, struct trimmer *trimmer,
                };
 
                if (gmt) {
-                       value = timegm(&tm);
+                       value = bt_timegm(&tm);
                        if (value < 0) {
                                return -1;
                        }
@@ -225,7 +227,7 @@ int timestamp_from_arg(const char *arg, struct trimmer *trimmer,
        if (ret == 3) {
                if (!trimmer->date) {
                        /* We don't know which day until we get an event. */
-                       result_bound->lazy_values.hh = hh;
+                       result_bound->lazy_value.hh = hh;
                        result_bound->lazy_values.mm = mm;
                        result_bound->lazy_values.ss = ss;
                        result_bound->lazy_values.ns = 0;
@@ -244,7 +246,7 @@ int timestamp_from_arg(const char *arg, struct trimmer *trimmer,
                        time_t result;
 
                        if (gmt) {
-                               result = timegm(&tm);
+                               result = bt_timegm(&tm);
                                if (result < 0) {
                                        return -1;
                                }
@@ -274,6 +276,7 @@ int timestamp_from_arg(const char *arg, struct trimmer *trimmer,
                goto set;
        }
 
+error:
        /* Not found. */
        return -1;
 
@@ -295,57 +298,46 @@ enum bt_component_status init_from_params(struct trimmer *trimmer,
        bt_bool gmt = BT_FALSE;
        enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
 
-       assert(params);
+       BT_ASSERT(params);
 
         value = bt_value_map_get(params, "clock-gmt");
        if (value) {
                enum bt_value_status value_ret;
 
-               value_ret = bt_value_bool_get(value, &gmt);
-               if (value_ret) {
-                       ret = BT_COMPONENT_STATUS_INVALID;
-                       printf_error("Failed to retrieve clock-gmt value. Expecting a boolean");
-               }
+               gmt = bt_value_bool_get(value);
        }
-       bt_put(value);
        if (ret != BT_COMPONENT_STATUS_OK) {
                goto end;
        }
 
+       BT_OBJECT_PUT_REF_AND_RESET(value);
         value = bt_value_map_get(params, "begin");
        if (value) {
-               enum bt_value_status value_ret;
-               const char *str;
-
-               value_ret = bt_value_string_get(value, &str);
-               if (value_ret || timestamp_from_arg(str,
+               if (timestamp_from_param("begin", value,
                                trimmer, &trimmer->begin, gmt)) {
+                       BT_LOGE_STR("Failed to convert `begin` parameter to a timestamp.");
                        ret = BT_COMPONENT_STATUS_INVALID;
-                       printf_error("Failed to retrieve begin value. Expecting a timestamp string");
+                       goto end;
                }
        }
-       bt_put(value);
-       if (ret != BT_COMPONENT_STATUS_OK) {
-               goto end;
-       }
 
+       BT_OBJECT_PUT_REF_AND_RESET(value);
         value = bt_value_map_get(params, "end");
        if (value) {
-               enum bt_value_status value_ret;
-               const char *str;
-
-               value_ret = bt_value_string_get(value, &str);
-               if (value_ret || timestamp_from_arg(str,
+               if (timestamp_from_param("end", value,
                                trimmer, &trimmer->end, gmt)) {
+                       BT_LOGE_STR("Failed to convert `end` parameter to a timestamp.");
                        ret = BT_COMPONENT_STATUS_INVALID;
-                       printf_error("Failed to retrieve end value. Expecting a timestamp string");
+                       goto end;
                }
        }
-       bt_put(value);
+
 end:
+       bt_object_put_ref(value);
+
        if (trimmer->begin.set && trimmer->end.set) {
                if (trimmer->begin.value > trimmer->end.value) {
-                       printf_error("Unexpected: time range begin value is above end value");
+                       BT_LOGE_STR("Unexpected: time range begin value is above end value");
                        ret = BT_COMPONENT_STATUS_INVALID;
                }
        }
@@ -353,12 +345,11 @@ end:
 }
 
 enum bt_component_status trimmer_component_init(
-       struct bt_private_component *component, struct bt_value *params,
+       struct bt_self_component *component, struct bt_value *params,
        UNUSED_VAR void *init_method_data)
 {
        enum bt_component_status ret;
        struct trimmer *trimmer = create_trimmer_data();
-       struct bt_private_port *priv_port = NULL;
 
        if (!trimmer) {
                ret = BT_COMPONENT_STATUS_NOMEM;
@@ -366,27 +357,25 @@ enum bt_component_status trimmer_component_init(
        }
 
        /* Create input and output ports */
-       priv_port = bt_private_component_filter_add_input_private_port(
-               component, "in", NULL);
-       if (!priv_port) {
+       ret = bt_self_component_filter_add_input_port(
+               component, "in", NULL, NULL);
+       if (ret != BT_COMPONENT_STATUS_OK) {
                goto error;
        }
 
-       bt_put(priv_port);
-       priv_port = bt_private_component_filter_add_output_private_port(
-               component, "out", NULL);
-       if (!priv_port) {
+       ret = bt_self_component_filter_add_output_port(
+               component, "out", NULL, NULL);
+       if (ret != BT_COMPONENT_STATUS_OK) {
                goto error;
        }
 
-       ret = bt_private_component_set_user_data(component, trimmer);
+       ret = bt_self_component_set_user_data(component, trimmer);
        if (ret != BT_COMPONENT_STATUS_OK) {
                goto error;
        }
 
        ret = init_from_params(trimmer, params);
 end:
-       bt_put(priv_port);
        return ret;
 error:
        destroy_trimmer_data(trimmer);
This page took 0.027641 seconds and 4 git commands to generate.