[GRASS-SVN] r32819 - grass/trunk/lib/g3d
svn_grass at osgeo.org
svn_grass at osgeo.org
Sat Aug 16 12:05:36 EDT 2008
Author: glynn
Date: 2008-08-16 12:05:36 -0400 (Sat, 16 Aug 2008)
New Revision: 32819
Modified:
grass/trunk/lib/g3d/g3dcats.c
grass/trunk/lib/g3d/g3dcolor.c
grass/trunk/lib/g3d/g3dhistory.c
grass/trunk/lib/g3d/g3dmask.c
grass/trunk/lib/g3d/g3dopen.c
grass/trunk/lib/g3d/g3drange.c
Log:
Fix handling of grid3 elements
Modified: grass/trunk/lib/g3d/g3dcats.c
===================================================================
--- grass/trunk/lib/g3d/g3dcats.c 2008-08-16 16:03:50 UTC (rev 32818)
+++ grass/trunk/lib/g3d/g3dcats.c 2008-08-16 16:05:36 UTC (rev 32819)
@@ -30,19 +30,10 @@
int i;
const char *descr;
DCELL val1, val2;
- char str1[100], str2[100], buf[200], buf2[200], xname[GNAME_MAX],
- xmapset[GMAPSET_MAX];
+ char str1[100], str2[100];
- if (G__name_is_fully_qualified(name, xname, xmapset)) {
- sprintf(buf, "%s/%s", G3D_DIRECTORY, xname);
- sprintf(buf2, "%s@%s", G3D_CATS_ELEMENT, xmapset); /* == cats at mapset */
- }
- else {
- sprintf(buf, "%s/%s", G3D_DIRECTORY, name);
- sprintf(buf2, "%s", G3D_CATS_ELEMENT);
- }
-
- if (!(fd = G_fopen_new(buf, buf2)))
+ fd = G_fopen_new_misc(G3D_DIRECTORY, G3D_CATS_ELEMENT, name);
+ if (!fd)
return -1;
/* write # cats - note # indicate 3.0 or later */
@@ -86,22 +77,14 @@
/* adapted from G__read_cats */
{
FILE *fd;
- char buff[1024], buf2[200], xname[GNAME_MAX], xmapset[GMAPSET_MAX];
+ char buff[1024];
CELL cat;
DCELL val1, val2;
int old;
long num = -1;
- if (G__name_is_fully_qualified(name, xname, xmapset)) {
- sprintf(buff, "%s/%s", G3D_DIRECTORY, xname);
- sprintf(buf2, "%s@%s", G3D_CATS_ELEMENT, xmapset); /* == cats at mapset */
- }
- else {
- sprintf(buff, "%s/%s", G3D_DIRECTORY, name);
- sprintf(buf2, "%s", G3D_CATS_ELEMENT);
- }
-
- if (!(fd = G_fopen_old(buff, buf2, mapset)))
+ fd = G_fopen_old_misc(G3D_DIRECTORY, G3D_CATS_ELEMENT, name, mapset);
+ if (!fd)
return -2;
/* Read the number of categories */
Modified: grass/trunk/lib/g3d/g3dcolor.c
===================================================================
--- grass/trunk/lib/g3d/g3dcolor.c 2008-08-16 16:03:50 UTC (rev 32818)
+++ grass/trunk/lib/g3d/g3dcolor.c 2008-08-16 16:05:36 UTC (rev 32819)
@@ -8,10 +8,10 @@
#include <rpc/types.h>
#include <rpc/xdr.h>
#include <grass/gis.h>
+#include <grass/glocale.h>
#include "G3d_intern.h"
-static int read_colors(const char *, const char *, const char *,
- struct Colors *);
+static int read_colors(const char *, const char *, struct Colors *);
static int read_new_colors(FILE *, struct Colors *);
static int read_old_colors(FILE *, struct Colors *);
@@ -20,25 +20,7 @@
int G3d_removeColor(const char *name)
/* adapted from G_remove_colr */
{
- char buf[200], secondary[500], buf2[200], xname[GNAME_MAX],
- xmapset[GMAPSET_MAX];
-
- if (G__name_is_fully_qualified(name, xname, xmapset)) {
- sprintf(buf, "%s/%s", G3D_DIRECTORY, xname);
- sprintf(buf2, "%s@%s", G3D_COLOR_ELEMENT, xmapset); /* == color at mapset */
- }
- else {
- sprintf(buf, "%s/%s", G3D_DIRECTORY, name);
- sprintf(buf2, "%s", G3D_COLOR_ELEMENT);
- }
-
- G_remove(buf, buf2);
-
- sprintf(secondary, "%s/%s/%s",
- G3D_DIRECTORY, G3D_COLOR2_DIRECTORY, G_mapset());
- G_remove(secondary, name);
-
- return 0;
+ return G_remove_misc(G3D_DIRECTORY, G3D_COLOR_ELEMENT, name);
}
/*---------------------------------------------------------------------------*/
@@ -47,37 +29,15 @@
G3d_readColors(const char *name, const char *mapset, struct Colors *colors)
/* adapted from G_read_colors */
{
- char buf[512], buf2[200];
const char *err;
- char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
struct FPRange drange;
DCELL dmin, dmax;
G_init_colors(colors);
- if (G__name_is_fully_qualified(name, xname, xmapset)) {
- if (strcmp(xmapset, mapset) != 0)
- return -1;
- name = xname;
- }
-
- sprintf(buf, "%s/%s/%s", G3D_DIRECTORY, G3D_COLOR2_DIRECTORY, mapset);
- if (read_colors(buf, name, G_mapset(), colors) >= 0)
- return 1;
-
G_mark_colors_as_fp(colors);
- /* now look for the regular color table */
- /*if (G__name_is_fully_qualified (name, xname, xmapset)) {
- sprintf (buf, "%s/%s", G3D_DIRECTORY, xname);
- sprintf (buf2, "%s@%s", G3D_COLOR_ELEMENT, xmapset); // == color at mapset
- //} else { */
- sprintf(buf, "%s/%s", G3D_DIRECTORY, name);
- sprintf(buf2, "%s", G3D_COLOR_ELEMENT);
- /*//}
- */
-
- switch (read_colors(buf, buf2, mapset, colors)) {
+ switch (read_colors(name, mapset, colors)) {
case -2:
if (G3d_readRange(name, mapset, &drange) >= 0) {
G_get_fp_range_min_max(&drange, &dmin, &dmax);
@@ -98,14 +58,15 @@
return -1;
}
-static int read_colors(const char *element, const char *name,
- const char *mapset, struct Colors *colors)
+static int read_colors(const char *name, const char *mapset,
+ struct Colors *colors)
{
FILE *fd;
int stat;
char buf[1024];
- if (!(fd = G_fopen_old(element, name, mapset)))
+ fd = G_fopen_old_misc(G3D_DIRECTORY, G3D_COLOR_ELEMENT, name, mapset);
+ if (!fd)
return -2;
/*
@@ -364,46 +325,21 @@
G3d_writeColors(const char *name, const char *mapset, struct Colors *colors)
/* adapted from G_write_colors */
{
- char element[512], buf[512], buf2[200];
- char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
FILE *fd;
int stat;
- if (G__name_is_fully_qualified(name, xname, xmapset)) {
- if (strcmp(xmapset, mapset) != 0)
- return -1;
- name = xname;
+ if (strcmp(mapset, G_mapset()) != 0) {
+ G_warning(_("mapset <%s> is not the current mapset"), mapset);
+ return -1;
}
- /*
- * if mapset is current mapset, remove colr2 file (created by pre 3.0 grass)
- * and then write original color table
- * else write secondary color table
- */
+ fd = G_fopen_new_misc(G3D_DIRECTORY, G3D_COLOR_ELEMENT, name);
+ if (!fd)
+ return -1;
- sprintf(element, "%s/%s/%s", G3D_DIRECTORY, G3D_COLOR2_DIRECTORY, mapset);
- if (strcmp(mapset, G_mapset()) == 0) {
- G_remove(element, name); /* get rid of existing colr2, if any */
-
- if (G__name_is_fully_qualified(name, xname, xmapset)) {
- sprintf(buf, "%s/%s", G3D_DIRECTORY, xname);
- sprintf(buf2, "%s@%s", G3D_COLOR_ELEMENT, xmapset); /* == color at mapset */
- }
- else {
- sprintf(buf, "%s/%s", G3D_DIRECTORY, name);
- sprintf(buf2, "%s", G3D_COLOR_ELEMENT);
- }
-
- if (!(fd = G_fopen_new(buf, buf2)))
- return -1;
- }
- else {
- if (!(fd = G_fopen_new(element, name)))
- return -1;
- }
-
stat = G__write_colors(fd, colors);
fclose(fd);
+
return stat;
}
Modified: grass/trunk/lib/g3d/g3dhistory.c
===================================================================
--- grass/trunk/lib/g3d/g3dhistory.c 2008-08-16 16:03:50 UTC (rev 32818)
+++ grass/trunk/lib/g3d/g3dhistory.c 2008-08-16 16:05:36 UTC (rev 32819)
@@ -60,29 +60,17 @@
* \return int
*/
-int G3d_readHistory(const char *name, const char *mapset,
- struct History *hist)
+int G3d_readHistory(const char *name, const char *mapset, struct History *hist)
/* This function is adapted from G_read_history */
{
FILE *fd;
- char buff[1024], buf2[200], xname[GNAME_MAX], xmapset[GMAPSET_MAX];
G_zero(hist, sizeof(struct History));
- /*this construct takes care of the correct history file path */
- if (G__name_is_fully_qualified(name, xname, xmapset)) {
- sprintf(buff, "%s/%s", G3D_DIRECTORY, xname);
- sprintf(buf2, "%s@%s", G3D_HISTORY_ELEMENT, xmapset); /* == hist at mapset */
- }
- else {
- sprintf(buff, "%s/%s", G3D_DIRECTORY, name);
- sprintf(buf2, "%s", G3D_HISTORY_ELEMENT);
- }
-
- if (!(fd = G_fopen_old(buff, buf2, mapset)))
+ fd = G_fopen_old_misc(G3D_DIRECTORY, G3D_HISTORY_ELEMENT, name, mapset);
+ if (!fd)
return -2;
-
if (!G_getl(hist->mapid, sizeof(hist->mapid), fd)) {
SimpleErrorMessage(fd, name, mapset);
return -1;
@@ -164,18 +152,9 @@
{
FILE *fd;
int i;
- char buf[200], buf2[200], xname[GNAME_MAX], xmapset[GMAPSET_MAX];
- if (G__name_is_fully_qualified(name, xname, xmapset)) {
- sprintf(buf, "%s/%s", G3D_DIRECTORY, xname);
- sprintf(buf2, "%s@%s", G3D_HISTORY_ELEMENT, xmapset); /* == hist at mapset */
- }
- else {
- sprintf(buf, "%s/%s", G3D_DIRECTORY, name);
- sprintf(buf2, "%s", G3D_HISTORY_ELEMENT);
- }
-
- if (!(fd = G_fopen_new(buf, buf2)))
+ fd = G_fopen_new_misc(G3D_DIRECTORY, G3D_HISTORY_ELEMENT, name);
+ if (!fd)
return -1;
fprintf(fd, "%s\n", hist->mapid);
Modified: grass/trunk/lib/g3d/g3dmask.c
===================================================================
--- grass/trunk/lib/g3d/g3dmask.c 2008-08-16 16:03:50 UTC (rev 32818)
+++ grass/trunk/lib/g3d/g3dmask.c 2008-08-16 16:05:36 UTC (rev 32819)
@@ -61,19 +61,16 @@
* \return int
*/
-int G3d_maskFileExists()
+int G3d_maskFileExists(void)
{
- char buf[200];
-
- sprintf(buf, "%s/%s", G3D_DIRECTORY, G3D_MASK_MAP);
- return (G_find_file(buf, G3D_CELL_ELEMENT, G_mapset()) != NULL);
+ return G_find_file_misc(G3D_DIRECTORY, G3D_CELL_ELEMENT, G3D_MASK_MAP, G_mapset()) != NULL;
}
/*--------------------------------------------------------------------------*/
static int maskOpenOldCacheDefault = G3D_USE_CACHE_DEFAULT;
-int G3d_maskOpenOld()
+int G3d_maskOpenOld(void)
{
G3D_Region region;
Modified: grass/trunk/lib/g3d/g3dopen.c
===================================================================
--- grass/trunk/lib/g3d/g3dopen.c 2008-08-16 16:03:50 UTC (rev 32818)
+++ grass/trunk/lib/g3d/g3dopen.c 2008-08-16 16:05:36 UTC (rev 32819)
@@ -12,7 +12,7 @@
void *G3d_openCellOldNoHeader(const char *name, const char *mapset)
{
G3D_Map *map;
- char buf[200], buf2[200], xname[GNAME_MAX], xmapset[GMAPSET_MAX];
+ char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
G3d_initDefaults();
@@ -27,20 +27,12 @@
return (void *)NULL;
}
- if (G__name_is_fully_qualified(name, xname, xmapset)) {
- sprintf(buf, "%s/%s", G3D_DIRECTORY, xname);
- sprintf(buf2, "%s@%s", G3D_CELL_ELEMENT, xmapset); /* == cell at mapset */
- map->fileName = G_store(xname);
- }
- else {
- sprintf(buf, "%s/%s", G3D_DIRECTORY, name);
- sprintf(buf2, "%s", G3D_CELL_ELEMENT);
- map->fileName = G_store(name);
- }
+ G__unqualified_name(name, mapset, xname, xmapset);
- map->mapset = G_store(mapset);
+ map->fileName = G_store(xname);
+ map->mapset = G_store(xmapset);
- map->data_fd = G_open_old(buf, buf2, mapset);
+ map->data_fd = G_open_old_misc(G3D_DIRECTORY, G3D_CELL_ELEMENT, xname, xmapset);
if (map->data_fd < 0) {
G3d_error(_("G3d_openCellOldNoHeader: error in G_open_old"));
return (void *)NULL;
@@ -233,12 +225,14 @@
return (void *)NULL;
}
- if (G__name_is_fully_qualified(name, xname, xmapset))
- map->fileName = G_store(xname);
- else
- map->fileName = G_store(name);
- map->mapset = G_store(G_mapset());
+ if (G__unqualified_name(name, G_mapset(), xname, xmapset) < 0) {
+ G_warning(_("map <%s> is not in the current mapset"), name);
+ return (void *)NULL;
+ }
+ map->fileName = G_store(xname);
+ map->mapset = G_store(xmapset);
+
map->tempName = G_tempfile();
map->data_fd = open(map->tempName, O_RDWR | O_CREAT | O_TRUNC, 0666);
if (map->data_fd < 0) {
Modified: grass/trunk/lib/g3d/g3drange.c
===================================================================
--- grass/trunk/lib/g3d/g3drange.c 2008-08-16 16:03:50 UTC (rev 32818)
+++ grass/trunk/lib/g3d/g3drange.c 2008-08-16 16:05:36 UTC (rev 32819)
@@ -5,6 +5,7 @@
#include <rpc/types.h>
#include <rpc/xdr.h>
#include <grass/gis.h>
+#include <grass/glocale.h>
#include "G3d_intern.h"
/*---------------------------------------------------------------------------*/
@@ -58,9 +59,6 @@
/* adapted from G_read_fp_range */
{
int fd;
- char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
- char buf[GNAME_MAX + sizeof(G3D_DIRECTORY) + 2],
- buf2[GMAPSET_MAX + sizeof(G3D_RANGE_ELEMENT) + 2];
char xdr_buf[100];
DCELL dcell1, dcell2;
XDR xdr_str;
@@ -69,42 +67,32 @@
fd = -1;
- if (G__name_is_fully_qualified(name, xname, xmapset)) {
- sprintf(buf, "%s/%s", G3D_DIRECTORY, xname);
- sprintf(buf2, "%s@%s", G3D_RANGE_ELEMENT, xmapset); /* == range at mapset */
+ fd = G_open_old_misc(G3D_DIRECTORY, G3D_RANGE_ELEMENT, name, mapset);
+ if (fd < 0) {
+ G_warning(_("Unable to open range file for [%s in %s]"), name, mapset);
+ return -1;
}
- else {
- sprintf(buf, "%s/%s", G3D_DIRECTORY, name);
- sprintf(buf2, "%s", G3D_RANGE_ELEMENT);
+
+ if (read(fd, xdr_buf, 2 * G3D_XDR_DOUBLE_LENGTH) != 2 * G3D_XDR_DOUBLE_LENGTH) {
+ close(fd);
+ G_warning(_("Error reading range file for [%s in %s]"), name, mapset);
+ return 2;
}
- if (G_find_file2(buf, buf2, mapset)) {
- fd = G_open_old(buf, buf2, mapset);
- if (fd < 0)
- goto error;
+ xdrmem_create(&xdr_str, xdr_buf, (u_int) G3D_XDR_DOUBLE_LENGTH * 2,
+ XDR_DECODE);
- if (read(fd, xdr_buf, 2 * G3D_XDR_DOUBLE_LENGTH) !=
- 2 * G3D_XDR_DOUBLE_LENGTH)
- return 2;
-
- xdrmem_create(&xdr_str, xdr_buf, (u_int) G3D_XDR_DOUBLE_LENGTH * 2,
- XDR_DECODE);
-
- /* if the f_range file exists, but empty */
- if (!xdr_double(&xdr_str, &dcell1) || !xdr_double(&xdr_str, &dcell2))
- goto error;
-
- G_update_fp_range(dcell1, drange);
- G_update_fp_range(dcell2, drange);
+ /* if the f_range file exists, but empty */
+ if (!xdr_double(&xdr_str, &dcell1) || !xdr_double(&xdr_str, &dcell2)) {
close(fd);
- return 1;
+ G_warning(_("Error reading range file for [%s in %s]"), name, mapset);
+ return -1;
}
- error:
- if (fd > 0)
- close(fd);
- G_warning("can't read range file for [%s in %s]", name, mapset);
- return -1;
+ G_update_fp_range(dcell1, drange);
+ G_update_fp_range(dcell2, drange);
+ close(fd);
+ return 1;
}
/*---------------------------------------------------------------------------*/
@@ -156,26 +144,16 @@
static int writeRange(const char *name, struct FPRange *range)
/* adapted from G_write_fp_range */
{
- char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
- char buf[GNAME_MAX + sizeof(G3D_DIRECTORY) + 2],
- buf2[GMAPSET_MAX + sizeof(G3D_RANGE_ELEMENT) + 2];
char xdr_buf[100];
int fd;
XDR xdr_str;
- if (G__name_is_fully_qualified(name, xname, xmapset)) {
- sprintf(buf, "%s/%s", G3D_DIRECTORY, xname);
- sprintf(buf2, "%s@%s", G3D_RANGE_ELEMENT, xmapset); /* == range at mapset */
+ fd = G_open_new_misc(G3D_DIRECTORY, G3D_RANGE_ELEMENT, name);
+ if (fd < 0) {
+ G_warning(_("Unable to open range file for <%s>"), name);
+ return -1;
}
- else {
- sprintf(buf, "%s/%s", G3D_DIRECTORY, name);
- sprintf(buf2, "%s", G3D_RANGE_ELEMENT);
- }
- fd = G_open_new(buf, buf2);
- if (fd < 0)
- goto error;
-
if (range->first_time) {
/* if range hasn't been updated, write empty file meaning NULLs */
close(fd);
@@ -190,14 +168,16 @@
if (!xdr_double(&xdr_str, &(range->max)))
goto error;
- write(fd, xdr_buf, G3D_XDR_DOUBLE_LENGTH * 2);
+ if (write(fd, xdr_buf, G3D_XDR_DOUBLE_LENGTH * 2) != G3D_XDR_DOUBLE_LENGTH * 2)
+ goto error;
+
close(fd);
return 0;
error:
- G_remove(buf, buf2); /* remove the old file with this name */
- sprintf(buf, "can't write range file for [%s in %s]", name, G_mapset());
- G_warning(buf);
+ close(fd);
+ G_remove_misc(G3D_DIRECTORY, G3D_RANGE_ELEMENT, name); /* remove the old file with this name */
+ G_warning("can't write range file for [%s in %s]", name, G_mapset());
return -1;
}
More information about the grass-commit
mailing list