[GRASS-SVN] r40463 - in grass/trunk: include lib/gis lib/raster
svn_grass at osgeo.org
svn_grass at osgeo.org
Fri Jan 15 22:32:31 EST 2010
Author: glynn
Date: 2010-01-15 22:32:30 -0500 (Fri, 15 Jan 2010)
New Revision: 40463
Added:
grass/trunk/lib/gis/handler.c
Modified:
grass/trunk/include/gisdefs.h
grass/trunk/include/rasterdefs.h
grass/trunk/lib/gis/error.c
grass/trunk/lib/raster/closecell.c
grass/trunk/lib/raster/init.c
Log:
Add fatal error handlers
Add Rast__unopen_all()
Invoke Rast__unopen_all() on fatal errors
Modified: grass/trunk/include/gisdefs.h
===================================================================
--- grass/trunk/include/gisdefs.h 2010-01-15 23:00:57 UTC (rev 40462)
+++ grass/trunk/include/gisdefs.h 2010-01-16 03:32:30 UTC (rev 40463)
@@ -281,6 +281,11 @@
void G__check_gisinit(void);
void G_init_all(void);
+/* handler.c */
+void G_add_error_handler(void (*)(void *), void *);
+void G_remove_error_handler(void (*)(void *), void *);
+void G__call_error_handlers(void);
+
/* home.c */
const char *G_home(void);
const char *G__home(void);
Modified: grass/trunk/include/rasterdefs.h
===================================================================
--- grass/trunk/include/rasterdefs.h 2010-01-15 23:00:57 UTC (rev 40462)
+++ grass/trunk/include/rasterdefs.h 2010-01-16 03:32:30 UTC (rev 40463)
@@ -84,6 +84,7 @@
/* closecell.c */
void Rast_close(int);
void Rast_unopen(int);
+void Rast__unopen_all(void);
/* color_compat.c */
void Rast_make_ryg_colors(struct Colors *, CELL, CELL);
@@ -344,6 +345,7 @@
void Rast__check_init(void);
void Rast_init_all(void);
void Rast__init(void);
+void Rast__error_handler(void *);
/* interp.c */
DCELL Rast_interp_linear(double, DCELL, DCELL);
Modified: grass/trunk/lib/gis/error.c
===================================================================
--- grass/trunk/lib/gis/error.c 2010-01-15 23:00:57 UTC (rev 40462)
+++ grass/trunk/lib/gis/error.c 2010-01-16 03:32:30 UTC (rev 40463)
@@ -152,6 +152,8 @@
vfprint_error(ERR, msg, ap);
va_end(ap);
+ G__call_error_handlers();
+
exit(EXIT_FAILURE);
}
Added: grass/trunk/lib/gis/handler.c
===================================================================
--- grass/trunk/lib/gis/handler.c (rev 0)
+++ grass/trunk/lib/gis/handler.c 2010-01-16 03:32:30 UTC (rev 40463)
@@ -0,0 +1,65 @@
+
+#include <stddef.h>
+#include <grass/gis.h>
+
+struct handler {
+ void (*func)(void *);
+ void *closure;
+};
+
+static struct handler *handlers;
+
+static int num_handlers;
+static int max_handlers;
+
+static struct handler *alloc_handler(void)
+{
+ int i;
+
+ for (i = 0; i < num_handlers; i++) {
+ struct handler *h = &handlers[i];
+ if (!h->func)
+ return h;
+ }
+
+ if (num_handlers >= max_handlers) {
+ max_handlers += 10;
+ handlers = G_realloc(handlers, max_handlers * sizeof(struct handler));
+ }
+
+ return &handlers[num_handlers++];
+}
+
+void G_add_error_handler(void (*func)(void *), void *closure)
+{
+ struct handler *h = alloc_handler();
+
+ h->func = func;
+ h->closure = closure;
+}
+
+void G_remove_error_handler(void (*func)(void *), void *closure)
+{
+ int i;
+
+ for (i = 0; i < num_handlers; i++) {
+ struct handler *h = &handlers[i];
+
+ if (h->func == func && h->closure == closure) {
+ h->func = NULL;
+ h->closure = NULL;
+ }
+ }
+}
+
+void G__call_error_handlers(void)
+{
+ int i;
+
+ for (i = 0; i < num_handlers; i++) {
+ struct handler *h = &handlers[i];
+ if (h->func)
+ (*h->func)(h->closure);
+ }
+}
+
Modified: grass/trunk/lib/raster/closecell.c
===================================================================
--- grass/trunk/lib/raster/closecell.c 2010-01-15 23:00:57 UTC (rev 40462)
+++ grass/trunk/lib/raster/closecell.c 2010-01-16 03:32:30 UTC (rev 40463)
@@ -64,8 +64,7 @@
*
* \param fd file descriptor
*
- * \return -1 on error
- * \return 1 on success
+ * \return void
*/
void Rast_close(int fd)
{
@@ -98,8 +97,7 @@
*
* \param fd file descriptor
*
- * \return -1 on error
- * \return 1 on success
+ * \return void
*/
void Rast_unopen(int fd)
{
@@ -114,6 +112,30 @@
close_new(fd, 0);
}
+/*!
+ * \brief Unopen all raster maps
+ *
+ * Unopen all raster maps opened for write. Memory allocated for
+ * raster processing is freed, and the temporary file created when the
+ * raster map was opened is removed (see \ref
+ * Creating_and_Opening_New_Raster_Files). This routine is useful when
+ * errors are detected and it is desired to remove temporary files.
+ *
+ * \return void
+ */
+void Rast__unopen_all(void)
+{
+ int i;
+
+ for (i = 0; i < R__.fileinfo_count; i++) {
+ struct fileinfo *fcb = &R__.fileinfo[i];
+
+ if (fcb->open_mode == OPEN_NEW_COMPRESSED ||
+ fcb->open_mode == OPEN_NEW_UNCOMPRESSED)
+ close_new(i, 0);
+ }
+}
+
static int close_old(int fd)
{
struct fileinfo *fcb = &R__.fileinfo[fd];
Modified: grass/trunk/lib/raster/init.c
===================================================================
--- grass/trunk/lib/raster/init.c 2010-01-15 23:00:57 UTC (rev 40462)
+++ grass/trunk/lib/raster/init.c 2010-01-16 03:32:30 UTC (rev 40463)
@@ -71,6 +71,11 @@
G_initialize_done(&initialized);
}
+void Rast__error_handler(void *p)
+{
+ Rast__unopen_all();
+}
+
static int init(void)
{
G__init_window();
@@ -88,6 +93,8 @@
R__.nbytes = sizeof(CELL);
R__.compression_type = getenv("GRASS_INT_ZLIB") ? 2 : 1;
+ G_add_error_handler(Rast__error_handler, NULL);
+
initialized = 1;
return 0;
More information about the grass-commit
mailing list