Ver código fonte

libraster: use grass_int64 for count of non-null cells in R_stats

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@72268 15284696-431f-4ddb-bdfa-cd5b030d7da7
Markus Metz 7 anos atrás
pai
commit
7cb400001e
2 arquivos alterados com 10 adições e 7 exclusões
  1. 1 1
      include/raster.h
  2. 9 6
      lib/raster/range.c

+ 1 - 1
include/raster.h

@@ -219,7 +219,7 @@ struct R_stats
 {
 {
     DCELL sum;
     DCELL sum;
     DCELL sumsq;
     DCELL sumsq;
-    off_t count;
+    grass_int64 count;
 };
 };
 
 
 struct Range
 struct Range

+ 9 - 6
lib/raster/range.c

@@ -269,7 +269,7 @@ int Rast_read_rstats(const char *name, const char *mapset,
     unsigned char cc[8];
     unsigned char cc[8];
     char nbytes;
     char nbytes;
     int i;
     int i;
-    off_t count;
+    grass_int64 count;
 
 
     Rast_init();
     Rast_init();
     init_rstats(rstats);
     init_rstats(rstats);
@@ -312,7 +312,11 @@ int Rast_read_rstats(const char *name, const char *mapset,
 	return -1;
 	return -1;
     }
     }
 
 
-    if (nbytes < 1 || nbytes > sizeof(off_t)) {
+    count = 0;
+    if (nbytes == 0)
+	return 1;
+
+    if (nbytes < 1 || nbytes > sizeof(grass_int64)) {
 	close(fd);
 	close(fd);
 	G_debug(1, "Invalid byte count in stats file for <%s>",
 	G_debug(1, "Invalid byte count in stats file for <%s>",
 		  G_fully_qualified_name(name, mapset));
 		  G_fully_qualified_name(name, mapset));
@@ -326,7 +330,6 @@ int Rast_read_rstats(const char *name, const char *mapset,
 	return -1;
 	return -1;
     }
     }
 
 
-    count = 0;
     /* copy byte by byte */
     /* copy byte by byte */
     for (i = nbytes - 1; i >= 0; i--) {
     for (i = nbytes - 1; i >= 0; i--) {
 	count = (count << 8);
 	count = (count << 8);
@@ -437,7 +440,7 @@ void Rast_write_rstats(const char *name, const struct R_stats *rstats)
     unsigned char cc[8];
     unsigned char cc[8];
     char nbytes;
     char nbytes;
     int i;
     int i;
-    off_t count;
+    grass_int64 count;
 
 
     Rast_init();
     Rast_init();
 
 
@@ -465,7 +468,7 @@ void Rast_write_rstats(const char *name, const struct R_stats *rstats)
     count = rstats->count;
     count = rstats->count;
     nbytes = 0;
     nbytes = 0;
     /* copy byte by byte */
     /* copy byte by byte */
-    for (i = 0; i < sizeof(off_t); i++) {
+    for (i = 0; i < sizeof(grass_int64); i++) {
 	cc[i] = count & 0xff;
 	cc[i] = count & 0xff;
 	count >>= 8;
 	count >>= 8;
 	if (cc[i])
 	if (cc[i])
@@ -479,7 +482,7 @@ void Rast_write_rstats(const char *name, const struct R_stats *rstats)
 	G_fatal_error(_("Unable to write stats file for <%s>"), name);
 	G_fatal_error(_("Unable to write stats file for <%s>"), name);
     }
     }
 
 
-    if (write(fd, cc, nbytes) != nbytes) {
+    if (nbytes > 0 && write(fd, cc, nbytes) != nbytes) {
 	G_remove_misc("cell_misc", "stats", name);
 	G_remove_misc("cell_misc", "stats", name);
 	G_fatal_error(_("Unable to write stats file for <%s>"), name);
 	G_fatal_error(_("Unable to write stats file for <%s>"), name);
     }
     }