Skip to content

Commit

Permalink
Forgot to handle Euro dates dd.mm.yyyy in the first-record exam (#8233)
Browse files Browse the repository at this point in the history
* Forgot to handle Euro dates dd.mm.yyyy in the first-record exam

See Forum post for background. Now fixed.

* Update gmt_io.c
  • Loading branch information
PaulWessel authored Dec 22, 2023
1 parent 12d15c5 commit a9a89ea
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/gmt_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ EXTERN_MSC unsigned int gmtlib_is_coordinate (struct GMT_CTRL *GMT, unsigned int
EXTERN_MSC unsigned int gmtlib_is_time (struct GMT_CTRL *GMT, char *text);
EXTERN_MSC unsigned int gmtlib_is_string (struct GMT_CTRL *GMT, char *string);
EXTERN_MSC unsigned int gmtlib_determine_datatype (struct GMT_CTRL *GMT, char *text);
EXTERN_MSC void gmtlib_string_parser (struct GMT_CTRL *GMT, char *file);
EXTERN_MSC unsigned int gmtlib_string_parser (struct GMT_CTRL *GMT, char *file);
EXTERN_MSC int gmtlib_adjust_we_if_central_lon_set (struct GMT_CTRL *GMT, double *west, double *east);
EXTERN_MSC int gmtlib_colon_pos (struct GMT_CTRL *GMT, char *text);
EXTERN_MSC bool gmtlib_invalid_symbolname (struct GMT_CTRL *GMT, char *name);
Expand Down
32 changes: 29 additions & 3 deletions src/gmt_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -10214,6 +10214,23 @@ unsigned int gmtlib_is_string (struct GMT_CTRL *GMT, char *string) {
n_specials = gmtio_count_special (GMT, p); /* Non-printables, hashtags, semi-columns, parentheses, etc. */
if (n_specials) return (GMT_IS_STRING); /* Cannot be coordinates */
n_periods = gmt_count_char (GMT, p, '.'); /* How many periods. No valid coordinate has more than one */
if (n_periods == 2 && n_digits) { /* Might be dd.mm.yyyy section */
if (n_text == 0 || (n_text == 1 && strchr (string, 'T'))) { /* Valid dd.mm.yyyy[T][hh:mm:ss.xxx...] string */
char *text = strdup (string), *c = NULL;
int d, m, y, k = 0;
if ((c = strchr (text, 'T'))) c[0] = '\0'; /* Truncate at the possible T, so no must be dd.mm.yyyy */
gmt_strrepc (text, '.', ' '); /* Replace the 2 periods with spaces */
if (sscanf (text, "%d %d %d", &d, &m, &y) != 3) {
gmt_M_str_free (text);
return (GMT_IS_STRING);
}
gmt_M_str_free (text);
if ((d < 1 || d > 31) || (m < 1 || m > 12)) return (GMT_IS_STRING);
return (GMT_IS_ABSTIME);
}
else
return (GMT_IS_STRING);
}
if (n_periods > 1) return (GMT_IS_STRING); /* Cannot be coordinates */
n_colons = gmt_count_char (GMT, p, ':'); /* Number of colons. Max is 2 for hh:mm:ss */
if (n_colons > 2) return (GMT_IS_STRING); /* Cannot be coordinates */
Expand All @@ -10229,6 +10246,7 @@ unsigned int gmtlib_determine_datatype (struct GMT_CTRL *GMT, char *text) {

unsigned int code = gmtlib_is_string (GMT, text);
if (code == GMT_IS_STRING) return (code); /* Detect junk first */
if (code == GMT_IS_ABSTIME) return (code); /* Detect Non-ISO European dates dd.mm.yyyy[T] */
if (gmtio_is_float (GMT, text, true, 0.0)) /* Found a plain float */
return (GMT_IS_FLOAT);
code = gmtio_is_dimension (GMT, text); /* Check for dimensions and intervals */
Expand All @@ -10243,21 +10261,28 @@ unsigned int gmtlib_determine_datatype (struct GMT_CTRL *GMT, char *text) {
}


void gmtlib_string_parser (struct GMT_CTRL *GMT, char *file)
{ /* Debug function for testing string parser gmtlib_determine_datatype on input list */
unsigned int gmtlib_string_parser (struct GMT_CTRL *GMT, char *file)
{ /* Debug function for testing string parser gmtlib_determine_datatype on input list.
* Expects input records to be <whateverarg>|NAME, e.g.
* 2001-12-24T20:01:02.333|ABSTIME
* and it will then echo out that and the name it detected examining <wateverarg> */
int k, c;
unsigned int kind;
FILE *fp = fopen (file, "r");
char line[GMT_LEN256] = {""};
if (fp == NULL) { /* Not good, wrong filename? */
GMT_Report (GMT->parent, GMT_MSG_ERROR, "Option -/: File %s not found\n", file);
return;
return (GMT_RUNTIME_ERROR);
}
while (gmt_fgets (GMT, line, GMT_LEN256, fp)) {
if (line[0] == '#') { /* Just a header; echo it out */
printf ("%s", line);
continue;
}
if (strchr (line, '|') == NULL) {
GMT_Report (GMT->parent, GMT_MSG_ERROR, "Data file for -/ testing does not have format <string>|<NAME>\n");
return (GMT_RUNTIME_ERROR);
}
gmt_chop (line); /* Remove trailing newline/CR */
k = strlen (line) - 1; /* Last position in line */
while (line[k] != '|') k--; /* Search backwards to the vertical bar */
Expand All @@ -10270,4 +10295,5 @@ void gmtlib_string_parser (struct GMT_CTRL *GMT, char *file)
printf ("%14s\n", gmtio_type_name(kind)); /* Print data type detected */
}
fclose (fp);
return (GMT_NOERROR);
}
4 changes: 2 additions & 2 deletions src/gmtconvert.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ static int parse (struct GMT_CTRL *GMT, struct GMTCONVERT_CTRL *Ctrl, struct GMT
char p[GMT_BUFSIZ] = {""}, *c = NULL;
struct GMT_OPTION *opt = NULL;
struct GMTAPI_CTRL *API = GMT->parent;
EXTERN_MSC void gmtlib_string_parser (struct GMT_CTRL *GMT, char *file); /* For debug only */
EXTERN_MSC unsigned int gmtlib_string_parser (struct GMT_CTRL *GMT, char *file); /* For debug only */

for (opt = options; opt; opt = opt->next) {
switch (opt->option) {
Expand All @@ -243,7 +243,7 @@ static int parse (struct GMT_CTRL *GMT, struct GMTCONVERT_CTRL *Ctrl, struct GMT
if (GMT_Get_FilePath (API, GMT_IS_DATASET, GMT_IN, GMT_FILE_REMOTE, &(opt->arg))) n_errors++;
/* Hidden test of string parsing for developers */
if (Ctrl->debug.active) {
gmtlib_string_parser (API->GMT, opt->arg);
if (gmtlib_string_parser (API->GMT, opt->arg)) return (GMT_RUNTIME_ERROR);
return (NOT_REALLY_AN_ERROR);
}
break;
Expand Down

0 comments on commit a9a89ea

Please sign in to comment.