From c268b757dfc0aa9aa4d1b7fcd500660709e4d284 Mon Sep 17 00:00:00 2001 From: Gehad Date: Thu, 3 Apr 2014 21:00:13 +0200 Subject: Fixing dive notes escape characters in worldmap exporter Replacing the newlines in the string with
and changing the single quote to its HTML number. Also minor coding style updates to previous commit. Signed-off-by: Gehad elrobey Signed-off-by: Dirk Hohndel --- worldmap-save.c | 111 ++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 79 insertions(+), 32 deletions(-) (limited to 'worldmap-save.c') diff --git a/worldmap-save.c b/worldmap-save.c index f9a4bf3fe..620c4675c 100644 --- a/worldmap-save.c +++ b/worldmap-save.c @@ -7,13 +7,13 @@ #include "worldmap-save.h" #include "worldmap-options.h" -char* getGoogleApi() +char *getGoogleApi() { /* google maps api auth*/ return "https://maps.googleapis.com/maps/api/js?key=AIzaSyDzo9PWsqYDDSddVswg_13rpD9oH_dLuoQ"; } -void put_HTML_date(struct membuffer *b,struct dive *dive) +void put_HTML_date(struct membuffer *b, struct dive *dive) { struct tm tm; utc_mkdate(dive->when, &tm); @@ -21,16 +21,65 @@ void put_HTML_date(struct membuffer *b,struct dive *dive) put_format(b, "

time=%02u:%02u:%02u

",tm.tm_hour, tm.tm_min, tm.tm_sec); } -void put_HTML_temp(struct membuffer *b,struct dive *dive) +void put_HTML_temp(struct membuffer *b, struct dive *dive) { put_temperature(b, dive->airtemp, "

Air Temp: ", " C\\'

"); put_temperature(b, dive->watertemp, "

Water Temp: ", " C\\'

"); } -void put_HTML_notes(struct membuffer *b,struct dive *dive) +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 = strdup(str); + char *p0, *p1; + if (!result || !temp) + return 0; + 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', "
"); + char *single_quotes_removed = replace_char(new_line_removed, '\'', "'"); + free(new_line_removed); + return single_quotes_removed; +} + +void put_HTML_notes(struct membuffer *b, struct dive *dive) { if (dive->notes) { - put_format(b,"

Notes : %s

",dive->notes); + char *notes = quote(dive->notes); + put_format(b,"

Notes : %s

", notes); + free(notes); } } @@ -42,20 +91,19 @@ void writeMarkers(struct membuffer *b) for_each_dive(i, dive) { /*export selected dives only ?*/ - if (dive->latitude.udeg==0 && dive->longitude.udeg==0) { + if (dive->latitude.udeg == 0 && dive->longitude.udeg == 0) continue; - } - put_format(b,"temp = new google.maps.Marker({position: new google.maps.LatLng(%f,%f)});\n", - dive->latitude.udeg/1000000.0,dive->longitude.udeg/1000000.0); - put_string(b,"markers.push(temp);\ntempinfowindow = new google.maps.InfoWindow({content: '
'+'
'+'
'+'
"); - put_HTML_date(b,dive); + put_format(b, "temp = new google.maps.Marker({position: new google.maps.LatLng(%f,%f)});\n", + dive->latitude.udeg/1000000.0, dive->longitude.udeg/1000000.0); + put_string(b, "markers.push(temp);\ntempinfowindow = new google.maps.InfoWindow({content: '
'+'
'+'
'+'
"); + put_HTML_date(b, dive); put_duration(b, dive->duration, "

duration=", " min

"); - put_HTML_temp(b,dive); - put_HTML_notes(b,dive); - put_string(b,"

'+'
'+'
'});\ninfowindows.push(tempinfowindow);\n"); - put_format(b,"google.maps.event.addListener(markers[%d], 'mouseover', function() {\ninfowindows[%d].open(map,markers[%d]);}",i,i,i); - put_format(b,");google.maps.event.addListener(markers[%d], 'mouseout', function() {\ninfowindows[%d].close();});\n",i,i); + put_HTML_temp(b, dive); + put_HTML_notes(b, dive); + put_string(b, "

'+'
'+'
'});\ninfowindows.push(tempinfowindow);\n"); + put_format(b, "google.maps.event.addListener(markers[%d], 'mouseover', function() {\ninfowindows[%d].open(map,markers[%d]);}", i, i, i); + put_format(b, ");google.maps.event.addListener(markers[%d], 'mouseout', function() {\ninfowindows[%d].close();});\n", i, i); } } @@ -67,21 +115,21 @@ void insert_html_header(struct membuffer *b) void insert_css(struct membuffer *b) { - put_format(b,"\n",css); + put_format(b, "\n", css); } void insert_javascript(struct membuffer *b) { - put_string(b,"\n\n\n"); + put_string(b, "\nfor(var i=0;i\n"); } void export(struct membuffer *b) @@ -89,22 +137,21 @@ void export(struct membuffer *b) insert_html_header(b); insert_css(b); insert_javascript(b); - put_string(b,"\t\n\n
\n\n"); + put_string(b, "\t\n\n
\n\n"); } -void export_worldmap_HTML(const char* file_name) +void export_worldmap_HTML(const char *file_name) { FILE *f; struct membuffer buf = { 0 }; export(&buf); - f = fopen(file_name,"w+"); - if (!f) { + f = fopen(file_name, "w+"); + if (!f) printf("error"); /*report error*/ - } - flush_buffer(&buf,f); /*check for writing errors? */ + flush_buffer(&buf, f); /*check for writing errors? */ free_buffer(&buf); fclose(f); } -- cgit v1.2.3-70-g09d2