changeset 210:3fa93ab9b119

Improve escaping and quoting of strings saved in rc files.
author zas_
date Sat, 29 Mar 2008 21:47:10 +0000
parents ad78ad18523a
children d43153435be4
files src/rcfile.c
diffstat 1 files changed, 41 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/rcfile.c	Sat Mar 29 14:59:14 2008 +0000
+++ b/src/rcfile.c	Sat Mar 29 21:47:10 2008 +0000
@@ -29,8 +29,9 @@
 	const gchar *ptr;
 	gint c = 0;
 	gint l = strlen(text);
-
-	if (l == 0) return NULL;
+	gchar *retval = NULL;
+	
+	if (l == 0) return retval;
 
 	while (c < l && text[c] !='"') c++;
 	if (text[c] == '"')
@@ -39,12 +40,22 @@
 		c++;
 		ptr = text + c;
 		e = c;
-		while (e < l && text[e] !='"') e++;
+		while (e < l)
+			{
+			if (text[e-1] != '\\' && text[e] == '"') break;
+			e++;
+			}
 		if (text[e] == '"')
 			{
 			if (e - c > 0)
 				{
-				return g_strndup(ptr, e - c);
+				gchar *substring = g_strndup(ptr, e - c);
+				
+				if (substring)
+					{
+					retval = g_strcompress(substring);
+					g_free(substring);
+					}
 				}
 			}
 		}
@@ -56,17 +67,39 @@
 		while (c < l && text[c] !=' ' && text[c] !=8 && text[c] != '\n') c++;
 		if (c != 0)
 			{
-			return g_strndup(text, c);
+			retval = g_strndup(text, c);
 			}
 		}
 
-	return NULL;
+	return retval;
+}
+
+gchar *escquote_value(const gchar *text)
+{
+	gchar *e;
+	gchar *retval;
+
+	if (!text) return g_strdup("");
+
+	e = g_strescape(text, "");
+	if (e)
+		{
+		gchar *retval = g_strdup_printf("\"%s\"", e);
+		g_free(e);
+		return retval;
+		}
+	return g_strdup("");
 }
 
 static void write_char_option(FILE *f, gchar *label, gchar *text)
 {
-	if (text)
-		fprintf(f,"%s: \"%s\"\n", label, text);
+	gchar *escval = escquote_value(text);
+
+	if (escval)
+		{
+		fprintf(f,"%s: %s\n", label, escval);
+		g_free(escval);
+		}
 	else
 		fprintf(f,"%s: \n", label);
 }