1 | #include "madx.h" |
---|
2 | |
---|
3 | static int |
---|
4 | v_length(char* form) |
---|
5 | { |
---|
6 | int ret = 0; |
---|
7 | if (form[1] == 'I') sscanf(int_format, "%d", &ret); |
---|
8 | else if (form[1] == 'F') sscanf(float_format, "%d", &ret); |
---|
9 | return ret; |
---|
10 | } |
---|
11 | |
---|
12 | static int |
---|
13 | get_val_num(char* in_string, int start, int end) |
---|
14 | { |
---|
15 | int j, dot = 0, exp = 0, sign = 0; |
---|
16 | char c; |
---|
17 | for (j = start; j < end; j++) |
---|
18 | { |
---|
19 | c = in_string[j]; |
---|
20 | if(!isdigit(c)) |
---|
21 | { |
---|
22 | if ((c = in_string[j]) == '.') |
---|
23 | { |
---|
24 | if (dot || exp) return (j - 1); |
---|
25 | dot = 1; |
---|
26 | } |
---|
27 | else if (c == 'e' || c == 'd') |
---|
28 | { |
---|
29 | if (c == 'd') in_string[j] = 'e'; |
---|
30 | if (exp) return (j - 1); |
---|
31 | else exp = j+1; |
---|
32 | } |
---|
33 | else if(strchr("+-", c)) |
---|
34 | { |
---|
35 | if (exp != j || sign) return (j - 1); |
---|
36 | sign = 1; |
---|
37 | } |
---|
38 | else return (j - 1); |
---|
39 | } |
---|
40 | } |
---|
41 | return (j - 1); |
---|
42 | } |
---|
43 | |
---|
44 | // public interface |
---|
45 | |
---|
46 | char* |
---|
47 | v_format(char* string) |
---|
48 | /* copies string to gloval variable var_form |
---|
49 | replacing %S, %I, and %F by the user defined formats; |
---|
50 | %NF and %NI are replaced by the field lengths (!) of the defined formats */ |
---|
51 | { |
---|
52 | char *p, *q = string, *s = string, *t; |
---|
53 | char c; |
---|
54 | *var_form = '\0'; |
---|
55 | while ((p = strpbrk(s, "NIFS"))) |
---|
56 | { |
---|
57 | if (p > q) |
---|
58 | { |
---|
59 | t = p; t--; |
---|
60 | if (*t == '%') |
---|
61 | { |
---|
62 | c = *p; |
---|
63 | strncat(var_form, q, p - q); |
---|
64 | if (c == 'N') |
---|
65 | { |
---|
66 | sprintf(&var_form[strlen(var_form)], "%d", v_length(p)); |
---|
67 | p++; |
---|
68 | } |
---|
69 | else if (c == 'F') strcat(var_form, float_format); |
---|
70 | else if (c == 'S') strcat(var_form, string_format); |
---|
71 | else if (c == 'I') strcat(var_form, int_format); |
---|
72 | q = p; q++; |
---|
73 | } |
---|
74 | } |
---|
75 | s = ++p; |
---|
76 | } |
---|
77 | strcat(var_form, q); |
---|
78 | return var_form; |
---|
79 | } |
---|
80 | |
---|
81 | double |
---|
82 | simple_double(char** toks, int start, int end) |
---|
83 | { |
---|
84 | if (start > end && start + 1 != end) return INVALID; |
---|
85 | else if (start == end) return atof(toks[start]); |
---|
86 | else |
---|
87 | { |
---|
88 | if (*toks[start] == '-') return -atof(toks[end]); |
---|
89 | else if (*toks[start] == '+') return atof(toks[end]); |
---|
90 | else return INVALID; |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | int |
---|
95 | in_spec_list(char* string) |
---|
96 | /* checks for presence of special commands IF() etc. */ |
---|
97 | { |
---|
98 | char* cp; |
---|
99 | int i = 0, n = imin((int)strlen(string), 100); |
---|
100 | strncpy(c_dum->c, string, n); c_dum->c[n] = '\0'; stolower(c_dum->c); |
---|
101 | supp_char(' ', c_dum->c); |
---|
102 | while (special_comm_cnt[i]) |
---|
103 | { |
---|
104 | if (special_comm_desc[i][0] == '>') |
---|
105 | { |
---|
106 | if ((cp = strchr(c_dum->c, special_comm_desc[i][1])) != NULL) |
---|
107 | { |
---|
108 | if (strncmp(++cp, &special_comm_desc[i][2], special_comm_cnt[i]) |
---|
109 | == 0) return i+1; |
---|
110 | } |
---|
111 | } |
---|
112 | else if (strncmp(c_dum->c, &special_comm_desc[i][0],special_comm_cnt[i]) |
---|
113 | == 0) return i+1; |
---|
114 | i++; |
---|
115 | } |
---|
116 | return 0; |
---|
117 | } |
---|
118 | |
---|
119 | char* |
---|
120 | spec_join(char** it_list, int n) |
---|
121 | /* replaces variable in table(...) by original string */ |
---|
122 | { |
---|
123 | char rout_name[] = "spec_join"; |
---|
124 | int j; |
---|
125 | char** p; |
---|
126 | struct variable* var; |
---|
127 | *c_join->c = '\0'; |
---|
128 | if (n > 0) |
---|
129 | { |
---|
130 | p = (char**) mymalloc(rout_name,n*sizeof(char*)); |
---|
131 | for (j = 0; j < n; j++) p[j] = it_list[j]; |
---|
132 | for (j = 0; j < n; j++) |
---|
133 | if (strcmp(p[j], "table") == 0 && j+3 < n |
---|
134 | && (var = find_variable(p[j+2], variable_list)) != NULL) |
---|
135 | p[j+2] = var->string; |
---|
136 | for (j = 0; j < n; j++) strcat(c_join->c, p[j]); |
---|
137 | myfree(rout_name, p); |
---|
138 | } |
---|
139 | return c_join->c; |
---|
140 | } |
---|
141 | |
---|
142 | void |
---|
143 | pre_split(char* inbuf, struct char_array* outbuf, int fill_flag) |
---|
144 | /* inserts blanks between tokens */ |
---|
145 | /* fill_flag != 0 makes a 0 to be inserted into an empty "()" */ |
---|
146 | { |
---|
147 | char c, cp = ' ', cpnb = ' ', quote = ' '; |
---|
148 | int j, k, kn, sl = strlen(inbuf), cout = 0, quote_lv = 0, rb_level = 0; |
---|
149 | int left_b = 0, new_string = 1, c_digit = 0, f_equal = 0, comm_cnt = 0; |
---|
150 | int len = strlen(inbuf); |
---|
151 | while (2*len > outbuf->max) grow_char_array(outbuf); |
---|
152 | for (k = 0; k < sl; k++) |
---|
153 | { |
---|
154 | c = inbuf[k]; |
---|
155 | if (quote_lv > 0) |
---|
156 | { |
---|
157 | if (c == quote) |
---|
158 | { |
---|
159 | quote_lv--; outbuf->c[cout++] = c; outbuf->c[cout++] = ' '; |
---|
160 | } |
---|
161 | else outbuf->c[cout++] = c == ' ' ? '@' : c; |
---|
162 | } |
---|
163 | else |
---|
164 | { |
---|
165 | c = inbuf[k]; |
---|
166 | switch (c) |
---|
167 | { |
---|
168 | case '\"': |
---|
169 | case '\'': |
---|
170 | quote = c; |
---|
171 | quote_lv++; outbuf->c[cout++] = ' '; outbuf->c[cout++] = c; |
---|
172 | break; |
---|
173 | case '-': |
---|
174 | if (inbuf[k+1] == '>') |
---|
175 | { |
---|
176 | outbuf->c[cout++] = c; break; |
---|
177 | } |
---|
178 | case '+': |
---|
179 | if (left_b > 0) |
---|
180 | { |
---|
181 | outbuf->c[cout++] = ' '; |
---|
182 | outbuf->c[cout++] = '0'; |
---|
183 | outbuf->c[cout++] = ' '; |
---|
184 | left_b = 0; |
---|
185 | } |
---|
186 | if (!(new_string > 0 && c_digit > 0 && strchr("ed",cp)) && cout > 0) |
---|
187 | outbuf->c[cout++] = ' '; |
---|
188 | outbuf->c[cout++] = c; |
---|
189 | if (!(new_string > 0 && c_digit > 0 && strchr("ed",cp))) |
---|
190 | { |
---|
191 | outbuf->c[cout++] = ' '; |
---|
192 | new_string = 1; |
---|
193 | } |
---|
194 | break; |
---|
195 | case '(': |
---|
196 | rb_level++; |
---|
197 | left_b = 1; |
---|
198 | new_string = 1; |
---|
199 | outbuf->c[cout++] = ' '; |
---|
200 | outbuf->c[cout++] = c; |
---|
201 | outbuf->c[cout++] = ' '; |
---|
202 | break; |
---|
203 | case '>': |
---|
204 | if (cout > 0 && outbuf->c[cout-1] == '-') |
---|
205 | { |
---|
206 | outbuf->c[cout++] = c; |
---|
207 | } |
---|
208 | else |
---|
209 | { |
---|
210 | left_b = 0; |
---|
211 | new_string = 1; |
---|
212 | outbuf->c[cout++] = ' '; |
---|
213 | outbuf->c[cout++] = c; |
---|
214 | outbuf->c[cout++] = ' '; |
---|
215 | } |
---|
216 | break; |
---|
217 | case ')': |
---|
218 | rb_level--; |
---|
219 | if (fill_flag && cpnb == '(') outbuf->c[cout++] = '0'; |
---|
220 | case '<': |
---|
221 | case ':': |
---|
222 | case '*': |
---|
223 | case '/': |
---|
224 | case '^': |
---|
225 | case '{': |
---|
226 | case '}': |
---|
227 | case '[': |
---|
228 | case ']': |
---|
229 | case '|': |
---|
230 | case '&': |
---|
231 | left_b = 0; |
---|
232 | new_string = 1; |
---|
233 | outbuf->c[cout++] = ' '; |
---|
234 | outbuf->c[cout++] = c; |
---|
235 | outbuf->c[cout++] = ' '; |
---|
236 | break; |
---|
237 | case '=': |
---|
238 | f_equal = 1; |
---|
239 | left_b = 0; |
---|
240 | new_string = 1; |
---|
241 | outbuf->c[cout++] = ' '; |
---|
242 | outbuf->c[cout++] = c; |
---|
243 | outbuf->c[cout++] = ' '; |
---|
244 | break; |
---|
245 | case ',': /* kept behind first "=", or if not first "," */ |
---|
246 | /* not kept inside round brackets before '=' */ |
---|
247 | left_b = 0; |
---|
248 | new_string = 1; |
---|
249 | outbuf->c[cout++] = ' '; |
---|
250 | if (f_equal || (comm_cnt && rb_level == 0)) |
---|
251 | { |
---|
252 | outbuf->c[cout++] = c; |
---|
253 | outbuf->c[cout++] = ' '; |
---|
254 | } |
---|
255 | comm_cnt++; |
---|
256 | break; |
---|
257 | case ';': |
---|
258 | left_b = 0; |
---|
259 | new_string = 1; |
---|
260 | outbuf->c[cout++] = ' '; |
---|
261 | break; |
---|
262 | default: |
---|
263 | if (c == ' ') outbuf->c[cout++] = c; |
---|
264 | else |
---|
265 | { |
---|
266 | left_b = 0; |
---|
267 | if (new_string && (isdigit(c) || c == '.')) |
---|
268 | { |
---|
269 | kn = get_val_num(inbuf, k, sl); |
---|
270 | for (j = k; j <= kn; j++) outbuf->c[cout++] = inbuf[j]; |
---|
271 | outbuf->c[cout++] = ' '; |
---|
272 | k = kn; |
---|
273 | } |
---|
274 | else |
---|
275 | { |
---|
276 | new_string = 0; |
---|
277 | if (cout > 0 || c != ' ') outbuf->c[cout++] = c; |
---|
278 | } |
---|
279 | } |
---|
280 | } |
---|
281 | cp = c; if (c != ' ') cpnb = c; |
---|
282 | } |
---|
283 | } |
---|
284 | outbuf->c[cout] = '\0'; |
---|
285 | } |
---|
286 | |
---|
287 | |
---|