source: PSPA/madxPSPA/src/mad_table.c @ 441

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

import madx-5.01.00

File size: 61.4 KB
Line 
1#include "madx.h"
2
3#ifndef _WIN32
4#include <sys/utsname.h> // for uname
5#endif
6
7// private functions
8
9#if 0 // not used...
10static int
11table_org(char* table)
12  /* returns origin: 0  this job, 1 read or unknown */
13{
14  int pos;
15  int org = 1;
16  mycpy(c_dum->c, table);
17  if ((pos = name_list_pos(c_dum->c, table_register->names)) > -1)
18    org = table_register->tables[pos]->origin;
19  return org;
20}
21#endif
22
23static char*
24get_table_string(char* left, char* right)
25{
26/* for command tabstring(table,column,row) where table = table name, */
27/* column = name of a column containing strings, row = integer row number */
28/* starting at 0, returns the string found in that column and row, else NULL */
29  int col, ntok, pos, row;
30  char** toks;
31  struct table* table;
32  *right = '\0';
33  strcpy(c_dum->c, ++left);
34  supp_char(',', c_dum->c);
35  mysplit(c_dum->c, tmp_p_array);
36  toks = tmp_p_array->p; ntok = tmp_p_array->curr;
37  if (ntok == 3 && (pos = name_list_pos(toks[0], table_register->names)) > -1)
38  {
39    table = table_register->tables[pos];
40    if ((col = name_list_pos(toks[1], table->columns)) > -1)
41    {
42      row = atoi(toks[2]);
43      if(row > 0 && row <= table->curr && table->s_cols[col])
44        return table->s_cols[col][row-1];
45    }
46  }
47  return NULL;
48}
49
50static int
51tab_name_code(const char* name, const char* t_name)
52  /* returns 1 if name corresponds to t_name, else 0 */
53{
54  char tmp[2*NAME_L];
55  char *p, *n = one_string;
56  strcpy(tmp, name);
57  if ((p = strstr(tmp, "->")) != NULL)
58  {
59    *p = '\0'; p = strstr(name, "->"); p++; n = ++p;
60  }
61  if (strchr(t_name, ':'))
62  {
63    strcat(tmp, ":"); strcat(tmp, n);
64  }
65  return (strcmp(tmp, t_name) == 0 ? 1 : 0);
66}
67
68static int
69table_row(struct table* table, const char* name)
70{
71  int i, j, ret = -1;
72  for (i = 0; i < table->num_cols; i++)
73  {
74    if(table->columns->inform[i] == 3) {
75      if (debuglevel > 2)
76        printf("table_row: Column %d named <<%s>> is of strings. We use it to find the name.\n",
77               i,table->columns->names[i]);
78      break;
79    }
80  }
81
82  if (i < table->num_cols) {
83    for (j = 0; j < table->curr; j++)
84    {
85      if (debuglevel > 2) printf("table_row: Comparing <<%s>> <<%s>>\n",name, table->s_cols[i][j]);
86      if (tab_name_code(name, table->s_cols[i][j])) break;
87    }
88    if (j < table->curr) ret = j;
89  }
90  else
91  {
92    if (debuglevel > 1) printf("Can not find a column to search for row containing %s\n",name);
93  }
94/*  if(ret==-1) fatal_error("Name of row not found", name);*/
95  if(ret==-1) warning("table_row: Name of row not found:",name);
96  return ret;
97}
98
99static void
100add_table_vars(struct name_list* cols, struct command_list* select)
101  /* 1: adds user selected variables to table - always type 2 = double
102     2: adds aperture variables apertype (string) + aper_1, aper_2 etc. */
103{
104  int i, j, k, n, pos;
105  char* var_name;
106  char tmp[12];
107  struct name_list* nl;
108  struct command_parameter_list* pl;
109  for (i = 0; i < select->curr; i++)
110  {
111    nl = select->commands[i]->par_names;
112    pl = select->commands[i]->par;
113    pos = name_list_pos("column", nl);
114    if (nl->inform[pos])
115    {
116      for (j = 0; j < pl->parameters[pos]->m_string->curr; j++)
117      {
118        var_name = pl->parameters[pos]->m_string->p[j];
119        if (strcmp(var_name, "apertype") == 0)
120        {
121          if ((n = aperture_count(current_sequ)) > 0)
122          {
123            add_to_name_list(permbuff("apertype"), 3, cols);
124            for (k = 0; k < n; k++)
125            {
126              sprintf(tmp, "aper_%d", k+1);
127              add_to_name_list(permbuff(tmp), 2, cols);
128            }
129          }
130        }
131        else if (name_list_pos(var_name, cols) < 0) /* not yet in list */
132          add_to_name_list(permbuff(var_name), 2, cols);
133      }
134    }
135  }
136}
137
138static void
139grow_table_list(struct table_list* tl)
140{
141  char rout_name[] = "grow_table_list";
142  struct table** t_loc = tl->tables;
143  int j, new = 2*tl->max;
144
145  grow_name_list(tl->names);
146  tl->max = new;
147  tl->tables = mycalloc(rout_name,new, sizeof(struct table*));
148  for (j = 0; j < tl->curr; j++) tl->tables[j] = t_loc[j];
149  myfree(rout_name, t_loc);
150}
151
152static void
153grow_table_list_list(struct table_list_list* tll)
154{
155  char rout_name[] = "grow_table_list_list";
156  struct table_list** t_loc = tll->table_lists;
157  int j, new = 2*tll->max;
158
159  tll->max = new;
160  tll->table_lists = mycalloc(rout_name,new, sizeof(struct table_list*));
161  for (j = 0; j < tll->curr; j++) tll->table_lists[j] = t_loc[j];
162  myfree(rout_name, t_loc);
163}
164
165static void
166add_to_table_list_list(struct table_list* table_list, struct table_list_list* tll)
167  /* adds a table_list to a list of table_lists */
168{
169  int j;
170  for (j = 0; j < tll->curr; j++) 
171    if (tll->table_lists[j] == table_list) return;
172  if (tll->curr == tll->max) grow_table_list_list(tll);
173  tll->table_lists[tll->curr++] = table_list;
174}
175
176static void
177write_table(struct table* t, char* filename)
178  /* writes rows with columns listed in row and col */
179{
180  char l_name[NAME_L];
181  char t_pc[2*NAME_L];
182  char* pc = t_pc;
183  struct int_array* col = t->col_out;
184  struct int_array* row = t->row_out;
185  int i, j, k, tmp, n;
186  time_t now;
187  struct tm* tm;
188#if 0
189  char sys_name[200];
190#ifndef _WIN32
191  struct utsname u;
192  i = uname(&u); /* get system name */
193  strcpy(sys_name, u.sysname);
194#else // _WIN32
195  strcpy(sys_name, "Win32");
196#endif
197#endif
198
199  time(&now);    /* get system time */
200  tm = localtime(&now); /* split system time */
201  if (strcmp(filename, "terminal") == 0) out_file = stdout;
202  else if ((out_file = fopen(filename, "w")) == NULL)
203  {
204    warning("cannot open output file:", filename); return;
205  }
206  if (t != NULL)
207  {
208    strcpy(l_name, t->name);
209    n = strlen(t->name);
210    fprintf(out_file,
211            "@ NAME             %%%02ds \"%s\"\n", n,
212            stoupper(l_name));
213
214    strcpy(l_name, t->type);
215    n = strlen(t->type);
216    fprintf(out_file,
217            "@ TYPE             %%%02ds \"%s\"\n", n,
218            stoupper(l_name));
219
220    if (t->header != NULL)
221    {
222      for (j = 0; j < t->header->curr; j++)
223        fprintf(out_file, "%s\n", t->header->p[j]);
224    }
225    if (title != NULL)
226    {
227      n = strlen(title);
228      fprintf(out_file,
229              "@ TITLE            %%%02ds \"%s\"\n", n, title);
230    }
231
232    n = strlen(version_name)+strlen(version_ostype)+strlen(version_arch)+2;
233    fprintf(out_file,
234            "@ ORIGIN           %%%02ds \"%s %s %s\"\n",
235            n, version_name, version_ostype, version_arch);
236
237    fprintf(out_file,
238            "@ DATE             %%08s \"%02d/%02d/%02d\"\n",
239            tm->tm_mday, tm->tm_mon+1, tm->tm_year%100);
240
241    fprintf(out_file,
242            "@ TIME             %%08s \"%02d.%02d.%02d\"\n",
243            tm->tm_hour, tm->tm_min, tm->tm_sec);
244    fprintf(out_file, "* ");
245
246    for (i = 0; i < col->curr; i++)
247    {
248      strcpy(l_name, t->columns->names[col->i[i]]);
249      if (t->columns->inform[col->i[i]] == 1)
250        fprintf(out_file, v_format("%NIs "), stoupper(l_name));
251      else if (t->columns->inform[col->i[i]] == 2)
252        fprintf(out_file, v_format("%NFs "), stoupper(l_name));
253      else if (t->columns->inform[col->i[i]] == 3)
254        fprintf(out_file, v_format("%S "), stoupper(l_name));
255    }
256    fprintf(out_file, "\n");
257
258    fprintf(out_file, "$ ");
259    for (i = 0; i < col->curr; i++)
260    {
261      if (t->columns->inform[col->i[i]] == 1)
262        fprintf(out_file, v_format("%NIs "),"%d");
263      else if (t->columns->inform[col->i[i]] == 2)
264        fprintf(out_file, v_format("%NFs "),"%le");
265      else if (t->columns->inform[col->i[i]] == 3)
266        fprintf(out_file, v_format("%S "),"%s");
267    }
268    fprintf(out_file, "\n");
269
270    for (j = 0; j < row->curr; j++)
271    {
272      if (row->i[j])
273      {
274        if (t->l_head[j] != NULL)
275        {
276          for (k = 0; k < t->l_head[j]->curr; k++)
277            fprintf(out_file, "%s\n", t->l_head[j]->p[k]);
278        }
279        for (i = 0; i < col->curr; i++)
280        {
281/*          printf("row %d col %d datatype %d \n",j,i, t->columns->inform[col->i[i]] );*/
282          if (t->columns->inform[col->i[i]] == 1) {
283            tmp = t->d_cols[col->i[i]][j];
284            fprintf(out_file, v_format(" %I"), tmp);
285          }
286          else if (t->columns->inform[col->i[i]] == 2) {
287            fprintf(out_file, v_format(" %F"), t->d_cols[col->i[i]][j]);
288            /*printf("%s[%2d,%2d]=%+8.5f    ",t->name,col->i[i],j,t->d_cols[col->i[i]][j]);*/
289          }
290          else if (t->columns->inform[col->i[i]] == 3) {
291            pc[0] = c_dum->c[0] = '\"';
292            if (t->s_cols[col->i[i]][j] != NULL) {
293              strcpy(&c_dum->c[1], t->s_cols[col->i[i]][j]);
294              stoupper(c_dum->c);
295              pc = strip(c_dum->c); /* remove :<occ_count> */
296              k = strlen(pc);
297            }
298            else k = 1;
299            pc[k++] = '\"'; pc[k] = '\0';
300            fprintf(out_file, v_format(" %S "), pc);
301          }
302        }
303        fprintf(out_file, "\n");
304      }
305    }
306    if (strcmp(filename, "terminal") != 0) fclose(out_file);
307  }
308}
309
310#if 0 // not used...
311static int
312result_from_normal(char* name_var, int* order, double* val)
313  /* returns value of table normal_results corresponding to the given variable name
314     and to the given orders
315     function value return:
316     0  OK
317     -1 table  does not exist
318     -2 column does not exist
319     -3 row    does not exist
320  */
321{
322  int row,k,found,pos;
323  char string[AUX_LG],n_var[AUX_LG];
324  double d_val=zero;
325  struct table* t;
326
327  pos = name_list_pos("normal_results", table_register->names);
328  t = table_register->tables[pos];
329
330  *val = zero;
331  found = 0;
332  mycpy(n_var, name_var);
333  for (row = 1; row <= t->curr; row++)
334  {
335    k = string_from_table_row("normal_results","name", &row, string);
336    if (k != 0) return k;
337    if (strcmp(string,n_var) == 0)
338    {
339      found = 1;
340      k = double_from_table_row("normal_results","order1", &row, &d_val);
341      if ((int)d_val != order[0]) found = 0;
342      k = double_from_table_row("normal_results","order2", &row, &d_val);
343      if ((int)d_val != order[1]) found = 0;
344      k = double_from_table_row("normal_results","order3", &row, &d_val);
345      if ((int)d_val != order[2]) found = 0;
346      k = double_from_table_row("normal_results","order4", &row, &d_val);
347      if ((int)d_val != order[3]) found = 0;
348    }
349    if (found == 1) break;
350  }
351  if (found == 1)
352    k = double_from_table_row("normal_results","value", &row, &d_val);
353  *val = d_val;
354  return 0;
355}
356#endif
357
358#if 0 // not used...
359static struct table*
360read_his_table(struct in_cmd* cmd)
361  /* reads and stores TFS table */
362{
363  struct table* t = NULL;
364  struct char_p_array* tcpa = NULL;
365  struct name_list* tnl = NULL;
366  struct name_list* nl = cmd->clone->par_names;
367  struct command_parameter_list* pl = cmd->clone->par;
368  int pos = name_list_pos("file", nl);
369  int i, k, error = 0;
370  char *cc, *filename, *type = NULL, *tmp, *name;
371
372  if(nl->inform[pos] && (filename = pl->parameters[pos]->string) != NULL)
373  {
374    if ((tab_file = fopen(filename, "r")) == NULL)
375    {
376      warning("cannot open file:", filename); return NULL;
377    }
378  }
379  else
380  {
381    warning("no filename,","ignored"); return NULL;
382  }
383  while (fgets(aux_buff->c, aux_buff->max, tab_file))
384  {
385    cc = strtok(aux_buff->c, " \"\n");
386    if (*cc == '@')
387    {
388      if ((tmp = strtok(NULL, " \"\n")) != NULL
389          && strcmp(tmp, "TYPE") == 0)
390      {
391        if ((name = strtok(NULL, " \"\n")) != NULL) /* skip format */
392        {
393          if ((name = strtok(NULL, " \"\n")) != NULL)
394            type = permbuff(stolower(name));
395        }
396      }
397    }
398    else if (*cc == '*' && tnl == NULL)
399    {
400      tnl = new_name_list("table_names", 20);
401      while ((tmp = strtok(NULL, " \"\n")) != NULL)
402        add_to_name_list(permbuff(stolower(tmp)), 0, tnl);
403    }
404    else if (*cc == '$' && tcpa == NULL)
405    {
406      if (tnl == NULL)
407      {
408        warning("formats before names","skipped"); return NULL;
409      }
410      tcpa = new_char_p_array(20);
411      while ((tmp = strtok(NULL, " \"\n")) != NULL)
412      {
413        if (tcpa->curr == tcpa->max) grow_char_p_array(tcpa);
414        if (strcmp(tmp, "%s") == 0)       tnl->inform[tcpa->curr] = 3;
415        else if (strcmp(tmp, "%hd") == 0) tnl->inform[tcpa->curr] = 1;
416        else                              tnl->inform[tcpa->curr] = 2;
417        tcpa->p[tcpa->curr++] = permbuff(tmp);
418      }
419    }
420    else
421    {
422      if(t == NULL)
423      {
424        if (type == NULL)
425        {
426          warning("TFS table without type,","skipped"); error = 1;
427        }
428        else if (tcpa == NULL)
429        {
430          warning("TFS table without formats,","skipped"); error = 1;
431        }
432        else if (tnl == NULL)
433        {
434          warning("TFS table without column names,","skipped"); error = 1;
435        }
436        else if (tnl->curr == 0)
437        {
438          warning("TFS table: empty column name list,","skipped");
439          error = 1;
440        }
441        else if (tnl->curr != tcpa->curr)
442        {
443          warning("TFS table: number of names and formats differ,",
444                  "skipped");
445          error = 1;
446        }
447        if (error)
448        {
449          delete_name_list(tnl); return NULL;
450        }
451        t = new_table(type, "input", 500, tnl);
452      }
453      for (i = 0; i < tnl->curr; i++)
454      {
455        if (t->curr == t->max) grow_table(t);
456        tmp = tcpa->p[i];
457        if (strcmp(tmp,"%s") == 0)
458          t->s_cols[i][t->curr] = tmpbuff(stolower(cc));
459        else if (strcmp(tmp,"%d") == 0 || strcmp(tmp,"%hd") == 0)
460        {
461          sscanf(cc, tmp, &k); t->d_cols[i][t->curr] = k;
462        }
463        else sscanf(cc, tmp, &t->d_cols[i][t->curr]);
464        if (i+1 < tnl->curr)
465        {
466          if ((cc =strtok(NULL, " \"\n")) == NULL)
467          {
468            warning("incomplete table line starting with:", aux_buff->c);
469            return NULL;
470          }
471        }
472      }
473      t->curr++;
474    }
475  }
476  fclose(tab_file);
477  t->origin = 1;
478  add_to_table_list(t, table_register);
479  return NULL;
480}
481#endif
482
483static void
484set_selected_rows(struct table* t, struct command_list* select, struct command_list* deselect)
485{
486  int i, j;
487
488  if (!current_sequ) {
489    warning("No current selection available, skipping select", t->name);
490    return;
491  }
492
493  c_range_start = get_node_count(current_sequ->range_start);
494  c_range_end = get_node_count(current_sequ->range_end);
495
496  get_select_t_ranges(select, deselect, t);
497  if (select != 0)
498  {
499    for (j = 0; j < t->curr; j++)  t->row_out->i[j] = 0;
500    for (i = 0; i < select->curr; i++)
501    {
502      for (j = s_range->i[i]; j <= e_range->i[i]; j++)
503      {
504        if (t->row_out->i[j] == 0) t->row_out->i[j]
505                                     = pass_select(t->s_cols[0][j], select->commands[i]);
506      }
507    }
508  }
509  if (deselect != NULL)
510  {
511    for (i = 0; i < deselect->curr; i++)
512    {
513      for (j = sd_range->i[i]; j <= ed_range->i[i]; j++)
514      {
515        if (t->row_out->i[j] == 1) t->row_out->i[j]
516                                     = 1 - pass_select(t->s_cols[0][j], deselect->commands[i]);
517      }
518    }
519  }
520}
521
522// public interface
523
524struct table*
525new_table(char* name, char* type, int rows, struct name_list* cols)
526{
527  char rout_name[] = "new_table";
528  int i, n = cols->curr;
529  struct table* t
530    = (struct table*) mycalloc(rout_name,1, sizeof(struct table));
531
532  strcpy(t->name, name);
533  strcpy(t->type, type);
534  t->stamp = 123456;
535  if (watch_flag) fprintf(debug_file, "creating ++> %s\n", "table");
536  t->columns = cols;
537  t->num_cols = t->org_cols = n;
538  t->s_cols = (char***) mycalloc(rout_name,n, sizeof(char**));
539  t->d_cols = (double**) mycalloc(rout_name,n, sizeof(double*));
540  t->max = ++rows; /* +1 because of separate augment_count */
541  for (i = 0; i < n; i++)
542  {
543    if (cols->inform[i] < 3)
544      t->d_cols[i] = (double*) mycalloc(rout_name,rows, sizeof(double));
545    else if (cols->inform[i] == 3)
546      t->s_cols[i] = (char**) mycalloc(rout_name,rows, sizeof(char*));
547  }
548  t->row_out = new_int_array(rows);
549  t->col_out = new_int_array(n);
550  t->node_nm = new_char_p_array(rows);
551  t->p_nodes = mycalloc(rout_name,rows, sizeof(struct nodes*));
552  t->l_head  = mycalloc(rout_name,rows, sizeof(struct char_p_array*));
553  return t;
554}
555
556struct table_list*
557new_table_list(int size)
558{
559  char rout_name[] = "new_table_list";
560  struct table_list* tl
561    = (struct table_list*) mycalloc(rout_name,1, sizeof(struct table_list));
562  strcpy(tl->name, "table_list");
563  tl->stamp = 123456;
564  if (watch_flag) fprintf(debug_file, "creating ++> %s\n", tl->name);
565  tl->max = size;
566  tl->curr = 0;
567  tl->names = new_name_list(tl->name, size);
568  tl->tables
569    = (struct table**) mycalloc(rout_name,size, sizeof(struct table*));
570  add_to_table_list_list(tl, all_table_lists);
571  return tl;
572}
573
574struct table_list_list*
575new_table_list_list(int size)
576{
577  char rout_name[] = "new_table_list_list";
578  struct table_list_list* tll
579    = (struct table_list_list*) 
580    mycalloc(rout_name,1, sizeof(struct table_list_list));
581  strcpy(tll->name, "table_list_list");
582  tll->stamp = 123456;
583  if (watch_flag) fprintf(debug_file, "creating ++> %s\n", tll->name);
584  tll->max = size;
585  tll->curr = 0;
586  tll->table_lists
587    = (struct table_list**) 
588    mycalloc(rout_name,size, sizeof(struct table_list*));
589  return tll;
590}
591
592void
593check_table(char* string)
594  /* replaces argument of "table" if any by a string variable */
595{
596  char *pa, *pb, *pt, *pl, *pr, *sv;
597  pa = string;
598  while ((pb = strstr(pa, "table")) != NULL)
599  {
600    if (is_token(pb, string, 5))
601    {
602      if (quote_level(pa, pb) == 0)
603      {
604        mystrcpy(c_join, pa);                             // global var
605        pt = strstr(c_join->c, "table");
606        if ((pl = strchr(pt, '(')) == NULL) return;
607        if ((pr = strchr(pl, ')')) == NULL) return;
608        *pl = '\0';
609        *pr = '\0';
610        sv = make_string_variable(++pl);
611        *pa ='\0';
612        strcat(string, c_join->c);
613        strcat(string, " ( ");
614        strcat(string, sv);
615        strcat(string, " ) ");
616        strcat(string, ++pr);
617      }
618    }
619    pa = ++pb;
620  }
621}
622
623void
624check_tabstring(char* string)
625  /* replaces tabstring(tab_name, col_name, row_number) by the
626     string found in that column/row of table tab_name, or by "_void_"
627     if not found */
628{
629  char *pa, *pb, *pt, *pl, *pr, *sv;
630  pa = string;
631  while ((pb = strstr(pa, "tabstring")) != NULL)
632  {
633    if (is_token(pb, string, 9))
634    {
635      if (quote_level(pa, pb) == 0)
636      {
637        mystrcpy(c_join, pa);
638        pt = strstr(c_join->c, "tabstring");
639        if ((pl = strchr(pt, '(')) == NULL) return;
640        if ((pr = strchr(pl, ')')) == NULL) return;
641        if ((sv = get_table_string(pl,pr)) == NULL) sv = permbuff("_void_");
642        *pt = '\0';
643        *pa ='\0';
644        strcat(string, c_join->c);
645        strcat(string, sv);
646        strcat(string, ++pr);
647      }
648    }
649    pa = ++pb;
650  }
651}
652
653double
654table_value(void)
655{
656  double val = zero;
657  int ntok, pos, col, row;
658  char** toks;
659  struct table* table;
660  char temp[NAME_L];
661 
662  if (current_variable != NULL && current_variable->string != NULL) {
663    strcpy(c_dum->c, current_variable->string);
664    supp_char(',', c_dum->c);
665    mysplit(c_dum->c, tmp_p_array);
666    toks = tmp_p_array->p; ntok = tmp_p_array->curr;
667    /* toks[0] -> table name
668       toks[1] -> row name
669       toks[2] -> col name
670    */
671    if (ntok > 1) {
672      if ((pos = name_list_pos(toks[0], table_register->names)) > -1) {
673        table = table_register->tables[pos];
674        if ((col = name_list_pos(toks[ntok-1], table->columns)) > -1) {
675          if (ntok > 2) { /* find row - else current (dynamic), or 0 */
676            /* start mod - HG 26.3.2011 */
677            if (ntok > 5) { /* check for [ count ] and convert to ->count */
678              if (*toks[2] == '[' && *toks[4] == ']') {
679                strcat(toks[1], "->");
680                strcat(toks[1], toks[3]);
681              }
682                  }
683                  /* end mod - HG 26.3.2011 */
684            row = table_row(table, toks[1]);
685          }
686          else if (table->dynamic) row = table->curr;
687          else row = 0;
688          val = table->d_cols[col][row];
689        }
690        else if ((ntok == 3) && ((col = name_list_pos(toks[1], table->columns)) > -1)) {
691          row = atoi(toks[2])-1;
692          if(row < table->curr) val = table->d_cols[col][row];
693        }
694        else if(ntok == 2) {
695          strncpy(temp, toks[1], NAME_L);
696          if (strcmp(stolower(temp), "tablelength") == 0) val = table->curr;
697              }
698      }
699    }
700  }
701  return val;
702}
703
704struct column_info
705table_get_column(char* table_name,char* column_name)
706{
707  struct column_info info={NULL,0,'V',0};
708  int pos, col; // not used , i;
709  struct table* table;
710  if ((pos = name_list_pos(table_name, table_register->names)) > -1) {
711    table = table_register->tables[pos];
712    if ((col = name_list_pos(column_name, table->columns)) > -1) {
713      //printf("col: n %d type %d\n",col,table->columns->inform[col]);
714      info.length  = table->curr;
715      if (table->columns->inform[col]==2){
716        info.data =  table->d_cols[col];
717        info.datatype='d';
718        info.datasize=sizeof(double);
719      } else if (table->columns->inform[col]==3) {
720        info.data=table->s_cols[col];
721        info.datasize=NAME_L;
722        info.datatype='S';
723      };
724    }
725  }
726  return info;
727}
728
729void
730augment_count(char* table) /* increase table occ. by 1, fill missing */
731{
732  int pos;
733  struct table* t;
734  mycpy(c_dum->c, table);
735  if ((pos = name_list_pos(c_dum->c, table_register->names)) > -1)
736    t = table_register->tables[pos];
737  else {
738    warning("Can not find table",table);
739    return;
740  }
741
742  if (strcmp(t->type, "twiss") == 0) complete_twiss_table(t);
743
744  if (t->num_cols > t->org_cols)  add_vars_to_table(t);
745
746  if (t->p_nodes != NULL) t->p_nodes[t->curr] = current_node;
747
748  if (t->node_nm != NULL)
749  {
750    t->node_nm->p[t->curr] = current_node->name;
751    t->node_nm->curr = t->curr;
752  }
753  if (++t->curr == t->max) grow_table(t);
754}
755
756void
757augmentcountonly(char* table) /* increase table occ. by 1 */
758{
759  int pos;
760  struct table* t;
761  mycpy(c_dum->c, table);
762  if ((pos = name_list_pos(c_dum->c, table_register->names)) > -1)
763    t = table_register->tables[pos];
764  else
765  {
766    warning("Can not find table",table);
767    return;
768  }
769
770  if (t->num_cols > t->org_cols)  add_vars_to_table(t);
771
772  if (++t->curr == t->max) grow_table(t);
773}
774
775void
776add_to_table_list(struct table* t, struct table_list* tl)
777  /* adds table t to table list tl */
778{
779  int pos; //, j; not used
780  if ((pos = name_list_pos(t->name, tl->names)) < 0)
781  {
782    if (tl->curr == tl->max) grow_table_list(tl);
783    add_to_name_list(tmpbuff(t->name), 0, tl->names); // j = not used
784    tl->tables[tl->curr++] = t;
785  }
786  else
787  {
788    tl->tables[pos] = delete_table(tl->tables[pos]);
789    tl->tables[pos] = t;
790  }
791}
792
793void
794add_vars_to_table(struct table* t)
795  /* fills user-defined variables into current table_row) */
796{
797  int i;
798  char* p;
799
800  for (i = t->org_cols; i < t->num_cols; i++)
801  {
802    if (t->columns->inform[i] < 3)
803    {
804      if (strstr(t->columns->names[i], "aper_"))
805        t->d_cols[i][t->curr]
806          = get_aperture(current_node, t->columns->names[i]);
807      else if (strstr(t->columns->names[i], "aptol_"))
808        t->d_cols[i][t->curr]
809          = get_apertol(current_node, t->columns->names[i]);
810      else t->d_cols[i][t->curr] = get_variable(t->columns->names[i]);
811    }
812    else if (current_node)
813    {
814      if ((p = command_par_string(t->columns->names[i],
815                                  current_node->p_elem->def)) == NULL)
816        t->s_cols[i][t->curr] = tmpbuff("none");
817      else t->s_cols[i][t->curr] = tmpbuff(p);
818    }
819    else t->s_cols[i][t->curr] = get_varstring(t->columns->names[i]);
820  }
821}
822
823void
824set_vars_from_table(struct table* t)
825  /* set variables from current table_row) */
826{
827  int i;
828
829  for (i = 0; i < t->num_cols; i++)
830  {
831    if (t->columns->inform[i] ==2)
832    {
833      set_variable(t->columns->names[i],&t->d_cols[i][t->curr]) ;
834    }
835    else if (t->columns->inform[i] ==3)
836    {
837      set_stringvar(t->columns->names[i],t->s_cols[i][t->curr]) ;
838    }
839  }
840}
841
842struct table*
843delete_table(struct table* t)
844{
845  char rout_name[] = "delete_table";
846  int i, j;
847  if (t == NULL) return NULL;
848  if (stamp_flag && t->stamp != 123456)
849    fprintf(stamp_file, "d_t double delete --> %s\n", t->name);
850  if (watch_flag) fprintf(debug_file, "deleting --> %s\n", "table");
851  if (t->header != NULL) t->header = delete_char_p_array(t->header, 1);
852  if (t->col_out != NULL) t->col_out = delete_int_array(t->col_out);
853  if (t->row_out != NULL) t->row_out = delete_int_array(t->row_out);
854  if (t->node_nm != NULL) t->node_nm = delete_char_p_array(t->node_nm, 0);
855  for (i = 0; i < t->curr; i++) {
856    if (t->l_head[i] != NULL)
857      t->l_head[i] = delete_char_p_array(t->l_head[i], 1);
858  }
859  if (t->l_head)  myfree(rout_name, t->l_head);
860  if (t->p_nodes) myfree(rout_name, t->p_nodes);
861
862  if (t->d_cols) {
863    for (i = 0; i < t->num_cols; i++)
864      if (t->columns->inform[i] < 3 && t->d_cols[i])
865        myfree(rout_name, t->d_cols[i]);
866    myfree(rout_name, t->d_cols);
867  }
868
869  if (t->s_cols) {
870    for (i = 0; i < t->num_cols; i++) {
871      if (t->columns->inform[i] == 3 && t->s_cols[i]) {
872        for (j = 0; j < t->curr; j++)
873          if (t->s_cols[i][j]) myfree(rout_name, t->s_cols[i][j]);
874        myfree(rout_name, t->s_cols[i]);
875      }
876    }
877    myfree(rout_name, t->s_cols);
878  }
879  t->columns = delete_name_list(t->columns);
880  myfree(rout_name, t);
881  return NULL;
882}
883
884void
885double_table(char* table)
886{
887  int pos;
888  struct table* t;
889
890  mycpy(c_dum->c, table);
891  if ((pos = name_list_pos(c_dum->c, table_register->names)) > -1)
892    t = table_register->tables[pos];
893  else return;
894  grow_table(t);
895}
896
897void
898grow_table(struct table* t) /* doubles number of rows */
899{
900  char rout_name[] = "grow_table";
901  int i, j, new = 2*t->max;
902  char** s_loc;
903  struct char_p_array* t_loc = t->node_nm;
904  double* d_loc;
905  struct node** p_loc = t->p_nodes;
906  struct char_p_array** pa_loc = t->l_head;
907
908  t->max = new;
909  t->p_nodes = (struct node**) mycalloc(rout_name,new, sizeof(struct node*));
910  t->l_head
911    = (struct char_p_array**)
912    mycalloc(rout_name,new, sizeof(struct char_p_array*));
913  t->node_nm = new_char_p_array(new);
914
915  for (i = 0; i < t->curr; i++)
916  {
917    t->node_nm->p[i] = t_loc->p[i];
918    t->p_nodes[i] = p_loc[i];
919    t->l_head[i] = pa_loc[i];
920  }
921  delete_char_p_array(t_loc, 0);
922  myfree(rout_name, pa_loc);
923  t->node_nm->curr = t->curr; myfree(rout_name, p_loc);
924  for (j = 0; j < t->num_cols; j++)
925  {
926    if ((s_loc = t->s_cols[j]) != NULL)
927    {
928      t->s_cols[j] = (char**) mycalloc(rout_name,new, sizeof(char*));
929      for (i = 0; i < t->curr; i++) t->s_cols[j][i] = s_loc[i];
930      myfree(rout_name, s_loc);
931    }
932  }
933  for (j = 0; j < t->num_cols; j++)
934  {
935    if ((d_loc = t->d_cols[j]) != NULL)
936    {
937      t->d_cols[j] = (double*) mycalloc(rout_name,new, sizeof(double));
938      for (i = 0; i < t->curr; i++) t->d_cols[j][i] = d_loc[i];
939      myfree(rout_name, d_loc);
940    }
941  }
942}
943
944void
945print_table(struct table* t)
946{
947  int i, j, k, l, n, tmp, wpl = 4;
948  if (t != NULL)
949  {
950    fprintf(prt_file, "\n");
951    fprintf(prt_file, "++++++ table: %s\n", t->name);
952    l = (t->num_cols-1) / wpl + 1;
953    for (k = 0; k < l; k++)
954    {
955      n = wpl*(k+1) > t->num_cols ? t->num_cols : wpl*(k+1);
956      fprintf(prt_file, "\n");
957      for (i = wpl*k; i < n; i++)
958      {
959        if (t->columns->inform[i] == 1)
960          fprintf(prt_file, v_format("%NIs "), t->columns->names[i]);
961        else if (t->columns->inform[i] == 2)
962          fprintf(prt_file, v_format("%NFs "), t->columns->names[i]);
963        else if (t->columns->inform[i] == 3)
964          fprintf(prt_file, v_format("%S "), t->columns->names[i]);
965      }
966      fprintf(prt_file, "\n");
967      for (j = 0; j < t->curr; j++)
968      {
969        for (i = wpl*k; i < n; i++)
970        {
971          if (t->columns->inform[i] == 1)
972          {
973            tmp = t->d_cols[i][j];
974            fprintf(prt_file, v_format("%I "), tmp);
975          }
976          else if (t->columns->inform[i] == 2)
977            fprintf(prt_file, v_format("%F "), t->d_cols[i][j]);
978          else if (t->columns->inform[i] == 3)
979            fprintf(prt_file, v_format("%S "), t->s_cols[i][j]);
980        }
981        fprintf(prt_file, "\n");
982      }
983    }
984  }
985}
986
987void
988make_map_table(int* map_table_max_rows)
989{
990  int k, pos;
991  if ((pos = name_list_pos("map_table", table_register->names)) > -1)
992  {
993    delete_table(table_register->tables[pos]);
994    k = remove_from_name_list(table_register->tables[pos]->name,
995                              table_register->names);
996    table_register->tables[k] = table_register->tables[--table_register->curr];
997  }
998  /* initialise table */
999  map_table = make_table("map_table", "map_tab", map_tab_cols,
1000                         map_tab_types, *map_table_max_rows);
1001  add_to_table_list(map_table, table_register);
1002  map_table->dynamic = 1;
1003  reset_count("map_table");
1004}
1005
1006struct table*
1007make_table(char* name, char* type, char** table_cols, int* table_types, int rows)
1008{
1009  struct table* t;
1010  struct name_list *cols;
1011  struct command_list* scl;
1012  int i, n = 0;
1013  while (*table_cols[n] != ' ')
1014  {
1015/*     printf("make table %s col %d %s\n",name, n, table_cols[n]);*/
1016    n++;
1017  }
1018  cols = new_name_list("columns", n);
1019  for (i = 0; i < n; i++)
1020    add_to_name_list(table_cols[i], table_types[i], cols);
1021  if ((scl = find_command_list(name, table_select)) != NULL && scl->curr > 0)
1022    add_table_vars(cols, scl);
1023  t = new_table(name, type, rows, cols);
1024  t->org_cols = n;
1025  return t;
1026}
1027
1028void
1029reset_count(char* table) /* resets table counter to zero */
1030{
1031  int pos;
1032  struct table* t;
1033  mycpy(c_dum->c, table);
1034  if ((pos = name_list_pos(c_dum->c, table_register->names)) > -1)
1035    t = table_register->tables[pos];
1036  else return;
1037  t->curr = 0;
1038}
1039
1040void
1041sector_out(char* sector_table_name, double* pos, double* kick, double* rmatrix, double* tmatrix)
1042{
1043  int i;
1044  int j;
1045  int k;
1046  int index;
1047
1048  /* the name is not \0 finished and displays ugly in C */
1049  /* but well, this is how table names are handled... */
1050
1051  char * elementName = current_node->p_elem->name;
1052
1053  string_to_table_curr( sector_table_name, "name", elementName );
1054  double_to_table_curr( sector_table_name, "pos", pos );
1055
1056  /* 6 kicks */
1057  for (i=0; i<6; i++){
1058    char kickStr[2+1];
1059    sprintf(kickStr,"k%i",i+1);
1060    kickStr[2]='\0';
1061    index = i;
1062    double_to_table_curr( sector_table_name, kickStr,&kick[index]);
1063  }
1064  /* 36 R-matrix terms */
1065  for (j=0; j<6; j++){
1066    for (i=0; i<6; i++){
1067      char rStr[3+1];
1068      sprintf(rStr,"r%i%i",i+1,j+1);
1069      rStr[3]='\0';
1070      index = i+j*6;
1071      double_to_table_curr( sector_table_name, rStr,&rmatrix[index]);
1072    }
1073  }
1074  /* 216 T-matrix terms */
1075  for (k=0; k<6; k++){
1076    for (j=0; j<6; j++){
1077      for (i=0;i<6; i++){
1078        char tStr[4+1];
1079        sprintf(tStr,"t%i%i%i",i+1,j+1,k+1);
1080        tStr[4]='\0';
1081        index = i+j*6+k*36;
1082        double_to_table_curr( sector_table_name, tStr,&tmatrix[index]);
1083      }
1084    }
1085  }
1086
1087  augment_count( sector_table_name ); /* move to next record */
1088}
1089
1090void
1091out_table(char* tname, struct table* t, char* filename)
1092  /* output of a table */
1093{
1094  int j;
1095
1096  struct command_list* scl = find_command_list(tname, table_select);
1097  struct command_list* dscl = find_command_list(tname, table_deselect);
1098  while (t->num_cols > t->col_out->max)
1099    grow_int_array(t->col_out);
1100  while (t->curr > t->row_out->max)
1101    grow_int_array(t->row_out);
1102  t->row_out->curr = t->curr;
1103  if (par_present("full", NULL, scl))
1104    put_info("obsolete option 'full'"," ignored on 'select'");
1105  for (j = 0; j < t->curr; j++) t->row_out->i[j] = 1;
1106  for (j = 0; j < t->num_cols; j++) t->col_out->i[j] = j;
1107  t->col_out->curr = t->num_cols;
1108  if ((scl != NULL && scl->curr > 0) || (dscl != NULL && dscl->curr > 0))
1109  {
1110    set_selected_columns(t, scl);
1111    set_selected_rows(t, scl, dscl);
1112  }
1113  write_table(t, filename);
1114}
1115
1116struct table*
1117read_table(struct in_cmd* cmd)
1118  /* reads and stores TFS table */
1119{
1120  struct table* t = NULL;
1121  struct char_p_array* tcpa = NULL;
1122  struct name_list* tnl = NULL;
1123  struct name_list* nl = cmd->clone->par_names;
1124  struct command_parameter_list* pl = cmd->clone->par;
1125  int pos = name_list_pos("file", nl);
1126  short sk;
1127  int i, k, error = 0;
1128  char *cc, *filename, *type = NULL, *tmp, *name;
1129
1130  char* namtab;
1131
1132  if ((namtab = command_par_string("table",cmd->clone)) != NULL) {
1133    printf("Want to make named table: %s\n",namtab);
1134  }
1135  else
1136  {
1137    if (get_option("debug")) {
1138      printf("No table name requested\n");
1139      printf("Use default name (i.e. name from file) \n");
1140    }
1141    namtab = NULL;
1142  }
1143
1144  if(nl->inform[pos] && (filename = pl->parameters[pos]->string) != NULL)
1145  {
1146    if ((tab_file = fopen(filename, "r")) == NULL)
1147    {
1148      fatal_error("cannot open file:", filename); return NULL;
1149    }
1150  }
1151  else
1152  {
1153    warning("no filename,","ignored"); return NULL;
1154  }
1155  while (fgets(aux_buff->c, aux_buff->max, tab_file))
1156  {
1157    supp_char('\r', aux_buff->c);
1158    cc = strtok(aux_buff->c, " \"\n");
1159    if (*cc == '@')
1160    {
1161      if ((tmp = strtok(NULL, " \"\n")) != NULL
1162          && strcmp(tmp, "TYPE") == 0)
1163      {
1164        if ((name = strtok(NULL, " \"\n")) != NULL) /* skip format */
1165        {
1166          if ((name = strtok(NULL, " \"\n")) != NULL)
1167            type = permbuff(stolower(name));
1168        }
1169      }
1170      else if (strcmp(tmp, "NAME") == 0)
1171      {
1172        if ((name = strtok(NULL, " \"\n")) != NULL) /* skip format */
1173        {
1174          if ((name = strtok(NULL, " \"\n")) != NULL)
1175            namtab = permbuff(stolower(name));
1176        }
1177      }
1178    }
1179    else if (*cc == '*' && tnl == NULL)
1180    {
1181      tnl = new_name_list("table_names", 20);
1182      while ((tmp = strtok(NULL, " \"\n")) != NULL)
1183        add_to_name_list(permbuff(stolower(tmp)), 0, tnl);
1184    }
1185    else if (*cc == '$' && tcpa == NULL)
1186    {
1187      if (tnl == NULL)
1188      {
1189        warning("formats before names","skipped"); return NULL;
1190      }
1191      tcpa = new_char_p_array(20);
1192      while ((tmp = strtok(NULL, " \"\n")) != NULL)
1193      {
1194        if (tcpa->curr == tcpa->max) grow_char_p_array(tcpa);
1195        if (strcmp(tmp, "%s") == 0)       tnl->inform[tcpa->curr] = 3;
1196        else if (strcmp(tmp, "%d") == 0)  tnl->inform[tcpa->curr] = 1;
1197        else if (strcmp(tmp, "%hd") == 0) tnl->inform[tcpa->curr] = 1;
1198        else                              tnl->inform[tcpa->curr] = 2;
1199        tcpa->p[tcpa->curr++] = permbuff(tmp);
1200      }
1201    }
1202    else
1203    {
1204      if(t == NULL)
1205      {
1206        if (type == NULL)
1207        {
1208          warning("TFS table without type,","skipped"); error = 1;
1209        }
1210        else if (tcpa == NULL)
1211        {
1212          warning("TFS table without formats,","skipped"); error = 1;
1213        }
1214        else if (tnl == NULL)
1215        {
1216          warning("TFS table without column names,","skipped"); error = 1;
1217        }
1218        else if (tnl->curr == 0)
1219        {
1220          warning("TFS table: empty column name list,","skipped");
1221          error = 1;
1222        }
1223        else if (tnl->curr != tcpa->curr)
1224        {
1225          warning("TFS table: number of names and formats differ,",
1226                  "skipped");
1227          error = 1;
1228        }
1229        if (error)
1230        {
1231          delete_name_list(tnl); return NULL;
1232        }
1233        if(namtab != NULL) {
1234          t = new_table(namtab, type,    500, tnl);
1235        }
1236        else
1237        {
1238          t = new_table(type, type,    500, tnl);
1239        }
1240      }
1241      for (i = 0; i < tnl->curr; i++)
1242      {
1243        if (t->curr == t->max) grow_table(t);
1244        tmp = tcpa->p[i];
1245        if (strcmp(tmp,"%s") == 0) t->s_cols[i][t->curr] = stolower(tmpbuff(cc));
1246        else if (strcmp(tmp,"%d") == 0)
1247        {
1248          sscanf(cc, tmp, &k); t->d_cols[i][t->curr] = k;
1249        }
1250        else if (strcmp(tmp,"%hd") == 0)
1251        {
1252          sscanf(cc, tmp, &sk); t->d_cols[i][t->curr] = sk;
1253        }
1254        else sscanf(cc, tmp, &t->d_cols[i][t->curr]);
1255        if (i+1 < tnl->curr)
1256        {
1257          if ((cc =strtok(NULL, " \"\n")) == NULL)
1258          {
1259            warning("incomplete table line starting with:", aux_buff->c);
1260            return NULL;
1261          }
1262        }
1263      }
1264      t->curr++;
1265    }
1266  }
1267  fclose(tab_file);
1268  if ((tab_file = fopen(filename, "r")) == NULL)
1269  {
1270    warning("cannot open file:", filename); return NULL;
1271  }
1272/* read & store table header */
1273  t->header = new_char_p_array(50);
1274  while (fgets(aux_buff->c, aux_buff->max, tab_file))
1275  {
1276    supp_char('\r', aux_buff->c);
1277    if ((*aux_buff->c != ' ') &&
1278        ((*aux_buff->c == '@') || (*aux_buff->c == '*')))
1279    {
1280      if (t->header->curr == t->header->max) grow_char_p_array(t->header);
1281      t->header->p[t->header->curr]
1282        = (char*) mymalloc("read_table", strlen(aux_buff->c)+1);
1283      strcpy(t->header->p[t->header->curr], aux_buff->c);
1284      t->header->curr++;
1285    }
1286  }
1287  fclose(tab_file);
1288  t->origin = 1;
1289  add_to_table_list(t, table_register);
1290  return NULL;
1291}
1292
1293int
1294get_table_range(char* range, struct table* table, int* rows)
1295  /* returns start and end row (rows[0] and rows[1])
1296     of a range in a table; 0 if not found, 1 (1 row) or 2 ( > 1) */
1297{
1298  int i, n;
1299  char* c[2];
1300  char tmp[NAME_L], dumtex[3*NAME_L];;
1301  rows[0] = rows[1] = 0;
1302  mycpy(c_dum->c, range); stolower(c_dum->c); strcpy(dumtex, c_dum->c);
1303  c[0] = strtok(c_dum->c, "/");
1304  if ((c[1] = strtok(NULL,"/")) == NULL) /* only one element given */
1305    n = 1;
1306  else n = 2;
1307  for (i = 0; i < n; i++) {
1308    if (*c[i] == '#') {
1309      if (strncmp(c[i], "#s", 2) == 0) rows[i] = 0;
1310      else if (strncmp(c[i], "#e", 2) == 0) rows[i] = table->curr - 1;
1311      else {
1312        warning("illegal table range ignored:", dumtex);
1313        return 0;
1314      }
1315    }
1316    else {
1317      strcpy(tmp, c[i]);
1318      if (square_to_colon(tmp) == 0) {
1319        warning("illegal table range ignored:", dumtex);
1320        return 0;
1321      }
1322      if ((rows[i] = char_p_pos(tmp, table->node_nm)) < 0) {
1323        warning("illegal table range ignored:", dumtex);
1324        return 0;
1325      }
1326    }
1327  }
1328  if (n == 1) rows[1] = rows[0];
1329  return n;
1330}
1331
1332void
1333table_range(char* table, char* range, int* rows)
1334  /* returns first and last row numbers (start=1) in rows
1335     or 0 if table or range invalid */
1336{
1337  int pos;
1338  struct table* t;
1339
1340  rows[0] = rows[1] = 0;
1341  mycpy(c_dum->c, table);
1342  if ((pos = name_list_pos(c_dum->c, table_register->names)) > -1) {
1343    t = table_register->tables[pos];
1344    get_table_range(range, t, rows);
1345    rows[0]++; rows[1]++;
1346  }
1347}
1348
1349struct table*
1350read_my_table(struct in_cmd* cmd)
1351     /* reads and stores TFS table */
1352{
1353  struct table* t = NULL;
1354  struct char_p_array* tcpa = NULL;
1355  struct name_list* tnl = NULL;
1356  struct name_list* nl = cmd->clone->par_names;
1357  struct command_parameter_list* pl = cmd->clone->par;
1358  int pos = name_list_pos("file", nl);
1359  int i, k, error = 0;
1360  short  sk;
1361  char *cc, *filename, *type = NULL, *tmp, *name;
1362
1363  char* namtab;
1364
1365  if ((namtab = command_par_string("table",cmd->clone)) != NULL) {
1366       printf("Want to make named table: %s\n",namtab);
1367  } else {
1368       if (get_option("debug")) {
1369         printf("No table name requested\n");
1370         printf("Use default name (i.e. name from file) \n");
1371       }
1372       namtab = NULL;
1373  }
1374
1375  if(nl->inform[pos] && (filename = pl->parameters[pos]->string) != NULL)
1376    {
1377     if ((tab_file = fopen(filename, "r")) == NULL)
1378       {
1379         fatal_error("cannot open file:", filename); return NULL; /* frs: to avoid unwanted results */
1380       }
1381    }
1382  else
1383    {
1384     warning("no filename,","ignored"); return NULL;
1385    }
1386  while (fgets(aux_buff->c, aux_buff->max, tab_file))
1387    {
1388     cc = strtok(aux_buff->c, " \"\n");
1389     if (*cc == '@')
1390       {
1391       if ((tmp = strtok(NULL, " \"\n")) != NULL
1392              && strcmp(tmp, "TYPE") == 0)
1393        {
1394         if ((name = strtok(NULL, " \"\n")) != NULL) /* skip format */
1395           {
1396            if ((name = strtok(NULL, " \"\n")) != NULL)
1397                  type = permbuff(stolower(name));
1398           }
1399        }
1400       }
1401     else if (*cc == '*' && tnl == NULL)
1402       {
1403      tnl = new_name_list("table_names", 20);
1404        while ((tmp = strtok(NULL, " \"\n")) != NULL)
1405            add_to_name_list(permbuff(stolower(tmp)), 0, tnl);
1406       }
1407     else if (*cc == '$' && tcpa == NULL)
1408       {
1409      if (tnl == NULL)
1410        {
1411         warning("formats before names","skipped"); return NULL;
1412        }
1413      tcpa = new_char_p_array(20);
1414        while ((tmp = strtok(NULL, " \"\n")) != NULL)
1415        {
1416         if (tcpa->curr == tcpa->max) grow_char_p_array(tcpa);
1417           if (strcmp(tmp, "%s") == 0)       tnl->inform[tcpa->curr] = 3;
1418           else if (strcmp(tmp, "%hd") == 0) tnl->inform[tcpa->curr] = 1;
1419           else if (strcmp(tmp, "%d") == 0)  tnl->inform[tcpa->curr] = 1;
1420           else                              tnl->inform[tcpa->curr] = 2;
1421           tcpa->p[tcpa->curr++] = permbuff(tmp);
1422        }
1423       }
1424     else
1425       {
1426        if(t == NULL)
1427          {
1428         if (type == NULL)
1429           {
1430            warning("TFS table without type,","skipped"); error = 1;
1431           }
1432         else if (tcpa == NULL)
1433           {
1434            warning("TFS table without formats,","skipped"); error = 1;
1435           }
1436         else if (tnl == NULL)
1437           {
1438            warning("TFS table without column names,","skipped"); error = 1;
1439           }
1440         else if (tnl->curr == 0)
1441           {
1442            warning("TFS table: empty column name list,","skipped");
1443              error = 1;
1444           }
1445         else if (tnl->curr != tcpa->curr)
1446           {
1447            warning("TFS table: number of names and formats differ,",
1448                       "skipped");
1449              error = 1;
1450           }
1451           if (error)
1452           {
1453            delete_name_list(tnl); return NULL;
1454           }
1455           if(namtab != NULL) {
1456             t = new_table(namtab, type,    500, tnl);
1457           } else {
1458             t = new_table(type, type,    500, tnl);
1459           }
1460        }
1461      for (i = 0; i < tnl->curr; i++)
1462        {
1463         if (t->curr == t->max) grow_table(t);
1464         tmp = tcpa->p[i];
1465           if (strcmp(tmp,"%s") == 0) t->s_cols[i][t->curr] = stolower(tmpbuff(cc));
1466           else if (strcmp(tmp,"%d") == 0 )
1467           {
1468            sscanf(cc, tmp, &k); t->d_cols[i][t->curr] = k;
1469           }
1470           else if (strcmp(tmp,"%hd") == 0 )
1471           {
1472            sscanf(cc, tmp, &sk); t->d_cols[i][t->curr] = sk;
1473           }
1474           else sscanf(cc, tmp, &t->d_cols[i][t->curr]);
1475           if (i+1 < tnl->curr)
1476           {
1477              if ((cc =strtok(NULL, " \"\n")) == NULL)
1478              {
1479               warning("incomplete table line starting with:", aux_buff->c);
1480                 return NULL;
1481              }
1482           }
1483        }
1484        t->curr++;
1485       }
1486    }
1487  fclose(tab_file);
1488  t->origin = 1;
1489  add_to_table_list(t, table_register);
1490  return NULL;
1491}
1492
1493void
1494set_selected_columns(struct table* t, struct command_list* select)
1495{
1496  int i, j, pos, k, n = 0;
1497  char* p;
1498  struct name_list* nl;
1499  struct command_parameter_list* pl;
1500  if (select && par_present("column", NULL, select))
1501  {
1502    for (j = 0; j < t->num_cols; j++)  /* deselect all columns */
1503      t->col_out->i[j] = 0;
1504    t->col_out->curr = 0;
1505    for (i = 0; i < select->curr; i++)
1506    {
1507      nl = select->commands[i]->par_names;
1508      pl = select->commands[i]->par;
1509      pos = name_list_pos("column", nl);
1510      if (nl->inform[pos])
1511      {
1512        for (j = 0; j < pl->parameters[pos]->m_string->curr; j++)
1513        {
1514          if (strcmp(pl->parameters[pos]->m_string->p[j], "re") == 0)
1515          {
1516            for (k = 0; k < t->num_cols; k++)
1517            {
1518              if (strncmp("re", t->columns->names[k], 2) == 0)
1519              {
1520                if (k <  t->num_cols
1521                    && int_in_array(k, n, t->col_out->i) == 0)
1522                  t->col_out->i[n++] = k;
1523              }
1524            }
1525          }
1526          else if (strcmp(pl->parameters[pos]->m_string->p[j], "eign") == 0)
1527          {
1528            for (k = 0; k < t->num_cols; k++)
1529            {
1530              if (strncmp("eign", t->columns->names[k], 2) == 0)
1531              {
1532                if (k <  t->num_cols
1533                    && int_in_array(k, n, t->col_out->i) == 0)
1534                  t->col_out->i[n++] = k;
1535              }
1536            }
1537          }
1538          else if (strcmp(pl->parameters[pos]->m_string->p[j],
1539                          "apertype") == 0)
1540          {
1541            for (k = 0; k < t->num_cols; k++)
1542            {
1543              if (strncmp("aper", t->columns->names[k], 4) == 0)
1544              {
1545                if (k <  t->num_cols
1546                    && int_in_array(k, n, t->col_out->i) == 0)
1547                  t->col_out->i[n++] = k;
1548              }
1549            }
1550          }
1551          else
1552          {
1553            p = pl->parameters[pos]->m_string->p[j];
1554            if ((k = name_list_pos(p, t->columns)) > -1)
1555            {
1556              if (k <  t->num_cols
1557                  && int_in_array(k, n, t->col_out->i) == 0)
1558                t->col_out->i[n++] = k;
1559            }
1560          }
1561        }
1562      }
1563    }
1564    t->col_out->curr = n;
1565  }
1566}
1567
1568/*
1569  Grouping accessors
1570*/
1571
1572#if 0
1573int
1574str_from_table(char* table, char* name, int* row, char* val)
1575     /* WH 22.06.2004, corrected from: char_from_table */
1576     /* returns val at position row in column with name "name".
1577        function value return:
1578        0  OK
1579        -1 table  does not exist
1580        -2 column does not exist
1581        -3 row    does not exist
1582     */
1583{
1584  int pos;
1585  struct table* t;
1586
1587  strcpy(val,"No-Name");
1588  mycpy(c_dum->c, table);
1589  if ((pos = name_list_pos(c_dum->c, table_register->names)) > -1)
1590    t = table_register->tables[pos];
1591  else return -1;
1592  mycpy(c_dum->c, name);
1593  if ((pos = name_list_pos(c_dum->c, t->columns)) < 0) return -2;
1594  if (*row > t->curr)  return -3;
1595   strncpy(val,t->s_cols[pos][*row-1],NAME_L);
1596  // completely buggy and slow
1597  // while (strlen(val)<NAME_L) val[strlen(val)]=' ';
1598  val[NAME_L-1] = '\0';
1599  return 0;
1600}
1601
1602int
1603str_from_tablet(struct table *t, char* name, int* row, char* val)
1604     /* WH 22.06.2004, corrected from: char_from_table */
1605     /* returns val at position row in column with name "name".
1606        function value return:
1607        0  OK
1608        -1 table  does not exist
1609        -2 column does not exist
1610        -3 row    does not exist
1611     */
1612{
1613  int pos;
1614
1615  strcpy(val,"No-Name");
1616  mycpy(c_dum->c, name);
1617  if ((pos = name_list_pos(c_dum->c, t->columns)) < 0) return -2;
1618  if (*row > t->curr)  return -3;
1619   strncpy(val,t->s_cols[pos][*row-1],NAME_L);
1620  // competely buggy, useless and slow
1621  // while (strlen(val)<NAME_L) val[strlen(val)]=' ';
1622  val[NAME_L-1] = '\0';
1623  return 0;
1624}
1625
1626// dangerous function that uses table->node_nm sometimes corrupted or unmaintained
1627int
1628nodename_from_table_row(const char* table, const int* row, char* string)
1629  /* returns NODE NAME at position row (name is DISCARDED).
1630     function value return:
1631     0  OK
1632     -1 table  does not exist
1633     -2 column does not exist // not used...
1634     -3 row    does not exist
1635  */
1636{
1637  char buf[NAME_L];
1638  struct table* tbl;
1639  int pos;
1640
1641  *string = '\0';
1642
1643  mycpy(buf, table);
1644  if ((pos = name_list_pos(buf, table_register->names)) < 0 ||
1645     !(tbl = table_register->tables[pos])) {
1646    warning("nodename_from_table_row: name of table not found:" , buf);
1647    return -1;
1648  }
1649  if (*row < 1 || *row > tbl->curr) {
1650    warning("nodename_from_table_row: row out of range", "");
1651    return -3;
1652  }
1653
1654  strcpy(string, tbl->node_nm->p[*row-1]);
1655  return 0;
1656}
1657
1658#endif
1659
1660int
1661table_length(const char* table)
1662  /* returns no. of rows in table */
1663{
1664  char tbl_s[NAME_L];
1665  struct table *tbl;
1666  int pos;
1667
1668  mycpy(tbl_s, table);
1669  if ((pos = name_list_pos(tbl_s, table_register->names)) < 0 ||
1670     !(tbl = table_register->tables[pos])) {
1671    warning("table_length: table not found:", tbl_s);
1672    return 0;
1673  }
1674  return tbl->curr;
1675}
1676
1677int
1678table_exists(const char* table)
1679  /* returns no. of rows in table */
1680{
1681  char tbl_s[NAME_L];
1682  int pos;
1683
1684  mycpy(tbl_s, table);
1685  if ((pos = name_list_pos(tbl_s, table_register->names)) < 0 ||
1686     !table_register->tables[pos])
1687    return 0;
1688
1689  return 1;
1690}
1691
1692int
1693table_column_exists(const char* table, const char *name)
1694{
1695  char tbl_s[NAME_L], col_s[NAME_L];
1696  struct table *tbl;
1697  int pos;
1698
1699  mycpy(tbl_s, table);
1700  if ((pos = name_list_pos(tbl_s, table_register->names)) < 0 ||
1701     !(tbl = table_register->tables[pos]))
1702    return 0;
1703
1704  mycpy(col_s, name);
1705  if (name_list_pos(col_s, tbl->columns) < 0)
1706    return 0;
1707
1708  return 1;
1709}
1710
1711int
1712table_header_exists(const char* table, const char *name)
1713{
1714  char tbl_s[NAME_L], hdr_s[NAME_L], buf[256];
1715  struct table *tbl;
1716  int pos, hdr;
1717  char *p;
1718
1719  mycpy(tbl_s, table);
1720  if ((pos = name_list_pos(tbl_s, table_register->names)) < 0 ||
1721     !(tbl = table_register->tables[pos]))
1722    return 0;
1723 
1724  mycpy(hdr_s, name);
1725  if (tbl->header) {
1726    for (hdr = 0; hdr < tbl->header->curr; hdr++) {
1727      strcpy(buf, &tbl->header->p[hdr][1]);
1728      if ((p=strtok(buf, " \"\n")) && string_icmp(p, hdr_s) == 0)
1729        return 1;
1730    }
1731  }
1732  return 0;
1733}
1734
1735int
1736double_from_table_header(const char* table, const char* name, double* val)
1737  /* returns val from table header at position "name", if present.
1738     function value return:
1739     0  OK
1740     -1 table does not exist
1741     -2 header or parameter does not exist
1742     -3 parameter value does not exist or is not a number
1743  */
1744{
1745  char tbl_s[NAME_L], hdr_s[NAME_L], buf[256];
1746  struct table *tbl;
1747  int pos, hdr;
1748  char *p;
1749
1750  *val = 0.0;
1751
1752  mycpy(tbl_s, table);
1753  if ((pos = name_list_pos(tbl_s, table_register->names)) < 0 ||
1754     !(tbl = table_register->tables[pos])) {
1755    warning("double_from_table_header: table not found:", tbl_s);
1756    return -1;
1757  }
1758
1759  mycpy(hdr_s, name);
1760  if (tbl->header) {
1761    for (hdr = 0; hdr < tbl->header->curr; hdr++) {
1762      strcpy(buf, &tbl->header->p[hdr][1]);
1763      if ((p=strtok(buf, " \"\n")) && string_icmp(p, hdr_s) == 0) {
1764        if (strstr(strtok(NULL, " \"\n"), "%le") == NULL) {
1765          warning("double_from_table_header: parameter without value in table header:", (sprintf(buf,"%s->%s",tbl_s,hdr_s),buf));
1766          return -3;
1767        }
1768        if (sscanf(strtok(NULL, " \"\n"), "%le", val) != 1) {
1769          warning("double_from_table_header: invalid parameter value in table header:", (sprintf(buf,"%s->%s",tbl_s,hdr_s),buf));
1770          return -3;
1771        }
1772        return 0;
1773      }
1774    }
1775    warning("double_from_table_header: parameter not found in table header:", (sprintf(buf,"%s->%s",tbl_s,hdr_s),buf));
1776    return -2;
1777  } else {
1778    warning("double_from_table_header: table has no header:", tbl_s);
1779    return -2;
1780  }
1781}
1782
1783int
1784double_from_table_row(const char* table, const char* name, const int* row, double* val)
1785  /* returns val at position row in column with name "name".
1786     function value return:
1787     0  OK
1788     -1 table  does not exist
1789     -2 column does not exist
1790     -3 row    does not exist
1791  */
1792{
1793  char tbl_s[NAME_L], col_s[NAME_L], buf[5*NAME_L];
1794  struct table *tbl;
1795  int pos, col;
1796
1797  *val = 0.0;
1798 
1799  mycpy(tbl_s, table);
1800  if ((pos = name_list_pos(tbl_s, table_register->names)) < 0 ||
1801     !(tbl = table_register->tables[pos])) {
1802    warning("double_from_table_row: table not found:", tbl_s);
1803    return -1;
1804  }
1805  mycpy(col_s, name);
1806  if ((col = name_list_pos(col_s, tbl->columns)) < 0) {
1807    warning("double_from_table_row: column not found:", (sprintf(buf,"%s->%s",tbl_s,col_s),buf));
1808    return -2;
1809  }
1810  if (tbl->columns->inform[col] >= 3) {
1811    warning("double_from_table_row: invalid column type:", (sprintf(buf,"%s->%s",tbl_s,col_s),buf));
1812    return -2;
1813  }
1814  if (*row < 1 || *row > tbl->curr) {
1815    warning("double_from_table_row: row out of range:", (sprintf(buf,"%s->%s[1>=%d<=%d]",tbl_s,col_s,*row,tbl->curr),buf));
1816    return -3;
1817  }
1818
1819  *val = tbl->d_cols[col][*row-1];
1820  return 0;
1821}
1822
1823int
1824string_from_table_row(const char* table, const char* name, const int* row, char* string)
1825  /* returns string at position row in column with name "name".
1826     assumes string to be long enough...
1827     function value return:
1828     0  OK
1829     -1 table  does not exist
1830     -2 column does not exist
1831     -3 row    does not exist
1832  */
1833{
1834  char tbl_s[NAME_L], col_s[NAME_L], buf[5*NAME_L];
1835  struct table* tbl;
1836  int pos, col;
1837
1838  *string = '\0';
1839
1840  mycpy(tbl_s, table);
1841  if ((pos = name_list_pos(tbl_s, table_register->names)) < 0 ||
1842     !(tbl = table_register->tables[pos])) {
1843    warning("string_from_table_row: table not found:", tbl_s);
1844    return -1;
1845  }
1846  mycpy(col_s, name);
1847  if ((col = name_list_pos(col_s, tbl->columns)) < 0) {
1848    warning("string_from_table_row: column not found:", (sprintf(buf,"%s->%s",tbl_s,col_s),buf));
1849    return -2;
1850  }
1851  if (tbl->columns->inform[col] != 3) {
1852    warning("string_from_table_row: invalid column type:", (sprintf(buf,"%s->%s",tbl_s,col_s),buf));
1853    return -2;
1854  }
1855  if (*row < 1 || *row > tbl->curr) {
1856    warning("string_from_table_row: row out of range:", (sprintf(buf,"%s->%s[1>=%d<=%d]",tbl_s,col_s,*row,tbl->curr),buf));
1857    return -3;
1858  }
1859
1860  strcpy(string, tbl->s_cols[col][*row-1]);
1861  return 0;
1862}
1863
1864int
1865double_to_table_row(const char* table, const char* name, const int* row, const double* val)
1866  /* puts val at row position in column with name "name".
1867     0  OK
1868     -1 table  does not exist
1869     -2 column does not exist
1870     -3 row    does not exist
1871  */
1872{
1873  char tbl_s[NAME_L], col_s[NAME_L], buf[5*NAME_L];
1874  struct table* tbl;
1875  int pos, col;
1876
1877  mycpy(tbl_s, table);
1878  if ((pos = name_list_pos(tbl_s, table_register->names)) < 0 ||
1879     !(tbl = table_register->tables[pos])) {
1880    warning("double_to_table_row: table not found:", tbl_s);
1881    return -1;
1882  }
1883  mycpy(col_s, name);
1884  if ((col = name_list_pos(col_s, tbl->columns)) < 0) {
1885    warning("double_to_table_row: column not found:", (sprintf(buf,"%s->%s",tbl_s,col_s),buf));
1886    return -2;
1887  }
1888  if (tbl->columns->inform[col] >= 3) {
1889    warning("double_to_table_row: invalid column type:", (sprintf(buf,"%s->%s",tbl_s,col_s),buf));
1890    return -2;
1891  }
1892  if (*row < 1 || *row > tbl->curr) {
1893    warning("double_to_table_row: row out of range:", (sprintf(buf,"%s->%s[1>=%d<=%d]",tbl_s,col_s,*row,tbl->curr),buf));
1894    return -3;
1895  }
1896
1897  tbl->d_cols[col][*row-1] = *val;
1898  return 0;
1899}
1900
1901int
1902string_to_table_row(const char* table, const char* name, const int *row, const char* string)
1903  /* puts string at row position in column with name "name".
1904     0  OK
1905     -1 table  does not exist
1906     -2 column does not exist
1907     -3 row    does not exist
1908  */
1909{
1910  char tbl_s[NAME_L], col_s[NAME_L], buf[5*NAME_L];
1911  struct table* tbl;
1912  int pos, col;
1913
1914  mycpy(tbl_s, table);
1915  if ((pos = name_list_pos(tbl_s, table_register->names)) < 0 ||
1916     !(tbl = table_register->tables[pos])) {
1917    warning("string_to_table_row: table not found:", tbl_s);
1918    return -1;
1919  }
1920  mycpy(col_s, name);
1921  if ((col = name_list_pos(col_s, tbl->columns)) < 0) {
1922    warning("string_to_table_row: column not found:", (sprintf(buf,"%s->%s",tbl_s,col_s),buf));
1923    return -2;
1924  }
1925  if (tbl->columns->inform[col] != 3) {
1926    warning("string_to_table_row: invalid column type:", (sprintf(buf,"%s->%s",tbl_s,col_s),buf));
1927    return -2;
1928  }
1929  if (*row < 1 || *row > tbl->curr) {
1930    warning("string_to_table_row: row out of range:", (sprintf(buf,"%s->%s[1>=%d<=%d]",tbl_s,col_s,*row,tbl->curr),buf));
1931    return -3;
1932  }
1933
1934  if (tbl->s_cols[col][*row-1])
1935    myfree("string_to_table_row", tbl->s_cols[col][*row-1]);
1936
1937  mycpy(buf, string);
1938  if (strcmp(buf, "name") == 0)
1939    tbl->s_cols[col][*row-1] = tmpbuff(current_node->name);
1940  else
1941    tbl->s_cols[col][*row-1] = tmpbuff(string);
1942  return 0;
1943}
1944
1945int
1946double_to_table_curr(const char* table, const char* name, const double* val)
1947  /* puts val at current position in column with name "name".
1948     The table count is increased separately with "augment_count"
1949     0  OK
1950     -1 table  does not exist
1951     -2 column does not exist
1952     -3 row    does not exist (need expansion)
1953  */
1954{
1955  char tbl_s[NAME_L], col_s[NAME_L], buf[5*NAME_L];
1956  struct table* tbl;
1957  int pos, col;
1958
1959  mycpy(tbl_s, table);
1960  if ((pos = name_list_pos(tbl_s, table_register->names)) < 0 ||
1961     !(tbl = table_register->tables[pos])) {
1962    warning("double_to_table_curr: table not found:", tbl_s);
1963    return -1;
1964  }
1965  mycpy(col_s, name);
1966  if ((col = name_list_pos(col_s, tbl->columns)) < 0) {
1967    warning("double_to_table_curr: column not found:", (sprintf(buf,"%s->%s",tbl_s,col_s),buf));
1968    return -2;
1969  }
1970  if (tbl->columns->inform[col] >= 3) {
1971    warning("double_to_table_curr: invalid column type:", (sprintf(buf,"%s->%s",tbl_s,col_s),buf));
1972    return -2;
1973  }
1974  if (tbl->curr >= tbl->max) {
1975    warning("double_to_table_curr: row out of range (need expansion):", (sprintf(buf,"%s->%s[%d<%d]",tbl_s,col_s,tbl->curr,tbl->max),buf));
1976    return -3;
1977  }
1978
1979  tbl->d_cols[col][tbl->curr] = *val;
1980  return 0;
1981}
1982
1983int
1984vector_to_table_curr(const char* table, const char* name, const double* vals, const int* nval)
1985  /* puts nval values of array vals at the current line into columns starting with column name
1986     The table count is increased separately with "augment_count"
1987     0  OK
1988     -1 table  does not exist
1989     -2 column does not exist
1990     -3 row    does not exist (need expansion)
1991  */
1992{
1993  char tbl_s[NAME_L], col_s[NAME_L], buf[5*NAME_L];
1994  struct table* tbl;
1995  int pos, col, last, j;
1996
1997  mycpy(tbl_s, table);
1998  if ((pos = name_list_pos(tbl_s, table_register->names)) < 0 ||
1999     !(tbl = table_register->tables[pos])) {
2000    warning("vector_to_table_curr: table not found:", tbl_s);
2001    return -1;
2002  }
2003  mycpy(col_s, name);
2004  if ((col = name_list_pos(col_s, tbl->columns)) < 0) {
2005    warning("vector_to_table_curr: column not found:", (sprintf(buf,"%s->%s",tbl_s,col_s),buf));
2006    return -2;
2007  }
2008  if (tbl->curr >= tbl->max) {
2009    warning("vector_to_table_curr: row out of range (need expansion):",
2010      (sprintf(buf,"%s->%s[%d<%d]",tbl_s,col_s,tbl->curr,tbl->max),buf));
2011    return -3;
2012  }
2013
2014  if (col + *nval > tbl->num_cols) {
2015    warning("vector_to_table_curr: too many values provided - vector truncated:",
2016      (sprintf(buf,"%s->%s[%d<=%d]",tbl_s,col_s,col + *nval,tbl->num_cols),buf));
2017    last = tbl->num_cols;
2018  } else
2019    last = col + *nval;
2020
2021  for (j = col; j < last; j++) {
2022    if (tbl->columns->inform[j] >= 3)
2023      warning("vector_to_table_curr: invalid column type - value skipped:",
2024        (sprintf(buf,"%s->%s",tbl_s,tbl->columns->names[j]),buf));
2025    else
2026      tbl->d_cols[j][tbl->curr] = vals[j-col];
2027  }
2028
2029  return 0;
2030}
2031
2032int
2033string_to_table_curr(const char* table, const char* name, const char* string)
2034  /* puts string at current position in column with name "name".
2035     The table count is increased separately with "augment_count"
2036     0  OK
2037     -1 table  does not exist
2038     -2 column does not exist
2039     -3 row    does not exist (need expansion)
2040  */
2041{
2042  char tbl_s[NAME_L], col_s[NAME_L], buf[5*NAME_L];
2043  struct table* tbl;
2044  int pos, col;
2045
2046  mycpy(tbl_s, table);
2047  if ((pos = name_list_pos(tbl_s, table_register->names)) < 0 ||
2048     !(tbl = table_register->tables[pos])) {
2049    warning("string_to_table_curr: table not found:", tbl_s);
2050    return -1;
2051  }
2052  mycpy(col_s, name);
2053  if ((col = name_list_pos(col_s, tbl->columns)) < 0) {
2054    warning("string_to_table_curr: column not found:", (sprintf(buf,"%s->%s",tbl_s,col_s),buf));
2055    return -2;
2056  }
2057  if (tbl->columns->inform[col] != 3) {
2058    warning("string_to_table_curr: invalid column type:", (sprintf(buf,"%s->%s",tbl_s,col_s),buf));
2059    return -2;
2060  }
2061  if (tbl->curr >= tbl->max) {
2062    warning("string_to_table_curr: row out of range (need expansion):", (sprintf(buf,"%s->%s[%d<%d]",tbl_s,col_s,tbl->curr,tbl->max),buf));
2063    return -3;
2064  }
2065
2066  if (tbl->s_cols[col][tbl->curr])
2067    myfree("string_to_table_curr", tbl->s_cols[col][tbl->curr]);
2068
2069  mycpy(buf, string);
2070  if (strcmp(buf, "name") == 0)
2071    tbl->s_cols[col][tbl->curr] = tmpbuff(current_node->name);
2072  else
2073    tbl->s_cols[col][tbl->curr] = tmpbuff(string);
2074  return 0;
2075}
2076
2077int
2078comment_to_table_curr(const char* table, const char* comment, const int* length)
2079  /* Saves the comment string at the current line.
2080     This comment is then printed in front of this line.
2081     Several calls to the same current line are possible.
2082     0  OK
2083     -1 table does not exist
2084  */
2085{
2086  char tbl_s[NAME_L];
2087  struct table* tbl;
2088  int pos;
2089
2090  mycpy(tbl_s, table);
2091  if ((pos = name_list_pos(tbl_s, table_register->names)) < 0 ||
2092     !(tbl = table_register->tables[pos])) {
2093    warning("comment_to_table_curr: table not found:" , tbl_s);
2094    return -1;
2095  }
2096
2097  strncpy(c_dum->c, comment, *length); c_dum->c[*length] = '\0';
2098  if (tbl->l_head[tbl->curr] == NULL)
2099    tbl->l_head[tbl->curr] = new_char_p_array(2);
2100  else if (tbl->l_head[tbl->curr]->curr == tbl->l_head[tbl->curr]->max)
2101    grow_char_p_array(tbl->l_head[tbl->curr]);
2102  tbl->l_head[tbl->curr]->p[tbl->l_head[tbl->curr]->curr++] = tmpbuff(c_dum->c);
2103  return 0;
2104}
2105
2106/*
2107  LD: 2012.11.29
2108  - These "slow" accessors were added for stable access of "muting" tables
2109    that is tables where columns and rows may change during data processing
2110  - As for other access-by-name, it looks for the first column of strings
2111  - The row name used for searching is the mangled name as stored in the
2112    table, that is with the trailing :# (count number), if any.
2113*/
2114static int
2115get_table_row(const struct table* tbl, const char* name)
2116{
2117  int col, row = tbl->curr;
2118
2119  for (col = 0; col < tbl->num_cols; col++)
2120    if(tbl->columns->inform[col] == 3) break;
2121
2122  if (col < tbl->num_cols)
2123    for (row = 0; row < tbl->curr; row++)
2124      if (!strcmp(name, tbl->s_cols[col][row])) break;
2125
2126  return row == tbl->curr ? -1 : row;
2127}
2128
2129double
2130get_table_value(const char* tbl_s, const char *row_s, const char *col_s)
2131{
2132  int pos, row, col;
2133
2134  if ((pos = name_list_pos(tbl_s, table_register->names)) > -1) {
2135    const struct table *tbl = table_register->tables[pos];
2136    if ((col = name_list_pos(col_s, tbl->columns)) > -1) {
2137      if ((row = get_table_row(tbl, row_s)) > -1)
2138        return tbl->d_cols[col][row];
2139
2140      else warning("get_table_value: name of row not found:"   , row_s);
2141    } else warning("get_table_value: name of column not found:", col_s);
2142  }   else warning("get_table_value: name of table not found:" , tbl_s);
2143
2144  return 0;
2145}
2146
2147void
2148set_table_value(const char* tbl_s, const char *row_s, const char *col_s, double *val)
2149{
2150  int pos, row, col;
2151
2152  if ((pos = name_list_pos(tbl_s, table_register->names)) > -1) {
2153    const struct table *tbl = table_register->tables[pos];
2154    if ((col = name_list_pos(col_s, tbl->columns)) > -1) {
2155      if ((row = get_table_row(tbl, row_s)) > -1)
2156        tbl->d_cols[col][row] = *val;
2157
2158      else warning("get_table_value: name of row not found:"   , row_s);
2159    } else warning("get_table_value: name of column not found:", col_s);
2160  }   else warning("get_table_value: name of table not found:" , tbl_s);
2161}
2162
2163
Note: See TracBrowser for help on using the repository browser.