# include "data.h"

int vect_select (int argc, char **argv) {
  
  int  i, size;
  char *out;
  Vector *in1, *in2, *tvec, *ovec;

  out = NULL;
  in1 = in2 = ovec = tvec = NULL;

  /** check basic syntax **/
  if ((argc != 8) || strcmp(argv[2], "=") || strcmp (argv[4], "if") || strcmp (argv[6], "else")) {
    gprint (GP_ERR, "SYNTAX: select vec = vec if (logic expression) else vec\n");
    return (FALSE);
  }
  if ((in1  = SelectVector (argv[3], OLDVECTOR, TRUE)) == NULL) return (FALSE);
  if ((in2  = SelectVector (argv[7], OLDVECTOR, TRUE)) == NULL) return (FALSE);
  if ((ovec = SelectVector (argv[1], ANYVECTOR, TRUE)) == NULL) return (FALSE);

  out = dvomath (argc - 5, &argv[5], &size, 1);
  if (out == NULL) {
    print_error ();
    goto error;
  }
  if ((tvec = SelectVector (out, OLDVECTOR, TRUE)) == NULL) goto error;
  /* check size of in1, in2, tvec: must match */

  REALLOCATE (ovec[0].elements, float, MAX (tvec[0].Nelements, 1));
  for (i = 0; i < tvec[0].Nelements; i++) {
    ovec[0].elements[i] = tvec[0].elements[i] ? in1[0].elements[i] : in2[0].elements[i];
  }
  ovec[0].Nelements = tvec[0].Nelements;
  REALLOCATE (ovec[0].elements, float, MAX (ovec[0].Nelements, 1));
  
  DeleteVector (tvec);
  free (out);
  return (TRUE);

 error:
  DeleteVector (tvec);
  DeleteVector (ovec);
  DeleteNamedVector (out);
  free (out);
  return (FALSE);
}
