GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
vector/Vlib/handler.c
Go to the documentation of this file.
1 /*!
2  \file lib/vector/Vlib/handler.c
3 
4  \brief Vector library - standard error handlers
5 
6  Higher level functions for reading/writing/manipulating vectors.
7 
8  (C) 2011 by the GRASS Development Team
9 
10  This program is free software under the GNU General Public License
11  (>=v2). Read the file COPYING that comes with GRASS for details.
12 
13  \author Martin Landa <landa.martin gmail.com>
14 */
15 
16 #include <grass/gis.h>
17 #include <grass/vector.h>
18 
19 struct handler_data_io {
20  struct Map_info *In;
21  struct Map_info *Out;
22 };
23 
24 static struct handler_data_io *handler_io;
25 
26 static void error_handler_io(void *p)
27 {
28  char *name;
29  struct Map_info *In, *Out;
30 
31  In = handler_io->In;
32  Out = handler_io->Out;
33 
34  if (In && In->open == VECT_OPEN_CODE)
35  Vect_close(In);
36 
37  if (Out && Out->open == VECT_OPEN_CODE) {
38  name = G_store(Out->name);
39  Vect_close(Out);
40  Vect_delete(name);
41  G_free(name);
42  }
43 }
44 
45 /*!
46  \brief Define standard error handler for input and output vector maps
47 
48  This handler:
49  - close input vector map on error
50  - close and delete output vector map on error
51 
52  Note: It's recommended to call this routine after Vect_open_old() or
53  Vect_open_old2().
54 
55  \param In pointer in Map_info struct (input vector map) or NULL
56  \param Out pointer to Map_info struct (output vector map) or NULL
57 */
58 void Vect_set_error_handler_io(struct Map_info *In, struct Map_info *Out)
59 {
60  if (!handler_io)
61  handler_io = G_malloc(sizeof(struct handler_data_io));
62 
63  handler_io->In = In;
64  handler_io->Out = Out;
65 
66  G_add_error_handler(error_handler_io, handler_io);
67 }
#define G_malloc(n)
Definition: defs/gis.h:112
char * name
Map name (for 4.0)
Definition: dig_structs.h:1332
int Vect_delete(const char *)
Delete vector map including attribute tables.
Definition: map.c:370
int open
Open indicator.
Definition: dig_structs.h:1296
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:149
int Vect_close(struct Map_info *)
Close vector map.
void Vect_set_error_handler_io(struct Map_info *In, struct Map_info *Out)
Define standard error handler for input and output vector maps.
Vector map info.
Definition: dig_structs.h:1259
char * G_store(const char *)
Copy string to allocated memory.
Definition: strings.c:87
const char * name
Definition: named_colr.c:7
#define VECT_OPEN_CODE
Vector map open code.
Definition: dig_defines.h:111
void G_add_error_handler(void(*)(void *), void *)
Add new error handler.
Definition: gis/handler.c:70