-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathsmartparameter.h
79 lines (72 loc) · 1.99 KB
/
smartparameter.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <stdio.h>
#include <stdbool.h>
/* Original QNM's version from E. Meinhardt-Llopis
* Megawave port by G. Facciolo*/
/*/ 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.
//
*/
#ifndef VERBOSE_SMART_PARAMETER
#define VERBOSE_SMART_PARAMETER 0
#endif
#define SMART_PARAMETER(n,v) static double n(void)\
{\
static int smapa_known_ ## n = false;\
static double smapa_value_ ## n = v;\
if (!smapa_known_ ## n)\
{\
int r;\
char *sv;\
double y;\
sv = getenv(#n);\
if(VERBOSE_SMART_PARAMETER) fprintf(stderr,"scanning the environment for \"%s\"... ", #n);\
if (sv)\
r = sscanf(sv, "%lf", &y);\
if (sv && r == 1)\
{\
if(VERBOSE_SMART_PARAMETER) fprintf(stderr, "got value %g\n", y);\
smapa_value_ ## n = y;\
} else {\
if(VERBOSE_SMART_PARAMETER) fprintf(stderr, "kept default value %g\n",\
smapa_value_ ## n);\
}\
smapa_known_ ## n = true;\
}\
return smapa_value_ ## n;\
}
#define SMART_PARAMETER_INT(n,v) static int n(void)\
{\
static int smapa_known_ ## n = false;\
static int smapa_value_ ## n = v;\
if (!smapa_known_ ## n)\
{\
int r;\
int y;\
char *sv;\
sv = getenv(#n);\
if(VERBOSE_SMART_PARAMETER)fprintf(stderr,"scanning the environment for \"%s\"... ", #n);\
if (sv)\
r = sscanf(sv, "%d", &y);\
if (sv && r == 1)\
{\
if(VERBOSE_SMART_PARAMETER)fprintf(stderr, "got int value %d\n", y);\
smapa_value_ ## n = y;\
} else {\
if(VERBOSE_SMART_PARAMETER)fprintf(stderr, "kept default int value %d\n",\
smapa_value_ ## n);\
}\
smapa_known_ ## n = true;\
}\
return smapa_value_ ## n;\
}