[GRASS-SVN] r40739 - in grass/trunk: include lib/raster
raster/r.info
svn_grass at osgeo.org
svn_grass at osgeo.org
Fri Jan 29 15:04:19 EST 2010
Author: glynn
Date: 2010-01-29 15:04:18 -0500 (Fri, 29 Jan 2010)
New Revision: 40739
Modified:
grass/trunk/include/rasterdefs.h
grass/trunk/lib/raster/raster_metadata.c
grass/trunk/raster/r.info/main.c
Log:
Change interface of Rast_{read,write}_{units,vdatum}
read returns dynamically-allocated string rather than filling buffer
write generates fatal errors rather than returning status
Modified: grass/trunk/include/rasterdefs.h
===================================================================
--- grass/trunk/include/rasterdefs.h 2010-01-29 20:02:42 UTC (rev 40738)
+++ grass/trunk/include/rasterdefs.h 2010-01-29 20:04:18 UTC (rev 40739)
@@ -496,10 +496,10 @@
DCELL Rast_get_d_value(const void *, RASTER_MAP_TYPE);
/* raster_metadata.c */
-int Rast_read_units(const char *, const char *, char *);
-int Rast_read_vdatum(const char *, const char *, char *);
-int Rast_write_units(const char *, const char *);
-int Rast_write_vdatum(const char *, const char *);
+char *Rast_read_units(const char *, const char *);
+char *Rast_read_vdatum(const char *, const char *);
+void Rast_write_units(const char *, const char *);
+void Rast_write_vdatum(const char *, const char *);
/* reclass.c */
int Rast_is_reclass(const char *, const char *, char *, char *);
Modified: grass/trunk/lib/raster/raster_metadata.c
===================================================================
--- grass/trunk/lib/raster/raster_metadata.c 2010-01-29 20:02:42 UTC (rev 40738)
+++ grass/trunk/lib/raster/raster_metadata.c 2010-01-29 20:04:18 UTC (rev 40739)
@@ -19,8 +19,8 @@
#include <grass/raster.h>
#include <grass/glocale.h>
-static int misc_read_line(const char *, const char *, const char *, char *);
-static int misc_write_line(const char *, const char *, const char *);
+static char *misc_read_line(const char *, const char *, const char *);
+static void misc_write_line(const char *, const char *, const char *);
/*!
* \brief Get a raster map's units metadata string
@@ -29,14 +29,13 @@
*
* \param name raster map name
* \param mapset mapset name
- * \param str string to be populated with data
*
- * \return 0 on success
- * \return -1, EOF (fclose() result) on error
+ * \return string representing units on success
+ * \return NULL on error
*/
-int Rast_read_units(const char *name, const char *mapset, char *str)
+char *Rast_read_units(const char *name, const char *mapset)
{
- return misc_read_line("units", name, mapset, str);
+ return misc_read_line("units", name, mapset);
}
/*!
@@ -46,13 +45,10 @@
*
* \param name raster map name
* \param str string containing data to be written
- *
- * \return 0 on success
- * \return -1, EOF (fclose() result) on error
*/
-int Rast_write_units(const char *name, const char *str)
+void Rast_write_units(const char *name, const char *str)
{
- return misc_write_line("units", name, str);
+ misc_write_line("units", name, str);
}
/*!
@@ -62,14 +58,13 @@
*
* \param name raster map name
* \param mapset mapset name
- * \param str string to be populated with data
*
- * \return 0 on success
- * \return -1, EOF (fclose() result) on error
+ * \return string representing vertical datum on success
+ * \return NULL on error
*/
-int Rast_read_vdatum(const char *name, const char *mapset, char *str)
+char *Rast_read_vdatum(const char *name, const char *mapset)
{
- return misc_read_line("vertical_datum", name, mapset, str);
+ return misc_read_line("vertical_datum", name, mapset);
}
@@ -80,13 +75,10 @@
*
* \param name raster map name
* \param str string containing data to be written
- *
- * \return 0 on success
- * \return -1, EOF (fclose() result) on error
*/
-int Rast_write_vdatum(const char *name, const char *str)
+void Rast_write_vdatum(const char *name, const char *str)
{
- return misc_write_line("vertical_datum", name, str);
+ misc_write_line("vertical_datum", name, str);
}
@@ -99,34 +91,36 @@
* \param name
* \param mapset
* \param *str string to be populated with data
- * \return 0 on success
- * \return -1, EOF (fclose() result) on error
+ * \return dynamically-allocated string on success
+ * \return NULL on error
*/
-int misc_read_line(const char *elem, const char *name,
- const char *mapset, char *str)
+static char *misc_read_line(const char *elem,
+ const char *name, const char *mapset)
{
- FILE *fd;
char buff[GNAME_MAX];
+ FILE *fp;
buff[0] = '\0';
if (G_find_file2_misc("cell_misc", elem, name, mapset) == NULL)
- return -1;
+ return NULL;
- fd = G_fopen_old_misc("cell_misc", elem, name, mapset);
- if (!fd) {
- G_warning(_("Unable to read %s for raster map <%s@%s>"),
+ fp = G_fopen_old_misc("cell_misc", elem, name, mapset);
+ if (!fp) {
+ G_warning(_("Unable to read <%s> for raster map <%s@%s>"),
elem, name, mapset);
- return -1;
+ return NULL;
}
- if (G_getl2(buff, sizeof(buff) - 1, fd) == 0) {
+ if (G_getl2(buff, sizeof(buff) - 1, fp) == 0) {
/* file is empty */
- return fclose(fd);
+ *buff = '\0';
}
- strcpy(str, buff);
+ if (fclose(fp) != 0)
+ G_fatal_error(_("Error closing <%s> metadata file for raster map <%s@%s>"),
+ elem, name, mapset);
- return fclose(fd);
+ return *buff ? G_store(buff) : NULL;
}
@@ -139,21 +133,20 @@
* \param element metadata component filename
* \param name
* \param *str string containing data to be written
- * \return 0 on success
- * \return -1, EOF (fclose() result) on error
*/
-int misc_write_line(const char *elem, const char *name, const char *str)
+static void misc_write_line(const char *elem, const char *name, const char *str)
{
- FILE *fd;
+ FILE *fp;
- fd = G_fopen_new_misc("cell_misc", elem, name);
- if (fd == NULL) {
- G_warning(_("Unable to create %s metadata file for raster map <%s@%s>"),
- elem, name, G_mapset());
- return -1;
- }
+ fp = G_fopen_new_misc("cell_misc", elem, name);
+ if (!fp)
+ G_fatal_error(_("Unable to create <%s> metadata file for raster map <%s@%s>"),
+ elem, name, G_mapset());
- fprintf(fd, "%s", str);
+ fprintf(fp, "%s\n", str);
- return fclose(fd);
+ if (fclose(fp) != 0)
+ G_fatal_error(_("Error closing <%s> metadata file for raster map <%s@%s>"),
+ elem, name, G_mapset());
}
+
Modified: grass/trunk/raster/r.info/main.c
===================================================================
--- grass/trunk/raster/r.info/main.c 2010-01-29 20:02:42 UTC (rev 40738)
+++ grass/trunk/raster/r.info/main.c 2010-01-29 20:04:18 UTC (rev 40739)
@@ -42,7 +42,7 @@
const char *name, *mapset;
char tmp1[100], tmp2[100], tmp3[100];
char timebuff[256];
- char units[GNAME_MAX], vdatum[GNAME_MAX];
+ char *units, *vdatum;
int i;
CELL mincat = 0, maxcat = 0, cat;
double zmin, zmax; /* min and max data values */
@@ -125,11 +125,10 @@
is_reclass = Rast_get_reclass(name, "", &reclass);
data_type = Rast_map_type(name, "");
- if (Rast_read_units(name, "", units) != 0)
- units[0] = '\0';
- if (Rast_read_vdatum(name, "", vdatum) != 0)
- vdatum[0] = '\0';
+ units = Rast_read_units(name, "");
+ vdatum = Rast_read_vdatum(name, "");
+
/*Check the Timestamp */
time_ok = G_read_raster_timestamp(name, "", &ts) > 0;
/*Check for valid entries, show none if no timestamp available */
@@ -188,10 +187,9 @@
/* For now hide these unless they exist to keep the noise low. In
* future when the two are used more widely they can be printed
* along with the standard set. */
- if (units[0] || vdatum[0]) {
+ if (units && vdatum)
compose_line(out, " Data Units: %-20.20s Vertical datum: %s",
units, vdatum);
- }
{
compose_line(out, " Rows: %d", cellhd.rows);
@@ -397,9 +395,9 @@
}
if (uflag->answer)
- fprintf(out, "units=%s\n", units);
+ fprintf(out, "units=%s\n", units ? units : "(none)");
if (dflag->answer)
- fprintf(out, "vertical_datum=%s\n", vdatum);
+ fprintf(out, "vertical_datum=%s\n", vdatum ? vdatum : "(none)");
if (hflag->answer) {
if (hist_ok) {
More information about the grass-commit
mailing list