Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[discussion] struct info free/no-free sets #411

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions hardinfo/info.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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
};
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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) );
}
}
}
Expand Down
22 changes: 14 additions & 8 deletions includes/info.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down