[GRASS-SVN] r51950 - grass/trunk/lib/segment

svn_grass at osgeo.org svn_grass at osgeo.org
Sun Jun 3 06:00:59 PDT 2012


Author: mmetz
Date: 2012-06-03 06:00:58 -0700 (Sun, 03 Jun 2012)
New Revision: 51950

Added:
   grass/trunk/lib/segment/close.c
   grass/trunk/lib/segment/open.c
Modified:
   grass/trunk/lib/segment/address.c
   grass/trunk/lib/segment/flush.c
   grass/trunk/lib/segment/format.c
   grass/trunk/lib/segment/get.c
   grass/trunk/lib/segment/get_row.c
   grass/trunk/lib/segment/init.c
   grass/trunk/lib/segment/pagein.c
   grass/trunk/lib/segment/pageout.c
   grass/trunk/lib/segment/put.c
   grass/trunk/lib/segment/put_row.c
   grass/trunk/lib/segment/release.c
   grass/trunk/lib/segment/seek.c
   grass/trunk/lib/segment/setup.c
Log:
segment lib: add segment open/close routines, hide internal fns

Modified: grass/trunk/lib/segment/address.c
===================================================================
--- grass/trunk/lib/segment/address.c	2012-06-03 12:59:34 UTC (rev 51949)
+++ grass/trunk/lib/segment/address.c	2012-06-03 13:00:58 UTC (rev 51950)
@@ -12,7 +12,7 @@
  * \date 2005-2009
  */
 
-#include <grass/segment.h>
+#include "local_proto.h"
 
 #define SEG_N_ROW_NONZERO(SEG, row, col) \
     (((row) >> (SEG)->srowbits) * (SEG)->spr + ((col) >> (SEG)->scolbits))

Added: grass/trunk/lib/segment/close.c
===================================================================
--- grass/trunk/lib/segment/close.c	                        (rev 0)
+++ grass/trunk/lib/segment/close.c	2012-06-03 13:00:58 UTC (rev 51950)
@@ -0,0 +1,46 @@
+/**
+ * \file close.c
+ *
+ * \brief Segment closing routine.
+ *
+ * This program is free software under the GNU General Public License
+ * (>=v2). Read the file COPYING that comes with GRASS for details.
+ *
+ * \author GRASS GIS Development Team
+ *
+ * \date 2012
+ */
+
+#include <unistd.h>
+#include <fcntl.h>
+#include <grass/gis.h>
+#include <grass/glocale.h>
+#include "local_proto.h"
+
+/**
+ * \fn int segment_close (SEGMENT *SEG)
+ *
+ * \brief Free memory allocated to segment, delete temp file.
+ *
+ * Releases the allocated memory associated with the segment file 
+ * <b>seg</b> and deletes the temporary file.
+ *
+ * \param[in,out] seg
+ * \return 1 if successful
+ * \return -1 if SEGMENT is not available (not open)
+ */
+
+int segment_close(SEGMENT *SEG)
+{
+    if (SEG->open != 1)
+	return -1;
+
+    segment_release(SEG);
+    close(SEG->fd);
+    unlink(SEG->fname);
+
+    SEG->fd = -1;
+    SEG->fname = NULL;
+
+    return 1;
+}


Property changes on: grass/trunk/lib/segment/close.c
___________________________________________________________________
Added: svn:mime-type
   + text/x-csrc
Added: svn:eol-style
   + native

Modified: grass/trunk/lib/segment/flush.c
===================================================================
--- grass/trunk/lib/segment/flush.c	2012-06-03 12:59:34 UTC (rev 51949)
+++ grass/trunk/lib/segment/flush.c	2012-06-03 13:00:58 UTC (rev 51950)
@@ -12,7 +12,7 @@
  * \date 2005-2009
  */
 
-#include <grass/segment.h>
+#include "local_proto.h"
 
 
 /**

Modified: grass/trunk/lib/segment/format.c
===================================================================
--- grass/trunk/lib/segment/format.c	2012-06-03 12:59:34 UTC (rev 51949)
+++ grass/trunk/lib/segment/format.c	2012-06-03 13:00:58 UTC (rev 51950)
@@ -19,7 +19,7 @@
 #include <limits.h>
 #include <grass/gis.h>
 #include <grass/glocale.h>
-#include <grass/segment.h>
+#include "local_proto.h"
 
 
 static int _segment_format(int, off_t, off_t, int, int, int, int);

Modified: grass/trunk/lib/segment/get.c
===================================================================
--- grass/trunk/lib/segment/get.c	2012-06-03 12:59:34 UTC (rev 51949)
+++ grass/trunk/lib/segment/get.c	2012-06-03 13:00:58 UTC (rev 51950)
@@ -13,7 +13,7 @@
  */
 
 #include <string.h>
-#include <grass/segment.h>
+#include "local_proto.h"
 
 
 /*bugfix: buf: char* vs int* -> wrong pointer arithmetics!!!. Pierre de Mouveaux - 09 april 2000 */

Modified: grass/trunk/lib/segment/get_row.c
===================================================================
--- grass/trunk/lib/segment/get_row.c	2012-06-03 12:59:34 UTC (rev 51949)
+++ grass/trunk/lib/segment/get_row.c	2012-06-03 13:00:58 UTC (rev 51950)
@@ -17,7 +17,7 @@
 #include <string.h>
 #include <errno.h>
 #include <grass/gis.h>
-#include <grass/segment.h>
+#include "local_proto.h"
 
 
 /**

Modified: grass/trunk/lib/segment/init.c
===================================================================
--- grass/trunk/lib/segment/init.c	2012-06-03 12:59:34 UTC (rev 51949)
+++ grass/trunk/lib/segment/init.c	2012-06-03 13:00:58 UTC (rev 51950)
@@ -22,7 +22,7 @@
 #include <string.h>
 #include <errno.h>
 #include <grass/gis.h>
-#include <grass/segment.h>
+#include "local_proto.h"
 
 
 static int read_int(int, int *);
@@ -50,13 +50,13 @@
  *
  * \param[in,out] seg segment
  * \param[in] fd file descriptor
- * \param[in] nsegs number of segments to remain in memory
+ * \param[in] nseg number of segments to remain in memory
  * \return 1 if successful
  * \return -1 if unable to seek or read segment file
  * \return -2 if out of memory
  */
 
-int segment_init(SEGMENT * SEG, int fd, int nseg)
+int segment_init(SEGMENT *SEG, int fd, int nseg)
 {
     SEG->open = 0;
     SEG->fd = fd;

Added: grass/trunk/lib/segment/open.c
===================================================================
--- grass/trunk/lib/segment/open.c	                        (rev 0)
+++ grass/trunk/lib/segment/open.c	2012-06-03 13:00:58 UTC (rev 51950)
@@ -0,0 +1,105 @@
+
+/**
+ * \file open.c
+ *
+ * \brief Segment creation routine.
+ *
+ * This program is free software under the GNU General Public License
+ * (>=v2). Read the file COPYING that comes with GRASS for details.
+ *
+ * \author GRASS GIS Development Team
+ *
+ * \date 2012
+ */
+
+#include <unistd.h>
+#include <fcntl.h>
+#include <grass/gis.h>
+#include <grass/glocale.h>
+#include "local_proto.h"
+
+/**
+ * \fn int segment_open (SEGMENT *SEG, char *fname, off_t nrows, off_t ncols, int srows, int scols, int len, int nseg)
+ *
+ * \brief Initialize segment structure and open segment file.
+ *
+ * Initializes the <b>seg</b> structure and prepares a temporary file. 
+ * This fn is a wrapper for segment_format() and segment_init()
+ *
+ * <b>Note:</b> The file with name fname will be created anew.
+ *
+ * \param[in,out] seg segment
+ * \param[in] fname file name
+ * \param[in] nrows number of non-segmented rows
+ * \param[in] ncols number of non-segmented columns
+ * \param[in] srows segment rows
+ * \param[in] scols segment columns
+ * \param[in] len length of data type
+ * \param[in] nseg number of segments to remain in memory
+ * \return 1 if successful
+ * \return -1 if file name is invalid
+ * \return -2 if file write error
+ * \return -3 if illegal parameters are passed
+ * \return -4 if file could not be re-opened
+ * \return -5 if prepared file could not be read
+ * \return -6 if out of memory
+ */
+
+int
+segment_open(SEGMENT *SEG, char *fname, off_t nrows, off_t ncols,
+             int srows, int scols, int len, int nseg)
+{
+    int ret;
+
+    if (!fname) {
+	G_warning(_("Segment file name is NULL"));
+	return -1;
+    }
+    /* file exists? */
+    if (access(fname, F_OK) == 0) {
+	G_warning(_("Segment file exists already"));
+	return -1;
+    }
+    
+    SEG->fname = G_store(fname);
+    SEG->fd = -1;
+
+    if (-1 == (SEG->fd = creat(SEG->fname, 0666))) {
+	G_warning(_("Unable to create segment file"));
+	return -1;
+    }
+    if (0 > (ret = segment_format(SEG->fd, nrows, ncols, srows,
+							scols, len))) {
+	close(SEG->fd);
+	unlink(SEG->fname);
+	if (ret == -1) {
+	    G_warning(_("Could not write segment file"));
+	    return -2;
+	}
+	else { /* ret = -3 */
+	    G_warning(_("Illegal segment configuration parameter(s)"));
+	    return ret;
+	}
+    }
+    /* re-open for read and write */
+    close(SEG->fd);
+    if (-1 == (SEG->fd = open(SEG->fname, 2))) {
+	unlink(SEG->fname);
+	G_warning(_("Unable to re-open segment file"));
+	return -4;
+    }
+    if (0 > (ret = segment_init(SEG, fd, nseg))) {
+	close(SEG->fd);
+	unlink(SEG->fname);
+	if (ret == -1) {
+	    G_warning(_("Could not read segment file"));
+	    return -5;
+	}
+	else {
+	    G_warning(_("Out of memory"));
+	    return -6;
+	}
+    }
+
+    return 1;
+}


Property changes on: grass/trunk/lib/segment/open.c
___________________________________________________________________
Added: svn:mime-type
   + text/x-csrc
Added: svn:eol-style
   + native

Modified: grass/trunk/lib/segment/pagein.c
===================================================================
--- grass/trunk/lib/segment/pagein.c	2012-06-03 12:59:34 UTC (rev 51949)
+++ grass/trunk/lib/segment/pagein.c	2012-06-03 13:00:58 UTC (rev 51950)
@@ -17,7 +17,7 @@
 #include <string.h>
 #include <errno.h>
 #include <grass/gis.h>
-#include <grass/segment.h>
+#include "local_proto.h"
 
 
 /**

Modified: grass/trunk/lib/segment/pageout.c
===================================================================
--- grass/trunk/lib/segment/pageout.c	2012-06-03 12:59:34 UTC (rev 51949)
+++ grass/trunk/lib/segment/pageout.c	2012-06-03 13:00:58 UTC (rev 51950)
@@ -17,7 +17,7 @@
 #include <string.h>
 #include <errno.h>
 #include <grass/gis.h>
-#include <grass/segment.h>
+#include "local_proto.h"
 
 
 /**

Modified: grass/trunk/lib/segment/put.c
===================================================================
--- grass/trunk/lib/segment/put.c	2012-06-03 12:59:34 UTC (rev 51949)
+++ grass/trunk/lib/segment/put.c	2012-06-03 13:00:58 UTC (rev 51950)
@@ -14,7 +14,7 @@
 
 #include <string.h>
 #include <grass/gis.h>
-#include <grass/segment.h>
+#include "local_proto.h"
 
 
 /*bugfix: buf: char* vs int* -> wrong pointer arithmetics!!!. Pierre de Mouveaux - 09 april 2000 */

Modified: grass/trunk/lib/segment/put_row.c
===================================================================
--- grass/trunk/lib/segment/put_row.c	2012-06-03 12:59:34 UTC (rev 51949)
+++ grass/trunk/lib/segment/put_row.c	2012-06-03 13:00:58 UTC (rev 51950)
@@ -17,7 +17,7 @@
 #include <errno.h>
 #include <unistd.h>
 #include <grass/gis.h>
-#include <grass/segment.h>
+#include "local_proto.h"
 
 
 /*      buf is CELL *   WRAT code       */

Modified: grass/trunk/lib/segment/release.c
===================================================================
--- grass/trunk/lib/segment/release.c	2012-06-03 12:59:34 UTC (rev 51949)
+++ grass/trunk/lib/segment/release.c	2012-06-03 13:00:58 UTC (rev 51950)
@@ -14,7 +14,7 @@
 
 #include <stdlib.h>
 #include <grass/gis.h>
-#include <grass/segment.h>
+#include "local_proto.h"
 
 
 /**

Modified: grass/trunk/lib/segment/seek.c
===================================================================
--- grass/trunk/lib/segment/seek.c	2012-06-03 12:59:34 UTC (rev 51949)
+++ grass/trunk/lib/segment/seek.c	2012-06-03 13:00:58 UTC (rev 51950)
@@ -18,7 +18,7 @@
 #include <string.h>
 #include <errno.h>
 #include <grass/gis.h>
-#include <grass/segment.h>
+#include "local_proto.h"
 
 
 /**

Modified: grass/trunk/lib/segment/setup.c
===================================================================
--- grass/trunk/lib/segment/setup.c	2012-06-03 12:59:34 UTC (rev 51949)
+++ grass/trunk/lib/segment/setup.c	2012-06-03 13:00:58 UTC (rev 51950)
@@ -16,7 +16,7 @@
 #include <stdio.h>
 #include <math.h>
 #include <grass/gis.h>
-#include <grass/segment.h>
+#include "local_proto.h"
 
 
 /**



More information about the grass-commit mailing list