close.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*!
  2. \file close.c
  3. \brief Vector library - Close map
  4. Higher level functions for reading/writing/manipulating vectors.
  5. (C) 2001-2008 by the GRASS Development Team
  6. This program is free software under the
  7. GNU General Public License (>=v2).
  8. Read the file COPYING that comes with GRASS
  9. for details.
  10. \author Original author CERL, probably Dave Gerdes or Mike Higgins.
  11. Update to GRASS 5.7 Radim Blazek and David D. Gray.
  12. \date 2001-2008
  13. */
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <sys/types.h>
  18. #include <sys/stat.h>
  19. #include <unistd.h>
  20. #include <grass/Vect.h>
  21. #include <grass/glocale.h>
  22. static int clo_dummy()
  23. {
  24. return -1;
  25. }
  26. #ifndef HAVE_OGR
  27. static int format()
  28. {
  29. G_fatal_error(_("Requested format is not compiled in this version"));
  30. return 0;
  31. }
  32. #endif
  33. static int (*Close_array[][2]) () = {
  34. {
  35. clo_dummy, V1_close_nat}
  36. #ifdef HAVE_OGR
  37. , {
  38. clo_dummy, V1_close_ogr}
  39. #else
  40. , {
  41. clo_dummy, format}
  42. #endif
  43. };
  44. /*!
  45. \brief Close vector data file
  46. \param Map vector map to be closed
  47. \return 0 on success
  48. \return non-zero on error
  49. */
  50. int Vect_close(struct Map_info *Map)
  51. {
  52. struct Coor_info CInfo;
  53. G_debug(1,
  54. "Vect_close(): name = %s, mapset = %s, format = %d, level = %d",
  55. Map->name, Map->mapset, Map->format, Map->level);
  56. /* Store support files if in write mode on level 2 */
  57. if (strcmp(Map->mapset, G_mapset()) == 0 && Map->support_updated &&
  58. Map->plus.built == GV_BUILD_ALL) {
  59. char buf[GPATH_MAX];
  60. char file_path[GPATH_MAX];
  61. struct stat info;
  62. /* Delete old support files if available */
  63. sprintf(buf, "%s/%s", GRASS_VECT_DIRECTORY, Map->name);
  64. G__file_name(file_path, buf, GV_TOPO_ELEMENT, G_mapset());
  65. if (stat(file_path, &info) == 0) /* file exists? */
  66. unlink(file_path);
  67. G__file_name(file_path, buf, GV_SIDX_ELEMENT, G_mapset());
  68. if (stat(file_path, &info) == 0) /* file exists? */
  69. unlink(file_path);
  70. G__file_name(file_path, buf, GV_CIDX_ELEMENT, G_mapset());
  71. if (stat(file_path, &info) == 0) /* file exists? */
  72. unlink(file_path);
  73. Vect_coor_info(Map, &CInfo);
  74. Map->plus.coor_size = CInfo.size;
  75. Map->plus.coor_mtime = CInfo.mtime;
  76. Vect_save_topo(Map);
  77. /* Spatial index is not saved */
  78. /* Vect_save_spatial_index ( Map ); */
  79. Vect_cidx_save(Map);
  80. #ifdef HAVE_OGR
  81. if (Map->format == GV_FORMAT_OGR)
  82. V2_close_ogr(Map);
  83. #endif
  84. }
  85. if (Map->level == 2 && Map->plus.release_support) {
  86. G_debug(1, "free topology");
  87. dig_free_plus(&(Map->plus));
  88. if (!Map->head_only) {
  89. G_debug(1, "free spatial index");
  90. dig_spidx_free(&(Map->plus));
  91. }
  92. G_debug(1, "free category index");
  93. dig_cidx_free(&(Map->plus));
  94. }
  95. if (Map->format == GV_FORMAT_NATIVE) {
  96. G_debug(1, "close history file");
  97. if (Map->hist_fp != NULL)
  98. fclose(Map->hist_fp);
  99. }
  100. /* Close level 1 files / data sources if not head_only */
  101. if (!Map->head_only) {
  102. if (((*Close_array[Map->format][1]) (Map)) != 0) {
  103. G_warning(_("Unable to close vector <%s>"),
  104. Vect_get_full_name(Map));
  105. return 1;
  106. }
  107. }
  108. G_free((void *)Map->name);
  109. Map->name = NULL;
  110. G_free((void *)Map->mapset);
  111. Map->mapset = NULL;
  112. Map->open = VECT_CLOSED_CODE;
  113. return 0;
  114. }