[GRASS-SVN] r73510 - grass/trunk/raster/r.stream.extract

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Oct 9 06:21:00 PDT 2018


Author: mmetz
Date: 2018-10-09 06:21:00 -0700 (Tue, 09 Oct 2018)
New Revision: 73510

Modified:
   grass/trunk/raster/r.stream.extract/bseg.c
   grass/trunk/raster/r.stream.extract/cseg.c
   grass/trunk/raster/r.stream.extract/dseg.c
   grass/trunk/raster/r.stream.extract/seg.c
Log:
r.stream.extract: use Segment_[open|close]()

Modified: grass/trunk/raster/r.stream.extract/bseg.c
===================================================================
--- grass/trunk/raster/r.stream.extract/bseg.c	2018-10-09 13:09:01 UTC (rev 73509)
+++ grass/trunk/raster/r.stream.extract/bseg.c	2018-10-09 13:21:00 UTC (rev 73510)
@@ -7,7 +7,6 @@
 {
     char *filename;
     int errflag;
-    int fd;
 
     bseg->filename = NULL;
     bseg->fd = -1;
@@ -15,52 +14,44 @@
     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;
@@ -102,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;
@@ -112,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);
 	}
@@ -135,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/trunk/raster/r.stream.extract/cseg.c
===================================================================
--- grass/trunk/raster/r.stream.extract/cseg.c	2018-10-09 13:09:01 UTC (rev 73509)
+++ grass/trunk/raster/r.stream.extract/cseg.c	2018-10-09 13:21:00 UTC (rev 73510)
@@ -7,7 +7,6 @@
 {
     char *filename;
     int errflag;
-    int fd;
 
     cseg->filename = NULL;
     cseg->fd = -1;
@@ -15,53 +14,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;
@@ -102,7 +91,7 @@
 
 int cseg_read_raster(CSEG *cseg, char *map_name, char *mapset)
 {
-    int row, nrows;
+    int row, rows;
     int map_fd;
     CELL *buffer;
 
@@ -110,9 +99,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);
@@ -135,19 +124,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/trunk/raster/r.stream.extract/dseg.c
===================================================================
--- grass/trunk/raster/r.stream.extract/dseg.c	2018-10-09 13:09:01 UTC (rev 73509)
+++ grass/trunk/raster/r.stream.extract/dseg.c	2018-10-09 13:21:00 UTC (rev 73510)
@@ -7,7 +7,6 @@
 {
     char *filename;
     int errflag;
-    int fd;
 
     dseg->filename = NULL;
     dseg->fd = -1;
@@ -15,53 +14,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;
@@ -102,7 +91,7 @@
 
 int dseg_read_raster(DSEG *dseg, char *map_name, char *mapset)
 {
-    int row, nrows;
+    int row, rows;
     int map_fd;
     DCELL *dbuffer;
 
@@ -110,9 +99,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);
@@ -135,19 +124,19 @@
 int dseg_write_cellfile(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/trunk/raster/r.stream.extract/seg.c
===================================================================
--- grass/trunk/raster/r.stream.extract/seg.c	2018-10-09 13:09:01 UTC (rev 73509)
+++ grass/trunk/raster/r.stream.extract/seg.c	2018-10-09 13:21:00 UTC (rev 73510)
@@ -10,63 +10,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