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.
This commit is contained in:
Benjamin Berg 2022-05-24 19:32:29 +02:00
parent 66eb9b9d7a
commit 190d12e292
6 changed files with 87 additions and 77 deletions

View file

@ -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,

View file

@ -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;
}

View file

@ -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,

View file

@ -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"' ],

51
src/up-common.c Normal file
View file

@ -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';
}

23
src/up-common.h Normal file
View file

@ -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);