
#include <common.h>
#include <dbext.h>  /* NAME_LIST_ENTRY_TYPE */
#include <ioserv.h> /* TYPE_STRING, op_get_param() */
#include "map.h"
#include "vecplot.h"

#if PROTOTYPE_ALLOWED
int read_xy(FILE * fp, XY_TYPE * xy, int code)
#else
int read_xy(fp, xy, code)
   FILE *fp;
   XY_TYPE *xy;
   int code;
#endif
{
   char buf[80];
   switch (code)
   {
      case COORDINATE:  /* x,y */
         if (fscanf(fp, " %lf,%lf", &(xy->x), &(xy->y)) != 2)
            return (1);
         sprintf(buf, " %f,%f ", xy->x, xy->y);
         break;
      case SCALE:    /* x by y */
         if (fscanf(fp, " %lf by %lf", &(xy->x), &(xy->y)) != 2)
            return (1);
         sprintf(buf, " %f by %f ", xy->x, xy->y);
         break;
      default:
         fprintf(stderr, "\n ERROR: Unrecognized delimiter\n");
         return (1);
   }
   report_msg(buf);
   return (0);
}

#if PROTOTYPE_ALLOWED
int read_range(FILE *fp, RANGE_INTERVAL *range, int code)
#else
int read_range(fp, range, code)
   FILE *fp;
   RANGE_INTERVAL *range;
   int code;
#endif
{
   char buf[80];

   if (fscanf(fp, " %lf to %lf by %lf", &(range->min), &(range->max),
              &(range->step)) != 3)
   {
      fprintf(stderr, "\n ERROR: reading lon/lat range\n");
      return (1);
   }
   sprintf(buf, " %f to %f by %f", range->min, range->max, range->step);
   if (range->step == 0.0)
   {
      fprintf(stderr, "\n ERROR: invalid step size\n");
      return (1);
   }
   if (code == LONGITUDE)
   {
      if (range->max < range->min)
         range->max += 360.0;
   }
   range->span = range->max - range->min;
   report_msg(buf);
   return (0);
}

#if PROTOTYPE_ALLOWED
int read_files(FILE * fpcnt, char vecfiles[MAXNUMFILES][80], int code)
#else
int read_files(fpcnt, vecfiles, code)
   FILE *fpcnt;
   char vecfiles[MAXNUMFILES][80];
   int code;
#endif
{
   int i = 0, finished = 0;
   int nscan, eof;

   while (!finished)
   {
      nscan = fscanf(fpcnt, " %80s", vecfiles[i]);
      eof = feof(fpcnt);	/* Without this check, we get a segmentation
                                 * fault on eof! */
      if (code == 1 || nscan != 1 || !strcmp(vecfiles[i], "end") ||
          i == (MAXNUMFILES - 1) || eof)
         finished = 1;
      i++;
   }
   if (code == 1)
   {
      strcpy(vecfiles[1], "end");
   }
   check_error(eof, "EOF reached while reading vecfiles: is the list missing 'end'?");
   if (i == MAXNUMFILES)
   {
      fprintf(stderr, " ERROR:  Too many vector input files. Max. is %d \n ", MAXNUMFILES);
      exit(-1);
   }
   return (0);
}

/* Partition decides how many parts a given length should be divided into.
   The width argument is the size in paper inches of this given length.
   Initially, the function finds the largest power of 10, il, that yields
   an integral factor, ifactor, such that ifactor * 10^il = length.
   (It should always find something since 1 is 10^0.)  If this yields 
   too few partitions (as defined by too_sparse in v.h), then we
   drop down to the next lower power.  The check for too_dense is done
   when we get back to the calling routine (as this facilitates finding
   a nice long-tick interval). */

int partition(length, width)
double length, width;
{
   double l, factor;
   int il, ifactor;

   if (length == 0.0)
      return(0);
   l = log10(length);
   il = l;
   do
   {
      factor = length / pow(10.0, 1.0*il);
      ifactor = factor;
      il = il - 1;
   }
   while (!equal(factor, ifactor) || too_sparse(width/ifactor));
   return(ifactor);
}

#if PROTOTYPE_ALLOWED
double frem(double num, double den)
#else  
double frem(num, den)
   double num;
   double den;
#endif 
{
   long int inum, iden, quotient;

   inum = round(num * 1000000L);
   iden = round(den * 1000000L);
   quotient = inum / iden;
   return ((inum - quotient * iden) / 1000000.0);
}


#if PROTOTYPE_ALLOWED
int get_decimal_length(double value)
#else
int get_decimal_length(value)
   double value;
#endif
{
   int dec_len = 0;

   while (1)
   {
      if (value - (int) value < 0.000001)
         return dec_len;  /* only way out! */
      value *= 10.0;
      dec_len++;
   }
}



#if PROTOTYPE_ALLOWED
int clear_labels(FILE * fp, LABEL_TYPE **arg, int code)
#else
int clear_labels(fp, arg, code)
   FILE *fp;
   LABEL_TYPE **arg;
   int code;
#endif
{
   LABEL_TYPE *temp, *label;

   label = *arg;
   *arg = NULL;

   while (label)
   {
      temp = label->next;
      free(label);
      label = temp;
   }

   return 0;
}


