-
Notifications
You must be signed in to change notification settings - Fork 6
/
smapa.h
executable file
·57 lines (55 loc) · 1.25 KB
/
smapa.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// a smart parameter is just like a regular parameter, but it can be
// re-defined at the shell-environment. Instead of
//
// #define NUMBER 42
// ...
// printf("%g", NUMBER);
//
// do
// SMART_PARAMETER(NUMBER,42)
// ...
// printf("%g", NUMBER());
//
// Notice that the environment only gets queried once, at the first use.
//
#define SMART_PARAMETER(n,v) static double n(void)\
{\
static bool smapa_known_ ## n = false;\
static double smapa_value_ ## n = v;\
if (!smapa_known_ ## n)\
{\
fprintf(stderr,"scanning the environment for \"%s\"... ", #n);\
int r;\
char *sv = getenv(#n);\
double y;\
if (sv)\
r = sscanf(sv, "%lf", &y);\
if (sv && r == 1)\
{\
fprintf(stderr, "got value %g\n", y);\
smapa_value_ ## n = y;\
} else {\
fprintf(stderr, "kept default value %g\n",\
smapa_value_ ## n);\
}\
smapa_known_ ## n = true;\
}\
return smapa_value_ ## n;\
}
#define SMART_PARAMETER_SILENT(n,v) static double n(void)\
{\
static bool smapa_known_ ## n = false;\
static double smapa_value_ ## n = v;\
if (!smapa_known_ ## n)\
{\
int r;\
char *sv = getenv(#n);\
double y;\
if (sv)\
r = sscanf(sv, "%lf", &y);\
if (sv && r == 1)\
smapa_value_ ## n = y;\
smapa_known_ ## n = true;\
}\
return smapa_value_ ## n;\
}