Skip to content

Commit

Permalink
Add info_field_printf() to reduce some sturct Info field boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
lpereira committed Aug 15, 2017
1 parent 6e8c143 commit 4e45759
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
21 changes: 20 additions & 1 deletion hardinfo/info.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void info_add_group(struct Info *info, const gchar *group_name, ...)
g_array_append_val(info->groups, group);
}

struct InfoField info_field(const gchar *name, const gchar *value)
struct InfoField info_field(const gchar *name, gchar *value)
{
return (struct InfoField) {
.name = name,
Expand All @@ -73,6 +73,22 @@ struct InfoField info_field_update(const gchar *name, int update_interval)
};
}

struct InfoField info_field_printf(const gchar *name, const gchar *format, ...)
{
gchar *value;
va_list ap;

va_start(ap, format);
value = g_strdup_vprintf(format, ap);
va_end(ap);

return (struct InfoField) {
.name = name,
.value = value,
.free_value_on_flatten = TRUE,
};
}

struct InfoField info_field_last(void)
{
return (struct InfoField) {};
Expand Down Expand Up @@ -141,6 +157,9 @@ static void flatten_group(GString *output, const struct InfoGroup *group)
field = g_array_index(group->fields, struct InfoField, i);

g_string_append_printf(output, "%s=%s\n", field.name, field.value);

if (field.free_value_on_flatten)
g_free(field.value);
}
} else if (group->computed) {
g_string_append_printf(output, "%s\n", group->computed);
Expand Down
8 changes: 6 additions & 2 deletions includes/info.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,21 @@ struct InfoGroup {

struct InfoField {
const gchar *name;
const gchar *value;
gchar *value;

int update_interval;

gboolean free_value_on_flatten;
};

struct Info *info_new(void);

void info_add_group(struct Info *info, const gchar *group_name, ...);
void info_add_computed_group(struct Info *info, const gchar *name, const gchar *value);

struct InfoField info_field(const gchar *name, const gchar *value);
struct InfoField info_field(const gchar *name, gchar *value);
struct InfoField info_field_printf(const gchar *name, const gchar *format, ...)
__attribute__((format(printf, 2, 3)));
struct InfoField info_field_update(const gchar *name, int update_interval);
struct InfoField info_field_last(void);

Expand Down

0 comments on commit 4e45759

Please sign in to comment.