diff options
| author | Leandro Pereira <leandro@hardinfo.org> | 2017-08-15 02:25:45 -0700 | 
|---|---|---|
| committer | Leandro Pereira <leandro@hardinfo.org> | 2017-08-15 02:27:43 -0700 | 
| commit | 4e457596d9faacfd6334f48b79705449129aced7 (patch) | |
| tree | 2efa8c47c0dcf92a1c16ebb07b27af841597f808 | |
| parent | 6e8c143f4086f27d3ab9ce233e7a3a08ce948c1d (diff) | |
Add info_field_printf() to reduce some sturct Info field boilerplate
| -rw-r--r-- | hardinfo/info.c | 21 | ||||
| -rw-r--r-- | includes/info.h | 8 | 
2 files changed, 26 insertions, 3 deletions
diff --git a/hardinfo/info.c b/hardinfo/info.c index 1537881f..ef64a420 100644 --- a/hardinfo/info.c +++ b/hardinfo/info.c @@ -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, @@ -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) {}; @@ -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); diff --git a/includes/info.h b/includes/info.h index 78ee935c..253c06e0 100644 --- a/includes/info.h +++ b/includes/info.h @@ -45,9 +45,11 @@ 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); @@ -55,7 +57,9 @@ 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);  | 
