From 3e79296dcfa6b2c0407ba7baf5d592cb986fc57b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20H=C3=BCtter?= <sebastian.huetter@ovgu.de> Date: Thu, 14 Jun 2018 13:10:08 +0200 Subject: [PATCH] Variable style 'string' substitutes variables in definition --- doc/src/variable.txt | 9 +++++---- src/variable.cpp | 14 ++++++++++++-- src/variable.h | 1 + 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/doc/src/variable.txt b/doc/src/variable.txt index c0851464c3..3f432b3778 100644 --- a/doc/src/variable.txt +++ b/doc/src/variable.txt @@ -296,10 +296,11 @@ list of runs (e.g. 1000) without having to list N strings in the input script. For the {string} style, a single string is assigned to the variable. -The only difference between this and using the {index} style with a -single string is that a variable with {string} style can be redefined. -E.g. by another command later in the input script, or if the script is -read again in a loop. +Two differences between this this and using the {index} style exist: +a variable with {string} style can be redefined, e.g. by another command later +in the input script, or if the script is read again in a loop. The other +difference is that {string} performs variable substitution even if the +string parameter is quoted. For the {format} style, an equal-style variable is specified along with a C-style format string, e.g. "%f" or "%.10g", which must be diff --git a/src/variable.cpp b/src/variable.cpp index b5c19a4843..86296b4b40 100644 --- a/src/variable.cpp +++ b/src/variable.cpp @@ -284,12 +284,21 @@ void Variable::set(int narg, char **arg) } else if (strcmp(arg[1],"string") == 0) { if (narg != 3) error->all(FLERR,"Illegal variable command"); + + int maxcopy = strlen(arg[2]) + 1; + int maxwork = maxcopy; + char *scopy = new char[maxcopy]; + char *work = new char[maxwork]; + strcpy(scopy,arg[2]); + input->substitute(scopy,work,maxcopy,maxwork,1); + delete [] work; + int ivar = find(arg[0]); if (ivar >= 0) { if (style[ivar] != STRING) error->all(FLERR,"Cannot redefine variable as a different style"); delete [] data[ivar][0]; - copy(1,&arg[2],data[ivar]); + copy(1,&scopy,data[ivar]); replaceflag = 1; } else { if (nvar == maxvar) grow(); @@ -298,8 +307,9 @@ void Variable::set(int narg, char **arg) which[nvar] = 0; pad[nvar] = 0; data[nvar] = new char*[num[nvar]]; - copy(1,&arg[2],data[nvar]); + copy(1,&scopy,data[nvar]); } + delete [] scopy; // GETENV // remove pre-existing var if also style GETENV (allows it to be reset) diff --git a/src/variable.h b/src/variable.h index a74cdba980..b20eb7e6b9 100644 --- a/src/variable.h +++ b/src/variable.h @@ -16,6 +16,7 @@ #include <cstdlib> #include "pointers.h" +#include "input.h" namespace LAMMPS_NS { -- GitLab