# include "gastro.h"
# define BYTES_STAR 23
# define BLOCK 1000
# define DNSTARS 1000

int gcatalog (char *filename, SStars **stars, int *Nstars) {
  
  int i, NSTARS, nstar, Nbytes, nbytes;
  char *buffer;
  FILE *f;

  if (*Nstars == 0) ALLOCATE (*stars, SStars, 1);
  nstar = *Nstars;
  NSTARS = nstar + DNSTARS;

  f = fopen (filename, "r");
  if (f == NULL) {
    fprintf (stderr, "ERROR: can't find catalog file %s\n", filename);
    return (FALSE);
  }
  
  ALLOCATE (buffer, char, (BLOCK*BYTES_STAR));
  Nbytes = BLOCK*BYTES_STAR;
  REALLOCATE (*stars, SStars, NSTARS);
  while ((nbytes = fread (buffer, 1, Nbytes, f)) > 0) {
    for (i = 0; i < nbytes / BYTES_STAR; i++) {
      dparse (&(*stars)[nstar].X,   1, &buffer[i*BYTES_STAR]);
      dparse (&(*stars)[nstar].Y,   2, &buffer[i*BYTES_STAR]);
      dparse (&(*stars)[nstar].mag, 3, &buffer[i*BYTES_STAR]);
      nstar++;
      if (nstar == NSTARS - 1) {
	NSTARS += DNSTARS;
	REALLOCATE (*stars, SStars, NSTARS);
      }
    }
  }
  free (buffer);
  REALLOCATE (*stars, SStars, nstar);
  *Nstars = nstar;
  
  return (TRUE);
  
}
