From f965b659eb2b1750fbf76202a1f3eaa5ce686e62 Mon Sep 17 00:00:00 2001 From: Jehan Date: Thu, 17 Oct 2024 22:08:16 +0200 Subject: [PATCH] libgimpconfig: gimp_config_(de)serialize_strv() should not be public. These 2 functions are only used in one file each, there are absolutely zero reasons to make them public. These functions have nothing special. --- libgimpconfig/gimpconfig-array.c | 149 ------------------------- libgimpconfig/gimpconfig-array.h | 47 -------- libgimpconfig/gimpconfig-deserialize.c | 72 +++++++++++- libgimpconfig/gimpconfig-serialize.c | 68 +++++++++-- libgimpconfig/gimpconfig.def | 2 - libgimpconfig/meson.build | 2 - 6 files changed, 130 insertions(+), 210 deletions(-) delete mode 100644 libgimpconfig/gimpconfig-array.c delete mode 100644 libgimpconfig/gimpconfig-array.h diff --git a/libgimpconfig/gimpconfig-array.c b/libgimpconfig/gimpconfig-array.c deleted file mode 100644 index 012cb96993..0000000000 --- a/libgimpconfig/gimpconfig-array.c +++ /dev/null @@ -1,149 +0,0 @@ -/* LIBGIMP - The GIMP Library - * Copyright (C) 1995-1997 Spencer Kimball and Peter Mattis - * - * Copyright (C) 2001-2002 Sven Neumann - * - * This library is free software: you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This library 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 - * Library General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library. If not, see - * . - */ - -#include "config.h" - -#include -#include -#include - -#include "gimpconfigtypes.h" - -#include "gimpconfig-utils.h" -#include "gimpconfig-array.h" -#include "gimpscanner.h" - - -/* GStrv i.e. char** i.e. null terminated array of strings. */ - -/** - * gimp_config_serialize_strv: - * @value: source #GValue holding a #GStrv - * @str: destination string - * - * Appends a string repr of the #GStrv value of #GValue to @str. - * Repr is an integer literal greater than or equal to zero, - * followed by a possibly empty sequence - * of quoted and escaped string literals. - * - * Returns: %TRUE always - * - * Since: 3.0 - **/ -gboolean -gimp_config_serialize_strv (const GValue *value, - GString *str) -{ - GStrv gstrv; - - gstrv = g_value_get_boxed (value); - - if (gstrv) - { - gint length = g_strv_length (gstrv); - - /* Write length */ - g_string_append_printf (str, "%d", length); - - for (gint i = 0; i < length; i++) - { - g_string_append (str, " "); /* separator */ - gimp_config_string_append_escaped (str, gstrv[i]); - } - } - else - { - /* GValue has NULL value. Not quite the same as an empty GStrv. - * But handle it quietly as an empty GStrv: write a length of zero. - */ - g_string_append (str, "0"); - } - - return TRUE; -} - -/** - * gimp_config_deserialize_strv: - * @value: destination #GValue to hold a #GStrv - * @scanner: #GScanner positioned in serialization stream - * - * Sets @value to new #GStrv. - * Scans i.e. consumes serialization to fill the GStrv. - * - * Requires @value to be initialized to hold type #G_TYPE_BOXED. - * - * Returns: - * G_TOKEN_RIGHT_PAREN on success. - * G_TOKEN_INT on failure to scan length. - * G_TOKEN_STRING on failure to scan enough quoted strings. - * - * On failure, the value in @value is not touched and could be NULL. - * - * Since: 3.0 - **/ -GTokenType -gimp_config_deserialize_strv (GValue *value, - GScanner *scanner) -{ - gint n_values; - GTokenType result_token = G_TOKEN_RIGHT_PAREN; - GStrvBuilder *builder; - - /* Scan length of array. */ - if (! gimp_scanner_parse_int (scanner, &n_values)) - return G_TOKEN_INT; - - builder = g_strv_builder_new (); - - for (gint i = 0; i < n_values; i++) - { - gchar *scanned_string; - - if (! gimp_scanner_parse_string (scanner, &scanned_string)) - { - /* Error, missing a string. */ - result_token = G_TOKEN_STRING; - break; - } - - g_strv_builder_add (builder, scanned_string ? scanned_string : ""); - g_free (scanned_string); - } - - /* assert result_token is G_TOKEN_RIGHT_PAREN OR G_TOKEN_STRING */ - if (result_token == G_TOKEN_RIGHT_PAREN) - { - GStrv gstrv; - - /* Allocate new GStrv. */ - gstrv = g_strv_builder_end (builder); - /* Transfer ownership of the array and all strings it points to. */ - g_value_take_boxed (value, gstrv); - } - else - { - /* No GStrv to unref. */ - g_scanner_error (scanner, "Missing string."); - } - - g_strv_builder_unref (builder); - - return result_token; -} diff --git a/libgimpconfig/gimpconfig-array.h b/libgimpconfig/gimpconfig-array.h deleted file mode 100644 index e8c0d450cd..0000000000 --- a/libgimpconfig/gimpconfig-array.h +++ /dev/null @@ -1,47 +0,0 @@ -/* LIBGIMP - The GIMP Library - * Copyright (C) 1995 Spencer Kimball and Peter Mattis - * - * gimpconfig-array.h - * Copyright (C) 2001-2002 Sven Neumann - * - * This library is free software: you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This library 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 - * Library General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library. If not, see - * . - */ - -#if !defined (__GIMP_CONFIG_H_INSIDE__) && !defined (GIMP_CONFIG_COMPILATION) -#error "Only can be included directly." -#endif - -#ifndef __GIMP_CONFIG_ARRAY_H__ -#define __GIMP_CONFIG_ARRAY_H__ - -G_BEGIN_DECLS - -/* For information look into the C source or the html documentation */ - -/* Functions to ser/des arrays. */ - -/* FIXME: move gimp_config_serialize_value_array here. - * FIXME: implement other arrays for which plugins can declare args e.g. int32 array. - * FIXME: doesn't need to be introspected, these are internal - */ - -gboolean gimp_config_serialize_strv (const GValue *value, - GString *str); -GTokenType gimp_config_deserialize_strv (GValue *value, - GScanner *scanner); - -G_END_DECLS - -#endif /* __GIMP_CONFIG_ARRAY_H__ */ diff --git a/libgimpconfig/gimpconfig-deserialize.c b/libgimpconfig/gimpconfig-deserialize.c index 9c87210843..e7fe6a9199 100644 --- a/libgimpconfig/gimpconfig-deserialize.c +++ b/libgimpconfig/gimpconfig-deserialize.c @@ -32,7 +32,6 @@ #include "gimpconfigtypes.h" #include "gimpconfigwriter.h" -#include "gimpconfig-array.h" #include "gimpconfig-iface.h" #include "gimpconfig-deserialize.h" #include "gimpconfig-params.h" @@ -86,6 +85,8 @@ static GTokenType gimp_config_deserialize_value_array (GValue *value, GimpConfig *config, GParamSpec *prop_spec, GScanner *scanner); +static GTokenType gimp_config_deserialize_strv (GValue *value, + GScanner *scanner); static GimpUnit * gimp_config_get_unit_from_identifier (const gchar *identifier); static GTokenType gimp_config_deserialize_unit (GValue *value, GParamSpec *prop_spec, @@ -856,6 +857,75 @@ gimp_config_deserialize_value_array (GValue *value, return G_TOKEN_RIGHT_PAREN; } +/** + * gimp_config_deserialize_strv: + * @value: destination #GValue to hold a #GStrv + * @scanner: #GScanner positioned in serialization stream + * + * Sets @value to new #GStrv. + * Scans i.e. consumes serialization to fill the GStrv. + * + * Requires @value to be initialized to hold type #G_TYPE_BOXED. + * + * Returns: + * G_TOKEN_RIGHT_PAREN on success. + * G_TOKEN_INT on failure to scan length. + * G_TOKEN_STRING on failure to scan enough quoted strings. + * + * On failure, the value in @value is not touched and could be NULL. + * + * Since: 3.0 + **/ +static GTokenType +gimp_config_deserialize_strv (GValue *value, + GScanner *scanner) +{ + gint n_values; + GTokenType result_token = G_TOKEN_RIGHT_PAREN; + GStrvBuilder *builder; + + /* Scan length of array. */ + if (! gimp_scanner_parse_int (scanner, &n_values)) + return G_TOKEN_INT; + + builder = g_strv_builder_new (); + + for (gint i = 0; i < n_values; i++) + { + gchar *scanned_string; + + if (! gimp_scanner_parse_string (scanner, &scanned_string)) + { + /* Error, missing a string. */ + result_token = G_TOKEN_STRING; + break; + } + + g_strv_builder_add (builder, scanned_string ? scanned_string : ""); + g_free (scanned_string); + } + + /* assert result_token is G_TOKEN_RIGHT_PAREN OR G_TOKEN_STRING */ + if (result_token == G_TOKEN_RIGHT_PAREN) + { + GStrv gstrv; + + /* Allocate new GStrv. */ + gstrv = g_strv_builder_end (builder); + /* Transfer ownership of the array and all strings it points to. */ + g_value_take_boxed (value, gstrv); + } + else + { + /* No GStrv to unref. */ + g_scanner_error (scanner, "Missing string."); + } + + g_strv_builder_unref (builder); + + return result_token; +} + static GimpUnit * gimp_config_get_unit_from_identifier (const gchar *identifier) { diff --git a/libgimpconfig/gimpconfig-serialize.c b/libgimpconfig/gimpconfig-serialize.c index 5e2d3dbd13..d05aa79a42 100644 --- a/libgimpconfig/gimpconfig-serialize.c +++ b/libgimpconfig/gimpconfig-serialize.c @@ -32,7 +32,6 @@ #include "gimpconfigtypes.h" #include "gimpconfigwriter.h" -#include "gimpconfig-array.h" #include "gimpconfig-iface.h" #include "gimpconfig-params.h" #include "gimpconfig-path.h" @@ -49,6 +48,10 @@ **/ +static gboolean gimp_config_serialize_strv (const GValue *value, + GString *str); + + /** * gimp_config_serialize_properties: * @config: a #GimpConfig. @@ -425,14 +428,7 @@ gimp_config_serialize_property (GimpConfig *config, { GString *str = g_string_new (NULL); - if (G_VALUE_TYPE (&value) == G_TYPE_STRV) - { - success = gimp_config_serialize_strv (&value, str); - } - else - { - success = gimp_config_serialize_value (&value, str, TRUE); - } + success = gimp_config_serialize_value (&value, str, TRUE); if (success) { @@ -511,6 +507,11 @@ gimp_config_serialize_value (const GValue *value, GString *str, gboolean escaped) { + if (G_VALUE_TYPE (value) == G_TYPE_STRV) + { + return gimp_config_serialize_strv (value, str); + } + if (G_VALUE_HOLDS_BOOLEAN (value)) { gboolean bool; @@ -679,3 +680,52 @@ gimp_config_serialize_value (const GValue *value, return FALSE; } + + +/* Private functions */ + +/** + * gimp_config_serialize_strv: + * @value: source #GValue holding a #GStrv + * @str: destination string + * + * Appends a string repr of the #GStrv value of #GValue to @str. + * Repr is an integer literal greater than or equal to zero, + * followed by a possibly empty sequence + * of quoted and escaped string literals. + * + * Returns: %TRUE always + * + * Since: 3.0 + **/ +static gboolean +gimp_config_serialize_strv (const GValue *value, + GString *str) +{ + GStrv gstrv; + + gstrv = g_value_get_boxed (value); + + if (gstrv) + { + gint length = g_strv_length (gstrv); + + /* Write length */ + g_string_append_printf (str, "%d", length); + + for (gint i = 0; i < length; i++) + { + g_string_append (str, " "); /* separator */ + gimp_config_string_append_escaped (str, gstrv[i]); + } + } + else + { + /* GValue has NULL value. Not quite the same as an empty GStrv. + * But handle it quietly as an empty GStrv: write a length of zero. + */ + g_string_append (str, "0"); + } + + return TRUE; +} diff --git a/libgimpconfig/gimpconfig.def b/libgimpconfig/gimpconfig.def index e736c42738..dedc32de94 100644 --- a/libgimpconfig/gimpconfig.def +++ b/libgimpconfig/gimpconfig.def @@ -30,7 +30,6 @@ EXPORTS gimp_config_deserialize_return gimp_config_deserialize_stream gimp_config_deserialize_string - gimp_config_deserialize_strv gimp_config_diff gimp_config_duplicate gimp_config_error_quark @@ -49,7 +48,6 @@ EXPORTS gimp_config_serialize_properties gimp_config_serialize_property gimp_config_serialize_property_by_name - gimp_config_serialize_strv gimp_config_serialize_to_fd gimp_config_serialize_to_file gimp_config_serialize_to_parasite diff --git a/libgimpconfig/meson.build b/libgimpconfig/meson.build index 9acd1f2fde..4199d638c3 100644 --- a/libgimpconfig/meson.build +++ b/libgimpconfig/meson.build @@ -21,7 +21,6 @@ stamp_config_enums = custom_target('stamp-gimpconfigenums.h', libgimpconfig_sources_introspectable = files( 'gimpcolorconfig.c', - 'gimpconfig-array.c', 'gimpconfig-deserialize.c', 'gimpconfig-error.c', 'gimpconfig-iface.c', @@ -43,7 +42,6 @@ libgimpconfig_sources = [ libgimpconfig_headers_introspectable = files( 'gimpcolorconfig.h', - 'gimpconfig-array.h', 'gimpconfig-deserialize.h', 'gimpconfig-error.h', 'gimpconfig-iface.h',