[GRASS-SVN] r34526 - in grass/trunk: general/g.region include lib/gis

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Nov 26 18:16:59 EST 2008


Author: glynn
Date: 2008-11-26 18:16:59 -0500 (Wed, 26 Nov 2008)
New Revision: 34526

Modified:
   grass/trunk/general/g.region/printwindow.c
   grass/trunk/include/gisdefs.h
   grass/trunk/lib/gis/proj3.c
Log:
Eliminate use of static buffers for return values


Modified: grass/trunk/general/g.region/printwindow.c
===================================================================
--- grass/trunk/general/g.region/printwindow.c	2008-11-26 21:55:54 UTC (rev 34525)
+++ grass/trunk/general/g.region/printwindow.c	2008-11-26 23:16:59 UTC (rev 34526)
@@ -12,7 +12,7 @@
 
 int print_window(struct Cell_head *window, int print_flag)
 {
-    char *prj, *datum, *ellps;
+    const char *prj, *datum, *ellps;
     int x, width = 11;
 
     char north[30], south[30], east[30], west[30], nsres[30], ewres[30],

Modified: grass/trunk/include/gisdefs.h
===================================================================
--- grass/trunk/include/gisdefs.h	2008-11-26 21:55:54 UTC (rev 34525)
+++ grass/trunk/include/gisdefs.h	2008-11-26 23:16:59 UTC (rev 34526)
@@ -921,11 +921,11 @@
 char *G__projection_name(int);
 
 /* proj3.c */
-char *G_database_unit_name(int);
-char *G_database_projection_name(void);
+const char *G_database_unit_name(int);
+const char *G_database_projection_name(void);
+const char *G_database_datum_name(void);
+const char *G_database_ellipse_name(void);
 double G_database_units_to_meters_factor(void);
-char *G_database_datum_name(void);
-char *G_database_ellipse_name(void);
 
 /* put_cellhd.c */
 int G_put_cellhd(const char *, struct Cell_head *);

Modified: grass/trunk/lib/gis/proj3.c
===================================================================
--- grass/trunk/lib/gis/proj3.c	2008-11-26 21:55:54 UTC (rev 34525)
+++ grass/trunk/lib/gis/proj3.c	2008-11-26 23:16:59 UTC (rev 34526)
@@ -2,11 +2,25 @@
 #include <grass/gis.h>
 #include <grass/glocale.h>
 
-static int lookup(const char *, const char *, char *, int);
+static const char *lookup_proj(const char *);
+static const char *lookup_units(const char *);
 static int equal(const char *, const char *);
 static int lower(char);
 
+static int initialized;
+static struct Key_Value *proj_info, *proj_units;
 
+static void init(void)
+{
+    if (G_is_initialized(&initialized))
+	return;
+
+    proj_info = G_get_projinfo();
+    proj_units = G_get_projunits();
+
+    G_initialize_done(&initialized);
+}
+
 /*!
  * \brief database units
  *
@@ -18,10 +32,10 @@
  *  \return char * 
  */
 
-char *G_database_unit_name(int plural)
+const char *G_database_unit_name(int plural)
 {
     int n;
-    static char name[256];
+    const char *name;
 
     switch (n = G_projection()) {
     case PROJECTION_XY:
@@ -31,8 +45,10 @@
 	return G__unit_name(G__projection_units(n), plural);
     }
 
-    if (!lookup(UNIT_FILE, plural ? "units" : "unit", name, sizeof(name)))
-	strcpy(name, plural ? "units" : "unit");
+    name = lookup_units(plural ? "units" : "unit");
+    if (!name)
+	return plural ? "units" : "unit";
+
     return name;
 }
 
@@ -48,10 +64,10 @@
  *  \return char * 
  */
 
-char *G_database_projection_name(void)
+const char *G_database_projection_name(void)
 {
     int n;
-    static char name[256];
+    const char *name;
 
     switch (n = G_projection()) {
     case PROJECTION_XY:
@@ -60,8 +76,11 @@
     case PROJECTION_SP:
 	return G__projection_name(n);
     }
-    if (!lookup(PROJECTION_FILE, "name", name, sizeof(name)))
-	strcpy(name, _("Unknown projection"));
+
+    name = lookup_proj("name");
+    if (!name)
+	return _("Unknown projection");
+
     return name;
 }
 
@@ -79,9 +98,9 @@
 
 double G_database_units_to_meters_factor(void)
 {
-    char *unit;
+    const char *unit;
+    const char *buf;
     double factor;
-    char buf[256];
     int n;
 
     static const struct
@@ -97,7 +116,8 @@
     };
 
     factor = 0.0;
-    if (lookup(UNIT_FILE, "meters", buf, sizeof(buf)))
+    buf = lookup_units("meters");
+    if (buf)
 	sscanf(buf, "%lf", &factor);
     if (factor <= 0.0) {
 	unit = G_database_unit_name(0);
@@ -130,22 +150,22 @@
  *  \return char * 
  */
 
-char *G_database_datum_name(void)
+const char *G_database_datum_name(void)
 {
-    static char name[256], params[256];
-    struct Key_Value *projinfo;
+    const char *name;
+    char buf[256], params[256];
     int datumstatus;
 
-    if (lookup(PROJECTION_FILE, "datum", name, sizeof(name)))
+    name = lookup_proj("datum");
+    if (name)
 	return name;
-    else if ((projinfo = G_get_projinfo()) == NULL)
+    else if (!proj_info)
 	return NULL;
     else
-	datumstatus = G_get_datumparams_from_projinfo(projinfo, name, params);
+	datumstatus = G_get_datumparams_from_projinfo(proj_info, buf, params);
 
-    G_free_key_value(projinfo);
     if (datumstatus == 2)
-	return params;
+	return G_store(params);
     else
 	return NULL;
 }
@@ -159,32 +179,34 @@
  * NULL otherwise
  ***********************************************************************/
 
-char *G_database_ellipse_name(void)
+const char *G_database_ellipse_name(void)
 {
-    static char name[256];
+    const char *name;
 
-    if (!lookup(PROJECTION_FILE, "ellps", name, sizeof(name))) {
+    name = lookup_proj("ellps");
+    if (!name) {
+	char buf[256];
 	double a, es;
 
 	G_get_ellipsoid_parameters(&a, &es);
-	sprintf(name, "a=%.16g es=%.16g", a, es);
+	sprintf(buf, "a=%.16g es=%.16g", a, es);
+	name = G_store(buf);
     }
 
     /* strcpy (name, "Unknown ellipsoid"); */
     return name;
 }
 
-static int lookup(const char *file, const char *key, char *value, int len)
+static const char *lookup_proj(const char *key)
 {
-    char path[GPATH_MAX];
+    init();
+    return G_find_key_value(key, proj_info);
+}
 
-    /*
-       G__file_name (path, "", file, G_mapset());
-       if (access(path,0) == 0)
-       return G_lookup_key_value_from_file(path, key, value, len) == 1;
-     */
-    G__file_name(path, "", file, "PERMANENT");
-    return G_lookup_key_value_from_file(path, key, value, len) == 1;
+static const char *lookup_units(const char *key)
+{
+    init();
+    return G_find_key_value(key, proj_units);
 }
 
 static int equal(const char *a, const char *b)



More information about the grass-commit mailing list