I couldn't find an easy way to use the memBuffer but I added Miika's optimization, and fixed the memory leak. I think this is working fine.

Regards,
Gehad elrobey
>From d5df6b5c8d6274135edfc8d9ea98864c768fea27 Mon Sep 17 00:00:00 2001
From: Gehad elrobey <gehadelro...@gmail.com>
Date: Tue, 1 Apr 2014 21:30:03 +0200
Subject: [PATCH] Fixing dive notes escape characters in worldmap exporter

Replacing the newlines in the string with <br> and changing the single
quote to its HTML number.

Signed-off-by: Gehad elrobey <gehadelro...@gmail.com>
---
 worldmap-save.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 54 insertions(+), 1 deletion(-)

diff --git a/worldmap-save.c b/worldmap-save.c
index f9a4bf3..2222f13 100644
--- a/worldmap-save.c
+++ b/worldmap-save.c
@@ -27,10 +27,63 @@ void put_HTML_temp(struct membuffer *b,struct dive *dive)
 	put_temperature(b, dive->watertemp, "<p>Water Temp: ", " C\\'</p>");
 }
 
+char* replace_char(char * str, char replace, char* replace_by)
+{
+	/*
+		this fumction can't replace a character with a substring
+		where the substring contains the character, infinte loop.
+	*/
+
+	if (!str) {
+		return 0;
+	}
+
+	int i=0, char_count=0, new_size;
+
+	while (str[i]!='\0') {
+		if (str[i]==replace) {
+			char_count++;
+		}
+		i++;
+	}
+
+	new_size = strlen(str) + char_count * strlen(replace_by) + 1;
+	char* result = malloc(new_size);
+	char* temp = malloc(new_size);
+	char *p0, *p1;
+	if (!result || !temp) {
+		return 0;
+	}
+	strcpy(temp, str);
+	result[0] = '\0';
+	p0=temp;
+	p1 = strchr(temp, replace);
+	while (p1) {
+		*p1 = '\0';
+		strcat(result, p0);
+		strcat(result, replace_by);
+		p0 = p1 +1;
+		p1 = strchr(p0, replace);
+	}
+	strcat(result, p0);/*concat the rest of the string*/
+	free(temp);
+	return result;
+}
+
+char* quote(char * string)
+{
+	char* new_line_removed = replace_char(string,'\n',"<br>");
+	char* single_quotes_removed = replace_char(new_line_removed,'\'',"&#39;");
+	free(new_line_removed);
+	return single_quotes_removed;
+}
+
 void put_HTML_notes(struct membuffer *b,struct dive *dive)
 {
 	if (dive->notes) {
-		put_format(b,"<p>Notes : %s </p>",dive->notes);
+		char* notes = quote(dive->notes);
+		put_format(b,"<p>Notes : %s </p>",notes);
+		free(notes);
 	}
 }
 
-- 
1.8.3.2

_______________________________________________
subsurface mailing list
subsurface@hohndel.org
http://lists.hohndel.org/cgi-bin/mailman/listinfo/subsurface

Reply via email to