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
Show file tree
Hide file tree
Changes from all commits
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
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] == '\'' || 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[i] == '\"')) p[o++] = p[i]; /* Ignore any single or 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
26 changes: 10 additions & 16 deletions src/psconvert.c
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ static int usage (struct GMTAPI_CTRL *API, int level) {
GMT_Usage (API, -2, "Note: The EPS format can be combined with any of the other formats. "
"For example, -Tef creates both an EPS and PDF file.");
GMT_Option (API, "V");
GMT_Usage (API, -2, "Note: Shows the gdal_translate command, in case you want to use this program "
GMT_Usage (API, -2, "Note: Shows the grdgdal command, in case you want to use this program "
"to create a geoTIFF file.");
GMT_Usage (API, 1, "\n-W[+a<mode>[<alt]][+c][+f<minfade>/<maxfade>][+g][+k][+l<lodmin>/<lodmax>][+n<name>][+o<folder>][+t<title>][+u<URL>]");
GMT_Usage (API, -2, "Write an ESRI type world file suitable to make .tif files "
Expand All @@ -735,8 +735,7 @@ static int usage (struct GMTAPI_CTRL *API, int level) {
"computations. The world file naming follows the convention of jamming "
"a 'w' in the file extension. So, if the output is tif (-Tt) the world "
"file is a .tfw, for jpeg a .jgw, and so on. A few modifiers are available:");
GMT_Usage (API, 3, "+g Do a system call to gdal_translate and produce a true "
"eoTIFF image right away. The output file will have the extension "
GMT_Usage (API, 3, "+g Produce a true geoTIFF image right away. The output file will have the extension "
".tiff. See the man page for other 'gotchas'. Automatically sets -A -P.");
GMT_Usage (API, 3, "+k Create a minimalist KML file that allows loading the "
"image in Google Earth. Note that for this option the image must be "
Expand Down Expand Up @@ -1596,7 +1595,7 @@ EXTERN_MSC int GMT_psconvert (void *V_API, int mode, void *args) {
char ps_file[PATH_MAX] = "", no_U_file[PATH_MAX] = "", clean_PS_file[PATH_MAX] = "", tmp_file[PATH_MAX] = "",
out_file[PATH_MAX] = "", BB_file[PATH_MAX] = "", resolution[GMT_LEN128] = "", jpeg_device[GMT_LEN16] = {""};
char *line = NULL, c1[20] = {""}, c2[20] = {""}, c3[20] = {""}, c4[20] = {""}, GSstring[GMT_LEN64] = {""},
cmd[GMT_BUFSIZ] = {""}, proj4_name[20] = {""}, *quiet = NULL;
cmd[GMT_BUFSIZ] = {""}, proj4_name[20] = {""};
char *gs_BB = NULL, *proj4_cmd = NULL;
char *device[N_GS_DEVICES] = {"", "pdfwrite", "svg", "jpeg", "png16m", "ppmraw", "tiff24nc", "bmp16m", "pngalpha",
"jpeggray", "pnggray", "tiffgray", "bmpgray"};
Expand Down Expand Up @@ -2720,19 +2719,14 @@ EXTERN_MSC int GMT_psconvert (void *V_API, int mode, void *args) {
world_file[pos_ext] = '\0';
strcat (world_file, ".tiff");

if (GMT->current.setting.verbose < GMT_MSG_WARNING) /* Shut up the gdal_translate (low level) verbosity */
quiet = " -quiet";
else
quiet = "";

sprintf (cmd, "gdal_translate -mo TIFFTAG_XRESOLUTION=%g -mo TIFFTAG_YRESOLUTION=%g -a_srs %c%s%c "
"-co COMPRESS=LZW -co TILED=YES %s %c%s%c %c%s%c",
Ctrl->E.dpi, Ctrl->E.dpi, quote, proj4_cmd, quote, quiet, quote, out_file, quote, quote, world_file, quote);
sprintf (cmd, "%c%s%c -Atranslate -M -G%c%s%c -F\"-mo TIFFTAG_XRESOLUTION=%g -mo TIFFTAG_YRESOLUTION=%g -a_srs %c%s%c "
"-co COMPRESS=LZW -co TILED=YES\"",
quote, out_file, quote, quote, world_file, quote, Ctrl->E.dpi, Ctrl->E.dpi, quote, proj4_cmd, quote);
gmt_M_str_free (proj4_cmd);
sys_retval = system (cmd); /* Execute the gdal_translate command */
GMT_Report (API, GMT_MSG_INFORMATION, "The gdal_translate command: \n%s\n", cmd);
if (sys_retval) {
GMT_Report (API, GMT_MSG_ERROR, "System call [%s] returned error %d.\n", cmd, sys_retval);
GMT_Report (API, GMT_MSG_INFORMATION, "The grdgdal command: \n%s\n", cmd);

if (GMT_Call_Module (API, "grdgdal", GMT_MODULE_CMD, cmd) != GMT_OK) { /* Failed to do the conversion */
GMT_Report (API, GMT_MSG_ERROR, "Call to grdgdal [%s] returned error %d.\n", cmd, sys_retval);
Return (GMT_RUNTIME_ERROR);
}
if (!Ctrl->T.active) /* Get rid of the intermediate JPG file if -T was not set */
Expand Down