From dbd2a11e2de7955c2d27234707409efead367a6d Mon Sep 17 00:00:00 2001 From: Burt P Date: Sun, 7 Jul 2019 16:30:39 -0500 Subject: [PATCH] struct info free/no-free sets --- hardinfo/info.c | 36 ++++++++++++++++++++---------------- includes/info.h | 22 ++++++++++++++-------- 2 files changed, 34 insertions(+), 24 deletions(-) diff --git a/hardinfo/info.c b/hardinfo/info.c index 57e67ce65..b5156a575 100644 --- a/hardinfo/info.c +++ b/hardinfo/info.c @@ -40,7 +40,7 @@ void info_group_add_fieldsv(struct InfoGroup *group, va_list ap) while (1) { struct InfoField field = va_arg(ap, struct InfoField); - if (!field.name) + if (!info_field_get_name(&field)) break; g_array_append_val(group->fields, field); } @@ -83,8 +83,7 @@ struct InfoField info_field_printf(const gchar *name, const gchar *format, ...) return (struct InfoField) { .name = name, - .value = value, - .free_value_on_flatten = TRUE, + .value = value }; } @@ -201,25 +200,24 @@ static void flatten_group(GString *output, const struct InfoGroup *group, guint gchar tag[256] = ""; field = g_array_index(group->fields, struct InfoField, i); + const gchar *tp = info_field_get_tag(&field); + gboolean tagged = !!tp; - if (field.tag) - strncpy(tag, field.tag, 255); - else + if (!tp) { snprintf(tag, 255, "ITEM%d-%d", group_count, i); + tp = tag; + } - if (*tag != 0 || field.highlight || field.report_details) + if (tagged || field.highlight || field.report_details) g_string_append_printf(output, "$%s%s%s$", field.highlight ? "*" : "", field.report_details ? "!" : "", - tag); + tp); - g_string_append_printf(output, "%s=%s\n", field.name, field.value); - - if (field.free_value_on_flatten) - g_free((gchar *)field.value); - if (field.free_name_on_flatten) - g_free((gchar *)field.name); + g_string_append_printf(output, "%s=%s\n", info_field_get_name(&field), info_field_get_value(&field)); + g_free(field.name); + g_free(field.value); g_free(field.tag); } } else if (group->computed) { @@ -245,8 +243,14 @@ static void flatten_shell_param(GString *output, const struct InfoGroup *group, } if (field.icon) { - g_string_append_printf(output, "Icon$ITEM%d-%d$=%s\n", - group_count, i, field.icon); + gchar tag[256] = ""; + const gchar *tp = info_field_get_tag(&field); + if (!tp) { + snprintf(tag, 255, "ITEM%d-%d", group_count, i); + tp = tag; + } + g_string_append_printf(output, "Icon$%s$=%s\n", + tp, info_field_get_icon(&field) ); } } } diff --git a/includes/info.h b/includes/info.h index 1ac16dd52..82c7783fc 100644 --- a/includes/info.h +++ b/includes/info.h @@ -57,17 +57,18 @@ struct InfoGroup { }; struct InfoField { - const gchar *name; - const gchar *value; - const gchar *icon; - gchar *tag; /* moreinfo() lookup tag */ + gchar *name; + const gchar *name_const; + gchar *value; + const gchar *value_const; + gchar *icon; + const gchar *icon_const; + gchar *tag; + const gchar *tag_const; int update_interval; gboolean highlight; /* select in GUI, highlight in report (flag:*) */ gboolean report_details; /* show moreinfo() in report (flag:!) */ - - gboolean free_name_on_flatten; - gboolean free_value_on_flatten; }; struct Info *info_new(void); @@ -88,12 +89,17 @@ struct InfoField info_field_printf(const gchar *name, const gchar *format, ...) info_field_full(.name = (n), .value = (v), __VA_ARGS__) #define info_field_update(n, ui, ...) \ - info_field_full(.name = (n), .value = "...", .update_interval = (ui), \ + info_field_full(.name = (n), .value_const = "...", .update_interval = (ui), \ __VA_ARGS__) #define info_field_last() \ (struct InfoField) {} +#define info_field_get_name(fp) ((fp)->name ? (fp)->name : (fp)->name_const) +#define info_field_get_value(fp) ((fp)->value ? (fp)->value : (fp)->value_const) +#define info_field_get_tag(fp) ((fp)->tag ? (fp)->tag : (fp)->tag_const) +#define info_field_get_icon(fp) ((fp)->icon ? (fp)->icon : (fp)->icon_const) + void info_set_column_title(struct Info *info, const gchar *column, const gchar *title); void info_set_column_headers_visible(struct Info *info, gboolean setting); void info_set_zebra_visible(struct Info *info, gboolean setting);