[evolution-patches] evolution-addressbook-export escape less in csv export



[Resending as subscriber; http://bugzilla.ximian.com/show_bug.cgi?id=49519]

Hi,

when exporting in csv format evolution-addressbook-export escapes all
bytes > 127. For instance Körner becomes K\303\266rner. The
OpenOffice.org evolution data source doesn't unescape this, only ASCII
characters are displayed correctly. The attached patch replaces
g_strescape calls with a custom escape_string function similar to that
from the original evolution-addressbook-export patch(
http://bugzilla.ximian.com/show_bug.cgi?id=23036 ). It escapes only line
breaks, backslashes and quotation marks and leaves all other characters
utf-8-encoded. It escapes ``"'' to ``""'' which seems to be more common
than ``\"'' in csv-land.

This patch doesn't make sense if the majority of csv-processing apps out
there expect the current escaped utf-8 behavior, but I doubt that.


Martin

2003-10-09  Martin Kretzschmar  <martink ximian com>

	* addressbook/tools/evolution-addressbook-export-list-cards.c
	(escape_string): custom escape function, g_strescape escapes
	too much,
	(quote_string): removed, escape_string does quoting now,
	(e_card_simple_csv_get_name, e_card_simple_csv_get): use
	escape_string.


Index: addressbook/tools/evolution-addressbook-export-list-cards.c
===================================================================
RCS file: /cvs/gnome/evolution/addressbook/tools/evolution-addressbook-export-list-cards.c,v
retrieving revision 1.3
diff -u -p -r1.3 evolution-addressbook-export-list-cards.c
--- addressbook/tools/evolution-addressbook-export-list-cards.c	29 Apr 2003 18:39:45 -0000	1.3
+++ addressbook/tools/evolution-addressbook-export-list-cards.c	9 Oct 2003 13:37:38 -0000
@@ -23,6 +23,7 @@
 
 #include <config.h>
 
+#include <string.h>
 #include <sys/types.h>
 #include <unistd.h>
 
@@ -265,7 +266,7 @@ gchar *e_card_simple_to_csv (ECardSimple
 gchar *e_card_get_csv (ECard * card, GSList * csv_all_fields);
 gchar *delivery_address_get_sub_field (const ECardDeliveryAddress * delivery_address, DeliveryAddressField sub_field);
 gchar *check_null_pointer (gchar * orig);
-gchar *quote_string (gchar * orig);
+gchar *escape_string (gchar * orig);
 int output_n_cards_file (FILE * outputfile, ECardCursor * cursor, int size, int begin_no, CARD_FORMAT format);
 static void fork_to_background (void);
 static void action_list_cards_get_cursor_cb (EBook * book, EBookStatus status, ECardCursor * cursor, ActionContext * p_actctx);
@@ -287,7 +288,6 @@ e_card_simple_csv_get_name (ECardSimpleF
 {
 	gint simple_field;
 	gchar *name;
-	gchar *esc_name;
 	gchar *quoted_name;
 
 	ECardSimple *a_simple_card;
@@ -301,10 +301,8 @@ e_card_simple_csv_get_name (ECardSimpleF
 	} else {
 		name = g_strdup (csv_field_data[csv_field].csv_name);
 	}
-	esc_name = g_strescape (name, NULL);
+	quoted_name = escape_string (name);
 	g_free (name);
-	quoted_name = quote_string (esc_name);
-	g_free (esc_name);
 	return quoted_name;
 }
 
@@ -314,7 +312,6 @@ e_card_simple_csv_get (ECardSimple * sim
 {
 	gint simple_field;
 	gchar *field_value;
-	gchar *esc_field_value;
 	gchar *quoted_field_value;
 
 	const ECardDeliveryAddress *delivery_address = NULL;
@@ -407,12 +404,9 @@ e_card_simple_csv_get (ECardSimple * sim
 	if (field_value == NULL)
 		field_value =  g_strdup ("");
 
-	esc_field_value = g_strescape (field_value, NULL);
+	quoted_field_value = escape_string (field_value);
 	g_free (field_value);
 
-	quoted_field_value = quote_string (esc_field_value);
-	g_free (esc_field_value);
-
 	return quoted_field_value;
 }
 
@@ -540,11 +534,50 @@ gchar *delivery_address_get_sub_field (c
 }
 
 gchar *
-quote_string (gchar *orig)
+escape_string (gchar *orig)
 {
+	const guchar *p;
+	gchar *dest;
+	gchar *q;
+
 	if (orig == NULL)
 		return g_strdup ("\"\"");
-	return g_strdup_printf("\"%s\"", orig);
+
+	p = (guchar *) orig;
+	/* Each source byte needs maximally two destination chars (\n), and the extra 2 is for the leading and trailing '"' */
+	q = dest = g_malloc (strlen (orig) * 2 + 1 + 2);
+
+	*q++ = '\"';
+	while (*p)
+	{
+		switch (*p)
+		{
+		case '\n':
+			*q++ = '\\';
+			*q++ = 'n';
+			break;
+		case '\r':
+			*q++ = '\\';
+			*q++ = 'r';
+			break;
+		case '\\':
+			*q++ = '\\';
+			*q++ = '\\';
+			break;
+		case '"':
+			*q++ = '"';
+			*q++ = '"';
+			break;
+		default:
+			*q++ = *p;
+		}
+		p++;
+	}
+
+	*q++ = '\"';
+	*q = 0;
+
+	return dest;
 }
 
 int


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]