# include "data.h"

int write_vectors (int argc, char **argv) {
  
  int i, j, Nvec, Ne, N;
  FILE *f;
  char **fmtlist, *fmttype;
  char *p0, *p1, *p2, *format;
  Vector **vec;

  /* look for format option */
  format = (char *) NULL;
  if (N = get_argument (argc, argv, "-f")) {
    remove_argument (N, &argc, argv);
    format = strcreate (argv[N]);
    remove_argument (N, &argc, argv);
  }

  if (argc < 3) {
    fprintf (stderr, "USAGE: write [-f \"format\"] file vector vector ...\n");
    return (FALSE);
  }

  /* open file for outuput */
  f = fopen (argv[1], "w");
  if (f == (FILE *) NULL) {
    fprintf (stderr, "can't open file for write\n");
    return (FALSE);
  }

  /* find number of output vectors */
  Nvec = (argc - 2);
  if (Nvec < 1) {
      fprintf (stderr, "USAGE: write (file) vector vector ...\n");
      fclose (f);
      return (FALSE);
  }
  ALLOCATE (vec, Vector *, Nvec);

  /* select/check vectors from list */
  for (i = 0; i < Nvec; i++) {
    if ((vec[i] = SelectVector (argv[i + 2], OLDVECTOR, FALSE)) == NULL) {
      fprintf (stderr, "USAGE: write (file) vector vector ...\n");
      free (vec);
      fclose (f);
      return (FALSE);    
    }
  }
  
  /* select vector lengths */
  Ne = vec[0][0].Nelements;
  for (i = 0; i < Nvec; i++) {
    if (vec[0][0].Nelements != Ne) {
      fprintf (stderr, "error: vectors must all be the same size\n");
      free (vec);
      fclose (f);
      return (FALSE);    
    }
  }

  /* default output format */
  if (format == (char *) NULL) {
    for (i = 0; i < vec[0][0].Nelements; i++) {
      for (j = 0; j < Nvec; j++) {
	fprintf (f, "%.10g ", vec[j][0].elements[i]);
      }
      fprintf (f, "\n");
    } 
    fclose (f);
    free (vec);
    return (TRUE);
  }

  /* construct an array of format strings */
  ALLOCATE (fmttype, char, Nvec);
  ALLOCATE (fmtlist, char *, Nvec);
  for (i = 0; i < Nvec; i++) {
    ALLOCATE (fmtlist[i], char, 1024);
    bzero (fmtlist[i], 1024);
  }

  p0 = format;
  for (j = 0; j < Nvec; j++) {
    /* find this format character */
    p1 = strchr (p0, '%');
    if (p1 == (char *) NULL) {
      fprintf (stderr, "mismatch between format and values\n");
      free (fmttype);
      for (i = 0; i < Nvec; i++) free (fmtlist[i]);
      free (fmtlist);
      free (format);
      fclose (f);
      return (FALSE);
    }
    
    /* identify type (%NNNNd %NNNNf) */
    for (p2 = p1 + 1; (*p2 == '.') || (*p2 == '-') || (*p2 == '+') || (*p2 == ' ') || isdigit(*p2); p2++);
    strncpy (fmtlist[j], p0, p2 - p0 + 1);
    switch (*p2) {
      case 'e':
      case 'f':
	fmttype[j] = 'f';
	break;
      case 'd':
      case 'c':
      case 'x':
	fmttype[j] = 'd';
	break;
      default:
	fprintf (stderr, "syntax error in format (only e,f,d,c,x allowed)\n");
	return (FALSE);
    }
    p0 = p2 + 1;
  }
  strcat (fmtlist[Nvec-1], p0);
  
  for (i = 0; i < vec[0][0].Nelements; i++) {
    for (j = 0; j < Nvec; j++) {
      if (fmttype[j] == 'd') {
	fprintf (f, fmtlist[j], (int)(vec[j][0].elements[i]));
      } 
      if (fmttype[j] == 'f') {
	fprintf (f, fmtlist[j], (float)(vec[j][0].elements[i]));
      } 
    }
    fprintf (f, "\n");
  }
  fclose (f);

  free (fmttype);
  for (i = 0; i < Nvec; i++) free (fmtlist[i]);
  free (fmtlist);
  free (format);
  
  return (TRUE);

}


