add is_file() special variable function and unit tests for it

This commit is contained in:
Axel Kohlmeyer
2021-04-02 12:07:43 -04:00
parent c3eb52f46a
commit 2dfafe4adb
3 changed files with 39 additions and 7 deletions

View File

@ -65,7 +65,7 @@ enum{DONE,ADD,SUBTRACT,MULTIPLY,DIVIDE,CARAT,MODULO,UNARY,
SQRT,EXP,LN,LOG,ABS,SIN,COS,TAN,ASIN,ACOS,ATAN,ATAN2,
RANDOM,NORMAL,CEIL,FLOOR,ROUND,RAMP,STAGGER,LOGFREQ,LOGFREQ2,
LOGFREQ3,STRIDE,STRIDE2,VDISPLACE,SWIGGLE,CWIGGLE,GMASK,RMASK,
GRMASK,IS_ACTIVE,IS_DEFINED,IS_AVAILABLE,
GRMASK,IS_ACTIVE,IS_DEFINED,IS_AVAILABLE,IS_FILE,
VALUE,ATOMARRAY,TYPEARRAY,INTARRAY,BIGINTARRAY,VECTORARRAY};
// customize by adding a special function
@ -4079,7 +4079,7 @@ int Variable::special_function(char *word, char *contents, Tree **tree,
strcmp(word,"gmask") && strcmp(word,"rmask") &&
strcmp(word,"grmask") && strcmp(word,"next") &&
strcmp(word,"is_active") && strcmp(word,"is_defined") &&
strcmp(word,"is_available"))
strcmp(word,"is_available") && strcmp(word,"is_file"))
return 0;
// parse contents for comma-separated args
@ -4488,6 +4488,26 @@ int Variable::special_function(char *word, char *contents, Tree **tree,
// save value in tree or on argstack
if (tree) {
Tree *newtree = new Tree();
newtree->type = VALUE;
newtree->value = value;
newtree->first = newtree->second = nullptr;
newtree->nextra = 0;
treestack[ntreestack++] = newtree;
} else argstack[nargstack++] = value;
} else if (strcmp(word,"is_file") == 0) {
if (narg != 1)
print_var_error(FLERR,"Invalid is_file() function in "
"variable formula",ivar);
FILE *fp = fopen(args[0],"r");
value = (fp == nullptr) ? 0.0 : 1.0;
if (fp) fclose(fp);
// save value in tree or on argstack
if (tree) {
Tree *newtree = new Tree();
newtree->type = VALUE;