handler.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*!
  2. \file lib/vector/Vlib/handler.c
  3. \brief Vector library - standard error handlers
  4. Higher level functions for reading/writing/manipulating vectors.
  5. (C) 2011 by the GRASS Development Team
  6. This program is free software under the GNU General Public License
  7. (>=v2). Read the file COPYING that comes with GRASS for details.
  8. \author Martin Landa <landa.martin gmail.com>
  9. */
  10. #include <grass/gis.h>
  11. #include <grass/vector.h>
  12. struct handler_data_io {
  13. struct Map_info *In;
  14. struct Map_info *Out;
  15. };
  16. static struct handler_data_io *handler_io;
  17. static void error_handler_io(void *p)
  18. {
  19. char *name;
  20. struct Map_info *In, *Out;
  21. In = handler_io->In;
  22. Out = handler_io->Out;
  23. if (In && In->open == VECT_OPEN_CODE)
  24. Vect_close(In);
  25. if (Out && Out->open == VECT_OPEN_CODE) {
  26. name = G_store(Out->name);
  27. Vect_close(Out);
  28. Vect_delete(name);
  29. G_free(name);
  30. }
  31. }
  32. /*!
  33. \brief Define standard error handler for input and output vector maps
  34. This handler:
  35. - close input vector map on error
  36. - close and delete output vector map on error
  37. Note: It's recommended to call this routine after Vect_open_old() or
  38. Vect_open_old2().
  39. \param In pointer in Map_info struct (input vector map) or NULL
  40. \param Out pointer to Map_info struct (output vector map) or NULL
  41. */
  42. void Vect_set_error_handler_io(struct Map_info *In, struct Map_info *Out)
  43. {
  44. if (!handler_io)
  45. handler_io = G_malloc(sizeof(struct handler_data_io));
  46. handler_io->In = In;
  47. handler_io->Out = Out;
  48. G_add_error_handler(error_handler_io, handler_io);
  49. }