On 04/01/2014 06:50 AM, Dirk Hohndel wrote:
b) I know that different people have different preferences there, but
from an API perspective I would prefer replace_char(string, old, new)
I changed the order of the arguments, I am not sure if you me to change the names too.
c) I'm not in love with the realloc inside the loop. I realize that the
code is correct and that this maybe isn't the most performance critical
part of our application, but still... you could either brute force it
(worst case assumption of len(string)/len(old)*len(new) or you could
count the occurrences of old and then calculate the correct size of the
allocation
I don't usually use realloc() but I found it used a lot in the membuffer functions, I changed it to one memory allocation in the start after calculating the new size.
I tried to follow the coding style in this patch.

--
Regards,
Gehad Elrobey

>From a39dc1ffa549e9344ffc72d795850017b78ac4ee Mon Sep 17 00:00:00 2001
From: Gehad elrobey <[email protected]>
Date: Tue, 1 Apr 2014 13:02:37 +0200
Subject: [PATCH 3/3] 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 <[email protected]>
---
 worldmap-save.c | 46 +++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 45 insertions(+), 1 deletion(-)

diff --git a/worldmap-save.c b/worldmap-save.c
index f9a4bf3..be08fcc 100644
--- a/worldmap-save.c
+++ b/worldmap-save.c
@@ -27,10 +27,54 @@ 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.
+	*/
+
+	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);
+	if (!result || !temp) {
+		return "";
+	}
+	strcpy(result, str);
+	char* ptr = strchr(result, replace);
+	while (ptr) {
+		*ptr='\0';
+		strcpy(temp, result);
+		strcat(temp, replace_by);
+		strcat(temp, ptr + 1);
+		strcpy(result, temp);
+		ptr = strchr(result, replace);
+	}
+	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);
+		put_format(b,"<p>Notes : %s </p>",quote(dive->notes));
 	}
 }
 
-- 
1.8.3.2

_______________________________________________
subsurface mailing list
[email protected]
http://lists.hohndel.org/cgi-bin/mailman/listinfo/subsurface

Reply via email to