From 392dfcc84e809f01c42ad37f57acfd05e629553f Mon Sep 17 00:00:00 2001 From: Paul Wessel Date: Mon, 3 Jul 2023 19:34:35 +0200 Subject: [PATCH] Handle commas in meca if -A and CSV (#7599) * Handle commas in meca if -A and CSV WIth -A we examine the trailing text, but if those items are comma-separated then the scanning will fail since the format assumes white-space. This PR counts commas in trailing text and if 2 we change the scanning. * Update psmeca.c * Avoid read-only string changes * same treatment in pscoupe --- src/seis/pscoupe.c | 7 ++++++- src/seis/psmeca.c | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/seis/pscoupe.c b/src/seis/pscoupe.c index 388a02f906c..70b4635ef69 100644 --- a/src/seis/pscoupe.c +++ b/src/seis/pscoupe.c @@ -1294,7 +1294,12 @@ EXTERN_MSC int GMT_pscoupe (void *V_API, int mode, void *args) { /* Must examine the trailing text for optional columns: newX, newY and title */ /* newX and newY are not used in pscoupe, but we promised psmeca and pscoupe can use the same input file */ if (has_text) { - n_scanned = sscanf (S->text[row], "%s %s %[^\n]s\n", Xstring, Ystring, event_title); + unsigned int n_comma = gmt_count_char (GMT, S->text[row], ','); + char tmp_buffer[GMT_LEN256] = {""}; /* Local buffer in case S->text is read-only */ + strncpy (tmp_buffer, S->text[row], GMT_LEN256); + if (n_comma == 2) /* CSV file so we must handle that */ + gmt_strrepc (tmp_buffer, ',', ' '); + n_scanned = sscanf (tmp_buffer, "%s %s %[^\n]s\n", Xstring, Ystring, event_title); if (n_scanned >= 2) { /* Got new x,y coordinates and possibly event title */ unsigned int type; if (GMT->current.setting.io_lonlat_toggle[GMT_IN]) { /* Expect lat lon but watch for junk */ diff --git a/src/seis/psmeca.c b/src/seis/psmeca.c index 3d54bf010e1..cd30f824362 100644 --- a/src/seis/psmeca.c +++ b/src/seis/psmeca.c @@ -835,7 +835,12 @@ EXTERN_MSC int GMT_psmeca (void *V_API, int mode, void *args) { /* Must examine the trailing text for optional columns: newX, newY and title */ if (S->text && S->text[row]) { - n_scanned = sscanf (S->text[row], "%s %s %[^\n]s\n", Xstring, Ystring, event_title); + unsigned int n_comma = gmt_count_char (GMT, S->text[row], ','); + char tmp_buffer[GMT_LEN256] = {""}; /* Local buffer in case S->text is read-only */ + strncpy (tmp_buffer, S->text[row], GMT_LEN256); + if (n_comma == 2) /* CSV file so we must handle that */ + gmt_strrepc (tmp_buffer, ',', ' '); + n_scanned = sscanf (tmp_buffer, "%s %s %[^\n]s\n", Xstring, Ystring, event_title); if (n_scanned >= 2) { /* Got new x,y coordinates and possibly event title */ unsigned int type; if (GMT->current.setting.io_lonlat_toggle[GMT_IN]) { /* Expect lat lon but watch for junk */