close.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. #else
  38. , {
  39. clo_dummy, format}
  40. #endif
  41. };
  42. /*!
  43. \brief Close vector map
  44. \param Map pointer to Map_info
  45. \return 0 on success
  46. \return non-zero on error
  47. */
  48. int Vect_close(struct Map_info *Map)
  49. {
  50. struct Coor_info CInfo;
  51. G_debug(1,
  52. "Vect_close(): name = %s, mapset = %s, format = %d, level = %d",
  53. Map->name, Map->mapset, Map->format, Map->level);
  54. /* Store support files if in write mode on level 2 */
  55. if (strcmp(Map->mapset, G_mapset()) == 0 && Map->support_updated &&
  56. Map->plus.built == GV_BUILD_ALL) {
  57. char buf[GPATH_MAX];
  58. char file_path[GPATH_MAX];
  59. struct stat info;
  60. /* Delete old support files if available */
  61. sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
  62. G__file_name(file_path, buf, GV_TOPO_ELEMENT, G_mapset());
  63. if (stat(file_path, &info) == 0) /* file exists? */
  64. unlink(file_path);
  65. G__file_name(file_path, buf, GV_SIDX_ELEMENT, G_mapset());
  66. if (stat(file_path, &info) == 0) /* file exists? */
  67. unlink(file_path);
  68. G__file_name(file_path, buf, GV_CIDX_ELEMENT, G_mapset());
  69. if (stat(file_path, &info) == 0) /* file exists? */
  70. unlink(file_path);
  71. Vect_coor_info(Map, &CInfo);
  72. Map->plus.coor_size = CInfo.size;
  73. Map->plus.coor_mtime = CInfo.mtime;
  74. Vect_save_topo(Map);
  75. Vect_save_sidx(Map);
  76. Vect_cidx_save(Map);
  77. #ifdef HAVE_OGR
  78. if (Map->format == GV_FORMAT_OGR)
  79. V2_close_ogr(Map);
  80. #endif
  81. }
  82. else {
  83. /* spatial index must also be closed when opened with topo but not modified */
  84. if (Map->format == GV_FORMAT_NATIVE &&
  85. Map->plus.Spidx_built == 1 &&
  86. Map->plus.built == GV_BUILD_ALL)
  87. Vect_save_sidx(Map);
  88. }
  89. if (Map->level == 2 && Map->plus.release_support) {
  90. G_debug(1, "free topology");
  91. dig_free_plus(&(Map->plus));
  92. G_debug(1, "free spatial index");
  93. dig_spidx_free(&(Map->plus));
  94. G_debug(1, "free category index");
  95. dig_cidx_free(&(Map->plus));
  96. }
  97. if (Map->format == GV_FORMAT_NATIVE) {
  98. G_debug(1, "close history file");
  99. if (Map->hist_fp != NULL)
  100. fclose(Map->hist_fp);
  101. }
  102. /* Close level 1 files / data sources if not head_only */
  103. if (!Map->head_only) {
  104. if (((*Close_array[Map->format][1]) (Map)) != 0) {
  105. G_warning(_("Unable to close vector <%s>"),
  106. Vect_get_full_name(Map));
  107. return 1;
  108. }
  109. }
  110. G_free((void *)Map->name);
  111. Map->name = NULL;
  112. G_free((void *)Map->mapset);
  113. Map->mapset = NULL;
  114. Map->open = VECT_CLOSED_CODE;
  115. return 0;
  116. }