close.c 3.4 KB

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