source: PSPA/madxPSPA/src/mad_cmdpar.c @ 430

Last change on this file since 430 was 430, checked in by touze, 11 years ago

import madx-5.01.00

File size: 35.1 KB
Line 
1#include "madx.h"
2
3// public interface
4
5#if 0
6void
7set_command_par_string(char* parameter, struct command* cmd, char* val)
8{
9  char rout_name[] = "set_command_par_string";
10  struct command_parameter* cp;
11  int i, new_len;
12
13  if ((i = name_list_pos(parameter, cmd->par_names)) > -1)
14  {
15    cp = cmd->par->parameters[i];
16    if (cp->type == 3)
17    {
18      int len = strlen(cp->string);
19      new_len = strlen(val);
20
21      if(len < new_len)
22      {
23        myfree(rout_name,cp->string);
24        cp->string = (char*) mymalloc(rout_name,new_len);
25      }
26
27      strcpy(cp->string,val);
28
29      if (cp->expr != NULL) cp->expr = delete_expression(cp->expr);
30      cmd->par_names->inform[i] = 1; /* mark as set */
31    }
32  }
33}
34#endif
35
36struct command_parameter*
37clone_command_parameter(struct command_parameter* p)
38{
39  struct command_parameter* clone = new_command_parameter(p->name, p->type);
40  clone->call_def = p->call_def;
41  switch (p->type)
42  {
43    case 4:
44      clone->c_min = p->c_min;
45      clone->c_max = p->c_max;
46      clone->min_expr = clone_expression(p->min_expr);
47      clone->max_expr = clone_expression(p->max_expr);
48    case 0:
49    case 1:
50    case 2:
51      clone->double_value = p->double_value;
52      clone->expr = clone_expression(p->expr);
53      break;
54    case 3:
55      clone->string = p->string;
56      clone->expr = NULL;
57      break;
58    case 11:
59    case 12:
60      clone->double_array = clone_double_array(p->double_array);
61      clone->expr_list = clone_expr_list(p->expr_list);
62      break;
63    case 13:
64      clone->m_string = clone_char_p_array(p->m_string);
65  }
66  return clone;
67}
68
69struct command_parameter*
70new_command_parameter(char* name, int type)
71{
72  char rout_name[] = "new_command_parameter";
73  struct command_parameter* new = mycalloc(rout_name,1, sizeof(struct command_parameter));
74  strcpy(new->name, name); new->type = type;
75  new->stamp = 123456;
76  if (watch_flag) fprintf(debug_file, "creating ++> %s\n", new->name);
77  return new;
78}
79
80struct command_parameter_list*
81new_command_parameter_list(int length)
82{
83  int i;
84  char rout_name[] = "new_command_parameter_list";
85  struct command_parameter_list* il = mycalloc(rout_name,1, sizeof(struct command_parameter_list));
86  strcpy(il->name, "command_parameter_list");
87  il->stamp = 123456;
88  if (watch_flag) fprintf(debug_file, "creating ++> %s\n", il->name);
89  il->curr = 0;
90  il->max = length;
91  if (length > 0)
92  {
93    il->parameters = mycalloc(rout_name,length, sizeof(struct command_parameter*));
94    for (i = 0; i < length; i++) il->parameters[i] = NULL;
95  }
96  return il;
97}
98
99struct command_parameter*
100delete_command_parameter(struct command_parameter* par)
101{
102  char rout_name[] = "delete_command_parameter";
103  if (par == NULL) return NULL;
104  if (stamp_flag && par->stamp != 123456)
105    fprintf(stamp_file, "d_c_p double delete --> %s\n", par->name);
106  if (watch_flag) fprintf(debug_file, "deleting --> %s\n", par->name);
107  if (par->expr != NULL)         delete_expression(par->expr);
108  if (par->min_expr != NULL)     delete_expression(par->min_expr);
109  if (par->max_expr != NULL)     delete_expression(par->max_expr);
110  if (par->double_array != NULL) delete_double_array(par->double_array);
111  if (par->expr_list != NULL)    delete_expr_list(par->expr_list);
112  if (par->m_string != NULL)     delete_char_p_array(par->m_string, 0);
113  myfree(rout_name, par);
114  return NULL;
115}
116
117struct command_parameter_list*
118delete_command_parameter_list(struct command_parameter_list* parl)
119{
120  char rout_name[] = "delete_command_parameter_list";
121  if (parl == NULL) return NULL;
122  if (stamp_flag && parl->stamp != 123456)
123    fprintf(stamp_file, "d_c_p_l double delete --> %s\n", parl->name);
124  if (watch_flag) fprintf(debug_file, "deleting --> %s\n", parl->name);
125  if (parl->parameters != NULL)
126  {
127    for (int i = 0; i < parl->curr; i++)
128    {
129      if (parl->parameters[i] != NULL)
130      {
131        parl->parameters[i] = delete_command_parameter(parl->parameters[i]);
132      }
133    }
134    if (parl->parameters)  myfree(rout_name, parl->parameters);
135  }
136  myfree(rout_name, parl);
137  return NULL;
138}
139
140void
141dump_command_parameter(struct command_parameter* par)
142{
143  int i, k;
144  char logic[2][8] = {"false", "true"};
145  fprintf(prt_file, "parameter: %s   ", par->name);
146  switch (par->type)
147  {
148    case 0:
149      k = par->double_value;
150      fprintf(prt_file, "logical: %s\n", logic[k]);
151      break;
152    case 1:
153      if (par->expr != NULL)
154      {
155        dump_expression(par->expr);
156        par->double_value = expression_value(par->expr, 2);
157      }
158      k = par->double_value;
159      fprintf(prt_file, v_format("integer: %I\n"), k);
160      break;
161    case 2:
162      if (par->expr != NULL)
163      {
164        dump_expression(par->expr);
165        par->double_value = expression_value(par->expr, 2);
166      }
167      fprintf(prt_file, v_format("double value: %F\n"), par->double_value);
168      break;
169    case 11:
170    case 12:
171      if (par->double_array != NULL)
172      {
173        if (par->expr_list != NULL)
174        {
175          for (i = 0; i < par->double_array->curr; i++)
176          {
177            if (i < par->expr_list->curr && par->expr_list->list[i] != NULL)
178              par->double_array->a[i]
179                = expression_value(par->expr_list->list[i], 2);
180          }
181        }
182        fprintf(prt_file, "double array: ");
183        for (i = 0; i < par->double_array->curr; i++)
184          fprintf(prt_file, v_format("%e "), par->double_array->a[i]);
185        fprintf(prt_file, "\n");
186      }
187      break;
188    case 3:
189      fprintf(prt_file, "string: %s\n", par->string);
190      break;
191    case 13:
192      dump_char_p_array(par->m_string);
193  }
194}
195
196void
197print_command_parameter(struct command_parameter* par)
198{
199  int i, k;
200  char logic[2][8] = {"false", "true"};
201  switch (par->type)
202  {
203    case 0:
204      k = par->double_value;
205      fprintf(prt_file, "%s = %s, ", par->name, logic[k]);
206      break;
207    case 1:
208      k = par->double_value;
209      fprintf(prt_file, v_format("%s = %I, "), par->name, k);
210      break;
211    case 2:
212      fprintf(prt_file, v_format("%s = %F, "), par->name, par->double_value);
213      break;
214    case 11:
215    case 12:
216      if (par->double_array != NULL)
217      {
218        fprintf(prt_file, "double array: ");
219        for (i = 0; i < par->double_array->curr; i++)
220          fprintf(prt_file, v_format("%F, "), par->double_array->a[i]);
221        fprintf(prt_file, "\n");
222      }
223      break;
224    case 3:
225      fprintf(prt_file, "%s = %s, ", par->name, par->string);
226  }
227}
228
229void
230grow_command_parameter_list(struct command_parameter_list* p)
231{
232  char rout_name[] = "grow_command_parameter_list";
233  struct command_parameter** c_loc = p->parameters;
234  int j, new = 2*p->max;
235
236  p->max = new;
237  p->parameters = (struct command_parameter**)
238    mycalloc(rout_name,new, sizeof(struct command_parameter*));
239  for (j = 0; j < p->curr; j++) p->parameters[j] = c_loc[j];
240  myfree(rout_name, c_loc);
241}
242
243void
244export_comm_par(struct command_parameter* par, char* string)
245  /* exports a command parameter */
246{
247  int i, k, last;
248  char num[2*NAME_L];
249  strcat(string, ",");
250  strcat(string, par->name);
251  switch(par->type)
252  {
253    case 0:
254      strcat(string, "=");
255      if (par->double_value == zero) strcat(string, "false");
256      else                           strcat(string, "true");
257      break;
258    case 1:
259    case 2:
260      strcat(string, ":=");
261      if (par->expr != NULL) strcat(string, par->expr->string);
262      else
263      {
264        if (par->type == 1)
265        {
266          k = par->double_value; sprintf(num, v_format("%I"), k);
267        }
268        else sprintf(num, v_format("%F"), par->double_value);
269        strcat(string, supp_tb(num));
270      }
271      break;
272    case 3:
273      if (par->string != NULL)
274      {
275        strcat(string, "=");
276        strcat(string, par->string);
277      }
278      break;
279    case 11:
280    case 12:
281      strcat(string, ":=");
282      for (last = par->double_array->curr-1; last > 0; last--)
283      {
284        if (par->expr_list->list[last] != NULL)
285        {
286          if (zero_string(par->expr_list->list[last]->string) == 0) break;
287        }
288        else if (par->double_array->a[last] != zero) break;
289      }
290      strcat(string, "{");
291      for (i = 0; i <= last; i++)
292      {
293        if (i > 0) strcat(string, ",");
294        if (par->expr_list->list[i] != NULL)
295          strcat(string, par->expr_list->list[i]->string);
296        else
297        {
298          if (par->type == 11)
299          {
300            k = par->double_array->a[i]; sprintf(num, v_format("%I"), k);
301          }
302          else sprintf(num, v_format("%F"), par->double_array->a[i]);
303          strcat(string, supp_tb(num));
304        }
305      }
306      strcat(string, "}");
307  }
308}
309
310struct expression*
311command_par_expr(const char* parameter, struct command* cmd)
312  /* returns a command parameter expression if found, else NULL */
313{
314  struct expression* expr = NULL;
315  int i;
316  if ((i = name_list_pos(parameter, cmd->par_names)) > -1)
317    expr = cmd->par->parameters[i]->expr;
318  return expr;
319}
320
321double
322command_par_special(const char* parameter, struct element* el)
323/* construct missing tilt from normal and skew  */
324{
325  double val = zero;
326
327  if (strcmp(parameter, "tilt") == 0)
328  {
329    if ((val = command_par_value("tilt", el->def)) == zero)
330    {
331      val = zero;
332    }
333  }
334  else val = command_par_value(parameter, el->def);
335  return val;
336}
337
338char*
339command_par_string(const char* parameter, struct command* cmd)
340  /* returns a command parameter string if found, else NULL */
341{
342  struct command_parameter* cp;
343  char* p = NULL;
344  int i;
345  if ((i = name_list_pos(parameter, cmd->par_names)) > -1)
346  {
347    cp = cmd->par->parameters[i];
348    if (cp->type == 3) p = cp->string;
349  }
350  return p;
351}
352
353void
354add_cmd_parameter_clone(struct command* cmd,struct command_parameter *param,char* par_name,int inf) /*hbu add an identical copy (clone) of param to cmd */
355{
356  if(param)
357  {
358    cmd->par->parameters[cmd->par->curr] = clone_command_parameter(param); /* set current to identical copy (clone) of param */
359    add_to_name_list(par_name,inf,cmd->par_names);
360    cmd->par->curr++;
361  }
362}
363
364void
365add_cmd_parameter_new(struct command* cmd,double par_value,char* par_name,int inf) /*hbu add a new param with one value to cmd */
366{
367  cmd->par->parameters[cmd->par->curr] = new_command_parameter(par_name, 2);
368  cmd->par->parameters[cmd->par->curr]->double_value = par_value;
369  add_to_name_list(par_name,inf,cmd->par_names);
370  cmd->par->curr++;
371}
372
373double
374command_par_value(const char* parameter, struct command* cmd)
375  /* returns a command parameter value if found, else zero */
376{
377  struct command_parameter* cp;
378  double val = zero;
379  int i;
380  if ((i = name_list_pos(parameter, cmd->par_names)) > -1)
381  {
382    cp = cmd->par->parameters[i];
383    if (cp->type < 3)
384    {
385      if (cp->expr == NULL)  val = cp->double_value;
386      else val = expression_value(cp->expr, 2);
387    }
388  }
389  return val;
390}
391
392int
393command_par_value2(const char* parameter, struct command* cmd, double* val)
394  /* returns a command parameter value val
395     if found returns 1, else 0 */
396{
397  struct command_parameter* cp;
398  int i;
399  int ret = 0;
400
401  *val = zero;
402  if ((i = name_list_pos(parameter, cmd->par_names)) > -1)
403  {
404    cp = cmd->par->parameters[i];
405    if (cp->type < 3)
406    {
407      if (cp->expr == NULL)  *val = cp->double_value;
408      else *val = expression_value(cp->expr, 2);
409      ret = 1;
410    }
411  }
412
413  return ret;
414}
415
416struct double_array*
417command_par_array(const char* parameter, struct command* cmd)
418  /* returns an updated command parameter array if found, else NULL */
419{
420  struct double_array* arr = NULL;
421  int i;
422  if ((i = name_list_pos(parameter, cmd->par_names)) > -1)
423  {
424    struct command_parameter* cp = cmd->par->parameters[i];
425    if (cp->type == 11 || cp->type == 12)
426    {
427      arr = cp->double_array;
428      if (cp->expr_list != NULL) update_vector(cp->expr_list, arr);
429    }
430  }
431  return arr;
432}
433
434int
435command_par_vector(const char* parameter, struct command* cmd, double* vector)
436  /* returns the length of, and an updated command parameter vector
437     if found, else 0 */
438
439{
440  int i;
441  if ((i = name_list_pos(parameter, cmd->par_names)) > -1)
442  {
443    struct command_parameter* cp = cmd->par->parameters[i];
444    if (cp->double_array != NULL)
445    {
446      if (cp->expr_list != NULL)
447        update_vector(cp->expr_list, cp->double_array);
448      copy_double(cp->double_array->a, vector, cp->double_array->curr);
449      return cp->double_array->curr;
450    }
451  }
452  return 0;
453}
454
455void
456set_command_par_value(const char* parameter, struct command* cmd, double val)
457{
458  struct command_parameter* cp;
459  int i;
460  if ((i = name_list_pos(parameter, cmd->par_names)) > -1)
461  {
462    cp = cmd->par->parameters[i];
463    if (cp->type < 3)
464    {
465      cp->double_value = val;
466      if (cp->expr != NULL) cp->expr = delete_expression(cp->expr);
467      cmd->par_names->inform[i] = 1; /* mark as set */
468    }
469  }
470}
471
472struct command_parameter*
473store_comm_par_def(char* toks[], int start, int end)
474{
475  struct command_parameter *pl[2];
476  int i, j, jj, k, n, dummy, type, s_start, s_end, ss_end;
477  char c = *toks[start];
478
479  if (c == 'l')      type = 0;
480  else if (c == 'i') type = 1;
481  else if (c == 'r') type = 2;
482  else if (c == 's') type = 3;
483  else if (c == 'c') type = 4;
484  else               return NULL; /* error */
485  pl[0] = new_command_parameter(toks[start-3], type);
486  pl[1] = pl[0]->call_def = new_command_parameter(toks[start-3], type);
487  if (++start == end) return pl[0]; /* empty (r) etc. */
488  start++; /* skip , */
489  switch (type)
490  {
491    case 0:
492      jj = 0;
493      for (j = 0; j <= imin((end - start),2); j++)
494      {
495        if (strcmp(toks[start+j], "true") == 0
496            )  pl[jj]->double_value = 1;
497        jj++; j++; /* skip , */
498      }
499      break;
500    case 1: /* int */
501    case 2: /* double */
502    case 4: /* constraint */
503      for (i = 0; i < 2; i++)
504      {
505        if (start <= end)
506        {
507          get_bracket_t_range(toks, '{', '}', start, end, &s_start, &s_end);
508          if (s_start < start) /* no curly bracket */
509          {
510            if (pl[0]->type > 10) /* no call defaults */
511            {
512              pl[1]->double_array = pl[0]->double_array;
513              break;
514            }
515            if ((n = next_char(',', toks, start, end+1)) < 0) n = end+1;
516            if ((dummy = loc_expr(toks, n, start, &k)) > 1)
517            {
518              if (polish_expr(k + 1 - start, &toks[start]) ==  0)
519              {
520                pl[i]->expr =
521                  new_expression(join(&toks[start], k+1-start), deco);
522                pl[i]->double_value = expression_value(pl[i]->expr, 2);
523              }
524            }
525            else if (dummy > 0)
526              pl[i]->double_value  = simple_double(toks, start, k);
527            start = k + 2; /* skip , */
528          }
529          else
530          {
531            start = s_end + 1; s_start++; s_end--;
532            pl[i]->double_array = new_double_array(s_end + 1 - s_start);
533            pl[i]->expr_list = new_expr_list(s_end + 1 - s_start);
534            fill_expr_list(toks, s_start, s_end, pl[i]->expr_list);
535            update_vector(pl[i]->expr_list, pl[i]->double_array);
536            pl[i]->type += 10;
537          }
538        }
539      }
540      break;
541    case 3: /* string */
542      if (start <= end)
543      {
544        get_bracket_t_range(toks, '{', '}', start, end, &s_start, &s_end);
545        if (s_start < start) /* no curly bracket */
546        {
547          for (ss_end = start+1; ss_end < end; ss_end++)
548          {
549            if (*toks[ss_end] == ',') break;
550          }
551          if (strcmp(toks[start], none) != 0)
552          {
553            if (ss_end == start+1) pl[0]->string = permbuff(toks[start]);
554            else pl[0]->string = permbuff(join(&toks[start], ss_end-start));
555          }
556          if (ss_end < end)
557          {
558            start = ss_end + 1; /* skip , */
559            if (strcmp(toks[start], none) != 0)
560              pl[1]->string = permbuff(toks[start]);
561          }
562        }
563        else
564        {
565          start = s_end + 1; s_start++; s_end--;
566          pl[0]->m_string = new_char_p_array(s_end + 1 - s_start);
567          i = 0;
568          for (j = 0; j < pl[0]->m_string->max; j++)
569          {
570            if (*toks[s_start+j] != ',' && strcmp(toks[s_start+j], none) != 0)
571              pl[0]->m_string->p[i++] = permbuff(toks[s_start+j]);
572          }
573          pl[0]->m_string->curr = i;
574          pl[0]->type += 10;
575        }
576      }
577  }
578  return pl[0];
579}
580
581void
582store_comm_par_value(const char* parameter, double val, struct command* cmd)
583{
584  struct command_parameter* cp;
585  int i;
586  if ((i = name_list_pos(parameter, cmd->par_names)) > -1)
587  {
588    cp = cmd->par->parameters[i];
589    cp->type = 2;
590    if(cp->expr != NULL) cp->expr = delete_expression(cp->expr);
591    cp->double_value = val;
592  }
593}
594
595void
596store_set(struct command* comm, int flag)
597{
598  char* p;
599  char* name;
600  struct command_parameter* cp;
601  struct name_list* nl = comm->par_names;
602  int i, lp, n = 0, posf = name_list_pos("format", nl),
603    poss = name_list_pos("sequence", nl);
604  if (flag == 0 || (posf > -1 && nl->inform[posf]))
605  {
606    n++;
607    cp = comm->par->parameters[posf];
608    for (i = 0; i < cp->m_string->curr; i++)
609    {
610      p = noquote(cp->m_string->p[i]);
611      if (strchr(p, 's')) strcpy(string_format, p);
612      else if (strpbrk(p, "id")) strcpy(int_format, p);
613      else if (strpbrk(p, "feEgG")) strcpy(float_format, p);
614    }
615  }
616  if (flag != 0 && poss > -1 && nl->inform[poss])
617  {
618    n++;
619    name = comm->par->parameters[poss]->string;
620    if ((lp = name_list_pos(name, sequences->list)) > -1
621        && sequences->sequs[lp]->ex_start != NULL)
622      current_sequ = sequences->sequs[lp];
623    else
624    {
625      warning("ignoring unknown or unused sequence:", name);
626      return;
627    }
628  }
629  if (n == 0)
630  {
631    warning("no parameter specified,", "ignored");
632    return;
633  }
634}
635
636void
637fill_par_var_list(struct el_list* ell, struct command_parameter* par, struct var_list* varl)
638  /* puts all variables an element parameter depends on, in a list */
639{
640  int i;
641  switch (par->type)
642  {
643    case 1:
644    case 2:
645      if (par->expr != NULL) fill_expr_var_list(ell, par->expr, varl);
646      break;
647    case 11:
648    case 12:
649      for (i = 0; i < par->double_array->curr; i++)
650        if (i < par->expr_list->curr && par->expr_list->list[i] != NULL)
651          fill_expr_var_list(ell, par->expr_list->list[i], varl);
652      break;
653  }
654}
655
656int
657par_present(char* par, struct command* cmd, struct command_list* c_list)
658  /* returns 1 if in cmd or in c_list par is read, else returns 0 */
659{
660  struct name_list* nl;
661  int i, pos;
662  if (cmd != NULL)
663  {
664    nl = cmd->par_names;
665    pos = name_list_pos(par, nl);
666    if (pos > -1 && nl->inform[pos] > 0)  return 1;
667  }
668  if (c_list != NULL)
669  {
670    for (i = 0; i < c_list->curr; i++)
671    {
672      nl = c_list->commands[i]->par_names;
673      pos = name_list_pos(par, nl);
674      if (pos > -1 && nl->inform[pos] > 0)  return 1;
675    }
676  }
677  return 0;
678}
679
680void
681comm_para(char* name, int* n_int, int* n_double, int* n_string, int* int_array, double* double_array, char* strings, int* string_lengths)
682  /* returns the value for command parameter "name" being either
683     one or several integers (including logicals),
684     one or several doubles,
685     one or several strings (packed in one, with length array)
686     Input:
687     name                  parameter name
688     Output:
689     n_int                 # integers
690     n_double              # double
691     n_string              # strings
692     int_array             array for integers
693     double_array          array for doubles
694     strings               one string for all, packed
695     string_lengths        length of each string in char
696
697     ATTENTION: no check on sufficient array sizes
698  */
699{
700  char buf[NAME_L];
701  int i, l, pos;
702  struct command_parameter* cp;
703  struct double_array* arr = NULL;
704  *n_int = *n_double = *n_string = 0;
705  mycpy(buf, name);
706  if (this_cmd != NULL && this_cmd->clone != NULL)
707  {
708    if ((pos = name_list_pos(buf, this_cmd->clone->par_names)) > -1)
709    {
710      cp = this_cmd->clone->par->parameters[pos];
711      switch (cp->type)
712      {
713        case 0:
714          *n_int = 1;
715          *int_array = cp->double_value;
716          break;
717        case 1:
718          *n_int = 1;
719          if (cp->expr == NULL) *int_array = cp->double_value;
720          else *int_array = expression_value(cp->expr, 2);
721          break;
722        case 2:
723          *n_double = 1;
724          if (cp->expr == NULL) *double_array = cp->double_value;
725          else *double_array = expression_value(cp->expr, 2);
726          break;
727        case 3:
728          if (cp->string != NULL)
729          {
730            *n_string = 1;
731            l = *string_lengths = strlen(cp->string);
732            strncpy(strings, cp->string, l);
733          }
734          break;
735        case 11:
736        case 12:
737          arr = cp->double_array;
738          if (cp->expr_list != NULL) update_vector(cp->expr_list, arr);
739          if (cp->type == 11)
740          {
741            for (i = 0; i < arr->curr; i++) int_array[i] = arr->a[i];
742            *n_int = arr->curr;
743          }
744          else
745          {
746            for (i = 0; i < arr->curr; i++) double_array[i] = arr->a[i];
747            *n_double = arr->curr;
748          }
749          break;
750        case 13:
751          for (i = 0; i < cp->m_string->curr; i++)
752          {
753            string_lengths[i] = l = strlen(cp->m_string->p[i]);
754            strncpy(strings, cp->m_string->p[i], l);
755            strings += l;
756          }
757          *n_string = cp->m_string->curr;
758      }
759    }
760  }
761}
762
763void
764store_comm_par_vector(const char* parameter, double* val, struct command* cmd)
765{
766  struct command_parameter* cp;
767  int i;
768  if ((i = name_list_pos(parameter, cmd->par_names)) > -1)
769  {
770    cp = cmd->par->parameters[i];
771    if (cp->double_array != NULL)
772    {
773      copy_double(val, cp->double_array->a, cp->double_array->curr);
774      if (cp->expr_list != NULL)
775        cp->expr_list = delete_expr_list(cp->expr_list);
776    }
777  }
778}
779
780
781// LD: this function is horrible and probably buggy...
782int
783decode_par(struct in_cmd* cmd, int start, int number, int pos, int log)
784{
785  /* matches + stores in clone one keyword + following expression(s);
786     returns the position of last token
787     or -i where i is the start of an illegal item.
788  */
789  char** toks = cmd->tok_list->p;
790  struct expression* expr = NULL;
791  struct command_parameter* lp = cmd->cmd_def->par->parameters[pos];
792  struct command_parameter* clp = cmd->clone->par->parameters[pos];
793  int j, k, ks, i = start, e_type, ival, end, e_end, tot_end = 0, c_type = 0,
794      val_type = 0, cnt = 0, con_flag = 0, t_num;
795  double val = zero;
796
797  if (lp->type < 10)
798  {
799    if (lp->type == 0)
800    {
801      start = i - log;
802      if (log == 0)
803      {
804        if (i+2 < number && *toks[i+1] == '=')
805        {
806          if     (strncmp(toks[i+2], "true", 4) == 0)  ival = 1;
807          else if(strncmp(toks[i+2], "false", 5) == 0) ival = 0;
808          else return -i;
809          end = i+2;
810        }
811        else
812        {
813          end = i;
814          ival = lp->call_def->double_value;
815        }
816      }
817      else
818      {
819        end = i;
820        ival = 0;
821      }
822      tot_end = end;
823      clp->double_value = ival;
824    }
825    else if (lp->type == 3)  /* string */
826    {
827      if (i+2 < number && *toks[i+1] == '=')
828      {
829        i += 2;
830        for (j = i; j < number; j++)
831          if (name_list_pos(alias(toks[j]), cmd->cmd_def->par_names) >= 0) break;
832//        dirty quick fix for ticket #165
833//        if (*toks[j-1] == ',') j--;
834          while (*toks[j-1] == ',') j--;
835        tot_end = j - 1;
836        clp->string = permbuff(noquote(join(&toks[i], j - i)));
837      }
838      else tot_end = i;
839    }
840    else if (lp->type < 3)  /* one int or double value */
841    {
842      if ((i+2 < number && *toks[i+1] == '=') ||
843          (i+3 < number && *toks[i+1] == ':' && *toks[i+2] == '='))
844      {
845        val_type = (*toks[i+1] == ':' && *toks[i+2] == '=') ? 1 : 0;
846        start = val_type + i + 2;
847        for (t_num = start; t_num < number; t_num++) if(*toks[t_num] == ',')
848          break;
849        if ((e_type = loc_expr(toks, t_num, start, &end)) == 0) return -i;
850        tot_end = end;
851        if (e_type == 1) /* simple number */
852        {
853          val = simple_double(toks, start, end);
854          clp->expr = NULL;
855        }
856        else /* expression */
857        {
858          if ((expr =
859               make_expression(end + 1 - start, &toks[start])) == NULL)
860            return -i;
861          if (val_type) /* definition with ":=" */
862          {
863            val = zero;
864            clp->expr = clone_expression(expr);
865          }
866          else
867          {
868            val = expression_value(expr, 2);
869            clp->expr = NULL;
870          }
871          expr = delete_expression(expr);
872        }
873        clp->double_value = val;
874      }
875      else
876      {
877        clp->double_value = lp->call_def->double_value;
878        clp->expr = NULL;
879        tot_end = i;
880      }
881    }
882    else if (lp->type == 4) { /* one constraint */
883      if (i+1 < number && *toks[i+1] == ':')
884        con_flag = 1, i++;
885      else
886        con_flag = 0; /* if != zero, := or :< or :> */
887
888      if (i+2 < number) {
889             if (*toks[i+1] == '=') c_type = 4;
890        else if (*toks[i+1] == '>') c_type = 1;
891        else if (*toks[i+1] == '<') c_type = 2;
892
893        if (c_type) {
894          start = i + 2;
895
896          for (t_num = start; t_num < number; t_num++)
897            if(*toks[t_num] == ',') break;
898
899          if ((e_type = loc_expr(toks, t_num, start, &end)) == 0)
900            return -i;
901
902          tot_end = end;
903          if (e_type == 1) { /* simple number */
904            val = simple_double(toks, start, end);
905            expr = NULL;
906          }
907          else { /* expression */
908            if ((expr = make_expression(end + 1 - start, &toks[start])) == NULL)
909              return -i;
910            val = expression_value(expr, 2);
911          }
912        }
913      }
914      else {
915        c_type = 4;
916        val = zero;
917        expr = NULL;
918        tot_end = i;
919      }
920    }
921
922         if (clp->c_type == 1 && c_type == 2) clp->c_type = 3; /* min */
923    else if (clp->c_type == 2 && c_type == 1) clp->c_type = 3; /* max */
924    else                                      clp->c_type = c_type;
925    if (con_flag == 0) expr = NULL;
926
927    switch(c_type)
928    {
929      case 1:
930        clp->c_min = val;
931        clp->min_expr = clone_expression(expr);
932        break;
933      case 2:
934        clp->c_max = val;
935        clp->max_expr = clone_expression(expr);
936        break;
937      case 4:
938        clp->double_value = val;
939        clp->expr = clone_expression(expr);
940    }
941  }
942  else /* array */
943  {
944    if (lp->type == 13)  /* string array */
945    {
946      if (i+2 < number && *toks[i+1] == '=')
947      {
948        i += 2;
949        get_bracket_t_range(toks, '{', '}', i, number-1, &start, &end);
950        if (start >= i) /* {} found */
951        {
952          j = tot_end = end;
953          start++;
954        }
955        else /* terminate with next keyword */
956        {
957          start = i;
958          for (j = start; j < number; j++)
959            if (name_list_pos(toks[j], cmd->cmd_def->par_names) > -1)
960              break;
961          tot_end = j - 1;
962        }
963        ks = start;
964        for (k = start; k < j; k++)
965        {
966          if ((k+1 == j || *toks[k+1] == ','))
967          {
968            if (cnt == clp->m_string->max)
969              grow_char_p_array(clp->m_string);
970            clp->m_string->p[cnt++]
971              = permbuff(noquote(join(&toks[ks], k+1-ks)));
972            clp->m_string->curr = cnt;
973            ks = k + 2;  k++;
974          }
975        }
976      }
977      else
978      {
979        clp->m_string->curr = 0;
980        tot_end = i;
981      }
982    }
983    else if ((i+2 < number && *toks[i+1] == '=') ||
984             (i+3 < number && *toks[i+1] == ':' && *toks[i+2] == '='))
985    {
986      val_type = (*toks[i+1] == ':' && *toks[i+2] == '=') ? 1 : 0;
987      i += val_type + 2;
988      get_bracket_t_range(toks, '{', '}', i, number-1, &start, &end);
989      if (start >= i) /* {} found */
990      {
991        j = tot_end = end;
992        start++;
993      }
994      else /* terminate with next keyword */
995      {
996        start = i;
997        for (j = start; j < number; j++)
998          if (name_list_pos(toks[j], cmd->cmd_def->par_names) > -1)
999            break;
1000        tot_end = j - 1;
1001      }
1002      if (clp->expr_list == NULL)
1003        clp->expr_list = new_expr_list(clp->double_array->max);
1004      while (start < j)
1005      {
1006        if ((end = next_char(',', toks, start, j)) < 0) end = j;
1007        e_type = loc_expr(toks, end, start, &e_end);
1008        if (e_end != end - 1) return -1;
1009        if (cnt == clp->double_array->max)
1010        {
1011          grow_double_array(clp->double_array);
1012          grow_expr_list(clp->expr_list);
1013        }
1014        if ((expr = clp->expr_list->list[cnt]) != NULL)
1015          delete_expression(expr);
1016        if ((expr =
1017             make_expression(end - start, &toks[start])) == NULL)
1018          return -i;
1019        if (val_type && e_type == 2)
1020        {
1021          clp->expr_list->list[cnt] = clone_expression(expr);
1022          val = zero;
1023        }
1024        else
1025        {
1026          clp->expr_list->list[cnt] = NULL;
1027          val = expression_value(expr, e_type);
1028        }
1029        expr = delete_expression(expr);
1030        clp->double_array->a[cnt++] = val;
1031        if (cnt > clp->double_array->curr)
1032          clp->double_array->curr = clp->expr_list->curr = cnt;
1033        start = end + 1;
1034      }
1035    }
1036    else return -i;
1037  }
1038  if (expr != NULL) delete_expression(expr);
1039  return tot_end;
1040}
1041
1042char*
1043alias(char* par_string) /* returns main parameter for alias */
1044{
1045  if (strcmp(par_string, "filename") == 0)  return file_string;
1046  else if(strcmp(par_string, "true_") == 0) return vrai;
1047  else if(strcmp(par_string, "false_") == 0) return faux;
1048  else return par_string;
1049}
1050
1051int
1052log_val(char* name, struct command* cmd)
1053  /* returns 0 = false, 1 = true for a logical command parameter */
1054{
1055  struct name_list* nl = cmd->par_names;
1056  struct command_parameter_list* pl = cmd->par;
1057  int pos = name_list_pos(name, nl);
1058  if (pos > -1 && nl->inform[pos]) /* "name" has beem read */
1059    return pl->parameters[pos]->double_value == zero ? 0 : 1;
1060  else return 0;
1061}
1062
1063// public interface (used by Fortran)
1064
1065double
1066get_value(char* name, char* par)
1067  /* this function is used by fortran to get the parameters values
1068     returns parameter value "par" for command or store "name" if present,
1069     else INVALID */
1070{
1071  struct name_list* nl = NULL;
1072  mycpy(c_dum->c, name);
1073  mycpy(aux_buff->c, par);
1074  if (strcmp(c_dum->c, "beam") == 0)
1075    return command_par_value(aux_buff->c, current_beam);
1076  else if (strcmp(c_dum->c, "probe") == 0)
1077    return command_par_value(aux_buff->c, probe_beam);
1078  else if (strcmp(c_dum->c, "survey") == 0)
1079  {
1080    if (current_survey != NULL) nl = current_survey->par_names;
1081    if (nl != NULL && nl->inform[name_list_pos(aux_buff->c, nl)])
1082      return command_par_value(aux_buff->c, current_survey);
1083    else return zero;
1084  }
1085  else if (strcmp(c_dum->c, "twiss") == 0)
1086  {
1087    if (current_twiss != NULL) nl = current_twiss->par_names;
1088    if (nl != NULL && nl->inform[name_list_pos(aux_buff->c, nl)])
1089      return command_par_value(aux_buff->c, current_twiss);
1090    else return zero;
1091  }
1092  else if (strcmp(c_dum->c, "sequence") == 0)
1093  {
1094    if (strcmp(aux_buff->c, "l") == 0) return sequence_length(current_sequ);
1095    else if (strcmp(aux_buff->c, "range_start") == 0)
1096      return (current_sequ->range_start->position
1097              - 0.5 * current_sequ->range_start->length);
1098    else if (strcmp(aux_buff->c, "add_pass") == 0)
1099      return ((double)current_sequ->add_pass);
1100    else return INVALID;
1101  }
1102  else if (current_command != NULL
1103           && strcmp(c_dum->c, current_command->name) == 0)
1104    return command_par_value(aux_buff->c, current_command);
1105  else return INVALID;
1106}
1107
1108int
1109get_vector(char* name, char* par, double* vector)
1110  /* returns double "vector" for "par" of command or store "name";
1111     length is returned as function value (0 if not found) */
1112{
1113  mycpy(c_dum->c, name);
1114  mycpy(aux_buff->c, par);
1115  if (strcmp(c_dum->c, "threader") == 0)
1116    return command_par_vector(aux_buff->c, threader_par, vector);
1117  else return 0;
1118}
1119
1120int
1121get_string(char* name, char* par, char* string)
1122  /* returns string for  value "par" of command or store "name" if present,
1123     length = string length, else length = 0 if not present */
1124{
1125  struct name_list* nl = NULL;
1126  struct command* cmd;
1127  char* p;
1128  int length = 0;
1129  mycpy(c_dum->c, name);
1130  if (strcmp(c_dum->c, "beam") == 0)
1131  {
1132    mycpy(c_dum->c, par);
1133    if ((p = command_par_string(c_dum->c, current_beam)) != NULL)
1134    {
1135      strcpy(string, p); length = strlen(p);
1136    }
1137  }
1138  else if (strcmp(c_dum->c, "probe") == 0)
1139  {
1140    mycpy(c_dum->c, par);
1141    if ((p = command_par_string(c_dum->c, probe_beam)) != NULL)
1142    {
1143      strcpy(string, p); length = strlen(p);
1144    }
1145  }
1146  else if (strcmp(c_dum->c, "survey") == 0)
1147  {
1148    mycpy(c_dum->c, par);
1149    if (current_survey != NULL) nl = current_survey->par_names;
1150    if (nl != NULL && nl->inform[name_list_pos(c_dum->c, nl)])
1151    {
1152      if ((p = command_par_string(c_dum->c, current_survey)) != NULL)
1153      {
1154        strcpy(string, p); length = strlen(p);
1155      }
1156    }
1157  }
1158  /*else if (strcmp(c_dum->c, "ptc") == 0)
1159    {
1160    mycpy(c_dum->c, par);
1161    if (current_ptc != NULL) nl = current_ptc->par_names;
1162    if (nl != NULL )
1163    {
1164    if ((p = command_par_string(c_dum->c, current_ptc)) != NULL)
1165    {
1166    strcpy(string, p); length = strlen(p);
1167    }
1168    }
1169    }  */
1170  else if (strcmp(c_dum->c, "twiss") == 0)
1171  {
1172    mycpy(c_dum->c, par);
1173    if (current_twiss != NULL) nl = current_twiss->par_names;
1174    if (nl != NULL && nl->inform[name_list_pos(c_dum->c, nl)])
1175    {
1176      if ((p = command_par_string(c_dum->c, current_twiss)) != NULL)
1177      {
1178        strcpy(string, p); length = strlen(p);
1179      }
1180    }
1181  }
1182  else if (strcmp(c_dum->c, "sequence") == 0)
1183  {
1184    mycpy(c_dum->c, par);
1185    if (current_sequ != NULL && strcmp(c_dum->c, "name") == 0)
1186    {
1187      p = current_sequ->name;
1188      strcpy(string, p); length = strlen(p);
1189    }
1190  }
1191  else if (strcmp(c_dum->c, "element") == 0)
1192  {
1193    mycpy(c_dum->c, par);
1194    if (current_sequ != NULL && strcmp(c_dum->c, "name") == 0)
1195    {
1196      p = current_node->p_elem->name;
1197      strcpy(string, p); length = strlen(p);
1198    }
1199  }
1200  else
1201  {
1202/*     printf("<madxp.c: get_string>: Looking for command %s \n",c_dum->c);*/
1203
1204    cmd = find_command(c_dum->c, stored_commands);
1205    if (cmd == NULL)
1206    {
1207      if (current_command != NULL)
1208      {
1209        if ( strcmp(c_dum->c, current_command->name) == 0)
1210        {
1211          cmd = current_command;
1212        }
1213      }
1214    }
1215
1216    if (cmd != NULL)
1217    {
1218/*        printf("<madxp.c: get_string>: Found command %s \n",c_dum->c);*/
1219      mycpy(c_dum->c, par);
1220/*        printf("<madxp.c: get_string>: Looking for parameter %s \n",c_dum->c);*/
1221      if ((p = command_par_string(c_dum->c, cmd)) != NULL)
1222      {
1223        strcpy(string, p); length = strlen(p);
1224      }
1225      else
1226      {
1227        printf("<madxp.c: get_string>: Did not found parameter %s \n",c_dum->c);
1228      }
1229    }
1230
1231    else
1232    {
1233      printf("<madxp.c: get_string>: Did not found command %s \n",c_dum->c);
1234    }
1235  }
1236  return length;
1237}
1238
1239void
1240set_value(char* name, char* par, double* value)
1241  /* sets parameter value "par" for command or store "name" if present */
1242{
1243  mycpy(c_dum->c, name);
1244  mycpy(aux_buff->c, par);
1245  if (strcmp(c_dum->c, "beam") == 0)
1246    set_command_par_value(aux_buff->c, current_beam, *value);
1247  else if (strcmp(c_dum->c, "probe") == 0)
1248    set_command_par_value(aux_buff->c, probe_beam, *value);
1249  else if (strcmp(c_dum->c, "survey") == 0)
1250    set_command_par_value(aux_buff->c, current_survey, *value);
1251  else if (strcmp(c_dum->c, "twiss") == 0)
1252    set_command_par_value(aux_buff->c, current_twiss, *value);
1253  else if (current_command != NULL
1254           && strcmp(c_dum->c, current_command->name) == 0)
1255    set_command_par_value(aux_buff->c, current_command, *value);
1256}
1257
1258
Note: See TracBrowser for help on using the repository browser.