# include "dvoshell.h"

int dbCheckStack (dbStack *stack, int Nstack, int table, dbField **inFields, int *Nfields) {

  int i, j, status, NFIELDS;
  char *c;
  dbField *fields;

  NFIELDS = *Nfields + 10;
  REALLOCATE (*inFields, dbField, NFIELDS);
  fields = *inFields;

  for (i = 0; i < Nstack; i++) {
    if (stack[i].type == 'X') {

      /** if this is a number, put it on the list of scalers and move on **/
      stack[i].Float = strtod (stack[i].name, &c);
      if (c == stack[i].name + strlen (stack[i].name)) {
	stack[i].type  = 'S';
	continue;
      } 

      // this must be a field : is it already in the list?
      for (j = 0; (j < *Nfields) && strcasecmp (stack[i].name, fields[j].name); j++);
      if (j < *Nfields) {
	stack[i].field = j;
	stack[i].type  = 'F';
	continue;
      }

      // this must be a field : is it a valid name?
      if (table == DVO_TABLE_MEASURE) {
	status = ParseMeasureField (&fields[*Nfields], stack[i].name);
      } 
      if (table == DVO_TABLE_AVERAGE) {
	status = ParseAverageField (&fields[*Nfields], stack[i].name);
      } 
      if (!status) {
	gprint (GP_ERR, "unknown database field %s\n", stack[i].name);
	return (FALSE);
      }
      stack[i].field = *Nfields;
      stack[i].type  = 'F';

      *Nfields ++;
      CHECK_REALLOCATE (*inFields, dbField, NFIELDS, *Nfields, 10);
      fields = *inFields;
    }
  }
  return (TRUE);
}

/* check stack identifies the data elements as plain scalars or table fields
   operators have already been identified.  
   check stack returns the total stack dimensionality (0,1,2)
   on error, check stack returns FALSE
*/
