share formatting and array helpers

This commit is contained in:
deepend committed 2026-12-04 18:23:36 -06:00
1 parent 96117decd6
commit 3abcb33d4f
4 files changed
+87 -154

No files matched your search

+3 -80
View File
@@ -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)
{