diff --git a/plugins/sysinfo/format.c b/plugins/sysinfo/format.c index 311e7cdc..4cef68c8 100644 --- a/plugins/sysinfo/format.c +++ b/plugins/sysinfo/format.c @@ -88,3 +88,81 @@ sysinfo_format_disk (guint64 total, guint64 free) g_free (used_fmt); return ret; } + +char * +sysinfo_format_usage (guint64 total, guint64 available) +{ + guint64 used; + char *used_fmt; + char *total_fmt; + char *result; + double percent; + + if (total == 0) + { + return NULL; + } + + if (available > total) + { + available = total; + } + used = total - available; + percent = ((double) used / (double) total) * 100.0; + + used_fmt = g_format_size_full (used, G_FORMAT_SIZE_IEC_UNITS); + total_fmt = g_format_size_full (total, G_FORMAT_SIZE_IEC_UNITS); + result = g_strdup_printf ("%s / %s used (%.0f%%)", used_fmt, total_fmt, percent); + g_free (used_fmt); + g_free (total_fmt); + return result; +} + +char * +sysinfo_join_ptr_array (GPtrArray *items, const char *separator) +{ + GString *joined; + guint i; + + if (!items || items->len == 0) + { + return NULL; + } + + joined = g_string_new (NULL); + for (i = 0; i < items->len; i++) + { + const char *item = g_ptr_array_index (items, i); + + if (i != 0) + { + g_string_append (joined, separator); + } + g_string_append (joined, item); + } + + return g_string_free (joined, FALSE); +} + +void +sysinfo_ptr_array_add_unique (GPtrArray *items, const char *value, gboolean ignore_case) +{ + guint i; + + if (!value || value[0] == '\0') + { + return; + } + + for (i = 0; i < items->len; i++) + { + const char *existing = g_ptr_array_index (items, i); + if (ignore_case ? g_ascii_strcasecmp (existing, value) == 0 : + g_strcmp0 (existing, value) == 0) + { + return; + } + } + + g_ptr_array_add (items, g_strdup (value)); +} diff --git a/plugins/sysinfo/format.h b/plugins/sysinfo/format.h index 0da474d3..a84e1d35 100644 --- a/plugins/sysinfo/format.h +++ b/plugins/sysinfo/format.h @@ -24,5 +24,8 @@ char *sysinfo_format_uptime(gint64 uptime); char *sysinfo_format_memory(guint64 totalmem, guint64 freemem); char *sysinfo_format_disk(guint64 total, guint64 free); +char *sysinfo_format_usage (guint64 total, guint64 available); +char *sysinfo_join_ptr_array (GPtrArray *items, const char *separator); +void sysinfo_ptr_array_add_unique (GPtrArray *items, const char *value, gboolean ignore_case); #endif diff --git a/plugins/sysinfo/unix/modern-backend.c b/plugins/sysinfo/unix/modern-backend.c index da804ddb..e3e99742 100644 --- a/plugins/sysinfo/unix/modern-backend.c +++ b/plugins/sysinfo/unix/modern-backend.c @@ -48,8 +48,8 @@ static gsize sound_initialized = 0; static char *read_trimmed_file (const char *path); static gboolean read_hex_file (const char *path, guint *value); -static char *join_ptr_array (GPtrArray *items, const char *separator); -static void ptr_array_add_unique (GPtrArray *items, const char *value); +#define join_ptr_array sysinfo_join_ptr_array +#define ptr_array_add_unique(items, value) sysinfo_ptr_array_add_unique ((items), (value), FALSE) static char *read_os_pretty_name (void); static char *read_cpu_model (void); static guint count_physical_cores (void); @@ -60,7 +60,7 @@ static void ensure_os_snapshot (void); static void ensure_cpu_snapshot (void); static void ensure_pci_snapshot (void); static void ensure_sound_snapshot (void); -static char *format_usage (guint64 total, guint64 available); +#define format_usage sysinfo_format_usage static char *read_driver_name (const char *interface_path); static char * @@ -110,54 +110,6 @@ read_hex_file (const char *path, guint *value) return TRUE; } -static char * -join_ptr_array (GPtrArray *items, const char *separator) -{ - GString *joined; - guint i; - - if (!items || items->len == 0) - { - return NULL; - } - - joined = g_string_new (NULL); - for (i = 0; i < items->len; i++) - { - const char *item = g_ptr_array_index (items, i); - - if (i != 0) - { - g_string_append (joined, separator); - } - g_string_append (joined, item); - } - - return g_string_free (joined, FALSE); -} - -static void -ptr_array_add_unique (GPtrArray *items, const char *value) -{ - guint i; - - if (!value || value[0] == '\0') - { - return; - } - - for (i = 0; i < items->len; i++) - { - const char *existing = g_ptr_array_index (items, i); - if (g_strcmp0 (existing, value) == 0) - { - return; - } - } - - g_ptr_array_add (items, g_strdup (value)); -} - static char * unquote_os_release_value (const char *value) { @@ -727,35 +679,6 @@ ensure_sound_snapshot (void) } } -static char * -format_usage (guint64 total, guint64 available) -{ - guint64 used; - char *used_fmt; - char *total_fmt; - char *result; - double percent; - - if (total == 0) - { - return NULL; - } - - if (available > total) - { - available = total; - } - used = total - available; - percent = ((double) used / (double) total) * 100.0; - - used_fmt = g_format_size_full (used, G_FORMAT_SIZE_IEC_UNITS); - total_fmt = g_format_size_full (total, G_FORMAT_SIZE_IEC_UNITS); - result = g_strdup_printf ("%s / %s used (%.0f%%)", used_fmt, total_fmt, percent); - g_free (used_fmt); - g_free (total_fmt); - return result; -} - char * sysinfo_backend_get_os (void) { diff --git a/plugins/sysinfo/win32/backend.c b/plugins/sysinfo/win32/backend.c index c12b6699..7ae96349 100644 --- a/plugins/sysinfo/win32/backend.c +++ b/plugins/sysinfo/win32/backend.c @@ -69,9 +69,9 @@ static void ensure_os_snapshot (void); static void ensure_cpu_snapshot (void); static void ensure_gpu_snapshot (void); static void ensure_sound_snapshot (void); -static char *format_usage (guint64 total, guint64 available); -static char *join_ptr_array (GPtrArray *items, const char *separator); -static void ptr_array_add_unique (GPtrArray *items, const char *value); +#define format_usage sysinfo_format_usage +#define join_ptr_array sysinfo_join_ptr_array +#define ptr_array_add_unique(items, value) sysinfo_ptr_array_add_unique ((items), (value), TRUE) static gboolean adapter_looks_virtual (const char *name); static char * @@ -329,49 +329,6 @@ build_cpu_string (void) return result; } -static char * -join_ptr_array (GPtrArray *items, const char *separator) -{ - GString *joined; - guint i; - - if (!items || items->len == 0) - { - return NULL; - } - - joined = g_string_new (NULL); - for (i = 0; i < items->len; i++) - { - if (i != 0) - { - g_string_append (joined, separator); - } - g_string_append (joined, g_ptr_array_index (items, i)); - } - return g_string_free (joined, FALSE); -} - -static void -ptr_array_add_unique (GPtrArray *items, const char *value) -{ - guint i; - - if (!value || value[0] == '\0') - { - return; - } - - for (i = 0; i < items->len; i++) - { - if (g_ascii_strcasecmp (g_ptr_array_index (items, i), value) == 0) - { - return; - } - } - g_ptr_array_add (items, g_strdup (value)); -} - static char * build_gpu_string (void) { @@ -524,34 +481,6 @@ ensure_sound_snapshot (void) } } -static char * -format_usage (guint64 total, guint64 available) -{ - guint64 used; - char *used_fmt; - char *total_fmt; - char *result; - double percent; - - if (total == 0) - { - return NULL; - } - - if (available > total) - { - available = total; - } - used = total - available; - percent = ((double) used / (double) total) * 100.0; - used_fmt = g_format_size_full (used, G_FORMAT_SIZE_IEC_UNITS); - total_fmt = g_format_size_full (total, G_FORMAT_SIZE_IEC_UNITS); - result = g_strdup_printf ("%s / %s used (%.0f%%)", used_fmt, total_fmt, percent); - g_free (used_fmt); - g_free (total_fmt); - return result; -} - char * sysinfo_backend_get_os (void) {