Created
January 29, 2013 07:37
-
-
Save derekbrokeit/4662502 to your computer and use it in GitHub Desktop.
Add "environment" style to variables. Allows the user to import variables directly from the environment. It should be noted that this was just an experiment. I have since found that the `-v` commandline flag is the best way to do this. The problem with environment variables here is that 1) it does not handle array data well `export blah=(1 2 3)`…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| diff --git a/src/variable.cpp b/src/variable.cpp | |
| index d60accc..b52220c 100644 | |
| --- a/src/variable.cpp | |
| +++ b/src/variable.cpp | |
| @@ -43,7 +43,7 @@ using namespace MathConst; | |
| #define MYROUND(a) (( a-floor(a) ) >= .5) ? ceil(a) : floor(a) | |
| -enum{INDEX,LOOP,WORLD,UNIVERSE,ULOOP,STRING,FILEVAR,EQUAL,ATOM}; | |
| +enum{INDEX,LOOP,WORLD,UNIVERSE,ULOOP,STRING,FILEVAR,EQUAL,ATOM,ENVIRONMENT}; | |
| enum{ARG,OP}; | |
| // customize by adding a function | |
| @@ -330,6 +330,27 @@ void Variable::set(int narg, char **arg) | |
| data[nvar] = new char*[num[nvar]]; | |
| copy(1,&arg[2],data[nvar]); | |
| + // ENVIRONMENT | |
| + // remove pre-existing var if also style ENVIRONMENT (allows it to be reset) | |
| + // num = 1, which = 1st value | |
| + // data = 1 value, string to eval | |
| + | |
| + } else if (strcmp(arg[1],"environment") == 0) { | |
| + if (narg != 3) error->all(FLERR,"Illegal variable command"); | |
| + if (find(arg[0]) >= 0) { | |
| + if (style[find(arg[0])] != STRING) | |
| + error->all(FLERR,"Cannot redefine variable as a different style"); | |
| + remove(find(arg[0])); | |
| + } | |
| + if (nvar == maxvar) grow(); | |
| + style[nvar] = ENVIRONMENT; | |
| + num[nvar] = 1; | |
| + which[nvar] = 0; | |
| + pad[nvar] = 0; | |
| + data[nvar] = new char*[num[nvar]]; | |
| + arg[2] = getenv(arg[2]); | |
| + copy(1,&arg[2],data[nvar]); | |
| + | |
| } else error->all(FLERR,"Illegal variable command"); | |
| // set name of variable | |
| @@ -389,7 +410,7 @@ int Variable::next(int narg, char **arg) | |
| // invalid styles STRING or EQUAL or WORLD or ATOM | |
| int istyle = style[find(arg[0])]; | |
| - if (istyle == STRING || istyle == EQUAL || istyle == WORLD || istyle == ATOM) | |
| + if (istyle == STRING || istyle == EQUAL || istyle == WORLD || istyle == ATOM || istyle == ENVIRONMENT) | |
| error->all(FLERR,"Invalid variable style with next command"); | |
| // increment all variables in list | |
| @@ -479,7 +500,7 @@ char *Variable::retrieve(char *name) | |
| char *str; | |
| if (style[ivar] == INDEX || style[ivar] == WORLD || | |
| style[ivar] == UNIVERSE || style[ivar] == STRING || | |
| - style[ivar] == FILEVAR) { | |
| + style[ivar] == FILEVAR || style[ivar] == ENVIRONMENT) { | |
| str = data[ivar][which[ivar]]; | |
| } else if (style[ivar] == LOOP || style[ivar] == ULOOP) { | |
| char result[16]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment