Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP Improve grdcall.c string breaking #5972

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Improve string breaking
  • Loading branch information
PaulWessel committed Nov 10, 2021
commit 234ca99a714cddf443798640b963d8550024fae1
12 changes: 6 additions & 6 deletions src/gmt_gdalcall.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ GMT_LOCAL GDALDatasetH gdal_vector (struct GMT_CTRL *GMT, char *fname) {

/* ------------------------------------------------------------------------------------------------------------ */
GMT_LOCAL char ** breakMe(struct GMT_CTRL *GMT, char *in) {
/* Breake a string "-aa -bb -cc dd" into tokens "-aa" "-bb" "-cc" "dd" */
/* Break a string "-mo TIFFTAG_XRESOLUTION=300 -a_srs '+proj=stere +lat_0=90'" into tokens */
/* Based on GMT_Create_Options() */
unsigned int pos = 0, k, n_args = 0;
bool quoted;
Expand All @@ -122,11 +122,11 @@ GMT_LOCAL char ** breakMe(struct GMT_CTRL *GMT, char *in) {
txt_in = strdup (in);
args = gmt_M_memory (GMT, NULL, n_alloc, char *);

/* txt_in can contain options that take multi-word text strings, e.g., -B+t"My title". We avoid the problem of splitting
* these items by temporarily replacing spaces inside quoted strings with ASCII 31 US (Unit Separator), do the strtok on
/* txt_in can contain options that take multi-word text strings, e.g., '+proj=stere +lat_0=90'. We avoid the problem of splitting
* these items by temporarily replacing spaces inside single quoted strings with ASCII 31 US (Unit Separator), do the strtok on
* space, and then replace all ASCII 31 with space at the end (we do the same for tab using ASCII 29 GS (group separator) */
for (k = 0, quoted = false; txt_in[k]; k++) {
if (txt_in[k] == '\"') quoted = !quoted; /* Initially false, becomes true at start of quote, then false when exit quote */
if (txt_in[k] == '\'') quoted = !quoted; /* Initially false, becomes true at start of quote, then false when exit quote */
else if (quoted && txt_in[k] == '\t') txt_in[k] = GMT_ASCII_GS;
else if (quoted && txt_in[k] == ' ') txt_in[k] = GMT_ASCII_US;
}
Expand All @@ -139,7 +139,7 @@ GMT_LOCAL char ** breakMe(struct GMT_CTRL *GMT, char *in) {
p[k] = ' '; /* Replace spaces and tabs masked above */
}
for (i = o = 0; p[i]; i++)
if (p[i] != '\"') p[o++] = p[i]; /* Ignore double quotes */
if (p[i] != '\"') p[o++] = p[i]; /* Ignore any double quotes */
p[o] = '\0';
args[n_args++] = strdup(p);

Expand All @@ -148,7 +148,7 @@ GMT_LOCAL char ** breakMe(struct GMT_CTRL *GMT, char *in) {
args = gmt_M_memory(GMT, args, n_alloc, char *);
}
}
for (k = 0; txt_in[k]; k++) /* Restore input string to prestine condition */
for (k = 0; txt_in[k]; k++) /* Restore input string to pristine condition */
if (txt_in[k] == GMT_ASCII_GS) txt_in[k] = '\t';
else if (txt_in[k] == GMT_ASCII_US) txt_in[k] = ' '; /* Replace spaces and tabs masked above */

Expand Down