printers: Use g_autoptr with strings

This commit is contained in:
Robert Ancell 2018-06-26 11:52:41 +12:00
parent 9a2c72e231
commit e9b9966e7d
15 changed files with 243 additions and 385 deletions

View file

@ -111,17 +111,15 @@ get_tag_value (const gchar *tag_string, const gchar *tag_name)
static gchar *
normalize (const gchar *input_string)
{
gchar *tmp = NULL;
gchar *res = NULL;
gchar *result = NULL;
gint i, j = 0, k = -1;
if (input_string)
{
tmp = g_strstrip (g_ascii_strdown (input_string, -1));
g_autofree gchar *tmp = g_strstrip (g_ascii_strdown (input_string, -1));
if (tmp)
{
res = g_new (gchar, 2 * strlen (tmp));
g_autofree gchar *res = g_new (gchar, 2 * strlen (tmp));
for (i = 0; i < strlen (tmp); i++)
{
@ -154,8 +152,6 @@ normalize (const gchar *input_string)
res[j] = '\0';
result = g_strdup (res);
g_free (tmp);
g_free (res);
}
}
@ -263,11 +259,11 @@ printer_rename (const gchar *old_name,
cups_dest_t *dest = NULL;
cups_job_t *jobs = NULL;
GDBusConnection *bus;
const char *printer_location = NULL;
const char *printer_info = NULL;
const char *printer_uri = NULL;
const char *device_uri = NULL;
const char *job_sheets = NULL;
const gchar *printer_location = NULL;
const gchar *printer_info = NULL;
const gchar *printer_uri = NULL;
const gchar *device_uri = NULL;
const gchar *job_sheets = NULL;
gboolean result = FALSE;
gboolean accepting = TRUE;
gboolean printer_paused = FALSE;
@ -275,16 +271,16 @@ printer_rename (const gchar *old_name,
gboolean printer_shared = FALSE;
g_autoptr(GError) error = NULL;
http_t *http;
gchar *ppd_link;
gchar *ppd_filename = NULL;
g_autofree gchar *ppd_link = NULL;
g_autofree gchar *ppd_filename = NULL;
gchar **sheets = NULL;
gchar **users_allowed = NULL;
gchar **users_denied = NULL;
gchar **member_names = NULL;
gchar *start_sheet = NULL;
gchar *end_sheet = NULL;
gchar *error_policy = NULL;
gchar *op_policy = NULL;
const gchar *start_sheet = NULL;
const gchar *end_sheet = NULL;
g_autofree gchar *error_policy = NULL;
g_autofree gchar *op_policy = NULL;
ipp_t *request;
ipp_t *response;
gint i;
@ -498,8 +494,6 @@ printer_rename (const gchar *old_name,
if (ppd_link)
{
g_unlink (ppd_link);
g_free (ppd_link);
g_free (ppd_filename);
}
num_dests = cupsGetDests (&dests);
@ -525,8 +519,6 @@ printer_rename (const gchar *old_name,
printer_set_accepting_jobs (old_name, accepting, NULL);
cupsFreeDests (num_dests, dests);
g_free (op_policy);
g_free (error_policy);
if (sheets)
g_strfreev (sheets);
if (users_allowed)
@ -1285,7 +1277,7 @@ get_ipp_attributes_func (gpointer user_data)
GIAData *data = (GIAData *) user_data;
ipp_t *request;
ipp_t *response = NULL;
gchar *printer_uri;
g_autofree gchar *printer_uri = NULL;
char **requested_attrs = NULL;
gint i, j, length = 0;
@ -1377,8 +1369,6 @@ get_ipp_attributes_func (gpointer user_data)
g_free (requested_attrs[i]);
g_free (requested_attrs);
g_free (printer_uri);
get_ipp_attributes_cb (data);
return NULL;
@ -1764,13 +1754,12 @@ get_ppds_attribute_func (gpointer user_data)
ppd_file_t *ppd_file;
ppd_attr_t *ppd_attr;
GPAData *data = (GPAData *) user_data;
gchar *ppd_filename;
gint i;
data->result = g_new0 (gchar *, g_strv_length (data->ppds_names) + 1);
for (i = 0; data->ppds_names[i]; i++)
{
ppd_filename = g_strdup (cupsGetServerPPD (CUPS_HTTP_DEFAULT, data->ppds_names[i]));
g_autofree gchar *ppd_filename = g_strdup (cupsGetServerPPD (CUPS_HTTP_DEFAULT, data->ppds_names[i]));
if (ppd_filename)
{
ppd_file = ppdOpenFile (ppd_filename);
@ -1784,7 +1773,6 @@ get_ppds_attribute_func (gpointer user_data)
}
g_unlink (ppd_filename);
g_free (ppd_filename);
}
}
@ -1943,8 +1931,6 @@ get_ppd_names_async_dbus_scb (GObject *source_object,
{
GVariantIter *iter;
GVariant *item;
gchar *driver;
gchar *match;
for (j = 0; j < G_N_ELEMENTS (match_levels) && n < data->count; j++)
{
@ -1954,8 +1940,10 @@ get_ppd_names_async_dbus_scb (GObject *source_object,
while ((item = g_variant_iter_next_value (iter)))
{
const gchar *driver, *match;
g_variant_get (item,
"(ss)",
"(&s&s)",
&driver,
&match);
@ -1980,8 +1968,6 @@ get_ppd_names_async_dbus_scb (GObject *source_object,
n++;
}
g_free (driver);
g_free (match);
g_variant_unref (item);
}
}
@ -2186,11 +2172,7 @@ get_device_attributes_async_dbus_cb (GObject *source_object,
if (data->device_uri)
{
gchar *key;
gchar *value;
gchar *number;
gchar *endptr;
gchar *suffix;
g_autofree gchar *suffix = NULL;
g_variant_get (devices_variant,
"a{ss}",
@ -2198,16 +2180,20 @@ get_device_attributes_async_dbus_cb (GObject *source_object,
while ((item = g_variant_iter_next_value (iter)))
{
const gchar *key, *value;
g_variant_get (item,
"{ss}",
"{&s&s}",
&key,
&value);
if (g_str_equal (value, data->device_uri))
{
number = g_strrstr (key, ":");
gchar *number = g_strrstr (key, ":");
if (number != NULL)
{
gchar *endptr;
number++;
index = g_ascii_strtoll (number, &endptr, 10);
if (index == 0 && endptr == (number))
@ -2215,8 +2201,6 @@ get_device_attributes_async_dbus_cb (GObject *source_object,
}
}
g_free (key);
g_free (value);
g_variant_unref (item);
}
@ -2228,11 +2212,10 @@ get_device_attributes_async_dbus_cb (GObject *source_object,
while ((item = g_variant_iter_next_value (iter)))
{
gchar *key;
gchar *value;
const gchar *key, *value;
g_variant_get (item,
"{ss}",
"{&s&s}",
&key,
&value);
@ -2249,12 +2232,8 @@ get_device_attributes_async_dbus_cb (GObject *source_object,
}
}
g_free (key);
g_free (value);
g_variant_unref (item);
}
g_free (suffix);
}
g_variant_unref (devices_variant);
@ -2631,14 +2610,6 @@ get_all_ppds_func (gpointer user_data)
ipp_t *request;
ipp_t *response;
GList *list;
const gchar *ppd_make_and_model;
const gchar *ppd_device_id;
const gchar *ppd_name;
const gchar *ppd_product;
const gchar *ppd_make;
gchar *mfg;
gchar *mfg_normalized;
gchar *mdl;
gchar *manufacturer_display_name;
gint i, j;
@ -2671,21 +2642,21 @@ get_all_ppds_func (gpointer user_data)
for (attr = ippFirstAttribute (response); attr != NULL; attr = ippNextAttribute (response))
{
const gchar *ppd_device_id = NULL;
const gchar *ppd_make_and_model = NULL;
const gchar *ppd_name = NULL;
const gchar *ppd_product = NULL;
const gchar *ppd_make = NULL;
g_autofree gchar *mdl = NULL;
g_autofree gchar *mfg = NULL;
g_autofree gchar *mfg_normalized = NULL;
while (attr != NULL && ippGetGroupTag (attr) != IPP_TAG_PRINTER)
attr = ippNextAttribute (response);
if (attr == NULL)
break;
ppd_device_id = NULL;
ppd_make_and_model = NULL;
ppd_name = NULL;
ppd_product = NULL;
ppd_make = NULL;
mfg = NULL;
mfg_normalized = NULL;
mdl = NULL;
while (attr != NULL && ippGetGroupTag (attr) == IPP_TAG_PRINTER)
{
if (g_strcmp0 (ippGetName (attr), "ppd-device-id") == 0 &&
@ -2779,10 +2750,6 @@ get_all_ppds_func (gpointer user_data)
}
}
g_free (mdl);
g_free (mfg);
g_free (mfg_normalized);
if (attr == NULL)
break;
}
@ -2961,27 +2928,23 @@ ppd_list_free (PPDList *list)
gchar *
get_standard_manufacturers_name (const gchar *name)
{
gchar *normalized_name;
gchar *result = NULL;
g_autofree gchar *normalized_name = NULL;
gint i;
if (name)
if (name == NULL)
return NULL;
normalized_name = normalize (name);
for (i = 0; i < G_N_ELEMENTS (manufacturers_names); i++)
{
normalized_name = normalize (name);
for (i = 0; i < G_N_ELEMENTS (manufacturers_names); i++)
if (g_strcmp0 (manufacturers_names[i].normalized_name, normalized_name) == 0)
{
if (g_strcmp0 (manufacturers_names[i].normalized_name, normalized_name) == 0)
{
result = g_strdup (manufacturers_names[i].display_name);
break;
}
return g_strdup (manufacturers_names[i].display_name);
}
g_free (normalized_name);
}
return result;
return NULL;
}
typedef struct
@ -3307,7 +3270,7 @@ typedef struct
} GCDData;
static gint
get_suffix_index (gchar *string)
get_suffix_index (const gchar *string)
{
gchar *number;
gchar *endptr;
@ -3361,21 +3324,19 @@ get_cups_devices_async_dbus_cb (GObject *source_object,
{
GVariantIter *iter;
GVariant *item;
gchar *key;
gchar *value;
gint index = -1, max_index = -1, i;
g_variant_get (devices_variant, "a{ss}", &iter);
while ((item = g_variant_iter_next_value (iter)))
{
g_variant_get (item, "{ss}", &key, &value);
const gchar *key, *value;
g_variant_get (item, "{&s&s}", &key, &value);
index = get_suffix_index (key);
if (index > max_index)
max_index = index;
g_free (key);
g_free (value);
g_variant_unref (item);
}
@ -3387,7 +3348,9 @@ get_cups_devices_async_dbus_cb (GObject *source_object,
g_variant_get (devices_variant, "a{ss}", &iter);
while ((item = g_variant_iter_next_value (iter)))
{
g_variant_get (item, "{ss}", &key, &value);
const gchar *key, *value;
g_variant_get (item, "{&s&s}", &key, &value);
index = get_suffix_index (key);
if (index >= 0)
@ -3419,8 +3382,6 @@ get_cups_devices_async_dbus_cb (GObject *source_object,
g_object_set (devices[index], "acquisition-method", ACQUISITION_METHOD_DEFAULT_CUPS_SERVER, NULL);
}
g_free (key);
g_free (value);
g_variant_unref (item);
}
@ -3459,9 +3420,9 @@ get_cups_devices_async_dbus_cb (GObject *source_object,
{
if (!g_cancellable_is_cancelled (data->cancellable))
{
GVariantBuilder *include_scheme_builder = NULL;
GVariantBuilder *exclude_scheme_builder = NULL;
gchar *backend_name;
GVariantBuilder *include_scheme_builder = NULL;
GVariantBuilder *exclude_scheme_builder = NULL;
g_autofree gchar *backend_name = NULL;
backend_name = data->backend_list->data;
@ -3480,7 +3441,6 @@ get_cups_devices_async_dbus_cb (GObject *source_object,
exclude_scheme_builder = create_other_backends_array ();
}
g_free (backend_name);
data->backend_list = g_list_remove_link (data->backend_list, data->backend_list);
g_dbus_connection_call (G_DBUS_CONNECTION (g_object_ref (source_object)),
@ -3542,7 +3502,7 @@ get_cups_devices_async (GCancellable *cancellable,
GVariantBuilder include_scheme_builder;
GCDData *data;
g_autoptr(GError) error = NULL;
gchar *backend_name;
g_autofree gchar *backend_name = NULL;
bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
if (!bus)
@ -3564,7 +3524,6 @@ get_cups_devices_async (GCancellable *cancellable,
g_variant_builder_init (&include_scheme_builder, G_VARIANT_TYPE ("as"));
g_variant_builder_add (&include_scheme_builder, "s", backend_name);
g_free (backend_name);
data->backend_list = g_list_remove_link (data->backend_list, data->backend_list);
g_dbus_connection_call (bus,
@ -3665,9 +3624,7 @@ canonicalize_device_name (GList *device_names,
gboolean already_present;
GList *iter;
gsize len;
gchar *name = NULL;
gchar *new_name;
gchar *lower_name;
g_autofree gchar *name = NULL;
gchar *occurrence;
gint name_index, j;
static const char * const residues[] = {
@ -3725,7 +3682,7 @@ canonicalize_device_name (GList *device_names,
/* Remove common strings found in driver names */
for (j = 0; j < G_N_ELEMENTS (residues); j++)
{
lower_name = g_ascii_strdown (name, -1);
g_autofree gchar *lower_name = g_ascii_strdown (name, -1);
occurrence = g_strrstr (lower_name, residues[j]);
if (occurrence != NULL)
@ -3733,8 +3690,6 @@ canonicalize_device_name (GList *device_names,
occurrence[0] = '\0';
name[strlen (lower_name)] = '\0';
}
g_free (lower_name);
}
/* Remove trailing "-" */
@ -3756,8 +3711,10 @@ canonicalize_device_name (GList *device_names,
name_index = 2;
already_present = FALSE;
do
while (TRUE)
{
g_autofree gchar *new_name = NULL;
if (already_present)
{
new_name = g_strdup_printf ("%s-%d", name, name_index);
@ -3787,14 +3744,9 @@ canonicalize_device_name (GList *device_names,
already_present = TRUE;
}
if (already_present)
g_free (new_name);
} while (already_present);
g_free (name);
return new_name;
if (!already_present)
return g_steal_pointer (&new_name);
}
}
void