Basilisk CFD
Adaptive Cartesian mesh PDE framework
Loading...
Searching...
No Matches
parse.h
Go to the documentation of this file.
1/** @file parse.h
2 */
3/**
4# Parser for interactive bview
5*/
6
8
9typedef struct {
10 char * key;
12 void * val;
13 int n;
14} Params;
15
16static bool atobool (char * s)
17{
18 if (!strcmp (s, "true"))
19 return true;
20 if (!strcmp (s, "false"))
21 return false;
22 return atoi (s) != 0;
23}
24
25static bool args (Params * p, char * val)
26{
27 static char * name[] = { "string", "int", "unsigned",
28 "bool", "float", "double", "colormap" };
29 switch (p->type) {
30
31 case pstring:
32 if (val[0] != '"') {
33 fprintf (stderr, "expecting a string for '%s' got '%s'\n", p->key, val);
34 return false;
35 }
36 if (val[strlen(val) - 1] != '"') {
37 fprintf (stderr, "unterminated quoted string '%s'\n", val);
38 return false;
39 }
40 val[strlen(val) - 1] = '\0';
41 char * s = &val[1];
42 int nc = 0; // number of non-blank characters
43 while (*s != '\0') {
44 if (!strchr (" \t\n\r", *s))
45 nc++;
46 s++;
47 }
48 *((char **)p->val) = nc > 0 ? &val[1] : NULL;
49 break;
50
51 case pcolormap:
52 if (!strcmp (val, "jet"))
53 *((Colormap *)p->val) = jet;
54 else if (!strcmp (val, "cool_warm"))
55 *((Colormap *)p->val) = cool_warm;
56 else if (!strcmp (val, "gray"))
57 *((Colormap *)p->val) = gray;
58 else if (!strcmp (val, "randomap"))
59 *((Colormap *)p->val) = randomap;
60 else {
61 fprintf (stderr, "unknown colormap '%s'\n", val);
62 return false;
63 }
64 break;
65
66 case pint: case punsigned: case pbool: case pdouble: case pfloat:
67 if (val[0] == '"') {
68 fprintf (stderr, "expecting a %s for '%s' got %s\n",
69 name[p->type], p->key, val);
70 return false;
71 }
72 if (!p->n) {
73 switch (p->type) {
74 case pint: *((int *)p->val) = atoi(val); break;
75 case punsigned: *((unsigned *)p->val) = atoi(val); break;
76 case pbool: *((bool *)p->val) = atobool(val); break;
77 case pfloat: *((float *)p->val) = atof(val); break;
78 case pdouble: *((double *)p->val) = atof(val); break;
79 default: assert (false);
80 }
81 }
82 else {
83 if (val[0] != '{') {
84 fprintf (stderr, "expecting an array for '%s' got %s\n", p->key, val);
85 return false;
86 }
87 val++;
88 int i = 0;
89 char c = ',';
90 while (i < p->n && c != '}') {
91 char * s = strchr (val, ',');
92 if (!s)
93 s = strchr (val, '}');
94 if (!s) {
95 fprintf (stderr, "expecting an array for '%s' got %s\n", p->key, val);
96 return false;
97 }
98 c = *s;
99 *s++ = '\0';
100 switch (p->type) {
101 case pint: ((int *)p->val)[i++] = atoi (val); break;
102 case punsigned: ((unsigned *)p->val)[i++] = atoi (val); break;
103 case pbool: ((bool *)p->val)[i++] = atobool (val); break;
104 case pfloat: ((float *)p->val)[i++] = atof (val); break;
105 case pdouble: ((double *)p->val)[i++] = atof (val); break;
106 default: assert (false);
107 }
108 val = s;
109 }
110 if (c != '}') {
111 fprintf (stderr, "expecting '}' for '%s' got %s\n", p->key, val);
112 return false;
113 }
114 }
115 break;
116
117 default:
118 assert (false);
119 }
120 return true;
121}
122
123static char * find_comma (char * s)
124{
125 int par = 0;
126 while (*s != '\0') {
127 if (*s == ',' && par == 0) {
128 *s = '\0';
129 return s + 1;
130 }
131 if (*s == '{')
132 par++;
133 else if (*s == '}')
134 par--;
135 s++;
136 }
137 return NULL;
138}
139
140static char * mystrtok (char * str, const char * delim)
141{
142 static char * s = NULL;
143 char * start = str ? str : s;
144 bool string = false;
145 s = start;
146 while (*s != '\0') {
147 if (*s == '"')
148 string = !string;
149 if (!string && strchr(delim, *s))
150 break;
151 s++;
152 }
153 if (*s != '\0')
154 *s++ = '\0';
155 return start;
156}
157
159{
160 char * s;
161 int i = 0, n = 0;
162 Params * p = params;
163 while (p->key) p++, n++;
164 if (!(s = mystrtok (NULL, ");")) || s[0] == '\n')
165 return false;
166 while (s && *s != '\0') {
167 char * next = find_comma (s), * key = s;
168 if ((s = strchr (key, '='))) {
169 s[0] = '\0', s++;
170 i = -1;
171 Params * p = params;
172 while (p->key && strcmp(p->key, key)) p++;
173 if (!p->key) {
174 fprintf (stderr, "unknown key '%s'\n", key);
175 return false;
176 }
177 if (!args (p, s))
178 return false;
179 }
180 else {
181 if (i < 0) {
182 fprintf (stderr, "anonymous value '%s' after keys\n", key);
183 return false;
184 }
185 if (i >= n) {
186 fprintf (stderr, "too many parameters: '%s' %d %d\n", key, i, n);
187 return false;
188 }
189 if (!args (&params[i], key))
190 return false;
191 i++;
192 }
193 s = next;
194 }
195 return true;
196}
if TRASH define &&NewPid *& val(newpid, 0, 0, 0)) -> pid > 0) @else @ define is_newpid()(((NewPid *)&val(newpid, 0, 0, 0)) ->pid > 0) @endif Array *linear_tree(size_t size, scalar newpid)
Definition balance.h:13
int x
Definition common.h:76
#define p
Definition tree.h:320
#define assert(a)
Definition config.h:107
else return n
Definition curvature.h:101
scalar s
Definition embed-tree.h:56
scalar int i
Definition embed.h:74
#define str(a)
Definition grid.h:406
void gray(double cmap[127][3])
Definition output.h:271
void(* Colormap)(double cmap[127][3])
Definition output.h:196
void jet(double cmap[127][3])
Definition output.h:198
void randomap(double cmap[127][3])
Definition output.h:278
void cool_warm(double cmap[127][3])
Definition output.h:219
static bool args(Params *p, char *val)
Definition parse.h:25
static char * mystrtok(char *str, const char *delim)
Definition parse.h:140
static char * find_comma(char *s)
Definition parse.h:123
static bool atobool(char *s)
Definition parse.h:16
ParamsType
Definition parse.h:7
@ pstring
Definition parse.h:7
@ pfloat
Definition parse.h:7
@ punsigned
Definition parse.h:7
@ pint
Definition parse.h:7
@ pbool
Definition parse.h:7
@ pdouble
Definition parse.h:7
@ pcolormap
Definition parse.h:7
int parse_params(Params *params)
Definition parse.h:158
Definition parse.h:9
void * val
Definition parse.h:12
enum ParamsType type
Definition parse.h:11
char * key
Definition parse.h:10
int n
Definition parse.h:13
long nc
Definition utils.h:17
scalar c
Definition vof.h:57