1 | #include "madx.h" |
---|
2 | |
---|
3 | void |
---|
4 | copy_double(double* source, double* target, int n) |
---|
5 | /* copies n double precision values from source to target */ |
---|
6 | { |
---|
7 | for (int j = 0; j < n; j++) |
---|
8 | target[j] = source[j]; |
---|
9 | } |
---|
10 | |
---|
11 | int |
---|
12 | char_p_pos(char* name, struct char_p_array* p) |
---|
13 | /* returns the position of name in character pointer array p, |
---|
14 | or -1 if not found */ |
---|
15 | { |
---|
16 | for (int i = 0; i < p->curr; i++) |
---|
17 | if (strcmp(name, p->p[i]) == 0) return i; |
---|
18 | |
---|
19 | return -1; |
---|
20 | } |
---|
21 | |
---|
22 | struct char_array* |
---|
23 | new_char_array(int length) |
---|
24 | { |
---|
25 | char rout_name[] = "new_char_array"; |
---|
26 | struct char_array* il = mycalloc(rout_name, 1, sizeof(struct char_array)); |
---|
27 | il->stamp = 123456; |
---|
28 | il->curr = 0; |
---|
29 | il->max = length; |
---|
30 | il->c = mymalloc(rout_name, length); |
---|
31 | return il; |
---|
32 | } |
---|
33 | |
---|
34 | struct char_p_array* |
---|
35 | new_char_p_array(int length) |
---|
36 | { |
---|
37 | char rout_name[] = "new_char_p_array"; |
---|
38 | struct char_p_array* il = mycalloc(rout_name,1, sizeof(struct char_p_array)); |
---|
39 | strcpy(il->name, "char_p_array"); |
---|
40 | il->stamp = 123456; |
---|
41 | if (watch_flag) fprintf(debug_file, "creating ++> %s\n", il->name); |
---|
42 | il->curr = 0; |
---|
43 | il->max = length; |
---|
44 | il->p = mycalloc(rout_name, length, sizeof(char*)); |
---|
45 | memset(il->p, 0, length*sizeof(char*)); |
---|
46 | return il; |
---|
47 | } |
---|
48 | |
---|
49 | struct int_array* |
---|
50 | new_int_array(int length) |
---|
51 | { |
---|
52 | char rout_name[] = "new_int_array"; |
---|
53 | struct int_array* il = mycalloc(rout_name,1, sizeof(struct int_array)); |
---|
54 | strcpy(il->name, "int_array"); |
---|
55 | il->stamp = 123456; |
---|
56 | if (watch_flag) fprintf(debug_file, "creating ++> %s\n", il->name); |
---|
57 | il->curr = 0; |
---|
58 | il->max = length; |
---|
59 | il->i = mymalloc(rout_name,length * sizeof(int)); |
---|
60 | return il; |
---|
61 | } |
---|
62 | |
---|
63 | struct double_array* |
---|
64 | new_double_array(int length) |
---|
65 | { |
---|
66 | char rout_name[] = "new_double_array"; |
---|
67 | struct double_array* il = mycalloc(rout_name,1, sizeof(struct double_array)); |
---|
68 | il->stamp = 123456; |
---|
69 | il->curr = 0; |
---|
70 | il->max = length; |
---|
71 | il->a = mycalloc(rout_name,length, sizeof(double)); |
---|
72 | return il; |
---|
73 | } |
---|
74 | |
---|
75 | struct char_array_list* |
---|
76 | new_char_array_list(int size) |
---|
77 | { |
---|
78 | char rout_name[] = "new_char_array_list"; |
---|
79 | struct char_array_list* tl = mycalloc(rout_name, 1, sizeof(struct char_array_list)); |
---|
80 | strcpy(tl->name, "char_array_list"); |
---|
81 | tl->stamp = 123456; |
---|
82 | if (watch_flag) fprintf(debug_file, "creating ++> %s\n", tl->name); |
---|
83 | tl->ca = mycalloc(rout_name, size, sizeof(struct char_array*)); |
---|
84 | tl->max = size; |
---|
85 | return tl; |
---|
86 | } |
---|
87 | |
---|
88 | struct char_p_array* |
---|
89 | clone_char_p_array(struct char_p_array* p) |
---|
90 | { |
---|
91 | int i; |
---|
92 | struct char_p_array* clone = new_char_p_array(p->max); |
---|
93 | for (i = 0; i < p->curr; i++) clone->p[i] = permbuff(p->p[i]); |
---|
94 | clone->curr = p->curr; |
---|
95 | return clone; |
---|
96 | } |
---|
97 | |
---|
98 | struct double_array* |
---|
99 | clone_double_array(struct double_array* p) |
---|
100 | { |
---|
101 | struct double_array* clone = new_double_array(p->curr); |
---|
102 | clone->curr = p->curr; |
---|
103 | for (int i = 0; i < p->curr; i++) clone->a[i] = p->a[i]; |
---|
104 | return clone; |
---|
105 | } |
---|
106 | |
---|
107 | struct int_array* |
---|
108 | clone_int_array(struct int_array* p) |
---|
109 | { |
---|
110 | struct int_array* clone = new_int_array(p->curr); |
---|
111 | clone->curr = p->curr; |
---|
112 | for (int i = 0; i < p->curr; i++) clone->i[i] = p->i[i]; |
---|
113 | return clone; |
---|
114 | } |
---|
115 | |
---|
116 | struct char_array* |
---|
117 | delete_char_array(struct char_array* pa) |
---|
118 | { |
---|
119 | char rout_name[] = "delete_char_array"; |
---|
120 | if (pa == NULL) return NULL; |
---|
121 | if (pa->c != NULL) myfree(rout_name, pa->c); |
---|
122 | myfree(rout_name, pa); |
---|
123 | return NULL; |
---|
124 | } |
---|
125 | |
---|
126 | struct char_p_array* |
---|
127 | delete_char_p_array(struct char_p_array* pa, int flag) |
---|
128 | /* flag = 0: delete only pointer array, = 1: delete char. buffers, too */ |
---|
129 | { |
---|
130 | char rout_name[] = "delete_char_p_array"; |
---|
131 | if (pa == NULL) return NULL; |
---|
132 | if (stamp_flag && pa->stamp != 123456) |
---|
133 | fprintf(stamp_file, "d_c_p_a double delete --> %s\n", pa->name); |
---|
134 | if (watch_flag) fprintf(debug_file, "deleting --> %s\n", pa->name); |
---|
135 | if (flag) |
---|
136 | for (int i = 0; i < pa->curr; i++) |
---|
137 | myfree(rout_name, pa->p[i]); |
---|
138 | if (pa->p != NULL) myfree(rout_name, pa->p); |
---|
139 | myfree(rout_name, pa); |
---|
140 | return NULL; |
---|
141 | } |
---|
142 | |
---|
143 | struct int_array* |
---|
144 | delete_int_array(struct int_array* i) |
---|
145 | { |
---|
146 | char rout_name[] = "delete_int_array"; |
---|
147 | if (i == NULL) return NULL; |
---|
148 | if (stamp_flag && i->stamp != 123456) |
---|
149 | fprintf(stamp_file, "d_i_a double delete --> %s\n", i->name); |
---|
150 | if (watch_flag) fprintf(debug_file, "deleting --> %s\n", i->name); |
---|
151 | if (i->i != NULL) myfree(rout_name, i->i); |
---|
152 | myfree(rout_name, i); |
---|
153 | return NULL; |
---|
154 | } |
---|
155 | |
---|
156 | struct double_array* |
---|
157 | delete_double_array(struct double_array* a) |
---|
158 | { |
---|
159 | char rout_name[] = "delete_double_array"; |
---|
160 | if (a != NULL) |
---|
161 | { |
---|
162 | if (a->a != NULL) myfree(rout_name, a->a); |
---|
163 | myfree(rout_name, a); |
---|
164 | } |
---|
165 | return NULL; |
---|
166 | } |
---|
167 | |
---|
168 | void |
---|
169 | dump_char_array(struct char_array* a) |
---|
170 | { |
---|
171 | char* c = a->c; |
---|
172 | int n = 0, l_cnt = 60, k; |
---|
173 | while (n < a->curr) |
---|
174 | { |
---|
175 | k = a->curr - n; if (k > l_cnt) k = l_cnt; |
---|
176 | strncpy(c_dum->c, c, k); |
---|
177 | c += k; n += k; |
---|
178 | c_dum->c[k] = '\0'; |
---|
179 | fprintf(prt_file, "%s\n", c_dum->c); |
---|
180 | } |
---|
181 | } |
---|
182 | |
---|
183 | void |
---|
184 | dump_char_p_array(struct char_p_array* p) |
---|
185 | { |
---|
186 | int i; |
---|
187 | for (i = 0; i < p->curr; i++) |
---|
188 | fprintf(prt_file, "%s\n", p->p[i]); |
---|
189 | } |
---|
190 | |
---|
191 | void |
---|
192 | dump_int_array(struct int_array* ia) |
---|
193 | { |
---|
194 | fprintf(prt_file, "dump integer array, length: %d\n", ia->curr); |
---|
195 | for (int i = 0; i < ia->curr; i++) |
---|
196 | { |
---|
197 | fprintf(prt_file, v_format("%d "), ia->i[i]); |
---|
198 | if ((i+1)%10 == 0) fprintf(prt_file, "\n"); |
---|
199 | } |
---|
200 | if (ia->curr%10 != 0) fprintf(prt_file, "\n"); |
---|
201 | } |
---|
202 | |
---|
203 | void |
---|
204 | grow_char_array(struct char_array* p) |
---|
205 | { |
---|
206 | char rout_name[] = "grow_char_array"; |
---|
207 | char* p_loc = p->c; |
---|
208 | int new = 2*p->max; |
---|
209 | |
---|
210 | p->max = new; |
---|
211 | p->c = mymalloc(rout_name, new); |
---|
212 | for (int j = 0; j < p->curr; j++) p->c[j] = p_loc[j]; |
---|
213 | myfree(rout_name, p_loc); |
---|
214 | } |
---|
215 | |
---|
216 | void |
---|
217 | grow_char_p_array(struct char_p_array* p) |
---|
218 | { |
---|
219 | char rout_name[] = "grow_char_p_array"; |
---|
220 | char** p_loc = p->p; |
---|
221 | int new = 2*p->max; |
---|
222 | |
---|
223 | p->max = new; |
---|
224 | p->p = mycalloc(rout_name,new, sizeof(char*)); |
---|
225 | for (int j = 0; j < p->curr; j++) p->p[j] = p_loc[j]; |
---|
226 | myfree(rout_name, p_loc); |
---|
227 | } |
---|
228 | |
---|
229 | void |
---|
230 | grow_int_array(struct int_array* p) |
---|
231 | { |
---|
232 | char rout_name[] = "grow_int_array"; |
---|
233 | int* i_loc = p->i; |
---|
234 | int new = 2*p->max; |
---|
235 | |
---|
236 | p->max = new; |
---|
237 | p->i = mymalloc(rout_name,new * sizeof(int)); |
---|
238 | for (int j = 0; j < p->curr; j++) p->i[j] = i_loc[j]; |
---|
239 | myfree(rout_name, i_loc); |
---|
240 | } |
---|
241 | |
---|
242 | void |
---|
243 | grow_double_array(struct double_array* p) |
---|
244 | { |
---|
245 | char rout_name[] = "grow_double_array"; |
---|
246 | double* a_loc = p->a; |
---|
247 | int new = 2*p->max; |
---|
248 | |
---|
249 | p->max = new; |
---|
250 | p->a = mymalloc(rout_name, new * sizeof(double)); |
---|
251 | for (int j = 0; j < p->curr; j++) p->a[j] = a_loc[j]; |
---|
252 | myfree(rout_name, a_loc); |
---|
253 | } |
---|
254 | |
---|
255 | void |
---|
256 | grow_char_array_list(struct char_array_list* p) |
---|
257 | { |
---|
258 | char rout_name[] = "grow_char_array_list"; |
---|
259 | struct char_array** c_loc = p->ca; |
---|
260 | int new = 2*p->max; |
---|
261 | |
---|
262 | p->max = new; |
---|
263 | p->ca = mycalloc(rout_name, new, sizeof(struct char_array*)); |
---|
264 | for (int j = 0; j < p->curr; j++) p->ca[j] = c_loc[j]; |
---|
265 | myfree(rout_name, c_loc); |
---|
266 | } |
---|
267 | |
---|
268 | void |
---|
269 | ftoi_array(struct double_array* da, struct int_array* ia) |
---|
270 | /* converts and copies double array into integer array */ |
---|
271 | { |
---|
272 | int l = da->curr; |
---|
273 | while (l >= ia->max) grow_int_array(ia); |
---|
274 | for (int i = 0; i < l; i++) ia->i[i] = da->a[i]; |
---|
275 | ia->curr = l; |
---|
276 | } |
---|
277 | |
---|
278 | int |
---|
279 | int_in_array(int k, int n, int* array) |
---|
280 | /* returns 1 if k in first n elements of array, else 0 */ |
---|
281 | { |
---|
282 | for (int j = 0; j < n; j++) if (k == array[j]) return 1; |
---|
283 | return 0; |
---|
284 | } |
---|
285 | |
---|
286 | |
---|
287 | |
---|