diff options
author | Benjamin Berg <bberg@redhat.com> | 2022-05-24 19:32:29 +0200 |
---|---|---|
committer | Benjamin Berg <bberg@redhat.com> | 2022-05-25 15:32:29 +0200 |
commit | 190d12e2929ea40327c6dcae073ed582dcea9f66 (patch) | |
tree | 229723db19776710a9e837dfe7fb01fbe71b7d29 | |
parent | 66eb9b9d7a56b5956e4b9df1bbd391226fd1bccf (diff) | |
download | upower-190d12e2929ea40327c6dcae073ed582dcea9f66.tar.gz |
common: Add a file with common helper
This removes some code duplication between the linux and freebsd
backend. And, this file could become home to other small helper
functions in the future.
-rw-r--r-- | src/freebsd/up-device-supply.c | 6 | ||||
-rw-r--r-- | src/freebsd/up-util.c | 35 | ||||
-rw-r--r-- | src/linux/up-device-supply.c | 47 | ||||
-rw-r--r-- | src/meson.build | 2 | ||||
-rw-r--r-- | src/up-common.c | 51 | ||||
-rw-r--r-- | src/up-common.h | 23 |
6 files changed, 87 insertions, 77 deletions
diff --git a/src/freebsd/up-device-supply.c b/src/freebsd/up-device-supply.c index 08bf2fc..f3770d1 100644 --- a/src/freebsd/up-device-supply.c +++ b/src/freebsd/up-device-supply.c @@ -204,9 +204,9 @@ up_device_supply_battery_set_properties (UpDevice *device, UpAcpiNative *native) goto end; } - vendor = up_make_safe_string (battif.bif.oeminfo); - model = up_make_safe_string (battif.bif.model); - serial = up_make_safe_string (battif.bif.serial); + vendor = up_make_safe_string (g_strdup (battif.bif.oeminfo)); + model = up_make_safe_string (g_strdup (battif.bif.model)); + serial = up_make_safe_string (g_strdup (battif.bif.serial)); technology = up_device_supply_convert_device_technology (battif.bif.type); g_object_set (device, diff --git a/src/freebsd/up-util.c b/src/freebsd/up-util.c index 9da6f91..c257946 100644 --- a/src/freebsd/up-util.c +++ b/src/freebsd/up-util.c @@ -119,38 +119,3 @@ up_get_string_sysctl (GError **err, const gchar *format, ...) #endif } -/** - * up_util_make_safe_string: - * - * This is adapted from linux/up-device-supply.c. - **/ -gchar * -up_make_safe_string (const gchar *text) -{ - guint i; - guint idx = 0; - gchar *ret; - - /* no point in checking */ - if (text == NULL) - return NULL; - - ret = g_strdup (text); - - /* shunt up only safe chars */ - for (i = 0; ret[i] != '\0'; i++) { - if (g_ascii_isprint (ret[i])) { - /* only copy if the address is going to change */ - if (idx != i) - ret[idx] = ret[i]; - idx++; - } else { - g_debug ("invalid char 0x%02X", ret[i]); - } - } - - /* ensure null terminated */ - ret[idx] = '\0'; - - return ret; -} diff --git a/src/linux/up-device-supply.c b/src/linux/up-device-supply.c index 243f834..39907d0 100644 --- a/src/linux/up-device-supply.c +++ b/src/linux/up-device-supply.c @@ -35,6 +35,7 @@ #include "up-types.h" #include "up-constants.h" #include "up-device-supply.h" +#include "up-common.h" enum { PROP_0, @@ -407,38 +408,6 @@ out: return voltage; } -/** - * up_device_supply_make_safe_string: - **/ -static void -up_device_supply_make_safe_string (gchar *text) -{ - guint i; - guint idx = 0; - - /* no point checking */ - if (text == NULL) - return; - - if (g_utf8_validate (text, -1, NULL)) - return; - - /* shunt up only safe chars */ - for (i=0; text[i] != '\0'; i++) { - if (g_ascii_isprint (text[i])) { - /* only copy if the address is going to change */ - if (idx != i) - text[idx] = text[i]; - idx++; - } else { - g_debug ("invalid char: 0x%02X", text[i]); - } - } - - /* ensure null terminated */ - text[idx] = '\0'; -} - static gboolean up_device_supply_units_changed (UpDeviceSupply *supply, GUdevDevice *native) @@ -610,9 +579,9 @@ up_device_supply_refresh_battery (UpDeviceSupply *supply, serial_number = up_device_supply_get_string (native, "serial_number"); /* some vendors fill this with binary garbage */ - up_device_supply_make_safe_string (manufacturer); - up_device_supply_make_safe_string (model_name); - up_device_supply_make_safe_string (serial_number); + up_make_safe_string (manufacturer); + up_make_safe_string (model_name); + up_make_safe_string (serial_number); g_object_set (device, "vendor", manufacturer, @@ -898,8 +867,8 @@ up_device_supply_refresh_device (UpDeviceSupply *supply, serial_number = up_device_supply_get_string (native, "serial_number"); /* some vendors fill this with binary garbage */ - up_device_supply_make_safe_string (model_name); - up_device_supply_make_safe_string (serial_number); + up_make_safe_string (model_name); + up_make_safe_string (serial_number); g_object_set (device, "is-present", TRUE, @@ -990,8 +959,8 @@ up_device_supply_sibling_discovered (UpDevice *device, model_name = up_device_supply_get_string (input, "name"); serial_number = up_device_supply_get_string (input, "uniq"); - up_device_supply_make_safe_string (model_name); - up_device_supply_make_safe_string (serial_number); + up_make_safe_string (model_name); + up_make_safe_string (serial_number); g_object_set (device, "model", model_name, diff --git a/src/meson.build b/src/meson.build index 2e1edd1..f541f4c 100644 --- a/src/meson.build +++ b/src/meson.build @@ -41,6 +41,8 @@ upowerd_private = static_library('upowerd-private', 'up-history.c', 'up-backend.h', 'up-native.h', + 'up-common.h', + 'up-common.c', ], dependencies: [ upowerd_deps ], c_args: [ '-DG_LOG_DOMAIN="UPower"' ], diff --git a/src/up-common.c b/src/up-common.c new file mode 100644 index 0000000..27641a3 --- /dev/null +++ b/src/up-common.c @@ -0,0 +1,51 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * + * Copyright (C) 2009 Richard Hughes <richard@hughsie.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#include "up-common.h" +#include <glib.h> + +void +up_make_safe_string (char *text) +{ + guint i; + guint idx = 0; + + /* no point checking */ + if (text == NULL) + return; + + if (g_utf8_validate (text, -1, NULL)) + return; + + /* shunt up only safe chars */ + for (i=0; text[i] != '\0'; i++) { + if (g_ascii_isprint (text[i])) { + /* only copy if the address is going to change */ + if (idx != i) + text[idx] = text[i]; + idx++; + } else { + g_debug ("invalid char: 0x%02X", text[i]); + } + } + + /* ensure null terminated */ + text[idx] = '\0'; +} diff --git a/src/up-common.h b/src/up-common.h new file mode 100644 index 0000000..bda9cfd --- /dev/null +++ b/src/up-common.h @@ -0,0 +1,23 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * + * Copyright (C) 2009 Richard Hughes <richard@hughsie.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#pragma once + +void up_make_safe_string (char *text); |