Commit b6dbe729 authored by Sven Neumann's avatar Sven Neumann Committed by Sven Neumann

added the new function gimp_config_string_append_escaped(), see inline

2003-03-17  Sven Neumann  <sven@gimp.org>

	* app/config/gimpconfig-utils.[ch]: added the new function
	gimp_config_string_append_escaped(), see inline docs.

	* app/config/gimpconfig-serialize.c
	* app/config/gimpconfigwriter.c
	* app/core/gimpcontainer.c: use the new function instead of
	g_strescape().
parent 21113a2f
2003-03-17 Sven Neumann <sven@gimp.org>
* app/config/gimpconfig-utils.[ch]: added the new function
gimp_config_string_append_escaped(), see inline docs.
* app/config/gimpconfig-serialize.c
* app/config/gimpconfigwriter.c
* app/core/gimpcontainer.c: use the new function instead of
g_strescape().
2003-03-17 Sven Neumann <sven@gimp.org>
Applied patches from David Necas <yeti@physics.muni.cz> that fix
......@@ -356,22 +356,16 @@ gimp_config_serialize_value (const GValue *value,
if (G_VALUE_HOLDS_STRING (value))
{
gchar *esc;
const gchar *cstr = g_value_get_string (value);
if (!cstr)
return FALSE;
if (escaped)
{
esc = g_strescape (cstr, NULL);
g_string_append_printf (str, "\"%s\"", esc);
g_free (esc);
}
gimp_config_string_append_escaped (str, cstr);
else
{
g_string_append (str, cstr);
}
g_string_append (str, cstr);
return TRUE;
}
......
......@@ -264,6 +264,92 @@ gimp_config_reset_properties (GObject *object)
}
/*
* GimpConfig string utilities
*/
/**
* gimp_config_string_append_escaped:
* @string: pointer to a #GString
* @val: a string to append or %NULL
*
* Escapes and quotes @val and appends it to @string. The escape
* algorithm is different from the one used by g_strescape() since it
* leaves non-ASCII characters intact and thus preserves UTF-8
* strings. Only control characters and quotes are being escaped.
**/
void
gimp_config_string_append_escaped (GString *string,
const gchar *val)
{
g_return_if_fail (string != NULL);
if (val)
{
const guchar *p;
gchar buf[4] = { '\\', 0, 0, 0 };
gint len;
g_string_append_c (string, '\"');
for (p = val, len = 0; *p; p++)
{
if (*p < ' ' || *p == '\\' || *p == '\"')
{
g_string_append_len (string, val, len);
len = 2;
switch (*p)
{
case '\b':
buf[1] = 'b';
break;
case '\f':
buf[1] = 'f';
break;
case '\n':
buf[1] = 'n';
break;
case '\r':
buf[1] = 'r';
break;
case '\t':
buf[1] = 't';
break;
case '\\':
case '"':
buf[1] = *p;
break;
default:
len = 4;
buf[1] = '0' + (((*p) >> 6) & 07);
buf[2] = '0' + (((*p) >> 3) & 07);
buf[3] = '0' + ((*p) & 07);
break;
}
g_string_append_len (string, buf, len);
val = p + 1;
len = 0;
}
else
{
len++;
}
}
g_string_append_len (string, val, len);
g_string_append_c (string, '\"');
}
else
{
g_string_append_len (string, "\"\"", 2);
}
}
/*
* GimpConfig path utilities
*/
......
......@@ -23,19 +23,22 @@
#define __GIMP_CONFIG_UTILS_H__
GList * gimp_config_diff (GObject *a,
GObject *b,
GParamFlags flags);
void gimp_config_connect (GObject *src,
GObject *dest);
void gimp_config_disconnect (GObject *src,
GObject *dest);
void gimp_config_copy_properties (GObject *src,
GObject *dest);
void gimp_config_reset_properties (GObject *object);
GList * gimp_config_diff (GObject *a,
GObject *b,
GParamFlags flags);
void gimp_config_connect (GObject *src,
GObject *dest);
void gimp_config_disconnect (GObject *src,
GObject *dest);
void gimp_config_copy_properties (GObject *src,
GObject *dest);
void gimp_config_reset_properties (GObject *object);
gchar * gimp_config_build_data_path (const gchar *name);
gchar * gimp_config_build_plug_in_path (const gchar *name);
void gimp_config_string_append_escaped (GString *string,
const gchar *val);
gchar * gimp_config_build_data_path (const gchar *name);
gchar * gimp_config_build_plug_in_path (const gchar *name);
#endif /* __GIMP_CONFIG_UTILS_H__ */
......@@ -41,6 +41,7 @@
#include "gimpconfig.h"
#include "gimpconfig-serialize.h"
#include "gimpconfig-utils.h"
#include "gimpconfigwriter.h"
#include "libgimp/gimpintl.h"
......@@ -194,26 +195,16 @@ gimp_config_writer_printf (GimpConfigWriter *writer,
}
void
gimp_config_writer_string (GimpConfigWriter *writer,
const gchar *string)
gimp_config_writer_string (GimpConfigWriter *writer,
const gchar *string)
{
g_return_if_fail (writer != NULL);
if (writer->error)
return;
if (string)
{
gchar *escaped = g_strescape (string, NULL);
g_string_append_printf (writer->buffer, " \"%s\"", escaped);
g_free (escaped);
}
else
{
g_string_append_len (writer->buffer, " \"\"", 3);
}
g_string_append_c (writer->buffer, ' ');
gimp_config_string_append_escaped (writer->buffer, string);
}
void
......
......@@ -216,7 +216,8 @@ output_unknown_token (const gchar *key,
*header = NULL;
}
g_print (" %s \"%s\"\n", key, value);
g_print (" %s \"%s\"\n", key, escaped);
g_free (escaped);
}
......
......@@ -410,17 +410,9 @@ gimp_container_serialize_foreach (GObject *object,
name = gimp_object_get_name (GIMP_OBJECT (object));
if (name)
{
gchar *escaped;
escaped = g_strescape (name, NULL);
gimp_config_writer_printf (serialize_data->writer, "\"%s\"", escaped);
g_free (escaped);
}
gimp_config_writer_string (serialize_data->writer, name);
else
{
gimp_config_writer_print (serialize_data->writer, "NULL", 4);
}
gimp_config_writer_print (serialize_data->writer, "NULL", 4);
serialize_data->success = config_iface->serialize (object,
serialize_data->writer,
......
......@@ -356,22 +356,16 @@ gimp_config_serialize_value (const GValue *value,
if (G_VALUE_HOLDS_STRING (value))
{
gchar *esc;
const gchar *cstr = g_value_get_string (value);
if (!cstr)
return FALSE;
if (escaped)
{
esc = g_strescape (cstr, NULL);
g_string_append_printf (str, "\"%s\"", esc);
g_free (esc);
}
gimp_config_string_append_escaped (str, cstr);
else
{
g_string_append (str, cstr);
}
g_string_append (str, cstr);
return TRUE;
}
......
......@@ -264,6 +264,92 @@ gimp_config_reset_properties (GObject *object)
}
/*
* GimpConfig string utilities
*/
/**
* gimp_config_string_append_escaped:
* @string: pointer to a #GString
* @val: a string to append or %NULL
*
* Escapes and quotes @val and appends it to @string. The escape
* algorithm is different from the one used by g_strescape() since it
* leaves non-ASCII characters intact and thus preserves UTF-8
* strings. Only control characters and quotes are being escaped.
**/
void
gimp_config_string_append_escaped (GString *string,
const gchar *val)
{
g_return_if_fail (string != NULL);
if (val)
{
const guchar *p;
gchar buf[4] = { '\\', 0, 0, 0 };
gint len;
g_string_append_c (string, '\"');
for (p = val, len = 0; *p; p++)
{
if (*p < ' ' || *p == '\\' || *p == '\"')
{
g_string_append_len (string, val, len);
len = 2;
switch (*p)
{
case '\b':
buf[1] = 'b';
break;
case '\f':
buf[1] = 'f';
break;
case '\n':
buf[1] = 'n';
break;
case '\r':
buf[1] = 'r';
break;
case '\t':
buf[1] = 't';
break;
case '\\':
case '"':
buf[1] = *p;
break;
default:
len = 4;
buf[1] = '0' + (((*p) >> 6) & 07);
buf[2] = '0' + (((*p) >> 3) & 07);
buf[3] = '0' + ((*p) & 07);
break;
}
g_string_append_len (string, buf, len);
val = p + 1;
len = 0;
}
else
{
len++;
}
}
g_string_append_len (string, val, len);
g_string_append_c (string, '\"');
}
else
{
g_string_append_len (string, "\"\"", 2);
}
}
/*
* GimpConfig path utilities
*/
......
......@@ -23,19 +23,22 @@
#define __GIMP_CONFIG_UTILS_H__
GList * gimp_config_diff (GObject *a,
GObject *b,
GParamFlags flags);
void gimp_config_connect (GObject *src,
GObject *dest);
void gimp_config_disconnect (GObject *src,
GObject *dest);
void gimp_config_copy_properties (GObject *src,
GObject *dest);
void gimp_config_reset_properties (GObject *object);
GList * gimp_config_diff (GObject *a,
GObject *b,
GParamFlags flags);
void gimp_config_connect (GObject *src,
GObject *dest);
void gimp_config_disconnect (GObject *src,
GObject *dest);
void gimp_config_copy_properties (GObject *src,
GObject *dest);
void gimp_config_reset_properties (GObject *object);
gchar * gimp_config_build_data_path (const gchar *name);
gchar * gimp_config_build_plug_in_path (const gchar *name);
void gimp_config_string_append_escaped (GString *string,
const gchar *val);
gchar * gimp_config_build_data_path (const gchar *name);
gchar * gimp_config_build_plug_in_path (const gchar *name);
#endif /* __GIMP_CONFIG_UTILS_H__ */
......@@ -41,6 +41,7 @@
#include "gimpconfig.h"
#include "gimpconfig-serialize.h"
#include "gimpconfig-utils.h"
#include "gimpconfigwriter.h"
#include "libgimp/gimpintl.h"
......@@ -194,26 +195,16 @@ gimp_config_writer_printf (GimpConfigWriter *writer,
}
void
gimp_config_writer_string (GimpConfigWriter *writer,
const gchar *string)
gimp_config_writer_string (GimpConfigWriter *writer,
const gchar *string)
{
g_return_if_fail (writer != NULL);
if (writer->error)
return;
if (string)
{
gchar *escaped = g_strescape (string, NULL);
g_string_append_printf (writer->buffer, " \"%s\"", escaped);
g_free (escaped);
}
else
{
g_string_append_len (writer->buffer, " \"\"", 3);
}
g_string_append_c (writer->buffer, ' ');
gimp_config_string_append_escaped (writer->buffer, string);
}
void
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment