
/* dvo fields and boolean expressions
   - evaluate the command line:
     * avextract ra,dec,g,r,i where (uRA < 0.1)
     1) identify all fields in extraction list
     2) parse boolean expression
     3) identify additional fields from expression
*/

enum {DVO_TABLE_AVERAGE, DVO_TABLE_MEASURE};

// a single db field 
typedef struct {
  char *name;
  int extract;
  int table;
  int ID;
  int magMode;
  PhotCode *photcode;
} dvoField;

// db boolean operations
typedef struct {
  char   *name;
  char    type;
  int     field;
  float   Float;
} dvoStack;

dvoField *ParseCmdlineFields (int argc, char **argv, int table, int *last, int *nfields) {

  int i, Nfields, NFIELDS, 

  *nfields = 0;
  Nfields = 0;
  NFIELDS = 10;
  ALLOCATE (fields, dvoField, NFIELDS);

  // examine each argv[i] entry until we reach a where 
  for (i = 1; (i < argc) && strcasecmp (argv[i], "where"); i++) {
    // split the word by ","
    p = argv[i];
    while (*p) {
      q = strchr (p, ',');
      if (q == NULL) {
	field = strcreate (p);
	p = p + strlen(p);
      } else {
	field = strncreate (p, q-p);
	p = q + 1;
      }
      // identify field for word
      // need to know which type of fields to look for...
      // xxx extend this more generally later
      if (table == DVO_TABLE_MEASURE) {
	status = ParseMeasureField (&fields[Nfields], field);
      } 
      if (table == DVO_TABLE_AVERAGE) {
	status = ParseAverageField (&fields[Nfields], field);
      } 
      if (!status) {
	gprint (GP_ERR, "unknown database field %s\n", field);
	free (field);
	free (fields);
	return (NULL);
      }
      free (field);

      Nfields ++;
      CHECK_REALLOCATE (fields, dvoField, NFIELDS, Nfields, 10);
    }
  }

  *last = i;
  *nfields = Nfields;
  return (fields);
}
