[GRASS-SVN] r73508 - grass-addons/grass7/raster/r.hydrodem
svn_grass at osgeo.org
svn_grass at osgeo.org
Tue Oct 9 06:01:42 PDT 2018
Author: mmetz
Date: 2018-10-09 06:01:42 -0700 (Tue, 09 Oct 2018)
New Revision: 73508
Modified:
grass-addons/grass7/raster/r.hydrodem/bseg.c
grass-addons/grass7/raster/r.hydrodem/cseg.c
grass-addons/grass7/raster/r.hydrodem/dseg.c
grass-addons/grass7/raster/r.hydrodem/seg.c
Log:
r.hydrodem: use Segment_[open|close]()
Modified: grass-addons/grass7/raster/r.hydrodem/bseg.c
===================================================================
--- grass-addons/grass7/raster/r.hydrodem/bseg.c 2018-10-09 12:35:12 UTC (rev 73507)
+++ grass-addons/grass7/raster/r.hydrodem/bseg.c 2018-10-09 13:01:42 UTC (rev 73508)
@@ -8,7 +8,6 @@
{
char *filename;
int errflag;
- int fd;
bseg->filename = NULL;
bseg->fd = -1;
@@ -16,52 +15,43 @@
bseg->mapset = NULL;
filename = G_tempfile();
- if (-1 == (fd = creat(filename, 0666))) {
- G_warning(_("Unable to create segment file"));
- return -2;
- }
- if (0 > (errflag = Segment_format(fd, Rast_window_rows(),
- Rast_window_cols(), srows, scols,
- sizeof(char)))) {
- close(fd);
- unlink(filename);
+ if (0 > (errflag = Segment_open(&(bseg->seg), filename, Rast_window_rows(),
+ Rast_window_cols(), srows, scols,
+ sizeof(char), nsegs_in_memory))) {
if (errflag == -1) {
- G_warning(_("Unable to write segment file"));
+ G_warning(_("File name is invalid"));
return -1;
}
- else {
- G_warning(_("Illegal configuration parameter(s)"));
+ else if (errflag == -2) {
+ G_warning(_("File write error"));
+ return -2;
+ }
+ else if (errflag == -3) {
+ G_warning(_("Illegal parameters are passed"));
return -3;
}
- }
- close(fd);
- if (-1 == (fd = open(filename, 2))) {
- unlink(filename);
- G_warning(_("Unable to re-open segment file"));
- return -4;
- }
- if (0 > (errflag = Segment_init(&(bseg->seg), fd, nsegs_in_memory))) {
- close(fd);
- unlink(filename);
- if (errflag == -1) {
- G_warning(_("Unable to read segment file"));
+ else if (errflag == -4) {
+ G_warning(_("File could not be re-opened"));
+ return -4;
+ }
+ else if (errflag == -5) {
+ G_warning(_("Prepared file could not be read"));
return -5;
}
- else {
+ else if (errflag == -6) {
G_warning(_("Out of memory"));
return -6;
}
}
+
bseg->filename = filename;
- bseg->fd = fd;
+
return 0;
}
int bseg_close(BSEG *bseg)
{
- Segment_release(&(bseg->seg));
- close(bseg->fd);
- unlink(bseg->filename);
+ Segment_close(&(bseg->seg));
if (bseg->name) {
G_free(bseg->name);
bseg->name = NULL;
@@ -103,8 +93,8 @@
int bseg_read_raster(BSEG *bseg, char *map_name, char *mapset)
{
- int row, nrows;
- int col, ncols;
+ int row, rows;
+ int col, cols;
int map_fd;
CELL *buffer;
char cbuf;
@@ -113,12 +103,12 @@
bseg->mapset = NULL;
map_fd = Rast_open_old(map_name, mapset);
- nrows = Rast_window_rows();
- ncols = Rast_window_cols();
+ rows = Rast_window_rows();
+ cols = Rast_window_cols();
buffer = Rast_allocate_c_buf();
- for (row = 0; row < nrows; row++) {
+ for (row = 0; row < rows; row++) {
Rast_get_c_row(map_fd, buffer, row);
- for (col = ncols; col >= 0; col--) {
+ for (col = cols; col >= 0; col--) {
cbuf = (char) buffer[col];
bseg_put(bseg, &cbuf, row, col);
}
@@ -136,24 +126,24 @@
int bseg_write_raster(BSEG *bseg, char *map_name)
{
int map_fd;
- int row, nrows;
- int col, ncols;
+ int row, rows;
+ int col, cols;
CELL *buffer;
char value;
map_fd = Rast_open_c_new(map_name);
- nrows = Rast_window_rows();
- ncols = Rast_window_cols();
+ rows = Rast_window_rows();
+ cols = Rast_window_cols();
buffer = Rast_allocate_c_buf();
- for (row = 0; row < nrows; row++) {
- G_percent(row, nrows, 1);
- for (col = 0; col < ncols; col++) {
+ for (row = 0; row < rows; row++) {
+ G_percent(row, rows, 1);
+ for (col = 0; col < cols; col++) {
bseg_get(bseg, &value, row, col);
buffer[col] = value;
}
Rast_put_row(map_fd, buffer, CELL_TYPE);
}
- G_percent(row, nrows, 1); /* finish it */
+ G_percent(row, rows, 1); /* finish it */
G_free(buffer);
Rast_close(map_fd);
return 0;
Modified: grass-addons/grass7/raster/r.hydrodem/cseg.c
===================================================================
--- grass-addons/grass7/raster/r.hydrodem/cseg.c 2018-10-09 12:35:12 UTC (rev 73507)
+++ grass-addons/grass7/raster/r.hydrodem/cseg.c 2018-10-09 13:01:42 UTC (rev 73508)
@@ -8,7 +8,6 @@
{
char *filename;
int errflag;
- int fd;
cseg->filename = NULL;
cseg->fd = -1;
@@ -16,53 +15,43 @@
cseg->mapset = NULL;
filename = G_tempfile();
- if (-1 == (fd = creat(filename, 0666))) {
- G_warning(_("Unable to create segment file"));
- return -2;
- }
- if (0 >
- (errflag =
- Segment_format(fd, Rast_window_rows(), Rast_window_cols(), srows, scols,
- sizeof(CELL)))) {
- close(fd);
- unlink(filename);
+ if (0 > (errflag = Segment_open(&(cseg->seg), filename, Rast_window_rows(),
+ Rast_window_cols(), srows, scols,
+ sizeof(CELL), nsegs_in_memory))) {
if (errflag == -1) {
- G_warning(_("Unable to write segment file"));
+ G_warning(_("File name is invalid"));
return -1;
}
- else {
- G_warning(_("Illegal configuration parameter(s)"));
+ else if (errflag == -2) {
+ G_warning(_("File write error"));
+ return -2;
+ }
+ else if (errflag == -3) {
+ G_warning(_("Illegal parameters are passed"));
return -3;
}
- }
- close(fd);
- if (-1 == (fd = open(filename, 2))) {
- unlink(filename);
- G_warning(_("Unable to re-open segment file"));
- return -4;
- }
- if (0 > (errflag = Segment_init(&(cseg->seg), fd, nsegs_in_memory))) {
- close(fd);
- unlink(filename);
- if (errflag == -1) {
- G_warning(_("Unable to read segment file"));
+ else if (errflag == -4) {
+ G_warning(_("File could not be re-opened"));
+ return -4;
+ }
+ else if (errflag == -5) {
+ G_warning(_("Prepared file could not be read"));
return -5;
}
- else {
+ else if (errflag == -6) {
G_warning(_("Out of memory"));
return -6;
}
}
+
cseg->filename = filename;
- cseg->fd = fd;
+
return 0;
}
int cseg_close(CSEG *cseg)
{
- Segment_release(&(cseg->seg));
- close(cseg->fd);
- unlink(cseg->filename);
+ Segment_close(&(cseg->seg));
if (cseg->name) {
G_free(cseg->name);
cseg->name = NULL;
@@ -103,7 +92,7 @@
int cseg_read_raster(CSEG *cseg, char *map_name, char *mapset)
{
- int row, nrows;
+ int row, rows;
int map_fd;
CELL *buffer;
@@ -111,9 +100,9 @@
cseg->mapset = NULL;
map_fd = Rast_open_old(map_name, mapset);
- nrows = Rast_window_rows();
+ rows = Rast_window_rows();
buffer = Rast_allocate_c_buf();
- for (row = 0; row < nrows; row++) {
+ for (row = 0; row < rows; row++) {
Rast_get_c_row(map_fd, buffer, row);
if (Segment_put_row(&(cseg->seg), buffer, row) < 0) {
G_free(buffer);
@@ -136,19 +125,19 @@
int cseg_write_raster(CSEG *cseg, char *map_name)
{
int map_fd;
- int row, nrows;
+ int row, rows;
CELL *buffer;
map_fd = Rast_open_c_new(map_name);
- nrows = Rast_window_rows();
+ rows = Rast_window_rows();
buffer = Rast_allocate_c_buf();
Segment_flush(&(cseg->seg));
- for (row = 0; row < nrows; row++) {
- G_percent(row, nrows, 1);
+ for (row = 0; row < rows; row++) {
+ G_percent(row, rows, 1);
Segment_get_row(&(cseg->seg), buffer, row);
Rast_put_row(map_fd, buffer, CELL_TYPE);
}
- G_percent(row, nrows, 1); /* finish it */
+ G_percent(row, rows, 1); /* finish it */
G_free(buffer);
Rast_close(map_fd);
return 0;
Modified: grass-addons/grass7/raster/r.hydrodem/dseg.c
===================================================================
--- grass-addons/grass7/raster/r.hydrodem/dseg.c 2018-10-09 12:35:12 UTC (rev 73507)
+++ grass-addons/grass7/raster/r.hydrodem/dseg.c 2018-10-09 13:01:42 UTC (rev 73508)
@@ -8,7 +8,6 @@
{
char *filename;
int errflag;
- int fd;
dseg->filename = NULL;
dseg->fd = -1;
@@ -16,53 +15,43 @@
dseg->mapset = NULL;
filename = G_tempfile();
- if (-1 == (fd = creat(filename, 0666))) {
- G_warning(_("dseg_open(): unable to create segment file"));
- return -2;
- }
- if (0 >
- (errflag =
- Segment_format(fd, Rast_window_rows(), Rast_window_cols(), srows, scols,
- sizeof(DCELL)))) {
- close(fd);
- unlink(filename);
+ if (0 > (errflag = Segment_open(&(dseg->seg), filename, Rast_window_rows(),
+ Rast_window_cols(), srows, scols,
+ sizeof(DCELL), nsegs_in_memory))) {
if (errflag == -1) {
- G_warning(_("Unable to write segment file"));
+ G_warning(_("File name is invalid"));
return -1;
}
- else {
- G_warning(_("Iillegal configuration parameter(s)"));
+ else if (errflag == -2) {
+ G_warning(_("File write error"));
+ return -2;
+ }
+ else if (errflag == -3) {
+ G_warning(_("Illegal parameters are passed"));
return -3;
}
- }
- close(fd);
- if (-1 == (fd = open(filename, 2))) {
- unlink(filename);
- G_warning(_("Unable to re-open segment file"));
- return -4;
- }
- if (0 > (errflag = Segment_init(&(dseg->seg), fd, nsegs_in_memory))) {
- close(fd);
- unlink(filename);
- if (errflag == -1) {
- G_warning(_("Unable to read segment file"));
+ else if (errflag == -4) {
+ G_warning(_("File could not be re-opened"));
+ return -4;
+ }
+ else if (errflag == -5) {
+ G_warning(_("Prepared file could not be read"));
return -5;
}
- else {
+ else if (errflag == -6) {
G_warning(_("Out of memory"));
return -6;
}
}
+
dseg->filename = filename;
- dseg->fd = fd;
+
return 0;
}
int dseg_close(DSEG *dseg)
{
- Segment_release(&(dseg->seg));
- close(dseg->fd);
- unlink(dseg->filename);
+ Segment_close(&(dseg->seg));
if (dseg->name) {
G_free(dseg->name);
dseg->name = NULL;
@@ -103,7 +92,7 @@
int dseg_read_raster(DSEG *dseg, char *map_name, char *mapset)
{
- int row, nrows;
+ int row, rows;
int map_fd;
DCELL *dbuffer;
@@ -111,9 +100,9 @@
dseg->mapset = NULL;
map_fd = Rast_open_old(map_name, mapset);
- nrows = Rast_window_rows();
+ rows = Rast_window_rows();
dbuffer = Rast_allocate_d_buf();
- for (row = 0; row < nrows; row++) {
+ for (row = 0; row < rows; row++) {
Rast_get_d_row(map_fd, dbuffer, row);
if (Segment_put_row(&(dseg->seg), (DCELL *) dbuffer, row) < 0) {
G_free(dbuffer);
@@ -136,19 +125,19 @@
int dseg_write_raster(DSEG *dseg, char *map_name)
{
int map_fd;
- int row, nrows;
+ int row, rows;
DCELL *dbuffer;
map_fd = Rast_open_new(map_name, DCELL_TYPE);
- nrows = Rast_window_rows();
+ rows = Rast_window_rows();
dbuffer = Rast_allocate_d_buf();
Segment_flush(&(dseg->seg));
- for (row = 0; row < nrows; row++) {
- G_percent(row, nrows, 1);
+ for (row = 0; row < rows; row++) {
+ G_percent(row, rows, 1);
Segment_get_row(&(dseg->seg), (DCELL *) dbuffer, row);
Rast_put_row(map_fd, dbuffer, DCELL_TYPE);
}
- G_percent(row, nrows, 1); /* finish it */
+ G_percent(row, rows, 1); /* finish it */
G_free(dbuffer);
Rast_close(map_fd);
return 0;
Modified: grass-addons/grass7/raster/r.hydrodem/seg.c
===================================================================
--- grass-addons/grass7/raster/r.hydrodem/seg.c 2018-10-09 12:35:12 UTC (rev 73507)
+++ grass-addons/grass7/raster/r.hydrodem/seg.c 2018-10-09 13:01:42 UTC (rev 73508)
@@ -9,63 +9,49 @@
{
char *filename;
int errflag;
- int fd;
sseg->filename = NULL;
sseg->fd = -1;
filename = G_tempfile();
- if (-1 == (fd = creat(filename, 0666))) {
- G_warning(_("Unable to create segment file"));
- return -2;
- }
- if (fill)
- errflag = Segment_format(fd, nrows, ncols, row_in_seg,
- col_in_seg, size_struct);
- else
- errflag = Segment_format_nofill(fd, nrows, ncols, row_in_seg,
- col_in_seg, size_struct);
-
- if (0 > errflag) {
- close(fd);
- unlink(filename);
+ if (0 > (errflag = Segment_open(&(sseg->seg), filename, nrows, ncols,
+ row_in_seg, col_in_seg,
+ size_struct, nsegs_in_memory))) {
if (errflag == -1) {
- G_warning(_("Unable to write segment file"));
+ G_warning(_("File name is invalid"));
return -1;
}
- else {
- G_warning(_("Illegal configuration parameter(s)"));
+ else if (errflag == -2) {
+ G_warning(_("File write error"));
+ return -2;
+ }
+ else if (errflag == -3) {
+ G_warning(_("Illegal parameters are passed"));
return -3;
}
- }
- close(fd);
- if (-1 == (fd = open(filename, 2))) {
- unlink(filename);
- G_warning(_("Unable to re-open file '%s'"), filename);
- return -4;
- }
- if (0 > (errflag = Segment_init(&(sseg->seg), fd, nsegs_in_memory))) {
- close(fd);
- unlink(filename);
- if (errflag == -1) {
- G_warning(_("Unable to read segment file"));
+ else if (errflag == -4) {
+ G_warning(_("File could not be re-opened"));
+ return -4;
+ }
+ else if (errflag == -5) {
+ G_warning(_("Prepared file could not be read"));
return -5;
}
- else {
+ else if (errflag == -6) {
G_warning(_("Out of memory"));
return -6;
}
}
+
sseg->filename = filename;
- sseg->fd = fd;
+
return 0;
}
int seg_close(SSEG *sseg)
{
- Segment_release(&(sseg->seg));
- close(sseg->fd);
- unlink(sseg->filename);
+ Segment_close(&(sseg->seg));
+
return 0;
}
More information about the grass-commit
mailing list