1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
#include <stdlib.h>
#include "distance.h"
static int cmp (const void *, const void *);
static int scan_dist (char *, double *);
int parse_distances (char **zone_list, double to_meters)
{
double dist;
double ew2 = 0.0;
int i;
int count;
for (count = 0; zone_list[count]; count++)
;
if (count <= 0) return 0;
/* create an array to hold the distances */
distances = (struct Distance *) G_calloc (count, sizeof(struct Distance));
/* scan the command line for the distances */
if (window.proj != PROJECTION_LL)
{
ew2 = window.ew_res * window.ew_res;
ns_to_ew_squared = window.ns_res * window.ns_res / ew2;
}
for (i = 0; i < count; i++)
{
if (!scan_dist(zone_list[i], &dist))
{
fprintf (stderr, "%s: %s - illegal distance specification\n",
pgm_name, zone_list[i]);
return 0;
}
else
{
dist *= (to_meters * meters_to_grid);
if (window.proj != PROJECTION_LL)
dist = dist*dist/ew2;
distances[i].dist = dist;
}
distances[i].label = zone_list[i];
}
/* sort the distances in increasing order */
qsort (distances, count, sizeof(struct Distance), cmp);
return count;
}
static int cmp (const void *aa, const void *bb)
{
const struct Distance *a = aa, *b = bb;
if (a->dist < b->dist) return -1;
return a->dist > b->dist;
}
static int scan_dist (char *s, double *dist)
{
char dummy[2];
*dummy = 0;
if (sscanf (s, "%lf%1s", dist, dummy) != 1) return 0;
if (*dummy) return 0;
if (*dist <= 0.0) return 0;
return 1;
}
|