[Commits] vcard-extension.c 1.7 1.8

miras at claws-mail.org miras at claws-mail.org
Mon Jan 28 00:54:43 CET 2013


Update of /home/claws-mail/contacts/extensions/vcard/src
In directory srv:/tmp/cvs-serv23166/extensions/vcard/src

Modified Files:
	vcard-extension.c 
Log Message:
2013-01-27 [mir]	0.6.0cvs91

	* extensions/vcard/src/vcard-extension.c
	    Improve importer to be able to handle
	    files with lines wrapped after 72 chars.

Index: vcard-extension.c
===================================================================
RCS file: /home/claws-mail/contacts/extensions/vcard/src/vcard-extension.c,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -d -r1.7 -r1.8
--- vcard-extension.c	27 Jan 2013 18:45:04 -0000	1.7
+++ vcard-extension.c	27 Jan 2013 23:54:41 -0000	1.8
@@ -56,6 +56,12 @@
 
 #define NAME "vCard extension"
 
+typedef struct {
+	gchar* buffer;
+	long   size;
+} NormalizeFile;
+
+
 static guint my_id;
 
 static gchar* vcard_file_chooser(MenuItem* item, int mode) {
@@ -96,9 +102,62 @@
 	g_free(msg);
 }
 
+static NormalizeFile* buffer_resize(NormalizeFile* buffer, long size) {
+	if (buffer->buffer) {
+		buffer->buffer = g_renew(gchar, buffer->buffer, size);
+	}
+	else {
+		buffer->buffer = g_new0(gchar, size);
+	}
+	buffer->size = size;
+
+	return buffer;
+}
+
+static NormalizeFile* normalize_file(const MainWindow* mainwindow, gchar* filename) {
+	NormalizeFile* file_buffer = NULL;
+	size_t numread;
+	gchar buf[BUFSIZ];
+	int i;
+	int size = 0;
+	long destpos = 0;
+	
+	FILE* fp = g_fopen(filename, "r");
+	if (! fp) {
+		show_message(mainwindow->window, GTK_UTIL_MESSAGE_ERROR, strerror(errno));
+		return NULL;
+	}
+	file_buffer = g_new0(NormalizeFile, 1);
+	while ((numread = fread(&buf, BUFSIZ, 1, fp)) != 0) {
+		size++;
+		file_buffer = buffer_resize(file_buffer, size * BUFSIZ);
+		if (! file_buffer->buffer) {
+			show_message(mainwindow->window, GTK_UTIL_MESSAGE_ERROR,
+				_("Insufficient memory"));
+			g_free(file_buffer->buffer);
+			g_free(file_buffer);
+			return NULL;
+		}
+		for (i = 0; i < BUFSIZ && buf[i]; i++) {
+			if (destpos > 0 && file_buffer->buffer[destpos - 1] == '\n' && buf[i] == ' ') {
+				destpos--;
+			}
+			else {
+				file_buffer->buffer[destpos++] = buf[i];
+			}
+		}
+	}
+	fclose(fp);
+	if ((size * BUFSIZ) % destpos) {
+		file_buffer = buffer_resize(file_buffer, destpos + 1);
+	}
+	file_buffer->buffer[++destpos] = 0;
+	
+	return file_buffer;
+}
+
 static void vcard_import(GtkWidget* widget, gpointer data) {
 	MenuItem* item = (MenuItem *) data;
-	FILE* fp;
 	VObject *t, *v;
 	gchar* error = NULL;
 	AddressBook* abook;
@@ -130,19 +189,19 @@
 	gchar* file = vcard_file_chooser(item, GTK_FILE_CHOOSER_ACTION_OPEN);
 	if (! file)
 		return;
-	fp = g_fopen(file, "r");
-	if (! fp) {
-		show_message(item->mainwindow->window, GTK_UTIL_MESSAGE_ERROR, strerror(errno));
-		g_free(file);
+	NormalizeFile* file_buffer = normalize_file(item->mainwindow, file);
+	g_free(file);
+	if (! file_buffer || ! file_buffer->buffer) {
+		if (file_buffer)
+			g_free(file_buffer);
 		return;
 	}
-	g_free(file);
 		
 	gchar* filename = g_strdup_printf("vcard_rejected.%d", getpid());
 	leftover = g_build_filename(get_home(), filename, NULL);
 	g_free(filename);
 	registerMimeErrorHandler(mime_error_handler, data);
-	v = Parse_MIME_FromFile(fp);
+	v = Parse_MIME(file_buffer->buffer, file_buffer->size);
 	while (v) {
 		total++;
 		const gchar* name = vObjectName(v);
@@ -181,6 +240,13 @@
 		  	  "Rejected Objects can be found in %s"),
 			  abook->abook_name, count, total, leftover);
 	}
+	else {
+		g_file_set_contents(leftover, file_buffer->buffer, file_buffer->size, NULL);
+		show_message(item->mainwindow->window, GTK_UTIL_MESSAGE_INFO,
+			_("Parsed file can be found in %s"), leftover);
+	}
+	g_free(file_buffer->buffer);
+	g_free(file_buffer);
 	g_free(leftover);
 }
 



More information about the Commits mailing list