[GRASS-SVN] r34461 - in grass/trunk: include lib/gis
svn_grass at osgeo.org
svn_grass at osgeo.org
Sun Nov 23 18:25:53 EST 2008
Author: glynn
Date: 2008-11-23 18:25:53 -0500 (Sun, 23 Nov 2008)
New Revision: 34461
Modified:
grass/trunk/include/gisdefs.h
grass/trunk/include/glocale.h
grass/trunk/lib/gis/clicker.c
grass/trunk/lib/gis/datum.c
grass/trunk/lib/gis/distance.c
grass/trunk/lib/gis/env.c
grass/trunk/lib/gis/gdal.c
grass/trunk/lib/gis/geodesic.c
grass/trunk/lib/gis/geodist.c
grass/trunk/lib/gis/get_ellipse.c
grass/trunk/lib/gis/locale.c
grass/trunk/lib/gis/ls.c
grass/trunk/lib/gis/mapset_nme.c
grass/trunk/lib/gis/parser.c
grass/trunk/lib/gis/percent.c
grass/trunk/lib/gis/plot.c
grass/trunk/lib/gis/rhumbline.c
grass/trunk/lib/gis/set_window.c
Log:
Bundle static variables into per-file structures
Add explicit init functions where appropriate
Make G_clicker() respect GRASS_MESSAGE_FORMAT and GRASS_VERBOSE
Remove unnecessary return values
Miscellaneous clean-up
Modified: grass/trunk/include/gisdefs.h
===================================================================
--- grass/trunk/include/gisdefs.h 2008-11-23 23:21:52 UTC (rev 34460)
+++ grass/trunk/include/gisdefs.h 2008-11-23 23:25:53 UTC (rev 34461)
@@ -201,7 +201,7 @@
int G_clear_screen(void);
/* clicker.c */
-int G_clicker(void);
+void G_clicker(void);
/* closecell.c */
int G_close_cell(int);
@@ -401,9 +401,10 @@
char *G_datum_name(int);
char *G_datum_description(int);
char *G_datum_ellipsoid(int);
-int G_get_datumparams_from_projinfo(const struct Key_Value *projinfo,
- char *datumname, char *params);
+int G_get_datumparams_from_projinfo(const struct Key_Value *, char *, char *);
+void G_read_datum_table(void);
+
/* debug.c */
int G_debug(int, const char *, ...) __attribute__ ((format(printf, 2, 3)));
@@ -430,19 +431,19 @@
char *G_getenv2(const char *, int);
char *G__getenv(const char *);
char *G__getenv2(const char *, int);
-int G_setenv(const char *, const char *);
-int G_setenv2(const char *, const char *, int);
-int G__setenv(const char *, const char *);
-int G__setenv2(const char *, const char *, int);
-int G_unsetenv(const char *);
-int G_unsetenv2(const char *, int);
-int G__write_env(void);
+void G_setenv(const char *, const char *);
+void G_setenv2(const char *, const char *, int);
+void G__setenv(const char *, const char *);
+void G__setenv2(const char *, const char *, int);
+void G_unsetenv(const char *);
+void G_unsetenv2(const char *, int);
+void G__write_env(void);
char *G__env_name(int);
-int G__read_env(void);
+void G__read_env(void);
void G_set_gisrc_mode(int);
int G_get_gisrc_mode(void);
-int G__create_alt_env(void);
-int G__switch_env(void);
+void G__create_alt_env(void);
+void G__switch_env(void);
/* error.c */
int G_info_format(void);
@@ -533,6 +534,7 @@
void G_fpreclass_perform_id(const struct FPReclass *, const CELL *, DCELL *,
int);
/* gdal.c */
+void G_init_gdal(void);
struct GDAL_link *G_get_gdal_link(const char *, const char *);
void G_close_gdal_link(struct GDAL_link *);
@@ -556,6 +558,7 @@
int G_get_ellipsoid_by_name(const char *, double *, double *);
char *G_ellipsoid_name(int);
char *G_ellipsoid_description(int);
+int G_read_ellipsoid_table(int);
/* get_projinfo.c */
struct Key_Value *G_get_projunits(void);
@@ -758,11 +761,12 @@
/* mapset_nme.c */
char *G__mapset_name(int);
-int G__create_alt_search_path(void);
-int G__switch_search_path(void);
-int G_reset_mapsets(void);
+void G_get_list_of_mapsets(void);
+void G__create_alt_search_path(void);
+void G__switch_search_path(void);
+void G_reset_mapsets(void);
char **G_available_mapsets(void);
-void G_add_mapset_to_search_path(const char *mapset);
+void G_add_mapset_to_search_path(const char *);
/* mask_info.c */
char *G_mask_info(void);
@@ -847,7 +851,7 @@
int G_check_overwrite(int argc, char **argv);
/* parser.c */
-int G_disable_interactive(void);
+void G_disable_interactive(void);
struct GModule *G_define_module(void);
struct Flag *G_define_flag(void);
struct Option *G_define_option(void);
@@ -867,8 +871,8 @@
int G_stat(const char *, struct stat *);
/* percent.c */
-int G_percent(long, long, int);
-int G_percent_reset(void);
+void G_percent(long, long, int);
+void G_percent_reset(void);
/* plot.c */
int G_setup_plot(double, double, double, double, int (*)(int, int),
@@ -1053,7 +1057,7 @@
INTERP_TYPE);
/* set_window.c */
-int G_get_set_window(struct Cell_head *);
+void G_get_set_window(struct Cell_head *);
int G_set_window(struct Cell_head *);
/* short_way.c */
Modified: grass/trunk/include/glocale.h
===================================================================
--- grass/trunk/include/glocale.h 2008-11-23 23:21:52 UTC (rev 34460)
+++ grass/trunk/include/glocale.h 2008-11-23 23:25:53 UTC (rev 34461)
@@ -3,6 +3,7 @@
#include <grass/config.h>
+extern void G_init_locale(void);
extern char *G_gettext(const char *, const char *);
#if defined(HAVE_LIBINTL_H) && defined(USE_NLS)
Modified: grass/trunk/lib/gis/clicker.c
===================================================================
--- grass/trunk/lib/gis/clicker.c 2008-11-23 23:21:52 UTC (rev 34460)
+++ grass/trunk/lib/gis/clicker.c 2008-11-23 23:25:53 UTC (rev 34461)
@@ -7,23 +7,25 @@
*
*/
#include <stdio.h>
+#include <grass/gis.h>
-static int G_clicker_prev = 0;
+static struct state {
+ int prev;
+} state;
-int G_clicker(void)
+static struct state *st = &state;
+
+void G_clicker(void)
{
- int x;
static const char clicks[] = "|/-\\";
+ int format = G_info_format();
- if (G_clicker_prev == -1 || G_clicker_prev == 3)
- x = 0;
+ if (format == G_INFO_FORMAT_SILENT || G_verbose() < 1)
+ return;
- else
- x = G_clicker_prev + 1;
+ st->prev++;
+ st->prev %= 4;
- fprintf(stderr, "%1c\b", clicks[x]);
+ fprintf(stderr, "%1c\b", clicks[st->prev]);
fflush(stderr);
- G_clicker_prev = x;
-
- return 0;
}
Modified: grass/trunk/lib/gis/datum.c
===================================================================
--- grass/trunk/lib/gis/datum.c 2008-11-23 23:21:52 UTC (rev 34460)
+++ grass/trunk/lib/gis/datum.c 2008-11-23 23:25:53 UTC (rev 34461)
@@ -26,28 +26,29 @@
static struct table
{
- char *name; /* Short Name / acronym of map datum */
- char *descr; /* Long Name for map datum */
- char *ellps; /* acronym for ellipsoid used with this datum */
- double dx; /* delta x */
- double dy; /* delta y */
- double dz; /* delta z */
-} *table;
+ struct datum
+ {
+ char *name; /* Short Name / acronym of map datum */
+ char *descr; /* Long Name for map datum */
+ char *ellps; /* acronym for ellipsoid used with this datum */
+ double dx; /* delta x */
+ double dy; /* delta y */
+ double dz; /* delta z */
+ } *datums;
+ int size;
+ int count;
+} table;
-static int size;
-static int count = -1;
-
static int compare_table_names(const void *, const void *);
-static void read_datum_table(void);
int G_get_datum_by_name(const char *name)
{
int i;
- read_datum_table();
+ G_read_datum_table();
- for (i = 0; i < count; i++)
- if (G_strcasecmp(name, table[i].name) == 0)
+ for (i = 0; i < table.count; i++)
+ if (G_strcasecmp(name, table.datums[i].name) == 0)
return i;
return -1;
@@ -55,32 +56,32 @@
char *G_datum_name(int n)
{
- read_datum_table();
+ G_read_datum_table();
- if (n < 0 || n >= count)
+ if (n < 0 || n >= table.count)
return NULL;
- return table[n].name;
+ return table.datums[n].name;
}
char *G_datum_description(int n)
{
- read_datum_table();
+ G_read_datum_table();
- if (n < 0 || n >= count)
+ if (n < 0 || n >= table.count)
return NULL;
- return table[n].descr;
+ return table.datums[n].descr;
}
char *G_datum_ellipsoid(int n)
{
- read_datum_table();
+ G_read_datum_table();
- if (n < 0 || n >= count)
+ if (n < 0 || n >= table.count)
return NULL;
- return table[n].ellps;
+ return table.datums[n].ellps;
}
/***********************************************************
@@ -141,18 +142,16 @@
}
-static void read_datum_table(void)
+void G_read_datum_table(void)
{
FILE *fd;
char file[GPATH_MAX];
char buf[1024];
int line;
- if (count >= 0)
+ if (table.count > 0)
return;
- count = 0;
-
sprintf(file, "%s%s", G_gisbase(), DATUMTABLE);
fd = fopen(file, "r");
@@ -163,18 +162,18 @@
for (line = 1; G_getl2(buf, sizeof(buf), fd); line++) {
char name[100], descr[100], ellps[100];
- struct table *t;
+ struct datum *t;
G_strip(buf);
if (*buf == '\0' || *buf == '#')
continue;
- if (count >= size) {
- size += 50;
- table = G_realloc(table, size * sizeof(struct table));
+ if (table.count >= table.size) {
+ table.size += 50;
+ table.datums = G_realloc(table.datums, table.size * sizeof(struct datum));
}
- t = &table[count];
+ t = &table.datums[table.count];
if (sscanf(buf, "%s \"%99[^\"]\" %s dx=%lf dy=%lf dz=%lf",
name, descr, ellps, &t->dx, &t->dy, &t->dz) != 6) {
@@ -186,16 +185,16 @@
t->descr = G_store(descr);
t->ellps = G_store(ellps);
- count++;
+ table.count++;
}
- qsort(table, count, sizeof(struct table), compare_table_names);
+ qsort(table.datums, table.count, sizeof(struct datum), compare_table_names);
}
static int compare_table_names(const void *aa, const void *bb)
{
- const struct table *a = aa;
- const struct table *b = bb;
+ const struct datum *a = aa;
+ const struct datum *b = bb;
return G_strcasecmp(a->name, b->name);
}
Modified: grass/trunk/lib/gis/distance.c
===================================================================
--- grass/trunk/lib/gis/distance.c 2008-11-23 23:21:52 UTC (rev 34460)
+++ grass/trunk/lib/gis/distance.c 2008-11-23 23:25:53 UTC (rev 34461)
@@ -25,11 +25,13 @@
static double min4(double, double, double, double);
static double min2(double, double);
+static struct state {
+ int projection;
+ double factor;
+} state;
-static int projection = 0;
-static double factor = 1.0;
+static struct state *st = &state;
-
/**
* \brief Begin distance calculations.
*
@@ -45,16 +47,16 @@
{
double a, e2;
- factor = 1.0;
- switch (projection = G_projection()) {
+ st->factor = 1.0;
+ switch (st->projection = G_projection()) {
case PROJECTION_LL:
G_get_ellipsoid_parameters(&a, &e2);
G_begin_geodesic_distance(a, e2);
return 2;
default:
- factor = G_database_units_to_meters_factor();
- if (factor <= 0.0) {
- factor = 1.0; /* assume meter grid */
+ st->factor = G_database_units_to_meters_factor();
+ if (st->factor <= 0.0) {
+ st->factor = 1.0; /* assume meter grid */
return 0;
}
return 1;
@@ -77,10 +79,10 @@
double G_distance(double e1, double n1, double e2, double n2)
{
- if (projection == PROJECTION_LL)
+ if (st->projection == PROJECTION_LL)
return G_geodesic_distance(e1, n1, e2, n2);
else
- return factor * hypot(e1 - e2, n1 - n2);
+ return st->factor * hypot(e1 - e2, n1 - n2);
}
Modified: grass/trunk/lib/gis/env.c
===================================================================
--- grass/trunk/lib/gis/env.c 2008-11-23 23:21:52 UTC (rev 34460)
+++ grass/trunk/lib/gis/env.c 2008-11-23 23:25:53 UTC (rev 34461)
@@ -22,27 +22,33 @@
#include <grass/gis.h>
#include <grass/glocale.h>
-#define ENV struct env
-
-ENV {
+struct bind {
int loc;
char *name;
char *value;
};
-static ENV *env = NULL;
-static ENV *env2 = NULL;
-static int count = 0;
-static int count2 = 0;
-static int init[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
-static char *gisrc = NULL;
-static int varmode = G_GISRC_MODE_FILE; /* where find/store variables */
+struct env {
+ struct bind *binds;
+ int count;
+ int size;
+};
+static struct state {
+ struct env env;
+ struct env env2;
+ char *gisrc;
+ int varmode;
+ int init[2];
+} state;
+
+static struct state *st = &state;
+
static int read_env(int);
static int set_env(const char *, const char *, int);
static int unset_env(const char *, int);
static char *get_env(const char *, int);
-static int write_env(int);
+static void write_env(int);
static FILE *open_env(const char *, int);
/**
@@ -58,7 +64,7 @@
*/
void G_set_gisrc_mode(int mode)
{
- varmode = mode;
+ st->varmode = mode;
}
/**
@@ -70,7 +76,7 @@
*/
int G_get_gisrc_mode(void)
{
- return (varmode);
+ return (st->varmode);
}
static int read_env(int loc)
@@ -78,16 +84,15 @@
char buf[200];
char *name;
char *value;
-
FILE *fd;
- if (loc == G_VAR_GISRC && varmode == G_GISRC_MODE_MEMORY)
+ if (loc == G_VAR_GISRC && st->varmode == G_GISRC_MODE_MEMORY)
return 0; /* don't use file for GISRC */
- if (init[loc])
+ if (st->init[loc])
return 1;
- init[loc] = 1;
+ st->init[loc] = 1;
if ((fd = open_env("r", loc))) {
while (G_getl2(buf, sizeof buf, fd)) {
@@ -103,6 +108,7 @@
if (*name && *value)
set_env(name, value, loc);
}
+
fclose(fd);
}
@@ -135,32 +141,39 @@
* and look for name in the environment
*/
empty = -1;
- for (n = 0; n < count; n++)
- if (!env[n].name) /* mark empty slot found */
+ for (n = 0; n < st->env.count; n++) {
+ struct bind *b = &st->env.binds[n];
+ if (!b->name) /* mark empty slot found */
empty = n;
- else if (strcmp(env[n].name, name) == 0 && env[n].loc == loc) {
- env[n].value = tv;
+ else if (strcmp(b->name, name) == 0 && b->loc == loc) {
+ b->value = tv;
return 1;
}
+ }
/* add name to env: to empty slot if any */
if (empty >= 0) {
- env[empty].loc = loc;
- env[empty].name = G_store(name);
- env[empty].value = tv;
+ struct bind *b = &st->env.binds[empty];
+ b->loc = loc;
+ b->name = G_store(name);
+ b->value = tv;
return 0;
}
/* must increase the env list and add in */
- if ((n = count++))
- env = (ENV *) G_realloc((char *)env, count * sizeof(ENV));
- else
- env = (ENV *) G_malloc(sizeof(ENV));
+ if (st->env.count >= st->env.size) {
+ st->env.size += 20;
+ st->env.binds = G_realloc(st->env.binds, st->env.size * sizeof(struct bind));
+ }
- env[n].loc = loc;
- env[n].name = G_store(name);
- env[n].value = tv;
+ {
+ struct bind *b = &st->env.binds[st->env.count++];
+ b->loc = loc;
+ b->name = G_store(name);
+ b->value = tv;
+ }
+
return 0;
}
@@ -168,13 +181,14 @@
{
int n;
- for (n = 0; n < count; n++)
- if (env[n].name && (strcmp(env[n].name, name) == 0) &&
- env[n].loc == loc) {
- G_free(env[n].name);
- env[n].name = 0;
+ for (n = 0; n < st->env.count; n++) {
+ struct bind *b = &st->env.binds[n];
+ if (b->name && strcmp(b->name, name) == 0 && b->loc == loc) {
+ G_free(b->name);
+ b->name = 0;
return 1;
}
+ }
return 0;
}
@@ -183,16 +197,17 @@
{
int n;
- for (n = 0; n < count; n++) {
- if (env[n].name && (strcmp(env[n].name, name) == 0) &&
- env[n].loc == loc)
- return env[n].value;
+ for (n = 0; n < st->env.count; n++) {
+ struct bind *b = &st->env.binds[n];
+ if (b->name && (strcmp(b->name, name) == 0) &&
+ b->loc == loc)
+ return b->value;
}
return NULL;
}
-static int write_env(int loc)
+static void write_env(int loc)
{
FILE *fd;
int n;
@@ -202,8 +217,8 @@
RETSIGTYPE (*sigquit)(int);
#endif
- if (loc == G_VAR_GISRC && varmode == G_GISRC_MODE_MEMORY)
- return 0; /* don't use file for GISRC */
+ if (loc == G_VAR_GISRC && st->varmode == G_GISRC_MODE_MEMORY)
+ return; /* don't use file for GISRC */
/*
* THIS CODE NEEDS TO BE PROTECTED FROM INTERRUPTS
@@ -214,10 +229,12 @@
sigquit = signal(SIGQUIT, SIG_IGN);
#endif
if ((fd = open_env("w", loc))) {
- for (n = 0; n < count; n++)
- if (env[n].name && env[n].value && env[n].loc == loc
- && (sscanf(env[n].value, "%1s", dummy) == 1))
- fprintf(fd, "%s: %s\n", env[n].name, env[n].value);
+ for (n = 0; n < st->env.count; n++) {
+ struct bind *b = &st->env.binds[n];
+ if (b->name && b->value && b->loc == loc
+ && (sscanf(b->value, "%1s", dummy) == 1))
+ fprintf(fd, "%s: %s\n", b->name, b->value);
+ }
fclose(fd);
}
@@ -225,23 +242,21 @@
#ifdef SIGQUIT
signal(SIGQUIT, sigquit);
#endif
-
- return 0;
}
static FILE *open_env(const char *mode, int loc)
{
- char buf[1000];
+ char buf[GPATH_MAX];
if (loc == G_VAR_GISRC) {
- if (!gisrc)
- gisrc = getenv("GISRC");
+ if (!st->gisrc)
+ st->gisrc = getenv("GISRC");
- if (!gisrc) {
+ if (!st->gisrc) {
G_fatal_error(_("GISRC - variable not set"));
- return (NULL);
+ return NULL;
}
- strcpy(buf, gisrc);
+ strcpy(buf, st->gisrc);
}
else if (loc == G_VAR_MAPSET) {
/* Warning: G_VAR_GISRC must be previously read -> */
@@ -348,12 +363,11 @@
\return 0
*/
-int G_setenv(const char *name, const char *value)
+void G_setenv(const char *name, const char *value)
{
read_env(G_VAR_GISRC);
set_env(name, value, G_VAR_GISRC);
write_env(G_VAR_GISRC);
- return 0;
}
/**
@@ -368,12 +382,11 @@
\return 0
*/
-int G_setenv2(const char *name, const char *value, int loc)
+void G_setenv2(const char *name, const char *value, int loc)
{
read_env(loc);
set_env(name, value, loc);
write_env(loc);
- return 0;
}
/**
@@ -384,11 +397,10 @@
\return 0
*/
-int G__setenv(const char *name, const char *value)
+void G__setenv(const char *name, const char *value)
{
read_env(G_VAR_GISRC);
set_env(name, value, G_VAR_GISRC);
- return 0;
}
/**
@@ -400,11 +412,10 @@
\return 0
*/
-int G__setenv2(const char *name, const char *value, int loc)
+void G__setenv2(const char *name, const char *value, int loc)
{
read_env(loc);
set_env(name, value, loc);
- return 0;
}
/**
@@ -416,13 +427,11 @@
\return 0
*/
-int G_unsetenv(const char *name)
+void G_unsetenv(const char *name)
{
read_env(G_VAR_GISRC);
unset_env(name, G_VAR_GISRC);
write_env(G_VAR_GISRC);
-
- return 0;
}
/**
@@ -434,13 +443,11 @@
\return 0
*/
-int G_unsetenv2(const char *name, int loc)
+void G_unsetenv2(const char *name, int loc)
{
read_env(loc);
unset_env(name, loc);
write_env(loc);
-
- return 0;
}
/**
@@ -450,12 +457,10 @@
\return 0
*/
-int G__write_env(void)
+void G__write_env(void)
{
- if (init[G_VAR_GISRC])
+ if (st->init[G_VAR_GISRC])
write_env(G_VAR_GISRC);
-
- return 0;
}
/**
@@ -478,9 +483,9 @@
read_env(G_VAR_GISRC);
if (n >= 0)
- for (i = 0; i < count; i++)
- if (env[i].name && *env[i].name && (n-- == 0))
- return env[i].name;
+ for (i = 0; i < st->env.count; i++)
+ if (st->env.binds[i].name && *st->env.binds[i].name && (n-- == 0))
+ return st->env.binds[i].name;
return NULL;
}
@@ -491,11 +496,9 @@
\return 0
*/
-int G__read_env(void)
+void G__read_env(void)
{
- init[G_VAR_GISRC] = 0;
-
- return 0;
+ st->init[G_VAR_GISRC] = 0;
}
/**
@@ -505,21 +508,20 @@
\return 0
*/
-int G__create_alt_env(void)
+void G__create_alt_env(void)
{
int i;
/* copy env to env2 */
- env2 = env;
- count2 = count;
- env = NULL;
- count = 0;
+ st->env2 = st->env;
- for (i = 0; i < count2; i++)
- if (env2[count].name)
- set_env(env2[count].name, env2[count].value, G_VAR_GISRC);
+ st->env.count = 0;
- return 0;
+ for (i = 0; i < st->env2.count; i++) {
+ struct bind *b = &st->env2.binds[i];
+ if (b->name)
+ set_env(b->name, b->value, G_VAR_GISRC);
+ }
}
/**
@@ -529,19 +531,11 @@
\return 0
*/
-int G__switch_env(void)
+void G__switch_env(void)
{
- ENV *tmp;
- int n;
+ struct env tmp;
- n = count;
- tmp = env;
-
- env = env2;
- count = count2;
-
- env2 = tmp;
- count2 = n;
-
- return 0;
+ tmp = st->env;
+ st->env = st->env2;
+ st->env2 = tmp;
}
Modified: grass/trunk/lib/gis/gdal.c
===================================================================
--- grass/trunk/lib/gis/gdal.c 2008-11-23 23:21:52 UTC (rev 34460)
+++ grass/trunk/lib/gis/gdal.c 2008-11-23 23:25:53 UTC (rev 34461)
@@ -120,10 +120,23 @@
#endif /* GDAL_LINK */
+void G_init_gdal(void)
+{
+#ifdef GDAL_LINK
+ static int initialized;
+
+ if (initialized)
+ return;
+
+ init_gdal();
+ (*pGDALAllRegister)();
+ initialized = 1;
+#endif
+}
+
struct GDAL_link *G_get_gdal_link(const char *name, const char *mapset)
{
#ifdef GDAL_LINK
- static int initialized;
GDALDatasetH data;
GDALRasterBandH band;
GDALDataType type;
@@ -200,11 +213,7 @@
if (req_type != map_type)
return NULL;
- if (!initialized) {
- init_gdal();
- (*pGDALAllRegister)();
- initialized = 1;
- }
+ G_init_gdal();
data = (*pGDALOpen)(filename, GA_ReadOnly);
if (!data)
Modified: grass/trunk/lib/gis/geodesic.c
===================================================================
--- grass/trunk/lib/gis/geodesic.c 2008-11-23 23:21:52 UTC (rev 34460)
+++ grass/trunk/lib/gis/geodesic.c 2008-11-23 23:25:53 UTC (rev 34461)
@@ -31,8 +31,11 @@
static int adjust_lat(double *);
static int adjust_lon(double *);
-static double A, B;
+static struct state {
+ double A, B;
+} state;
+static struct state *st = &state;
int G_begin_geodesic_equation(double lon1, double lat1, double lon2,
double lat2)
@@ -49,7 +52,7 @@
temp = lat1; lat1 = lat2; lat2 = temp;
}
if (lon1 == lon2) {
- A = B = 0.0;
+ st->A = st->B = 0.0;
return 0;
}
lon1 = Radians(lon1);
@@ -61,8 +64,8 @@
tan1 = tan(lat1);
tan2 = tan(lat2);
- A = (tan2 * cos(lon1) - tan1 * cos(lon2)) / sin21;
- B = (tan2 * sin(lon1) - tan1 * sin(lon2)) / sin21;
+ st->A = (tan2 * cos(lon1) - tan1 * cos(lon2)) / sin21;
+ st->B = (tan2 * sin(lon1) - tan1 * sin(lon2)) / sin21;
return 1;
}
@@ -74,7 +77,7 @@
adjust_lon(&lon);
lon = Radians(lon);
- return Degrees(atan(A * sin(lon) - B * cos(lon)));
+ return Degrees(atan(st->A * sin(lon) - st->B * cos(lon)));
}
static int adjust_lon(double *lon)
Modified: grass/trunk/lib/gis/geodist.c
===================================================================
--- grass/trunk/lib/gis/geodist.c 2008-11-23 23:21:52 UTC (rev 34460)
+++ grass/trunk/lib/gis/geodist.c 2008-11-23 23:25:53 UTC (rev 34461)
@@ -28,12 +28,15 @@
#include "pi.h"
-static double boa;
-static double f;
-static double ff64;
-static double al;
-static double t1, t2, t3, t4, t1r, t2r;
+static struct state {
+ double boa;
+ double f;
+ double ff64;
+ double al;
+ double t1, t2, t3, t4, t1r, t2r;
+} state;
+static struct state *st = &state;
/**
* \brief Begin geodesic distance.
@@ -51,10 +54,10 @@
int G_begin_geodesic_distance(double a, double e2)
{
- al = a;
- boa = sqrt(1 - e2);
- f = 1 - boa;
- ff64 = f * f / 64;
+ st->al = a;
+ st->boa = sqrt(1 - e2);
+ st->f = 1 - st->boa;
+ st->ff64 = st->f * st->f / 64;
return 0;
}
@@ -74,7 +77,7 @@
int G_set_geodesic_distance_lat1(double lat1)
{
- t1r = atan(boa * tan(Radians(lat1)));
+ st->t1r = atan(st->boa * tan(Radians(lat1)));
return 0;
}
@@ -96,24 +99,24 @@
double stm, ctm, sdtm, cdtm;
double tm, dtm;
- t2r = atan(boa * tan(Radians(lat2)));
+ st->t2r = atan(st->boa * tan(Radians(lat2)));
- tm = (t1r + t2r) / 2;
- dtm = (t2r - t1r) / 2;
+ tm = (st->t1r + st->t2r) / 2;
+ dtm = (st->t2r - st->t1r) / 2;
stm = sin(tm);
ctm = cos(tm);
sdtm = sin(dtm);
cdtm = cos(dtm);
- t1 = stm * cdtm;
- t1 = t1 * t1 * 2;
+ st->t1 = stm * cdtm;
+ st->t1 = st->t1 * st->t1 * 2;
- t2 = sdtm * ctm;
- t2 = t2 * t2 * 2;
+ st->t2 = sdtm * ctm;
+ st->t2 = st->t2 * st->t2 * 2;
- t3 = sdtm * sdtm;
- t4 = cdtm * cdtm - stm * stm;
+ st->t3 = sdtm * sdtm;
+ st->t4 = cdtm * cdtm - stm * stm;
return 0;
}
@@ -141,14 +144,14 @@
sdlmr = sin(Radians(lon2 - lon1) / 2);
/* special case - shapiro */
- if (sdlmr == 0.0 && t1r == t2r)
+ if (sdlmr == 0.0 && st->t1r == st->t2r)
return 0.0;
- q = t3 + sdlmr * sdlmr * t4;
+ q = st->t3 + sdlmr * sdlmr * st->t4;
/* special case - shapiro */
if (q == 1.0)
- return M_PI * al;
+ return M_PI * st->al;
/* Mod: shapiro
* cd=1-2q is ill-conditioned if q is small O(10**-23)
@@ -179,20 +182,18 @@
t = acos(cd) / sd; /* don't know how to fix acos(1-2*q) yet */
/* mod ends here */
- u = t1 / (1 - q);
- v = t2 / q;
+ u = st->t1 / (1 - q);
+ v = st->t2 / q;
d = 4 * t * t;
x = u + v;
e = -2 * cd;
y = u - v;
a = -d * e;
- return (al * sd *
- (t - f / 4 * (t * x - y) +
- ff64 * (x * (a + (t - (a + e) / 2) * x) + y * (-2 * d + e * y)
- + d * x * y)
- )
- );
+ return st->al * sd * (t
+ - st->f / 4 * (t * x - y)
+ + st->ff64 * (x * (a + (t - (a + e) / 2) * x)
+ + y * (-2 * d + e * y) + d * x * y));
}
Modified: grass/trunk/lib/gis/get_ellipse.c
===================================================================
--- grass/trunk/lib/gis/get_ellipse.c 2008-11-23 23:21:52 UTC (rev 34460)
+++ grass/trunk/lib/gis/get_ellipse.c 2008-11-23 23:25:53 UTC (rev 34461)
@@ -28,27 +28,26 @@
#include <grass/gis.h>
#include <grass/glocale.h>
-static struct table
-{
- char *name;
- char *descr;
- double a;
- double e2;
- double f;
-} *table = NULL;
-
-static int count = -1;
static const char PERMANENT[] = "PERMANENT";
+static struct table {
+ struct ellipse
+ {
+ char *name;
+ char *descr;
+ double a;
+ double e2;
+ double f;
+ } *ellipses;
+ int count;
+ int size;
+} table;
+
/* static int get_a_e2 (char *, char *, double *,double *); */
-static int get_a_e2_f(const char *, const char *, double *, double *,
- double *);
-void ellipsoid_table_file(char *);
-static int compare_table_names(const void *, const void *);
-static int read_ellipsoid_table(int);
+static int get_a_e2_f(const char *, const char *, double *, double *, double *);
+static int compare_ellipse_names(const void *, const void *);
static int get_ellipsoid_parameters(struct Key_Value *, double *, double *);
-
/*!
* \brief get ellipsoid parameters
*
@@ -111,12 +110,12 @@
{
int i;
- (void)read_ellipsoid_table(0);
+ G_read_ellipsoid_table(0);
- for (i = 0; i < count; i++) {
- if (G_strcasecmp(name, table[i].name) == 0) {
- *a = table[i].a;
- *e2 = table[i].e2;
+ for (i = 0; i < table.count; i++) {
+ if (G_strcasecmp(name, table.ellipses[i].name) == 0) {
+ *a = table.ellipses[i].a;
+ *e2 = table.ellipses[i].e2;
return 1;
}
}
@@ -137,8 +136,8 @@
*/
char *G_ellipsoid_name(int n)
{
- (void)read_ellipsoid_table(0);
- return n >= 0 && n < count ? table[n].name : NULL;
+ G_read_ellipsoid_table(0);
+ return n >= 0 && n < table.count ? table.ellipses[n].name : NULL;
}
/*
@@ -172,13 +171,13 @@
{
int i;
- (void)read_ellipsoid_table(0);
+ G_read_ellipsoid_table(0);
- for (i = 0; i < count; i++) {
- if (G_strcasecmp(name, table[i].name) == 0) {
- *a = table[i].a;
- *e2 = table[i].e2;
- *f = table[i].f;
+ for (i = 0; i < table.count; i++) {
+ if (G_strcasecmp(name, table.ellipses[i].name) == 0) {
+ *a = table.ellipses[i].a;
+ *e2 = table.ellipses[i].e2;
+ *f = table.ellipses[i].f;
return 1;
}
}
@@ -201,12 +200,11 @@
char *G_ellipsoid_description(int n)
{
- (void)read_ellipsoid_table(0);
- return n >= 0 && n < count ? table[n].descr : NULL;
+ G_read_ellipsoid_table(0);
+ return n >= 0 && n < table.count ? table.ellipses[n].descr : NULL;
}
-static int
-get_a_e2_f(const char *s1, const char *s2, double *a, double *e2, double *f)
+static int get_a_e2_f(const char *s1, const char *s2, double *a, double *e2, double *f)
{
double b, recipf;
@@ -246,54 +244,45 @@
return 0;
}
-void ellipsoid_table_file(char *file)
+static int compare_ellipse_names(const void *pa, const void *pb)
{
- sprintf(file, "%s/etc/ellipse.table", G_gisbase());
- return;
-}
+ const struct ellipse *a = pa;
+ const struct ellipse *b = pb;
-static int compare_table_names(const void *pa, const void *pb)
-{
- const struct table *a = pa, *b = pb;
-
- /* return strcmp(a->name,b->name); */
return G_strcasecmp(a->name, b->name);
}
-static int read_ellipsoid_table(int fatal)
+int G_read_ellipsoid_table(int fatal)
{
FILE *fd;
char file[GPATH_MAX];
char buf[1024];
- char name[100], descr[100], buf1[100], buf2[100];
char badlines[256];
int line;
int err;
- if (count >= 0)
+ if (table.count > 0)
return 1;
- count = 0;
- table = NULL;
- (void)ellipsoid_table_file(file);
+ sprintf(file, "%s/etc/ellipse.table", G_gisbase());
fd = fopen(file, "r");
if (fd == NULL) {
- perror(file);
- sprintf(buf, _("Unable to open ellipsoid table file <%s>"), file);
- fatal ? G_fatal_error(buf) : G_warning(buf);
+ (fatal ? G_fatal_error : G_warning)(_("Unable to open ellipsoid table file <%s>"), file);
return 0;
}
err = 0;
*badlines = 0;
for (line = 1; G_getl2(buf, sizeof buf, fd); line++) {
+ char name[100], descr[100], buf1[100], buf2[100];
+ struct ellipse *e;
+
G_strip(buf);
if (*buf == 0 || *buf == '#')
continue;
- if (sscanf(buf, "%s \"%99[^\"]\" %s %s", name, descr, buf1, buf2) !=
- 4) {
+ if (sscanf(buf, "%s \"%99[^\"]\" %s %s", name, descr, buf1, buf2) != 4) {
err++;
sprintf(buf, " %d", line);
if (*badlines)
@@ -302,17 +291,19 @@
continue;
}
- table =
- (struct table *)G_realloc((char *)table,
- (count + 1) * sizeof(*table));
- table[count].name = G_store(name);
- table[count].descr = G_store(descr);
+ if (table.count >= table.size) {
+ table.size += 60;
+ table.ellipses = G_realloc(table.ellipses, table.size * sizeof(struct ellipse));
+ }
- if (get_a_e2_f
- (buf1, buf2, &table[count].a, &table[count].e2, &table[count].f)
- || get_a_e2_f(buf2, buf1, &table[count].a, &table[count].e2,
- &table[count].f))
- count++;
+ e = &table.ellipses[table.count];
+
+ e->name = G_store(name);
+ e->descr = G_store(descr);
+
+ if (get_a_e2_f(buf1, buf2, &e->a, &e->e2, &e->f) ||
+ get_a_e2_f(buf2, buf1, &e->a, &e->e2, &e->f))
+ table.count++;
else {
err++;
sprintf(buf, " %d", line);
@@ -327,22 +318,20 @@
if (!err) {
/* over correct typed version */
- qsort(table, count, sizeof(*table), compare_table_names);
+ qsort(table.ellipses, table.count, sizeof(struct ellipse), compare_ellipse_names);
return 1;
}
- (fatal ? G_fatal_error : G_warning) ((err > 1)
- ?
- _("Lines%s of ellipsoid table file <%s> are invalid")
- :
- _("Line%s of ellipsoid table file <%s> is invalid"),
- badlines, file);
+ (fatal ? G_fatal_error : G_warning)(
+ (err > 1)
+ ? _("Lines%s of ellipsoid table file <%s> are invalid")
+ : _("Line%s of ellipsoid table file <%s> is invalid"),
+ badlines, file);
return 0;
}
-static int get_ellipsoid_parameters(struct Key_Value *proj_keys, double *a,
- double *e2)
+static int get_ellipsoid_parameters(struct Key_Value *proj_keys, double *a, double *e2)
{
char *str, *str1;
@@ -354,40 +343,35 @@
if (strncmp(str, "sphere", 6) == 0) {
str = G_find_key_value("a", proj_keys);
if (str != NULL) {
- if (sscanf(str, "%lf", a) != 1) {
+ if (sscanf(str, "%lf", a) != 1)
G_fatal_error(_("Invalid a: field '%s' in file %s in <%s>"),
str, PROJECTION_FILE, PERMANENT);
- }
}
- else {
+ else
*a = 6370997.0;
- }
+
*e2 = 0.0;
return 0;
}
else {
- if (G_get_ellipsoid_by_name(str, a, e2) == 0) {
+ if (G_get_ellipsoid_by_name(str, a, e2) == 0)
G_fatal_error(_("Invalid ellipsoid '%s' in file %s in <%s>"),
str, PROJECTION_FILE, PERMANENT);
- }
- else {
+ else
return 1;
- }
}
}
else {
str = G_find_key_value("a", proj_keys);
str1 = G_find_key_value("es", proj_keys);
if ((str != NULL) && (str1 != NULL)) {
- if (sscanf(str, "%lf", a) != 1) {
+ if (sscanf(str, "%lf", a) != 1)
G_fatal_error(_("Invalid a: field '%s' in file %s in <%s>"),
str, PROJECTION_FILE, PERMANENT);
- }
- if (sscanf(str1, "%lf", e2) != 1) {
+ if (sscanf(str1, "%lf", e2) != 1)
G_fatal_error(_("Invalid es: field '%s' in file %s in <%s>"),
str, PROJECTION_FILE, PERMANENT);
- }
return 1;
}
@@ -398,10 +382,9 @@
*e2 = .006694385;
return 0;
}
- else {
+ else
G_fatal_error(_("No ellipsoid info given in file %s in <%s>"),
PROJECTION_FILE, PERMANENT);
- }
}
}
Modified: grass/trunk/lib/gis/locale.c
===================================================================
--- grass/trunk/lib/gis/locale.c 2008-11-23 23:21:52 UTC (rev 34460)
+++ grass/trunk/lib/gis/locale.c 2008-11-23 23:25:53 UTC (rev 34461)
@@ -19,25 +19,31 @@
#include <string.h>
#include <locale.h>
#include <grass/glocale.h>
+#include <grass/gis.h>
-
#if defined(HAVE_LIBINTL_H) && defined(USE_NLS)
-static char *locale_dir(void)
+void G_init_locale(void)
{
- static char localedir[4096];
+ static int initialized;
+ char localedir[GPATH_MAX];
const char *gisbase;
- if (*localedir)
- return localedir;
+ if (initialized)
+ return;
+ initialized = 1;
+ setlocale(LC_CTYPE, "");
+ setlocale(LC_MESSAGES, "");
+
gisbase = getenv("GISBASE");
if (!gisbase || !*gisbase)
- return "";
+ return;
strcpy(localedir, gisbase);
strcat(localedir, "/locale");
- return localedir;
+ bindtextdomain("grasslibs", localedir);
+ bindtextdomain("grassmods", localedir);
}
#endif
@@ -53,20 +59,8 @@
char *G_gettext(const char *package, const char *msgid)
{
#if defined(HAVE_LIBINTL_H) && defined(USE_NLS)
- static char now_bound[4096];
- static int initialized;
+ G_init_locale();
- if (!initialized) {
- setlocale(LC_CTYPE, "");
- setlocale(LC_MESSAGES, "");
- initialized = 1;
- }
-
- if (strcmp(now_bound, package) != 0) {
- strcpy(now_bound, package);
- bindtextdomain(package, locale_dir());
- }
-
return dgettext(package, msgid);
#else
return (char *)msgid;
Modified: grass/trunk/lib/gis/ls.c
===================================================================
--- grass/trunk/lib/gis/ls.c 2008-11-23 23:21:52 UTC (rev 34460)
+++ grass/trunk/lib/gis/ls.c 2008-11-23 23:25:53 UTC (rev 34461)
@@ -30,11 +30,14 @@
typedef int ls_filter_func(const char * /*filename */ , void * /*closure */ );
-static ls_filter_func *ls_filter;
-static void *ls_closure;
+static struct state {
+ ls_filter_func *ls_filter;
+ void *ls_closure;
+ ls_filter_func *ls_ex_filter;
+ void *ls_ex_closure;
+} state;
-static ls_filter_func *ls_ex_filter;
-static void *ls_ex_closure;
+static struct state *st = &state;
static int cmp_names(const void *aa, const void *bb)
{
@@ -59,14 +62,14 @@
void G_set_ls_filter(ls_filter_func *func, void *closure)
{
- ls_filter = func;
- ls_closure = closure;
+ st->ls_filter = func;
+ st->ls_closure = closure;
}
void G_set_ls_exclude_filter(ls_filter_func *func, void *closure)
{
- ls_ex_filter = func;
- ls_ex_closure = closure;
+ st->ls_ex_filter = func;
+ st->ls_ex_closure = closure;
}
/**
@@ -98,9 +101,9 @@
while ((dp = readdir(dfd)) != NULL) {
if (dp->d_name[0] == '.') /* Don't list hidden files */
continue;
- if (ls_filter && !(*ls_filter)(dp->d_name, ls_closure))
+ if (st->ls_filter && !(*st->ls_filter)(dp->d_name, st->ls_closure))
continue;
- if (ls_ex_filter && (*ls_ex_filter)(dp->d_name, ls_ex_closure))
+ if (st->ls_ex_filter && (*st->ls_ex_filter)(dp->d_name, st->ls_ex_closure))
continue;
dir_listing = (char **)G_realloc(dir_listing, (1 + n) * sizeof(char *));
dir_listing[n] = G_store(dp->d_name);
Modified: grass/trunk/lib/gis/mapset_nme.c
===================================================================
--- grass/trunk/lib/gis/mapset_nme.c 2008-11-23 23:21:52 UTC (rev 34460)
+++ grass/trunk/lib/gis/mapset_nme.c 2008-11-23 23:25:53 UTC (rev 34461)
@@ -16,13 +16,18 @@
#include <unistd.h>
#include <grass/gis.h>
-static char **mapset_name;
-static char **mapset_name2;
-static int nmapset = 0;
-static int nmapset2 = 0;
-static int new_mapset(const char *);
-static int get_list_of_mapsets(void);
+static struct state {
+ struct list {
+ char **names;
+ int count;
+ int size;
+ } path, path2;
+} state;
+static struct state *st = &state;
+
+static void new_mapset(const char *);
+
/*!
\brief Get name of the n'th mapset from the mapset_name[] list.
@@ -35,67 +40,46 @@
*/
char *G__mapset_name(int n)
{
- /*
- * first call will detect no mapsets in list
- * and go look up the list
- */
- if (nmapset == 0)
- get_list_of_mapsets();
- /*
- * must not run off the bounds of the list
- */
- if (n < 0 || n >= nmapset)
- return ((char *)NULL);
+ if (st->path.count == 0)
+ G_get_list_of_mapsets();
- return mapset_name[n];
+ if (n < 0 || n >= st->path.count)
+ return NULL;
+
+ return st->path.names[n];
}
-static int get_list_of_mapsets(void)
+void G_get_list_of_mapsets(void)
{
- char name[GNAME_MAX];
- FILE *fd;
+ FILE *fp;
- /*
- * the list of mapsets is in SEARCH_PATH file in the mapset
- */
- mapset_name = NULL;
- if ((fd = G_fopen_old("", "SEARCH_PATH", G_mapset()))) {
- while (fscanf(fd, "%s", name) == 1)
+ fp = G_fopen_old("", "SEARCH_PATH", G_mapset());
+ if (fp) {
+ char name[GNAME_MAX];
+ while (fscanf(fp, "%s", name) == 1)
if (G__mapset_permissions(name) >= 0)
new_mapset(name);
- fclose(fd);
+ fclose(fp);
}
- /*
- * if no list, then set the list to the current mapset followed
- * by PERMANENT
- */
- if (!nmapset) {
- char *perm;
- char *cur;
- cur = G_mapset();
- perm = "PERMANENT";
+ if (!st->path.count) {
+ static const char perm[] = "PERMANENT";
+ const char *cur = G_mapset();
new_mapset(cur);
if (strcmp(perm, cur) != 0 && G__mapset_permissions(perm) >= 0)
new_mapset(perm);
}
-
- return 0;
}
-static int new_mapset(const char *name)
+static void new_mapset(const char *name)
{
- /*
- * extend mapset name list and store name
- * note: assumes G_realloc will become G_malloc if mapset_name == NULL
- */
- nmapset++;
- mapset_name =
- (char **)G_realloc((char *)mapset_name, nmapset * sizeof(char *));
- mapset_name[nmapset - 1] = G_store(name);
+ if (st->path.count >= st->path.size) {
+ st->path.size += 10;
+ st->path.names = G_realloc(st->path.names, st->path.size * sizeof(char *));
+ }
- return 0;
+ st->path.names[st->path.count++] = G_store(name);
}
/*!
@@ -103,14 +87,12 @@
\return 0
*/
-int G__create_alt_search_path(void)
+void G__create_alt_search_path(void)
{
- nmapset2 = nmapset;
- mapset_name2 = mapset_name;
+ st->path2.count = st->path.count;
+ st->path2.names = st->path.names;
- nmapset = 0;
-
- return 0;
+ st->path.count = 0;
}
/*!
@@ -118,21 +100,19 @@
\return 0
*/
-int G__switch_search_path(void)
+void G__switch_search_path(void)
{
- int n;
+ int count;
char **names;
- n = nmapset2;
- names = mapset_name2;
+ count = st->path2.count;
+ names = st->path2.names;
- nmapset2 = nmapset;
- mapset_name2 = mapset_name;
+ st->path2.count = st->path.count;
+ st->path2.names = st->path.names;
- nmapset = n;
- mapset_name = names;
-
- return 0;
+ st->path.count = count;
+ st->path.names = names;
}
/*!
@@ -140,11 +120,9 @@
\return 0
*/
-int G_reset_mapsets(void)
+void G_reset_mapsets(void)
{
- nmapset = 0;
-
- return 0;
+ st->path.count = 0;
}
/*!
@@ -156,51 +134,37 @@
*/
char **G_available_mapsets(void)
{
- int i, n;
- static int alloc = 0;
- static char **mapsets = NULL;
+ char **mapsets = NULL;
+ int alloc = 0;
+ int n = 0;
DIR *dir;
struct dirent *ent;
- char buf[1024];
- struct stat st;
G_debug(3, "G_available_mapsets");
- if (alloc == 0) { /* alloc some space, so that if something failes we can return array */
- alloc = 50;
- mapsets = (char **)G_calloc(alloc, sizeof(char *));
- }
- else { /* free old strings and reset pointers to NULL */
- i = 0;
- while (mapsets[i]) {
- G_free(mapsets[i]);
- mapsets[i] = NULL;
- }
- }
-
- n = 0;
dir = opendir(G_location_path());
- if (dir == NULL)
+ if (!dir)
return mapsets;
while ((ent = readdir(dir))) {
+ char buf[GPATH_MAX];
+ struct stat st;
+
sprintf(buf, "%s/%s/WIND", G_location_path(), ent->d_name);
- if (stat(buf, &st) == 0) {
- G_debug(4, "%s is mapset", ent->d_name);
- /* Realloc space if necessary */
- if (n + 2 >= alloc) {
- alloc += 50;
- mapsets = (char **)G_realloc(mapsets, alloc * sizeof(char *));
- for (i = n; i < alloc; i++)
- mapsets[i] = NULL;
- }
- /* Add to list */
- mapsets[n] = G_store(ent->d_name);
- n++;
- }
- else {
+
+ if (G_stat(buf, &st) != 0) {
G_debug(4, "%s is not mapset", ent->d_name);
+ continue;
}
+
+ G_debug(4, "%s is mapset", ent->d_name);
+
+ if (n + 2 >= alloc) {
+ alloc += 50;
+ mapsets = G_realloc(mapsets, alloc * sizeof(char *));
+ }
+
+ mapsets[n++] = G_store(ent->d_name);
}
closedir(dir);
@@ -220,9 +184,9 @@
{
int i;
- for (i = 0; i < nmapset; i++) {
- if (strcmp(mapset_name[i], mapset) == 0)
+ for (i = 0; i < st->path.count; i++)
+ if (strcmp(st->path.names[i], mapset) == 0)
return;
- }
+
new_mapset(mapset);
}
Modified: grass/trunk/lib/gis/parser.c
===================================================================
--- grass/trunk/lib/gis/parser.c 2008-11-23 23:21:52 UTC (rev 34460)
+++ grass/trunk/lib/gis/parser.c 2008-11-23 23:25:53 UTC (rev 34461)
@@ -91,23 +91,6 @@
#define REPLACED 5
#define KEYLENGTH 64
-static int interactive_ok = 1;
-static int n_opts;
-static int n_flags;
-static int overwrite;
-static int quiet;
-static int has_required;
-
-static struct Flag first_flag; /* First flag in a linked list */
-static struct Flag *current_flag; /* Pointer for traversing list */
-
-static struct Option first_option;
-static struct Option *current_option;
-
-static struct GModule module_info; /* general information on the corresponding module */
-
-static const char *pgm_name = NULL;
-
struct Item
{
struct Option *option;
@@ -115,9 +98,31 @@
struct Item *next_item;
};
-static struct Item first_item;
-static struct Item *current_item;
-static int n_items = 0;
+static struct state {
+ int no_interactive;
+ int n_opts;
+ int n_flags;
+ int overwrite;
+ int quiet;
+ int has_required;
+
+ struct GModule module_info; /* general information on the corresponding module */
+
+ const char *pgm_name;
+
+ struct Flag first_flag; /* First flag in a linked list */
+ struct Flag *current_flag; /* Pointer for traversing list */
+
+ struct Option first_option;
+ struct Option *current_option;
+
+ struct Item first_item;
+ struct Item *current_item;
+ int n_items;
+} state;
+
+static struct state *st = &state;
+
static int show_options(int, const char *);
static int show(const char *, int);
static int set_flag(int);
@@ -154,11 +159,9 @@
* \return always returns 0
*/
-int G_disable_interactive(void)
+void G_disable_interactive(void)
{
- interactive_ok = 0;
-
- return 0;
+ st->no_interactive = 1;
}
@@ -182,34 +185,34 @@
/* Allocate memory if not the first flag */
- if (n_flags) {
+ if (st->n_flags) {
flag = (struct Flag *)G_malloc(sizeof(struct Flag));
- current_flag->next_flag = flag;
+ st->current_flag->next_flag = flag;
}
else
- flag = &first_flag;
+ flag = &st->first_flag;
/* Zero structure */
G_zero((char *)flag, sizeof(struct Flag));
- current_flag = flag;
- n_flags++;
+ st->current_flag = flag;
+ st->n_flags++;
- if (n_items) {
+ if (st->n_items) {
item = (struct Item *)G_malloc(sizeof(struct Item));
- current_item->next_item = item;
+ st->current_item->next_item = item;
}
else
- item = &first_item;
+ item = &st->first_item;
G_zero((char *)item, sizeof(struct Item));
item->flag = flag;
item->option = NULL;
- current_item = item;
- n_items++;
+ st->current_item = item;
+ st->n_items++;
return (flag);
}
@@ -238,12 +241,12 @@
/* Allocate memory if not the first option */
- if (n_opts) {
+ if (st->n_opts) {
opt = (struct Option *)G_malloc(sizeof(struct Option));
- current_option->next_opt = opt;
+ st->current_option->next_opt = opt;
}
else
- opt = &first_option;
+ opt = &st->first_option;
/* Zero structure */
G_zero((char *)opt, sizeof(struct Option));
@@ -263,23 +266,23 @@
opt->descriptions = NULL;
opt->guisection = NULL;
- current_option = opt;
- n_opts++;
+ st->current_option = opt;
+ st->n_opts++;
- if (n_items) {
+ if (st->n_items) {
item = (struct Item *)G_malloc(sizeof(struct Item));
- current_item->next_item = item;
+ st->current_item->next_item = item;
}
else
- item = &first_item;
+ item = &st->first_item;
G_zero((char *)item, sizeof(struct Item));
item->option = opt;
item->flag = NULL;
- current_item = item;
- n_items++;
+ st->current_item = item;
+ st->n_items++;
return (opt);
}
@@ -687,7 +690,7 @@
/* Allocate memory */
- module = &module_info;
+ module = &st->module_info;
/* Zero structure */
@@ -753,14 +756,14 @@
}
}
G_basename(tmp_name, "exe");
- pgm_name = tmp_name;
+ st->pgm_name = tmp_name;
/* Stash default answers */
- opt = &first_option;
+ opt = &st->first_option;
while (opt != NULL) {
if (opt->required)
- has_required = 1;
+ st->has_required = 1;
/* Parse options */
if (opt->options) {
@@ -842,11 +845,11 @@
/* If there are NO arguments, go interactive */
- if (argc < 2 && has_required && interactive_ok && isatty(0)) {
+ if (argc < 2 && st->has_required && !st->no_interactive && isatty(0)) {
G_gui();
return -1;
}
- else if (argc < 2 && has_required && isatty(0)) {
+ else if (argc < 2 && st->has_required && isatty(0)) {
G_usage();
return -1;
}
@@ -894,7 +897,7 @@
/* Overwrite option */
if (strcmp(ptr, "--o") == 0 || strcmp(ptr, "--overwrite") == 0) {
- overwrite = 1;
+ st->overwrite = 1;
}
/* Verbose option */
@@ -902,13 +905,13 @@
char buff[32];
/* print everything: max verbosity level */
- module_info.verbose = G_verbose_max();
+ st->module_info.verbose = G_verbose_max();
sprintf(buff, "GRASS_VERBOSE=%d", G_verbose_max());
putenv(G_store(buff));
- if (quiet == 1) {
+ if (st->quiet == 1) {
G_warning(_("Use either --quiet or --verbose flag, not both. Assuming --verbose."));
}
- quiet = -1;
+ st->quiet = -1;
}
/* Quiet option */
@@ -916,13 +919,13 @@
char buff[32];
/* print nothing, but errors and warnings */
- module_info.verbose = G_verbose_min();
+ st->module_info.verbose = G_verbose_min();
sprintf(buff, "GRASS_VERBOSE=%d", G_verbose_min());
putenv(G_store(buff));
- if (quiet == -1) {
+ if (st->quiet == -1) {
G_warning(_("Use either --quiet or --verbose flag, not both. Assuming --quiet."));
}
- quiet = 1; /* for passing to gui init */
+ st->quiet = 1; /* for passing to gui init */
}
/* Force gui to come up */
@@ -943,8 +946,8 @@
}
/* If we see the first option with no equal sign */
- else if (need_first_opt && n_opts) {
- first_option.answer = G_store(ptr);
+ else if (need_first_opt && st->n_opts) {
+ st->first_option.answer = G_store(ptr);
need_first_opt = 0;
}
@@ -997,13 +1000,13 @@
char element[KEYLENGTH];
char desc[KEYLENGTH];
- if (module_info.overwrite)
+ if (st->module_info.overwrite)
return 1;
/* figure out if any of the options use a "new" gisprompt */
/* This is to see if we should spit out the --o flag */
- if (n_opts) {
- opt = &first_option;
+ if (st->n_opts) {
+ opt = &st->first_option;
while (opt != NULL) {
if (opt->gisprompt) {
split_gisprompt(opt->gisprompt, age, element, desc);
@@ -1052,34 +1055,34 @@
new_prompt = uses_new_gisprompt();
- if (!pgm_name) /* v.dave && r.michael */
- pgm_name = G_program_name();
- if (!pgm_name)
- pgm_name = "??";
+ if (!st->pgm_name) /* v.dave && r.michael */
+ st->pgm_name = G_program_name();
+ if (!st->pgm_name)
+ st->pgm_name = "??";
- if (module_info.label || module_info.description) {
+ if (st->module_info.label || st->module_info.description) {
fprintf(stderr, _("\nDescription:\n"));
- if (module_info.label)
- fprintf(stderr, " %s\n", module_info.label);
- if (module_info.description)
- fprintf(stderr, " %s\n", module_info.description);
+ if (st->module_info.label)
+ fprintf(stderr, " %s\n", st->module_info.label);
+ if (st->module_info.description)
+ fprintf(stderr, " %s\n", st->module_info.description);
}
- if (module_info.keywords) {
+ if (st->module_info.keywords) {
fprintf(stderr, _("\nKeywords:\n"));
- fprintf(stderr, " %s\n", module_info.keywords);
+ fprintf(stderr, " %s\n", st->module_info.keywords);
}
fprintf(stderr, _("\nUsage:\n "));
- len = show(pgm_name, 1);
+ len = show(st->pgm_name, 1);
/* Print flags */
- if (n_flags) {
+ if (st->n_flags) {
item[0] = ' ';
item[1] = '[';
item[2] = '-';
- flag = &first_flag;
+ flag = &st->first_flag;
for (n = 3; flag != NULL; n++, flag = flag->next_flag)
item[n] = flag->key;
item[n++] = ']';
@@ -1088,8 +1091,8 @@
}
maxlen = 0;
- if (n_opts) {
- opt = &first_option;
+ if (st->n_opts) {
+ opt = &st->first_option;
while (opt != NULL) {
if (opt->key_desc != NULL)
key_desc = opt->key_desc;
@@ -1139,8 +1142,8 @@
fprintf(stderr, _("\nFlags:\n"));
- if (n_flags) {
- flag = &first_flag;
+ if (st->n_flags) {
+ flag = &st->first_flag;
while (flag != NULL) {
fprintf(stderr, " -%c ", flag->key);
@@ -1167,9 +1170,9 @@
/* Print help info for options */
- if (n_opts) {
+ if (st->n_opts) {
fprintf(stderr, _("\nParameters:\n"));
- opt = &first_option;
+ opt = &st->first_option;
while (opt != NULL) {
fprintf(stderr, " %*s ", maxlen, opt->key);
@@ -1289,45 +1292,45 @@
encoding = "UTF-8";
#endif
- if (!pgm_name) /* v.dave && r.michael */
- pgm_name = G_program_name();
- if (!pgm_name)
- pgm_name = "??";
+ if (!st->pgm_name) /* v.dave && r.michael */
+ st->pgm_name = G_program_name();
+ if (!st->pgm_name)
+ st->pgm_name = "??";
fprintf(stdout, "<?xml version=\"1.0\" encoding=\"%s\"?>\n", encoding);
fprintf(stdout, "<!DOCTYPE task SYSTEM \"grass-interface.dtd\">\n");
- fprintf(stdout, "<task name=\"%s\">\n", pgm_name);
+ fprintf(stdout, "<task name=\"%s\">\n", st->pgm_name);
- if (module_info.label) {
+ if (st->module_info.label) {
fprintf(stdout, "\t<label>\n\t\t");
- print_escaped_for_xml(stdout, module_info.label);
+ print_escaped_for_xml(stdout, st->module_info.label);
fprintf(stdout, "\n\t</label>\n");
}
- if (module_info.description) {
+ if (st->module_info.description) {
fprintf(stdout, "\t<description>\n\t\t");
- print_escaped_for_xml(stdout, module_info.description);
+ print_escaped_for_xml(stdout, st->module_info.description);
fprintf(stdout, "\n\t</description>\n");
}
- if (module_info.keywords) {
+ if (st->module_info.keywords) {
fprintf(stdout, "\t<keywords>\n\t\t");
- print_escaped_for_xml(stdout, module_info.keywords);
+ print_escaped_for_xml(stdout, st->module_info.keywords);
fprintf(stdout, "\n\t</keywords>\n");
}
/***** Don't use parameter-groups for now. We'll reimplement this later
***** when we have a concept of several mutually exclusive option
***** groups
- if (n_opts || n_flags)
+ if (st->n_opts || st->n_flags)
fprintf(stdout, "\t<parameter-group>\n");
*****
*****
*****/
- if (n_opts) {
- opt = &first_option;
+ if (st->n_opts) {
+ opt = &st->first_option;
while (opt != NULL) {
/* TODO: make this a enumeration type? */
switch (opt->type) {
@@ -1439,8 +1442,8 @@
}
- if (n_flags) {
- flag = &first_flag;
+ if (st->n_flags) {
+ flag = &st->first_flag;
while (flag != NULL) {
fprintf(stdout, "\t<flag name=\"%c\">\n", flag->key);
@@ -1468,7 +1471,7 @@
/***** Don't use parameter-groups for now. We'll reimplement this later
***** when we have a concept of several mutually exclusive option
***** groups
- if (n_opts || n_flags)
+ if (st->n_opts || st->n_flags)
fprintf(stdout, "\t</parameter-group>\n");
*****
*****
@@ -1513,15 +1516,15 @@
new_prompt = uses_new_gisprompt();
- if (!pgm_name) /* v.dave && r.michael */
- pgm_name = G_program_name();
- if (!pgm_name)
- pgm_name = "??";
+ if (!st->pgm_name) /* v.dave && r.michael */
+ st->pgm_name = G_program_name();
+ if (!st->pgm_name)
+ st->pgm_name = "??";
fprintf(stdout,
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n");
fprintf(stdout, "<html>\n<head>\n");
- fprintf(stdout, "<title>GRASS GIS: %s</title>\n", pgm_name);
+ fprintf(stdout, "<title>GRASS GIS: %s</title>\n", st->pgm_name);
fprintf(stdout,
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">\n");
fprintf(stdout,
@@ -1531,34 +1534,34 @@
fprintf(stdout,
"<img src=\"grass_logo.png\" alt=\"GRASS logo\"><hr align=center size=6 noshade>\n\n");
fprintf(stdout, "<h2>%s</h2>\n", _("NAME"));
- fprintf(stdout, "<em><b>%s</b></em> ", pgm_name);
+ fprintf(stdout, "<em><b>%s</b></em> ", st->pgm_name);
- if (module_info.label || module_info.description)
+ if (st->module_info.label || st->module_info.description)
fprintf(stdout, " - ");
- if (module_info.label)
- fprintf(stdout, "%s<BR>\n", module_info.label);
+ if (st->module_info.label)
+ fprintf(stdout, "%s<BR>\n", st->module_info.label);
- if (module_info.description)
- fprintf(stdout, "%s\n", module_info.description);
+ if (st->module_info.description)
+ fprintf(stdout, "%s\n", st->module_info.description);
fprintf(stdout, "<h2>%s</h2>\n", _("KEYWORDS"));
- if (module_info.keywords) {
- fprintf(stdout, "%s", module_info.keywords);
+ if (st->module_info.keywords) {
+ fprintf(stdout, "%s", st->module_info.keywords);
fprintf(stdout, "\n");
}
fprintf(stdout, "<h2>%s</h2>\n", _("SYNOPSIS"));
- fprintf(stdout, "<b>%s</b><br>\n", pgm_name);
- fprintf(stdout, "<b>%s help</b><br>\n", pgm_name);
+ fprintf(stdout, "<b>%s</b><br>\n", st->pgm_name);
+ fprintf(stdout, "<b>%s help</b><br>\n", st->pgm_name);
- fprintf(stdout, "<b>%s</b>", pgm_name);
+ fprintf(stdout, "<b>%s</b>", st->pgm_name);
/* print short version first */
- if (n_flags) {
- flag = &first_flag;
+ if (st->n_flags) {
+ flag = &st->first_flag;
fprintf(stdout, " [-<b>");
while (flag != NULL) {
fprintf(stdout, "%c", flag->key);
@@ -1569,8 +1572,8 @@
else
fprintf(stdout, " ");
- if (n_opts) {
- opt = &first_option;
+ if (st->n_opts) {
+ opt = &st->first_option;
while (opt != NULL) {
if (opt->key_desc != NULL)
@@ -1614,11 +1617,11 @@
/* now long version */
fprintf(stdout, "\n");
- if (n_flags || new_prompt) {
- flag = &first_flag;
+ if (st->n_flags || new_prompt) {
+ flag = &st->first_flag;
fprintf(stdout, "<h3>%s:</h3>\n", _("Flags"));
fprintf(stdout, "<DL>\n");
- while (n_flags && flag != NULL) {
+ while (st->n_flags && flag != NULL) {
fprintf(stdout, "<DT><b>-%c</b></DT>\n", flag->key);
if (flag->label) {
@@ -1652,8 +1655,8 @@
}
fprintf(stdout, "\n");
- if (n_opts) {
- opt = &first_option;
+ if (st->n_opts) {
+ opt = &st->first_option;
fprintf(stdout, "<h3>%s:</h3>\n", _("Parameters"));
fprintf(stdout, "<DL>\n");
@@ -1767,18 +1770,18 @@
"#############################################################################/\n");
fprintf(fp, "#%%Module\n");
- if (module_info.label)
- fprintf(fp, "#%% label: %s\n", module_info.label);
- if (module_info.description)
- fprintf(fp, "#%% description: %s\n", module_info.description);
- if (module_info.keywords)
- fprintf(fp, "#%% keywords: %s\n", module_info.keywords);
+ if (st->module_info.label)
+ fprintf(fp, "#%% label: %s\n", st->module_info.label);
+ if (st->module_info.description)
+ fprintf(fp, "#%% description: %s\n", st->module_info.description);
+ if (st->module_info.keywords)
+ fprintf(fp, "#%% keywords: %s\n", st->module_info.keywords);
fprintf(fp, "#%%End\n");
- if (n_flags) {
+ if (st->n_flags) {
struct Flag *flag;
- for (flag = &first_flag; flag; flag = flag->next_flag) {
+ for (flag = &st->first_flag; flag; flag = flag->next_flag) {
fprintf(fp, "#%%Flag\n");
fprintf(fp, "#%% key: %c\n", flag->key);
if (flag->label)
@@ -1791,10 +1794,10 @@
}
}
- if (n_opts) {
+ if (st->n_opts) {
struct Option *opt;
- for (opt = &first_option; opt; opt = opt->next_opt) {
+ for (opt = &st->first_option; opt; opt = opt->next_opt) {
switch (opt->type) {
case TYPE_INTEGER:
type = "integer";
@@ -1857,20 +1860,20 @@
const char *type;
int optn;
- fprintf(fp, "begin_dialog {%s} {\n", pgm_name);
- fprintf(fp, " label {%s}\n", module_info.label ? module_info.label : "");
+ fprintf(fp, "begin_dialog {%s} {\n", st->pgm_name);
+ fprintf(fp, " label {%s}\n", st->module_info.label ? st->module_info.label : "");
fprintf(fp, " desc {%s}\n",
- module_info.description ? module_info.description : "");
+ st->module_info.description ? st->module_info.description : "");
fprintf(fp, " key {%s}\n",
- module_info.keywords ? module_info.keywords : "");
+ st->module_info.keywords ? st->module_info.keywords : "");
fprintf(fp, "}\n");
optn = 1;
- if (n_flags) {
+ if (st->n_flags) {
struct Flag *flag;
- for (flag = &first_flag; flag; flag = flag->next_flag, optn++) {
+ for (flag = &st->first_flag; flag; flag = flag->next_flag, optn++) {
fprintf(fp, "add_flag %d {\n", optn);
fprintf(fp, " name {%c}\n", flag->key);
fprintf(fp, " desc {%s}\n", flag->description);
@@ -1884,10 +1887,10 @@
}
}
- if (n_opts) {
+ if (st->n_opts) {
struct Option *opt;
- for (opt = &first_option; opt; opt = opt->next_opt, optn++) {
+ for (opt = &st->first_option; opt; opt = opt->next_opt, optn++) {
if (opt->key_desc != NULL)
type = opt->key_desc;
else
@@ -1932,7 +1935,7 @@
fprintf(fp, " name {overwrite}\n");
fprintf(fp, " desc {%s}\n",
_("Allow output files to overwrite existing files"));
- fprintf(fp, " answer %d\n", overwrite);
+ fprintf(fp, " answer %d\n", st->overwrite);
fprintf(fp, " label {%s}\n", _("Allow overwrite"));
fprintf(fp, " guisection {}\n");
fprintf(fp, "}\n");
@@ -1942,7 +1945,7 @@
fprintf(fp, "add_xflag %d {\n", optn);
fprintf(fp, " name {quiet}\n");
fprintf(fp, " desc {%s}\n", _("Run with minimal output messages"));
- fprintf(fp, " answer %d\n", quiet);
+ fprintf(fp, " answer %d\n", st->quiet);
fprintf(fp, " label {%s}\n", _("Run quietly"));
fprintf(fp, " guisection {}\n");
fprintf(fp, "}\n");
@@ -1958,10 +1961,10 @@
{
FILE *fp;
- if (!pgm_name)
- pgm_name = G_program_name();
- if (!pgm_name)
- pgm_name = "??";
+ if (!st->pgm_name)
+ st->pgm_name = G_program_name();
+ if (!st->pgm_name)
+ st->pgm_name = "??";
#ifdef __MINGW32__
if (getenv("GRASS_DEBUG_GUI"))
@@ -1992,14 +1995,14 @@
{
char script[GPATH_MAX];
- if (!pgm_name)
- pgm_name = G_program_name();
- if (!pgm_name)
+ if (!st->pgm_name)
+ st->pgm_name = G_program_name();
+ if (!st->pgm_name)
G_fatal_error(_("Unable to determine program name"));
sprintf(script, "%s/etc/wxpython/gui_modules/menuform.py",
getenv("GISBASE"));
- G_spawn("python", "menuform.py", script, pgm_name, NULL);
+ G_spawn("python", "menuform.py", script, st->pgm_name, NULL);
}
/**
@@ -2031,10 +2034,10 @@
**/
static void G_tcltk(void)
{
- if (!pgm_name)
- pgm_name = G_program_name();
- if (!pgm_name)
- pgm_name = "??";
+ if (!st->pgm_name)
+ st->pgm_name = G_program_name();
+ if (!st->pgm_name)
+ st->pgm_name = "??";
generate_tcl(stdout);
}
@@ -2096,14 +2099,14 @@
/* Flag is not valid if there are no flags to set */
- if (!n_flags) {
+ if (!st->n_flags) {
fprintf(stderr, _("Sorry, <%c> is not a valid flag\n"), f);
return (1);
}
/* Find flag with corrrect keyword */
- flag = &first_flag;
+ flag = &st->first_flag;
while (flag != NULL) {
if (flag->key == f) {
flag->answer = 1;
@@ -2153,7 +2156,7 @@
/* Find option with best keyword match */
got_one = 0;
key_len = strlen(the_key);
- for (at_opt = &first_option; at_opt != NULL; at_opt = at_opt->next_opt) {
+ for (at_opt = &st->first_option; at_opt != NULL; at_opt = at_opt->next_opt) {
if (at_opt->key == NULL || strncmp(the_key, at_opt->key, key_len))
continue;
@@ -2201,10 +2204,10 @@
error = 0;
- if (!n_opts)
+ if (!st->n_opts)
return (0);
- opt = &first_option;
+ opt = &st->first_option;
while (opt != NULL) {
/* Check answer against options if any */
@@ -2391,10 +2394,10 @@
err = 0;
- if (!n_opts)
+ if (!st->n_opts)
return (0);
- opt = &first_option;
+ opt = &st->first_option;
while (opt != NULL) {
if (opt->required && opt->answer == NULL) {
fprintf(stderr,
@@ -2418,10 +2421,10 @@
int len;
- if (!n_opts)
+ if (!st->n_opts)
return 0;
- opt = &first_option;
+ opt = &st->first_option;
while (opt != NULL) {
if ( /*opt->multiple && */ (opt->answer != NULL)) {
/* Allocate some memory to store array of pointers */
@@ -2476,11 +2479,11 @@
int n;
int error;
- if (!n_opts)
+ if (!st->n_opts)
return (0);
error = 0;
- opt = &first_option;
+ opt = &st->first_option;
while (opt != NULL) {
if ((opt->answer != NULL) && (opt->key_desc != NULL)) {
/* count commas */
@@ -2516,9 +2519,9 @@
char *overstr;
int over;
- module_info.overwrite = 0;
+ st->module_info.overwrite = 0;
- if (!n_opts)
+ if (!st->n_opts)
return (0);
over = 0;
@@ -2533,15 +2536,15 @@
over = 1;
}
- if (overwrite || over) {
- module_info.overwrite = 1;
+ if (st->overwrite || over) {
+ st->module_info.overwrite = 1;
/* Set the environment so that programs run in a script also obey --o */
putenv("GRASS_OVERWRITE=1");
/* No need to check options for existing files if overwrite is true */
return error;
}
- opt = &first_option;
+ opt = &st->first_option;
while (opt != NULL) {
if ((opt->answer != NULL) && (opt->gisprompt != NULL)) {
split_gisprompt(opt->gisprompt, age, element, desc);
@@ -2550,7 +2553,7 @@
int i;
for (i = 0; opt->answers[i]; i++) {
if (G_find_file(element, opt->answers[i], G_mapset())) { /* found */
- if (!overwrite && !over) {
+ if (!st->overwrite && !over) {
if (G_info_format() != G_INFO_FORMAT_GUI) {
fprintf(stderr,
_("ERROR: option <%s>: <%s> exists.\n"),
@@ -2641,8 +2644,8 @@
strcpy(cur, tmp);
cur += len;
- if (n_flags) {
- flag = &first_flag;
+ if (st->n_flags) {
+ flag = &st->first_flag;
while (flag != '\0') {
if (flag->answer == 1) {
flg[0] = ' ';
@@ -2664,7 +2667,7 @@
}
}
- opt = &first_option;
+ opt = &st->first_option;
while (opt != '\0') {
if (opt->answer != '\0' && opt->answers[0] != NULL) {
slen = strlen(opt->key) + strlen(opt->answers[0]) + 4; /* +4 for: ' ' = " " */
Modified: grass/trunk/lib/gis/percent.c
===================================================================
--- grass/trunk/lib/gis/percent.c 2008-11-23 23:21:52 UTC (rev 34460)
+++ grass/trunk/lib/gis/percent.c 2008-11-23 23:25:53 UTC (rev 34461)
@@ -17,12 +17,14 @@
#include <stdio.h>
#include <grass/gis.h>
+static struct state {
+ int prev;
+ int first;
+} state = {-1, 1};
-static int prev = -1;
-static int first = 1;
+static struct state *st = &state;
-
-int G_percent(long n, long d, int s)
+void G_percent(long n, long d, int s)
{
int x, format;
@@ -33,10 +35,10 @@
/* be verbose only 1> */
if (format == G_INFO_FORMAT_SILENT || G_verbose() < 1)
- return 0;
+ return;
- if (n <= 0 || n >= d || x > prev + s) {
- prev = x;
+ if (n <= 0 || n >= d || x > st->prev + s) {
+ st->prev = x;
if (format == G_INFO_FORMAT_STANDARD) {
fprintf(stderr, "%4d%%\b\b\b\b\b", x);
@@ -49,12 +51,12 @@
fprintf(stderr, "%d..", x);
}
else { /* GUI */
- if (first) {
+ if (st->first) {
fprintf(stderr, "\n");
}
fprintf(stderr, "GRASS_INFO_PERCENT: %d\n", x);
fflush(stderr);
- first = 0;
+ st->first = 0;
}
}
}
@@ -63,11 +65,11 @@
if (format == G_INFO_FORMAT_STANDARD) {
fprintf(stderr, "\n");
}
- prev = -1;
- first = 1;
+ st->prev = -1;
+ st->first = 1;
}
- return 0;
+ return;
}
@@ -77,10 +79,8 @@
* \return always returns 0
*/
-int G_percent_reset(void)
+void G_percent_reset(void)
{
- prev = -1;
- first = 1;
-
- return 0;
+ st->prev = -1;
+ st->first = 1;
}
Modified: grass/trunk/lib/gis/plot.c
===================================================================
--- grass/trunk/lib/gis/plot.c 2008-11-23 23:21:52 UTC (rev 34460)
+++ grass/trunk/lib/gis/plot.c 2008-11-23 23:25:53 UTC (rev 34461)
@@ -17,10 +17,6 @@
#include <math.h>
#include <grass/gis.h>
-static double xconv, yconv;
-static double left, right, top, bottom;
-static int ymin, ymax;
-static struct Cell_head window;
static int fastline(double, double, double, double);
static int slowline(double, double, double, double);
static int plot_line(double, double, double, double, int (*)());
@@ -28,21 +24,36 @@
static int edge(double, double, double, double);
static int edge_point(double, int);
-#define POINT struct point
-POINT {
- double x;
- int y;
-};
static int edge_order(const void *, const void *);
static int row_solid_fill(int, double, double);
static int row_dotted_fill(int, double, double);
-static int dotted_fill_gap = 2;
static int ifloor(double);
static int iceil(double);
-static int (*row_fill) () = row_solid_fill;
-static int (*move) (int, int);
-static int (*cont) (int, int);
+struct point {
+ double x;
+ int y;
+};
+#define POINT struct point
+
+static struct state {
+ struct Cell_head window;
+ double xconv, yconv;
+ double left, right, top, bottom;
+ int ymin, ymax;
+ int dotted_fill_gap;
+
+ POINT *P;
+ int np;
+ int npalloc;
+
+ int (*row_fill)(int, double, double);
+ int (*move)(int, int);
+ int (*cont)(int, int);
+} state;
+
+static struct state *st = &state;
+
/*!
* \brief returns east larger than west
*
@@ -97,27 +108,27 @@
int G_setup_plot(double t, double b, double l, double r,
int (*Move) (int, int), int (*Cont) (int, int))
{
- G_get_set_window(&window);
+ G_get_set_window(&st->window);
- left = l;
- right = r;
- top = t;
- bottom = b;
+ st->left = l;
+ st->right = r;
+ st->top = t;
+ st->bottom = b;
- xconv = (right - left) / (window.east - window.west);
- yconv = (bottom - top) / (window.north - window.south);
+ st->xconv = (st->right - st->left) / (st->window.east - st->window.west);
+ st->yconv = (st->bottom - st->top) / (st->window.north - st->window.south);
- if (top < bottom) {
- ymin = iceil(top);
- ymax = ifloor(bottom);
+ if (st->top < st->bottom) {
+ st->ymin = iceil(st->top);
+ st->ymax = ifloor(st->bottom);
}
else {
- ymin = iceil(bottom);
- ymax = ifloor(top);
+ st->ymin = iceil(st->bottom);
+ st->ymax = ifloor(st->top);
}
- move = Move;
- cont = Cont;
+ st->move = Move;
+ st->cont = Cont;
return 0;
}
@@ -136,20 +147,20 @@
int G_setup_fill(int gap)
{
if (gap > 0) {
- row_fill = row_dotted_fill;
- dotted_fill_gap = gap + 1;
+ st->row_fill = row_dotted_fill;
+ st->dotted_fill_gap = gap + 1;
}
else
- row_fill = row_solid_fill;
+ st->row_fill = row_solid_fill;
return 0;
}
-#define X(e) (left + xconv * ((e) - window.west))
-#define Y(n) (top + yconv * (window.north - (n)))
+#define X(e) (st->left + st->xconv * ((e) - st->window.west))
+#define Y(n) (st->top + st->yconv * (st->window.north - (n)))
-#define EAST(x) (window.west + ((x)-left)/xconv)
-#define NORTH(y) (window.north - ((y)-top)/yconv)
+#define EAST(x) (st->window.west + ((x)-st->left)/st->xconv)
+#define NORTH(y) (st->window.north - ((y)-st->top)/st->yconv)
/*!
@@ -167,7 +178,7 @@
int G_plot_where_xy(double east, double north, int *x, int *y)
{
- *x = ifloor(X(G_adjust_easting(east, &window)) + 0.5);
+ *x = ifloor(X(G_adjust_easting(east, &st->window)) + 0.5);
*y = ifloor(Y(north) + 0.5);
return 0;
@@ -189,7 +200,7 @@
int G_plot_where_en(int x, int y, double *east, double *north)
{
- *east = G_adjust_easting(EAST(x), &window);
+ *east = G_adjust_easting(EAST(x), &st->window);
*north = NORTH(y);
return 0;
@@ -200,8 +211,8 @@
int x, y;
G_plot_where_xy(east, north, &x, &y);
- move(x, y);
- cont(x, y);
+ st->move(x, y);
+ st->cont(x, y);
return 0;
}
@@ -243,8 +254,8 @@
static int fastline(double x1, double y1, double x2, double y2)
{
- move(ifloor(x1 + 0.5), ifloor(y1 + 0.5));
- cont(ifloor(x2 + 0.5), ifloor(y2 + 0.5));
+ st->move(ifloor(x1 + 0.5), ifloor(y1 + 0.5));
+ st->cont(ifloor(x2 + 0.5), ifloor(y2 + 0.5));
return 0;
}
@@ -278,9 +289,9 @@
}
if (xstart <= xstop) {
ystart = ifloor(m * xstart + b + 0.5);
- move(xstart, ystart);
+ st->move(xstart, ystart);
while (xstart <= xstop) {
- cont(xstart++, ystart);
+ st->cont(xstart++, ystart);
ystart = ifloor(m * xstart + b + 0.5);
}
}
@@ -302,9 +313,9 @@
}
if (ystart <= ystop) {
xstart = ifloor(m * ystart + b + 0.5);
- move(xstart, ystart);
+ st->move(xstart, ystart);
while (ystart <= ystop) {
- cont(xstart, ystart++);
+ st->cont(xstart, ystart++);
xstart = ifloor(m * ystart + b + 0.5);
}
}
@@ -321,18 +332,18 @@
y1 = Y(north1);
y2 = Y(north2);
- if (window.proj == PROJECTION_LL) {
+ if (st->window.proj == PROJECTION_LL) {
if (east1 > east2)
while ((east1 - east2) > 180)
east2 += 360;
else if (east2 > east1)
while ((east2 - east1) > 180)
east1 += 360;
- while (east1 > window.east) {
+ while (east1 > st->window.east) {
east1 -= 360.0;
east2 -= 360.0;
}
- while (east1 < window.west) {
+ while (east1 < st->window.west) {
east1 += 360.0;
east2 += 360.0;
}
@@ -341,12 +352,12 @@
line(x1, y1, x2, y2);
- if (east2 > window.east || east2 < window.west) {
- while (east2 > window.east) {
+ if (east2 > st->window.east || east2 < st->window.west) {
+ while (east2 > st->window.east) {
east1 -= 360.0;
east2 -= 360.0;
}
- while (east2 < window.west) {
+ while (east2 < st->window.west) {
east1 += 360.0;
east2 += 360.0;
}
@@ -377,10 +388,6 @@
* returns 0 ok, 2 n<3, -1 weird internal error, 1 no memory
*/
-static POINT *P;
-static int np;
-static int npalloc = 0;
-
#define OK 0
#define TOO_FEW_EDGES 2
#define NO_MEMORY 1
@@ -419,16 +426,19 @@
double e0, e1;
int shift1, shift2;
+ if (!st->row_fill)
+ st->row_fill = row_solid_fill;
+
if (n < 3)
return TOO_FEW_EDGES;
/* traverse the perimeter */
- np = 0;
+ st->np = 0;
shift1 = 0;
/* global wrap-around for lat-lon, part1 */
- if (window.proj == PROJECTION_LL) {
+ if (st->window.proj == PROJECTION_LL) {
/*
pole = G_pole_in_polygon(x,y,n);
*/
@@ -464,9 +474,9 @@
return NO_MEMORY;
shift = 0; /* shift into window */
- while (E + shift > window.east)
+ while (E + shift > st->window.east)
shift -= 360.0;
- while (E + shift < window.west)
+ while (E + shift < st->window.west)
shift += 360.0;
shift1 = X(x[n - 1] + shift) - X(x[n - 1]);
}
@@ -485,28 +495,28 @@
}
/* check if perimeter has odd number of points */
- if (np % 2)
+ if (st->np % 2)
return OUT_OF_SYNC;
/* sort the edge points by col(x) and then by row(y) */
- qsort(P, np, sizeof(POINT), &edge_order);
+ qsort(st->P, st->np, sizeof(POINT), &edge_order);
/* plot */
- for (i = 1; i < np; i += 2) {
- if (P[i].y != P[i - 1].y)
+ for (i = 1; i < st->np; i += 2) {
+ if (st->P[i].y != st->P[i - 1].y)
return OUT_OF_SYNC;
- row_fill(P[i].y, P[i - 1].x + shift1, P[i].x + shift1);
+ st->row_fill(st->P[i].y, st->P[i - 1].x + shift1, st->P[i].x + shift1);
}
- if (window.proj == PROJECTION_LL) { /* now do wrap-around, part 2 */
+ if (st->window.proj == PROJECTION_LL) { /* now do wrap-around, part 2 */
shift = 0;
- while (W + shift < window.west)
+ while (W + shift < st->window.west)
shift += 360.0;
- while (W + shift > window.east)
+ while (W + shift > st->window.east)
shift -= 360.0;
shift2 = X(x[n - 1] + shift) - X(x[n - 1]);
if (shift2 != shift1) {
- for (i = 1; i < np; i += 2) {
- row_fill(P[i].y, P[i - 1].x + shift2, P[i].x + shift2);
+ for (i = 1; i < st->np; i += 2) {
+ st->row_fill(st->P[i].y, st->P[i - 1].x + shift2, st->P[i].x + shift2);
}
}
}
@@ -549,9 +559,12 @@
double e0, e1;
int *shift1 = NULL, shift2;
+ if (!st->row_fill)
+ st->row_fill = row_solid_fill;
+
/* traverse the perimeter */
- np = 0;
+ st->np = 0;
shift1 = (int *)G_calloc(sizeof(int), rings);
for (j = 0; j < rings; j++) {
@@ -564,7 +577,7 @@
y = ys[j];
/* global wrap-around for lat-lon, part1 */
- if (window.proj == PROJECTION_LL) {
+ if (st->window.proj == PROJECTION_LL) {
/*
pole = G_pole_in_polygon(x,y,n);
*/
@@ -600,9 +613,9 @@
return NO_MEMORY;
shift = 0; /* shift into window */
- while (E + shift > window.east)
+ while (E + shift > st->window.east)
shift -= 360.0;
- while (E + shift < window.west)
+ while (E + shift < st->window.west)
shift += 360.0;
shift1[j] = X(x[n - 1] + shift) - X(x[n - 1]);
}
@@ -622,33 +635,33 @@
} /* for() */
/* check if perimeter has odd number of points */
- if (np % 2)
+ if (st->np % 2)
return OUT_OF_SYNC;
/* sort the edge points by col(x) and then by row(y) */
- qsort(P, np, sizeof(POINT), &edge_order);
+ qsort(st->P, st->np, sizeof(POINT), &edge_order);
/* plot */
for (j = 0; j < rings; j++) {
- for (i = 1; i < np; i += 2) {
- if (P[i].y != P[i - 1].y)
+ for (i = 1; i < st->np; i += 2) {
+ if (st->P[i].y != st->P[i - 1].y)
return OUT_OF_SYNC;
- row_fill(P[i].y, P[i - 1].x + shift1[j], P[i].x + shift1[j]);
+ st->row_fill(st->P[i].y, st->P[i - 1].x + shift1[j], st->P[i].x + shift1[j]);
}
- if (window.proj == PROJECTION_LL) { /* now do wrap-around, part 2 */
+ if (st->window.proj == PROJECTION_LL) { /* now do wrap-around, part 2 */
n = rpnts[j];
x = xs[j];
y = ys[j];
shift = 0;
- while (W + shift < window.west)
+ while (W + shift < st->window.west)
shift += 360.0;
- while (W + shift > window.east)
+ while (W + shift > st->window.east)
shift -= 360.0;
shift2 = X(x[n - 1] + shift) - X(x[n - 1]);
if (shift2 != shift1[j]) {
- for (i = 1; i < np; i += 2) {
- row_fill(P[i].y, P[i - 1].x + shift2, P[i].x + shift2);
+ for (i = 1; i < st->np; i += 2) {
+ st->row_fill(st->P[i].y, st->P[i - 1].x + shift2, st->P[i].x + shift2);
}
}
}
@@ -699,24 +712,24 @@
static int edge_point(double x, int y)
{
- if (y < ymin || y > ymax)
+ if (y < st->ymin || y > st->ymax)
return 1;
- if (np >= npalloc) {
- if (npalloc > 0) {
- npalloc *= 2;
- P = (POINT *) G_realloc(P, npalloc * sizeof(POINT));
+ if (st->np >= st->npalloc) {
+ if (st->npalloc > 0) {
+ st->npalloc *= 2;
+ st->P = (POINT *) G_realloc(st->P, st->npalloc * sizeof(POINT));
}
else {
- npalloc = 32;
- P = (POINT *) G_malloc(npalloc * sizeof(POINT));
+ st->npalloc = 32;
+ st->P = (POINT *) G_malloc(st->npalloc * sizeof(POINT));
}
- if (P == NULL) {
- npalloc = 0;
+ if (st->P == NULL) {
+ st->npalloc = 0;
return 0;
}
}
- P[np].x = x;
- P[np++].y = y;
+ st->P[st->np].x = x;
+ st->P[st->np++].y = y;
return 1;
}
@@ -744,8 +757,8 @@
i1 = iceil(x1);
i2 = ifloor(x2);
if (i1 <= i2) {
- move(i1, y);
- cont(i2, y);
+ st->move(i1, y);
+ st->cont(i2, y);
}
return 0;
@@ -755,15 +768,15 @@
{
int i1, i2, i;
- if (y != iceil(y / dotted_fill_gap) * dotted_fill_gap)
+ if (y != iceil(y / st->dotted_fill_gap) * st->dotted_fill_gap)
return 0;
- i1 = iceil(x1 / dotted_fill_gap) * dotted_fill_gap;
+ i1 = iceil(x1 / st->dotted_fill_gap) * st->dotted_fill_gap;
i2 = ifloor(x2);
if (i1 <= i2) {
- for (i = i1; i <= i2; i += dotted_fill_gap) {
- move(i, y);
- cont(i, y);
+ for (i = i1; i <= i2; i += st->dotted_fill_gap) {
+ st->move(i, y);
+ st->cont(i, y);
}
}
@@ -814,7 +827,7 @@
double incr;
- incr = fabs(1.0 / xconv);
+ incr = fabs(1.0 / st->xconv);
east = east1;
north = f(east1);
Modified: grass/trunk/lib/gis/rhumbline.c
===================================================================
--- grass/trunk/lib/gis/rhumbline.c 2008-11-23 23:21:52 UTC (rev 34460)
+++ grass/trunk/lib/gis/rhumbline.c 2008-11-23 23:25:53 UTC (rev 34461)
@@ -38,9 +38,12 @@
static int adjust_lon(double *);
#endif /* unused */
-static double TAN_A, TAN1, TAN2, L;
-static int parallel;
+static struct state {
+ double TAN_A, TAN1, TAN2, L;
+ int parallel;
+} state;
+static struct state *st = &state;
/**
* \brief Start rhumbline calculations.
@@ -61,25 +64,25 @@
adjust_lat(&lat2);
if (lon1 == lon2) {
- parallel = 1; /* a lie */
- L = lat1;
+ st->parallel = 1; /* a lie */
+ st->L = lat1;
return 0;
}
if (lat1 == lat2) {
- parallel = 1;
- L = lat1;
+ st->parallel = 1;
+ st->L = lat1;
return 1;
}
- parallel = 0;
+ st->parallel = 0;
lon1 = Radians(lon1);
lon2 = Radians(lon2);
lat1 = Radians(lat1);
lat2 = Radians(lat2);
- TAN1 = tan(M_PI_4 + lat1 / 2.0);
- TAN2 = tan(M_PI_4 + lat2 / 2.0);
- TAN_A = (lon2 - lon1) / (log(TAN2) - log(TAN1));
- L = lon1;
+ st->TAN1 = tan(M_PI_4 + lat1 / 2.0);
+ st->TAN2 = tan(M_PI_4 + lat2 / 2.0);
+ st->TAN_A = (lon2 - lon1) / (log(st->TAN2) - log(st->TAN1));
+ st->L = lon1;
return 1;
}
@@ -96,12 +99,12 @@
double G_rhumbline_lat_from_lon(double lon)
{
- if (parallel)
- return L;
+ if (st->parallel)
+ return st->L;
lon = Radians(lon);
- return Degrees(2 * atan(exp((lon - L) / TAN_A) * TAN1) - M_PI_2);
+ return Degrees(2 * atan(exp((lon - st->L) / st->TAN_A) * st->TAN1) - M_PI_2);
}
Modified: grass/trunk/lib/gis/set_window.c
===================================================================
--- grass/trunk/lib/gis/set_window.c 2008-11-23 23:21:52 UTC (rev 34460)
+++ grass/trunk/lib/gis/set_window.c 2008-11-23 23:25:53 UTC (rev 34461)
@@ -25,14 +25,12 @@
* 'window'.
*
* \param[out] window window structure to be set
- * \return 1
+ * \return
*/
-int G_get_set_window(struct Cell_head *window)
+void G_get_set_window(struct Cell_head *window)
{
G__init_window();
G_copy(window, &G__.window, sizeof(*window));
-
- return 1;
}
More information about the grass-commit
mailing list