Skip to content

Commit

Permalink
Fix #if test for geodata_address
Browse files Browse the repository at this point in the history
  • Loading branch information
David P. Chassin committed Apr 16, 2024
1 parent 3052402 commit f7368b4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
14 changes: 13 additions & 1 deletion docs/GLM/Macro/If.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,19 @@ GLM:

# Description

The `#if` macro introduces a conditional section of a GLM file. The `#else` and `#endif` macros are used to introduce the alternative section and terminate the conditional section.
The `#if` macro introduces a conditional section of a GLM file. The `#else`
and `#endif` macros are used to introduce the alternative section and
terminate the conditional section.

Valid comparisons are
* ==
* !=
* <
* <=
* >
* >=
* in
* not_in

# Examples

Expand Down
12 changes: 7 additions & 5 deletions source/load.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7621,13 +7621,13 @@ int GldLoader::process_macro(char *line, int size, char *_filename, int linenum)
}
else if (strncmp(line,"#if",3)==0)
{
char left[1024], op[4];
char left[1024], op[8];
const char *value;
char right[1024];
if ( sscanf(line+4,"\"%1023[^\"]\" %3[!<>=] \"%1023[^\"]\"",left,op,right) < 3
&& sscanf(line+4,"%1023[^!<>= \t] %3[!<>=] \"%1023[^\"]\"",left,op,right) < 3
&& sscanf(line+4,"\"%1023[^\"]\" %3[!<>=] %1023s",left,op,right) < 3
&& sscanf(line+4,"%1023[^!<>= \t] %3[!<>=] %1023s",left,op,right) < 3 )
if ( sscanf(line+4,"\"%1023[^\"]\" %7[!<>=inot_] \"%1023[^\"]\"",left,op,right) < 3
&& sscanf(line+4,"%1023[^!<>= \t] %7[!<>=inot_] \"%1023[^\"]\"",left,op,right) < 3
&& sscanf(line+4,"\"%1023[^\"]\" %7[!<>=inot_] %1023s",left,op,right) < 3
&& sscanf(line+4,"%1023[^!<>= \t] %7[!<>=inot_] %1023s",left,op,right) < 3 )
{
syntax_error(filename,linenum,"#if macro statement syntax error");
strcpy(line,"\n");
Expand All @@ -7651,6 +7651,8 @@ int GldLoader::process_macro(char *line, int size, char *_filename, int linenum)
else if (strcmp(op,"<=")==0) { if (!(strcmp(value,right)<=0)) suppress|=(1<<nesting); }
else if (strcmp(op,"==")==0) { if (!(strcmp(value,right)==0)) suppress|=(1<<nesting); }
else if (strcmp(op,"!=")==0) { if (!(strcmp(value,right)!=0)) suppress|=(1<<nesting); }
else if (strcmp(op,"in")==0) { if (!(strstr(value,right)!=NULL)) suppress|=(1<<nesting);}
else if (strcmp(op,"not_in")==0) { if (!(strstr(value,right)==NULL)) suppress|=(1<<nesting);}
else
{
syntax_error(filename,linenum,"operator %s is not recognized",op);
Expand Down
6 changes: 3 additions & 3 deletions subcommands/autotest/test_geodata_address.glm
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#setenv REQ=38.8977,-77.036553
#define RES=${SHELL gridlabd geodata merge -D address "$REQ" -f RAW:address}
#define ANS=White House, 1600, Pennsylvania Avenue Northwest, Washington, District of Columbia, 20500, United States
#if "${RES:1:11}" != "${ANS:1:11}"
#error "geodata -D address location ${REQ}" is incorrect: expected '${ANS:1:11}...' but got '${RES:1:11}...' instead
#define ANS=1600, Pennsylvania Avenue Northwest
#if ${ANS} in ${RES}
#error "geodata -D address location ${REQ}" is incorrect: expected '${ANS}' in '${RES}'
#endif

0 comments on commit f7368b4

Please sign in to comment.