handler.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. const 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. }
  30. }
  31. /*!
  32. \brief Define standard error handler for input and output vector maps
  33. This handler:
  34. - close input vector map on error
  35. - close and delete output vector map on error
  36. Note: It's recommended to call this routine after Vect_open_old() or
  37. Vect_open_old2().
  38. \param In pointer in Map_info struct (input vector map) or NULL
  39. \param Out pointer to Map_info struct (output vector map) or NULL
  40. */
  41. void Vect_set_error_handler_io(struct Map_info *In, struct Map_info *Out)
  42. {
  43. if (!handler_io)
  44. handler_io = G_malloc(sizeof(struct handler_data_io));
  45. handler_io->In = In;
  46. handler_io->Out = Out;
  47. G_add_error_handler(error_handler_io, handler_io);
  48. }