css: Change to_string() to print()

It's preferrable to print to an existing GString instead of returning a
char* everywhere that needs to be freed later.
This commit is contained in:
Benjamin Otte 2011-05-23 07:02:36 +02:00
parent 43cca78a98
commit 02a9cb5bc0
5 changed files with 169 additions and 150 deletions

View File

@ -123,15 +123,14 @@ _gtk_animation_description_from_string (const gchar *str)
return _gtk_animation_description_new ((gdouble) duration, progress_type, loop); return _gtk_animation_description_new ((gdouble) duration, progress_type, loop);
} }
char * void
_gtk_animation_description_to_string (GtkAnimationDescription *desc) _gtk_animation_description_print (GtkAnimationDescription *desc,
GString *str)
{ {
GString *str;
int duration; int duration;
g_return_val_if_fail (desc != NULL, NULL); g_return_if_fail (desc != NULL);
g_return_if_fail (str != NULL);
str = g_string_new ("");
duration = desc->duration; duration = desc->duration;
if (duration % 1000 == 0) if (duration % 1000 == 0)
@ -162,8 +161,6 @@ _gtk_animation_description_to_string (GtkAnimationDescription *desc)
if (desc->loop) if (desc->loop)
g_string_append (str, " loop"); g_string_append (str, " loop");
return g_string_free (str, FALSE);
} }
GType GType

View File

@ -43,7 +43,8 @@ GtkAnimationDescription * _gtk_animation_description_ref (GtkAnima
void _gtk_animation_description_unref (GtkAnimationDescription *desc); void _gtk_animation_description_unref (GtkAnimationDescription *desc);
GtkAnimationDescription * _gtk_animation_description_from_string (const gchar *str); GtkAnimationDescription * _gtk_animation_description_from_string (const gchar *str);
char * _gtk_animation_description_to_string (GtkAnimationDescription *desc); void _gtk_animation_description_print (GtkAnimationDescription *desc,
GString *string);
G_END_DECLS G_END_DECLS

View File

@ -39,13 +39,11 @@ struct _GtkShadowElement {
GtkSymbolicColor *symbolic_color; GtkSymbolicColor *symbolic_color;
}; };
static gchar * static void
shadow_element_to_string (GtkShadowElement *element) shadow_element_print (GtkShadowElement *element,
GString *str)
{ {
gchar *color_str; gchar *color_str;
GString *str;
str = g_string_new (NULL);
if (element->inset) if (element->inset)
g_string_append (str, "inset "); g_string_append (str, "inset ");
@ -67,8 +65,6 @@ shadow_element_to_string (GtkShadowElement *element)
g_string_append (str, color_str); g_string_append (str, color_str);
g_free (color_str); g_free (color_str);
return g_string_free (str, FALSE);
} }
static void static void
@ -226,33 +222,28 @@ _gtk_shadow_resolve (GtkShadow *shadow,
return resolved_shadow; return resolved_shadow;
} }
gchar * void
_gtk_shadow_to_string (GtkShadow *shadow) _gtk_shadow_print (GtkShadow *shadow,
GString *str)
{ {
GString *str;
gint length; gint length;
GList *l; GList *l;
length = g_list_length (shadow->elements); length = g_list_length (shadow->elements);
if (length == 0) if (length == 0)
return NULL; return;
str = g_string_new (NULL); shadow_element_print (shadow->elements->data, str);
g_string_append (str,
shadow_element_to_string (shadow->elements->data));
if (length == 1) if (length == 1)
return g_string_free (str, FALSE); return;
for (l = g_list_next (shadow->elements); l != NULL; l = l->next) for (l = g_list_next (shadow->elements); l != NULL; l = l->next)
{ {
g_string_append (str, ", "); g_string_append (str, ", ");
g_string_append (str, shadow_element_to_string (l->data)); shadow_element_print (l->data, str);
} }
return g_string_free (str, FALSE);
} }
void void

View File

@ -47,7 +47,8 @@ void _gtk_shadow_append (GtkShadow *shadow,
gboolean inset, gboolean inset,
GtkSymbolicColor *color); GtkSymbolicColor *color);
gchar *_gtk_shadow_to_string (GtkShadow *shadow); void _gtk_shadow_print (GtkShadow *shadow,
GString *string);
GtkShadow *_gtk_shadow_resolve (GtkShadow *shadow, GtkShadow *_gtk_shadow_resolve (GtkShadow *shadow,
GtkStyleProperties *props); GtkStyleProperties *props);

View File

@ -43,21 +43,22 @@
typedef gboolean (* ParseFunc) (GtkCssParser *parser, typedef gboolean (* ParseFunc) (GtkCssParser *parser,
GFile *base, GFile *base,
GValue *value); GValue *value);
typedef char * (* ToStringFunc) (const GValue *value); typedef void (* PrintFunc) (const GValue *value,
GString *string);
static GHashTable *parse_funcs = NULL; static GHashTable *parse_funcs = NULL;
static GHashTable *to_string_funcs = NULL; static GHashTable *print_funcs = NULL;
static GHashTable *properties = NULL; static GHashTable *properties = NULL;
static void static void
register_conversion_function (GType type, register_conversion_function (GType type,
ParseFunc parse, ParseFunc parse,
ToStringFunc to_string) PrintFunc print)
{ {
if (parse) if (parse)
g_hash_table_insert (parse_funcs, GSIZE_TO_POINTER (type), parse); g_hash_table_insert (parse_funcs, GSIZE_TO_POINTER (type), parse);
if (to_string) if (print)
g_hash_table_insert (to_string_funcs, GSIZE_TO_POINTER (type), to_string); g_hash_table_insert (print_funcs, GSIZE_TO_POINTER (type), print);
} }
/*** IMPLEMENTATIONS ***/ /*** IMPLEMENTATIONS ***/
@ -89,15 +90,20 @@ rgba_value_parse (GtkCssParser *parser,
return TRUE; return TRUE;
} }
static char * static void
rgba_value_to_string (const GValue *value) rgba_value_print (const GValue *value,
GString *string)
{ {
const GdkRGBA *rgba = g_value_get_boxed (value); const GdkRGBA *rgba = g_value_get_boxed (value);
if (rgba == NULL) if (rgba == NULL)
return g_strdup ("none"); g_string_append (string, "none");
else
return gdk_rgba_to_string (rgba); {
char *s = gdk_rgba_to_string (rgba);
g_string_append (string, s);
g_free (s);
}
} }
static gboolean static gboolean
@ -132,15 +138,20 @@ color_value_parse (GtkCssParser *parser,
return TRUE; return TRUE;
} }
static char * static void
color_value_to_string (const GValue *value) color_value_print (const GValue *value,
GString *string)
{ {
const GdkColor *color = g_value_get_boxed (value); const GdkColor *color = g_value_get_boxed (value);
if (color == NULL) if (color == NULL)
return g_strdup ("none"); g_string_append (string, "none");
else
return gdk_color_to_string (color); {
char *s = gdk_color_to_string (color);
g_string_append (string, s);
g_free (s);
}
} }
static gboolean static gboolean
@ -158,15 +169,20 @@ symbolic_color_value_parse (GtkCssParser *parser,
return TRUE; return TRUE;
} }
static char * static void
symbolic_color_value_to_string (const GValue *value) symbolic_color_value_print (const GValue *value,
GString *string)
{ {
GtkSymbolicColor *symbolic = g_value_get_boxed (value); GtkSymbolicColor *symbolic = g_value_get_boxed (value);
if (symbolic == NULL) if (symbolic == NULL)
return g_strdup ("none"); g_string_append (string, "none");
else
return gtk_symbolic_color_to_string (symbolic); {
char *s = gtk_symbolic_color_to_string (symbolic);
g_string_append (string, s);
g_free (s);
}
} }
static gboolean static gboolean
@ -187,15 +203,20 @@ font_description_value_parse (GtkCssParser *parser,
return TRUE; return TRUE;
} }
static char * static void
font_description_value_to_string (const GValue *value) font_description_value_print (const GValue *value,
GString *string)
{ {
const PangoFontDescription *desc = g_value_get_boxed (value); const PangoFontDescription *desc = g_value_get_boxed (value);
if (desc == NULL) if (desc == NULL)
return g_strdup ("none"); g_string_append (string, "none");
else
return pango_font_description_to_string (desc); {
char *s = pango_font_description_to_string (desc);
g_string_append (string, s);
g_free (s);
}
} }
static gboolean static gboolean
@ -222,13 +243,14 @@ boolean_value_parse (GtkCssParser *parser,
} }
} }
static char * static void
boolean_value_to_string (const GValue *value) boolean_value_print (const GValue *value,
GString *string)
{ {
if (g_value_get_boolean (value)) if (g_value_get_boolean (value))
return g_strdup ("true"); g_string_append (string, "true");
else else
return g_strdup ("false"); g_string_append (string, "false");
} }
static gboolean static gboolean
@ -248,10 +270,11 @@ int_value_parse (GtkCssParser *parser,
return TRUE; return TRUE;
} }
static char * static void
int_value_to_string (const GValue *value) int_value_print (const GValue *value,
GString *string)
{ {
return g_strdup_printf ("%d", g_value_get_int (value)); g_string_append_printf (string, "%d", g_value_get_int (value));
} }
static gboolean static gboolean
@ -271,10 +294,11 @@ uint_value_parse (GtkCssParser *parser,
return TRUE; return TRUE;
} }
static char * static void
uint_value_to_string (const GValue *value) uint_value_print (const GValue *value,
GString *string)
{ {
return g_strdup_printf ("%u", g_value_get_uint (value)); g_string_append_printf (string, "%u", g_value_get_uint (value));
} }
static gboolean static gboolean
@ -294,14 +318,14 @@ double_value_parse (GtkCssParser *parser,
return TRUE; return TRUE;
} }
static char * static void
double_value_to_string (const GValue *value) double_value_print (const GValue *value,
GString *string)
{ {
char buf[G_ASCII_DTOSTR_BUF_SIZE]; char buf[G_ASCII_DTOSTR_BUF_SIZE];
g_ascii_dtostr (buf, sizeof (buf), g_value_get_double (value)); g_ascii_dtostr (buf, sizeof (buf), g_value_get_double (value));
g_string_append (string, buf);
return g_strdup (buf);
} }
static gboolean static gboolean
@ -321,14 +345,14 @@ float_value_parse (GtkCssParser *parser,
return TRUE; return TRUE;
} }
static char * static void
float_value_to_string (const GValue *value) float_value_print (const GValue *value,
GString *string)
{ {
char buf[G_ASCII_DTOSTR_BUF_SIZE]; char buf[G_ASCII_DTOSTR_BUF_SIZE];
g_ascii_dtostr (buf, sizeof (buf), g_value_get_float (value)); g_ascii_dtostr (buf, sizeof (buf), g_value_get_float (value));
g_string_append (string, buf);
return g_strdup (buf);
} }
static gboolean static gboolean
@ -345,15 +369,15 @@ string_value_parse (GtkCssParser *parser,
return TRUE; return TRUE;
} }
static char * static void
string_value_to_string (const GValue *value) string_value_print (const GValue *value,
GString *str)
{ {
const char *string; const char *string;
gsize len; gsize len;
GString *str;
string = g_value_get_string (value); string = g_value_get_string (value);
str = g_string_new ("\""); g_string_append_c (str, '"');
do { do {
len = strcspn (string, "\"\n\r\f"); len = strcspn (string, "\"\n\r\f");
@ -382,7 +406,6 @@ string_value_to_string (const GValue *value)
} while (*string); } while (*string);
g_string_append_c (str, '"'); g_string_append_c (str, '"');
return g_string_free (str, FALSE);
} }
static gboolean static gboolean
@ -413,20 +436,23 @@ theming_engine_value_parse (GtkCssParser *parser,
return TRUE; return TRUE;
} }
static char * static void
theming_engine_value_to_string (const GValue *value) theming_engine_value_print (const GValue *value,
GString *string)
{ {
GtkThemingEngine *engine; GtkThemingEngine *engine;
char *name; char *name;
engine = g_value_get_object (value); engine = g_value_get_object (value);
if (engine == NULL) if (engine == NULL)
return g_strdup ("none"); g_string_append (string, "none");
else
{
/* XXX: gtk_theming_engine_get_name()? */ /* XXX: gtk_theming_engine_get_name()? */
g_object_get (engine, "name", &name, NULL); g_object_get (engine, "name", &name, NULL);
g_string_append (string, name);
return name; g_free (name);
}
} }
static gboolean static gboolean
@ -454,15 +480,16 @@ animation_description_value_parse (GtkCssParser *parser,
return TRUE; return TRUE;
} }
static char * static void
animation_description_value_to_string (const GValue *value) animation_description_value_print (const GValue *value,
GString *string)
{ {
GtkAnimationDescription *desc = g_value_get_boxed (value); GtkAnimationDescription *desc = g_value_get_boxed (value);
if (desc == NULL) if (desc == NULL)
return g_strdup ("none"); g_string_append (string, "none");
else
return _gtk_animation_description_to_string (desc); _gtk_animation_description_print (desc, string);
} }
static gboolean static gboolean
@ -506,21 +533,21 @@ border_value_parse (GtkCssParser *parser,
return TRUE; return TRUE;
} }
static char * static void
border_value_to_string (const GValue *value) border_value_print (const GValue *value, GString *string)
{ {
const GtkBorder *border = g_value_get_boxed (value); const GtkBorder *border = g_value_get_boxed (value);
if (border == NULL) if (border == NULL)
return g_strdup ("none"); g_string_append (string, "none");
else if (border->left != border->right) else if (border->left != border->right)
return g_strdup_printf ("%d %d %d %d", border->top, border->right, border->bottom, border->left); g_string_append_printf (string, "%d %d %d %d", border->top, border->right, border->bottom, border->left);
else if (border->top != border->bottom) else if (border->top != border->bottom)
return g_strdup_printf ("%d %d %d", border->top, border->right, border->bottom); g_string_append_printf (string, "%d %d %d", border->top, border->right, border->bottom);
else if (border->top != border->left) else if (border->top != border->left)
return g_strdup_printf ("%d %d", border->top, border->right); g_string_append_printf (string, "%d %d", border->top, border->right);
else else
return g_strdup_printf ("%d", border->top); g_string_append_printf (string, "%d", border->top);
} }
static gboolean static gboolean
@ -716,15 +743,20 @@ gradient_value_parse (GtkCssParser *parser,
return TRUE; return TRUE;
} }
static char * static void
gradient_value_to_string (const GValue *value) gradient_value_print (const GValue *value,
GString *string)
{ {
GtkGradient *gradient = g_value_get_boxed (value); GtkGradient *gradient = g_value_get_boxed (value);
if (gradient == NULL) if (gradient == NULL)
return g_strdup ("none"); g_string_append (string, "none");
else
return gtk_gradient_to_string (gradient); {
char *s = gtk_gradient_to_string (gradient);
g_string_append (string, s);
g_free (s);
}
} }
static GFile * static GFile *
@ -898,17 +930,18 @@ shadow_value_parse (GtkCssParser *parser,
return TRUE; return TRUE;
} }
static gchar * static void
shadow_value_to_string (const GValue *value) shadow_value_print (const GValue *value,
GString *string)
{ {
GtkShadow *shadow; GtkShadow *shadow;
shadow = g_value_get_boxed (value); shadow = g_value_get_boxed (value);
if (shadow == NULL) if (shadow == NULL)
return g_strdup ("none"); g_string_append (string, "none");
else
return _gtk_shadow_to_string (shadow); _gtk_shadow_print (shadow, string);
} }
static gboolean static gboolean
@ -1008,21 +1041,19 @@ enum_value_parse (GtkCssParser *parser,
return enum_value != NULL; return enum_value != NULL;
} }
static char * static void
enum_value_to_string (const GValue *value) enum_value_print (const GValue *value,
GString *string)
{ {
GEnumClass *enum_class; GEnumClass *enum_class;
GEnumValue *enum_value; GEnumValue *enum_value;
char *s;
enum_class = g_type_class_ref (G_VALUE_TYPE (value)); enum_class = g_type_class_ref (G_VALUE_TYPE (value));
enum_value = g_enum_get_value (enum_class, g_value_get_enum (value)); enum_value = g_enum_get_value (enum_class, g_value_get_enum (value));
s = g_strdup (enum_value->value_nick); g_string_append (string, enum_value->value_nick);
g_type_class_unref (enum_class); g_type_class_unref (enum_class);
return s;
} }
static gboolean static gboolean
@ -1071,16 +1102,15 @@ flags_value_parse (GtkCssParser *parser,
return TRUE; return TRUE;
} }
static char * static void
flags_value_to_string (const GValue *value) flags_value_print (const GValue *value,
GString *string)
{ {
GFlagsClass *flags_class; GFlagsClass *flags_class;
GString *string;
guint i, flags; guint i, flags;
flags_class = g_type_class_ref (G_VALUE_TYPE (value)); flags_class = g_type_class_ref (G_VALUE_TYPE (value));
flags = g_value_get_flags (value); flags = g_value_get_flags (value);
string = g_string_new (NULL);
for (i = 0; i < flags_class->n_values; i++) for (i = 0; i < flags_class->n_values; i++)
{ {
@ -1096,8 +1126,6 @@ flags_value_to_string (const GValue *value)
} }
g_type_class_unref (flags_class); g_type_class_unref (flags_class);
return g_string_free (string, FALSE);
} }
static gboolean static gboolean
@ -1139,26 +1167,23 @@ bindings_value_parse (GtkCssParser *parser,
return TRUE; return TRUE;
} }
static char * static void
bindings_value_to_string (const GValue *value) bindings_value_print (const GValue *value,
GString *string)
{ {
GPtrArray *array; GPtrArray *array;
GString *str;
guint i; guint i;
array = g_value_get_boxed (value); array = g_value_get_boxed (value);
str = g_string_new (NULL);
for (i = 0; i < array->len; i++) for (i = 0; i < array->len; i++)
{ {
GtkBindingSet *binding_set = g_ptr_array_index (array, i); GtkBindingSet *binding_set = g_ptr_array_index (array, i);
if (i > 0) if (i > 0)
g_string_append (str, ", "); g_string_append (string, ", ");
g_string_append (str, binding_set->set_name); g_string_append (string, binding_set->set_name);
} }
return g_string_free (str, FALSE);
} }
/*** PACKING ***/ /*** PACKING ***/
@ -1285,50 +1310,50 @@ css_string_funcs_init (void)
return; return;
parse_funcs = g_hash_table_new (NULL, NULL); parse_funcs = g_hash_table_new (NULL, NULL);
to_string_funcs = g_hash_table_new (NULL, NULL); print_funcs = g_hash_table_new (NULL, NULL);
register_conversion_function (GDK_TYPE_RGBA, register_conversion_function (GDK_TYPE_RGBA,
rgba_value_parse, rgba_value_parse,
rgba_value_to_string); rgba_value_print);
register_conversion_function (GDK_TYPE_COLOR, register_conversion_function (GDK_TYPE_COLOR,
color_value_parse, color_value_parse,
color_value_to_string); color_value_print);
register_conversion_function (GTK_TYPE_SYMBOLIC_COLOR, register_conversion_function (GTK_TYPE_SYMBOLIC_COLOR,
symbolic_color_value_parse, symbolic_color_value_parse,
symbolic_color_value_to_string); symbolic_color_value_print);
register_conversion_function (PANGO_TYPE_FONT_DESCRIPTION, register_conversion_function (PANGO_TYPE_FONT_DESCRIPTION,
font_description_value_parse, font_description_value_parse,
font_description_value_to_string); font_description_value_print);
register_conversion_function (G_TYPE_BOOLEAN, register_conversion_function (G_TYPE_BOOLEAN,
boolean_value_parse, boolean_value_parse,
boolean_value_to_string); boolean_value_print);
register_conversion_function (G_TYPE_INT, register_conversion_function (G_TYPE_INT,
int_value_parse, int_value_parse,
int_value_to_string); int_value_print);
register_conversion_function (G_TYPE_UINT, register_conversion_function (G_TYPE_UINT,
uint_value_parse, uint_value_parse,
uint_value_to_string); uint_value_print);
register_conversion_function (G_TYPE_DOUBLE, register_conversion_function (G_TYPE_DOUBLE,
double_value_parse, double_value_parse,
double_value_to_string); double_value_print);
register_conversion_function (G_TYPE_FLOAT, register_conversion_function (G_TYPE_FLOAT,
float_value_parse, float_value_parse,
float_value_to_string); float_value_print);
register_conversion_function (G_TYPE_STRING, register_conversion_function (G_TYPE_STRING,
string_value_parse, string_value_parse,
string_value_to_string); string_value_print);
register_conversion_function (GTK_TYPE_THEMING_ENGINE, register_conversion_function (GTK_TYPE_THEMING_ENGINE,
theming_engine_value_parse, theming_engine_value_parse,
theming_engine_value_to_string); theming_engine_value_print);
register_conversion_function (GTK_TYPE_ANIMATION_DESCRIPTION, register_conversion_function (GTK_TYPE_ANIMATION_DESCRIPTION,
animation_description_value_parse, animation_description_value_parse,
animation_description_value_to_string); animation_description_value_print);
register_conversion_function (GTK_TYPE_BORDER, register_conversion_function (GTK_TYPE_BORDER,
border_value_parse, border_value_parse,
border_value_to_string); border_value_print);
register_conversion_function (GTK_TYPE_GRADIENT, register_conversion_function (GTK_TYPE_GRADIENT,
gradient_value_parse, gradient_value_parse,
gradient_value_to_string); gradient_value_print);
register_conversion_function (CAIRO_GOBJECT_TYPE_PATTERN, register_conversion_function (CAIRO_GOBJECT_TYPE_PATTERN,
pattern_value_parse, pattern_value_parse,
NULL); NULL);
@ -1337,16 +1362,16 @@ css_string_funcs_init (void)
NULL); NULL);
register_conversion_function (GTK_TYPE_SHADOW, register_conversion_function (GTK_TYPE_SHADOW,
shadow_value_parse, shadow_value_parse,
shadow_value_to_string); shadow_value_print);
register_conversion_function (G_TYPE_ENUM, register_conversion_function (G_TYPE_ENUM,
enum_value_parse, enum_value_parse,
enum_value_to_string); enum_value_print);
register_conversion_function (G_TYPE_FLAGS, register_conversion_function (G_TYPE_FLAGS,
flags_value_parse, flags_value_parse,
flags_value_to_string); flags_value_print);
register_conversion_function (G_TYPE_PTR_ARRAY, register_conversion_function (G_TYPE_PTR_ARRAY,
bindings_value_parse, bindings_value_parse,
bindings_value_to_string); bindings_value_print);
} }
gboolean gboolean
@ -1381,18 +1406,22 @@ _gtk_css_value_parse (GValue *value,
char * char *
_gtk_css_value_to_string (const GValue *value) _gtk_css_value_to_string (const GValue *value)
{ {
ToStringFunc func; PrintFunc func;
css_string_funcs_init (); css_string_funcs_init ();
func = g_hash_table_lookup (to_string_funcs, func = g_hash_table_lookup (print_funcs,
GSIZE_TO_POINTER (G_VALUE_TYPE (value))); GSIZE_TO_POINTER (G_VALUE_TYPE (value)));
if (func == NULL) if (func == NULL)
func = g_hash_table_lookup (to_string_funcs, func = g_hash_table_lookup (print_funcs,
GSIZE_TO_POINTER (g_type_fundamental (G_VALUE_TYPE (value)))); GSIZE_TO_POINTER (g_type_fundamental (G_VALUE_TYPE (value))));
if (func) if (func)
return func (value); {
GString *string = g_string_new (NULL);
func (value, string);
return g_string_free (string, FALSE);
}
return g_strdup_value_contents (value); return g_strdup_value_contents (value);
} }