X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=src%2Fcommon%2Fcommon.h;h=8c74579b525ff1c38c751a3241d6c8e99dcbc4e5;hb=520cdc8260ea033f1c9f54b87d93fcb034ae8472;hp=809c2e5063556a3ec500d60694794a9bf8264ce4;hpb=d24d56638469189904fb6ddbb3c725817b3e9417;p=babeltrace.git diff --git a/src/common/common.h b/src/common/common.h index 809c2e50..8c74579b 100644 --- a/src/common/common.h +++ b/src/common/common.h @@ -32,16 +32,12 @@ #include #include #include +#include -#include -#include -#include -#include -#include +#include -#define __BT_FUNC_STATUS_ENABLE +#define __BT_IN_BABELTRACE_H #include -#undef __BT_FUNC_STATUS_ENABLE #include "common/assert.h" #include "common/macros.h" @@ -331,6 +327,30 @@ size_t bt_common_get_page_size(int log_level); BT_HIDDEN void bt_common_sep_digits(char *str, unsigned int digits_per_group, char sep); +/* + * This is similar to what the command `fold --spaces` does: it wraps + * the input lines of `str`, breaking at spaces, and indenting each line + * with `indent` spaces so that each line fits the total length + * `total_length`. + * + * If an original line in `str` contains a word which is >= the content + * length (`total_length - indent`), then the corresponding folded line + * is also larger than the content length. In other words, breaking at + * spaces is a best effort, but it might not be possible. + * + * The returned string, on success, is owned by the caller. + */ +BT_HIDDEN +GString *bt_common_fold(const char *str, unsigned int total_length, + unsigned int indent); + +/* + * Writes the terminal's width to `*width`, its height to `*height`, + * and returns 0 on success, or returns -1 on error. + */ +BT_HIDDEN +int bt_common_get_term_size(unsigned int *width, unsigned int *height); + /* * Wraps read() function to handle EINTR and partial reads. * On success, it returns `count` received as parameter. On error, it returns a @@ -405,8 +425,12 @@ const char *bt_common_field_class_type_string(enum bt_field_class_type class_typ return "BT_FIELD_CLASS_TYPE_STATIC_ARRAY"; case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY: return "BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY"; - case BT_FIELD_CLASS_TYPE_VARIANT: - return "BT_FIELD_CLASS_TYPE_VARIANT"; + case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR: + return "BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR"; + case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR: + return "BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR"; + case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR: + return "BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR"; default: return "(unknown)"; } @@ -560,8 +584,8 @@ const char *bt_common_logging_level_string( return "BT_LOGGING_LEVEL_DEBUG"; case BT_LOGGING_LEVEL_INFO: return "BT_LOGGING_LEVEL_INFO"; - case BT_LOGGING_LEVEL_WARN: - return "BT_LOGGING_LEVEL_WARN"; + case BT_LOGGING_LEVEL_WARNING: + return "BT_LOGGING_LEVEL_WARNING"; case BT_LOGGING_LEVEL_ERROR: return "BT_LOGGING_LEVEL_ERROR"; case BT_LOGGING_LEVEL_FATAL: @@ -577,16 +601,12 @@ static inline const char *bt_common_func_status_string(int status) { switch (status) { - case __BT_FUNC_STATUS_OVERFLOW: + case __BT_FUNC_STATUS_OVERFLOW_ERROR: return "OVERFLOW"; - case __BT_FUNC_STATUS_INVALID_PARAMS: - return "INVALID_PARAMS"; case __BT_FUNC_STATUS_INVALID_OBJECT: return "INVALID_OBJECT"; case __BT_FUNC_STATUS_MEMORY_ERROR: return "MEMORY_ERROR"; - case __BT_FUNC_STATUS_LOADING_ERROR: - return "LOADING_ERROR"; case __BT_FUNC_STATUS_ERROR: return "ERROR"; case __BT_FUNC_STATUS_OK: @@ -597,10 +617,8 @@ const char *bt_common_func_status_string(int status) return "NOT_FOUND"; case __BT_FUNC_STATUS_AGAIN: return "AGAIN"; - case __BT_FUNC_STATUS_UNSUPPORTED: - return "UNSUPPORTED"; - case __BT_FUNC_STATUS_CANCELED: - return "CANCELED"; + case __BT_FUNC_STATUS_INTERRUPTED: + return "INTERRUPTED"; default: return "(unknown)"; } @@ -697,6 +715,50 @@ end: return ret; } -#include +/* + * bt_g_string_append_printf cannot be inlined because it expects a + * variadic argument list. + */ +BT_HIDDEN +int bt_common_g_string_append_printf(GString *str, const char *fmt, ...); + +static inline +void bt_common_g_string_append(GString *str, const char *s) +{ + gsize len, allocated_len, s_len; + + /* str->len excludes \0. */ + len = str->len; + /* Exclude \0. */ + allocated_len = str->allocated_len - 1; + s_len = strlen(s); + if (G_UNLIKELY(allocated_len < len + s_len)) { + /* Resize. */ + g_string_set_size(str, len + s_len); + } else { + str->len = len + s_len; + } + memcpy(str->str + len, s, s_len + 1); +} + +static inline +void bt_common_g_string_append_c(GString *str, char c) +{ + gsize len, allocated_len, s_len; + + /* str->len excludes \0. */ + len = str->len; + /* Exclude \0. */ + allocated_len = str->allocated_len - 1; + s_len = 1; + if (G_UNLIKELY(allocated_len < len + s_len)) { + /* Resize. */ + g_string_set_size(str, len + s_len); + } else { + str->len = len + s_len; + } + str->str[len] = c; + str->str[len + 1] = '\0'; +} #endif /* BABELTRACE_COMMON_INTERNAL_H */