gimpconfig-array.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* LIBGIMP - The GIMP Library
  2. * Copyright (C) 1995-1997 Spencer Kimball and Peter Mattis
  3. *
  4. * Copyright (C) 2001-2002 Sven Neumann <sven@gimp.org>
  5. *
  6. * This library is free software: you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Library General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library. If not, see
  18. * <https://www.gnu.org/licenses/>.
  19. */
  20. #include "config.h"
  21. #include <cairo.h>
  22. #include <gegl.h>
  23. #include <gdk-pixbuf/gdk-pixbuf.h>
  24. #include "gimpconfigtypes.h"
  25. #include "gimpconfig-utils.h"
  26. #include "gimpconfig-array.h"
  27. #include "gimpscanner.h"
  28. /* GStrv i.e. char** i.e. null terminated array of strings. */
  29. /**
  30. * gimp_config_serialize_strv:
  31. * @value: source #GValue holding a #GStrv
  32. * @str: destination string
  33. *
  34. * Appends a string repr of the #GStrv value of #GValue to @str.
  35. * Repr is an integer literal greater than or equal to zero,
  36. * followed by a possibly empty sequence
  37. * of quoted and escaped string literals.
  38. *
  39. * Returns: %TRUE always
  40. *
  41. * Since: 3.0
  42. **/
  43. gboolean
  44. gimp_config_serialize_strv (const GValue *value,
  45. GString *str)
  46. {
  47. GStrv gstrv;
  48. gstrv = g_value_get_boxed (value);
  49. if (gstrv)
  50. {
  51. gint length = g_strv_length (gstrv);
  52. /* Write length */
  53. g_string_append_printf (str, "%d", length);
  54. for (gint i = 0; i < length; i++)
  55. {
  56. g_string_append (str, " "); /* separator */
  57. gimp_config_string_append_escaped (str, gstrv[i]);
  58. }
  59. }
  60. else
  61. {
  62. /* GValue has NULL value. Not quite the same as an empty GStrv.
  63. * But handle it quietly as an empty GStrv: write a length of zero.
  64. */
  65. g_string_append (str, "0");
  66. }
  67. return TRUE;
  68. }
  69. /**
  70. * gimp_config_deserialize_strv:
  71. * @value: destination #GValue to hold a #GStrv
  72. * @scanner: #GScanner positioned in serialization stream
  73. *
  74. * Sets @value to new #GStrv.
  75. * Scans i.e. consumes serialization to fill the GStrv.
  76. *
  77. * Requires @value to be initialized to hold type #G_TYPE_BOXED.
  78. *
  79. * Returns:
  80. * G_TOKEN_RIGHT_PAREN on success.
  81. * G_TOKEN_INT on failure to scan length.
  82. * G_TOKEN_STRING on failure to scan enough quoted strings.
  83. *
  84. * On failure, the value in @value is not touched and could be NULL.
  85. *
  86. * Since: 3.0
  87. **/
  88. GTokenType
  89. gimp_config_deserialize_strv (GValue *value,
  90. GScanner *scanner)
  91. {
  92. gint n_values;
  93. GTokenType result_token = G_TOKEN_RIGHT_PAREN;
  94. GStrvBuilder *builder;
  95. /* Scan length of array. */
  96. if (! gimp_scanner_parse_int (scanner, &n_values))
  97. return G_TOKEN_INT;
  98. builder = g_strv_builder_new ();
  99. for (gint i = 0; i < n_values; i++)
  100. {
  101. gchar *scanned_string;
  102. if (! gimp_scanner_parse_string (scanner, &scanned_string))
  103. {
  104. /* Error, missing a string. */
  105. result_token = G_TOKEN_STRING;
  106. break;
  107. }
  108. /* Not an error for scanned string to be empty.*/
  109. /* Adding string to builder DOES not transfer ownership,
  110. * the builder will copy the string.
  111. */
  112. g_strv_builder_add (builder, scanned_string);
  113. g_free (scanned_string);
  114. }
  115. /* assert result_token is G_TOKEN_RIGHT_PAREN OR G_TOKEN_STRING */
  116. if (result_token == G_TOKEN_RIGHT_PAREN)
  117. {
  118. GStrv gstrv;
  119. /* Allocate new GStrv. */
  120. gstrv = g_strv_builder_end (builder);
  121. /* Transfer ownership of the array and all strings it points to. */
  122. g_value_take_boxed (value, gstrv);
  123. }
  124. else
  125. {
  126. /* No GStrv to unref. */
  127. g_scanner_error (scanner, "Missing string.");
  128. }
  129. g_strv_builder_unref (builder);
  130. return result_token;
  131. }