GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-57a646b4a4
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 UNUSED)
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);
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 }
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:150
#define G_malloc(n)
Definition: defs/gis.h:94
void G_add_error_handler(void(*)(void *), void *)
Add new error handler.
Definition: gis/handler.c:71
char * G_store(const char *)
Copy string to allocated memory.
Definition: strings.c:87
int Vect_close(struct Map_info *)
Close vector map.
int Vect_delete(const char *)
Delete vector map including attribute tables.
Definition: map.c:367
#define VECT_OPEN_CODE
Vector map open code.
Definition: dig_defines.h:111
#define UNUSED
A macro for an attribute, if attached to a variable, indicating that the variable is not used.
Definition: gis.h:47
const char * name
Definition: named_colr.c:6
Vector map info.
Definition: dig_structs.h:1243
char * name
Map name (for 4.0)
Definition: dig_structs.h:1316
int open
Open indicator.
Definition: dig_structs.h:1280
void Vect_set_error_handler_io(struct Map_info *In, struct Map_info *Out)
Define standard error handler for input and output vector maps.