open_nat.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*!
  2. \file lib/vector/Vlib/open_nat.c
  3. \brief Vector library - open vector map (native format) - level 1
  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 <unistd.h>
  12. #include <sys/types.h>
  13. #include <sys/stat.h>
  14. #include <grass/vector.h>
  15. #include <grass/glocale.h>
  16. #include "local_proto.h"
  17. static int check_coor(struct Map_info *Map);
  18. /*!
  19. \brief Open existing vector map (level 1)
  20. Map->name and Map->mapset must be set before.
  21. \param Map pointer to Map_info structure
  22. \param update non-zero for write mode, otherwise read-only
  23. \return 0 success
  24. \return -1 error
  25. */
  26. int V1_open_old_nat(struct Map_info *Map, int update)
  27. {
  28. char path[GPATH_MAX];
  29. struct Coor_info CInfo;
  30. G_debug(1, "V1_open_old_nat(): name = %s mapset = %s", Map->name,
  31. Map->mapset);
  32. Vect__get_path(path, Map);
  33. dig_file_init(&(Map->dig_fp));
  34. if (update)
  35. Map->dig_fp.file = G_fopen_modify(path, GV_COOR_ELEMENT);
  36. else
  37. Map->dig_fp.file =
  38. G_fopen_old(path, GV_COOR_ELEMENT, Map->mapset);
  39. if (Map->dig_fp.file == NULL) {
  40. G_warning(_("Unable to open coor file for vector map <%s>"),
  41. Vect_get_full_name(Map));
  42. return -1;
  43. }
  44. /* needed to determine file size, Map->head.size will be updated
  45. by dig__read_head(Map) */
  46. Vect_coor_info(Map, &CInfo);
  47. Map->head.size = CInfo.size;
  48. if (!(dig__read_head(Map))) {
  49. G_debug(1, "dig__read_head(): failed");
  50. return -1;
  51. }
  52. /* compare coor size stored in head with real size */
  53. /* check should catch if LFS is required but not available */
  54. check_coor(Map);
  55. /* set conversion matrices */
  56. dig_init_portable(&(Map->head.port), Map->head.port.byte_order);
  57. /* load to memory */
  58. if (!update)
  59. dig_file_load(&(Map->dig_fp)); /* has currently no effect, file never loaded */
  60. return 0;
  61. }
  62. /*!
  63. \brief Create new vector map (level 1)
  64. \param[out] Map pointer to Map_info structure
  65. \param name vector map name to be created
  66. \param with_z 2D or 3D (unused?)
  67. \return 0 success
  68. \return -1 error
  69. */
  70. int V1_open_new_nat(struct Map_info *Map, const char *name, int with_z)
  71. {
  72. char path[GPATH_MAX];
  73. G_debug(1, "V1_open_new_nat(): name = %s with_z = %d is_tmp = %d",
  74. name, with_z, Map->temporary);
  75. /* Set the 'coor' file version */
  76. Map->head.coor_version.major = GV_COOR_VER_MAJOR;
  77. Map->head.coor_version.minor = GV_COOR_VER_MINOR;
  78. Map->head.coor_version.back_major = GV_COOR_EARLIEST_MAJOR;
  79. Map->head.coor_version.back_minor = GV_COOR_EARLIEST_MINOR;
  80. Vect__get_path(path, Map);
  81. /* TODO: open better */
  82. dig_file_init(&(Map->dig_fp));
  83. Map->dig_fp.file = G_fopen_new(path, GV_COOR_ELEMENT);
  84. if (Map->dig_fp.file == NULL)
  85. return -1;
  86. fclose(Map->dig_fp.file);
  87. dig_file_init(&(Map->dig_fp));
  88. Map->dig_fp.file = G_fopen_modify(path, GV_COOR_ELEMENT);
  89. if (Map->dig_fp.file == NULL)
  90. return -1;
  91. /* if overwrite OK, any existing files have already been deleted by
  92. * Vect_open_new(): remove this check ? */
  93. /* check to see if dig_plus file exists and if so, remove it */
  94. Vect__get_element_path(path, Map, GV_TOPO_ELEMENT);
  95. if (access(path, F_OK) == 0)
  96. unlink(path); /* remove topo file if exists */
  97. /* set conversion matrices */
  98. dig_init_portable(&(Map->head.port), dig__byte_order_out());
  99. /* write coor header */
  100. if (!(dig__write_head(Map)))
  101. return -1;
  102. return 0;
  103. }
  104. /* check file size */
  105. int check_coor(struct Map_info *Map)
  106. {
  107. struct Coor_info CInfo;
  108. off_t dif;
  109. /* NOTE: coor file is open */
  110. Vect_coor_info(Map, &CInfo);
  111. dif = CInfo.size - Map->head.size;
  112. G_debug(1, "coor size in head = %lu, real coor file size= %lu",
  113. (unsigned long) Map->head.size, (unsigned long) CInfo.size);
  114. if (dif > 0) {
  115. G_warning(_("Coor file of vector map <%s@%s> is larger than it should be "
  116. "(%" PRI_OFF_T " bytes excess)"), Map->name, Map->mapset, dif);
  117. }
  118. else if (dif < 0) {
  119. G_warning(_("Coor file of vector <%s@%s> is shorter than it should be "
  120. "(%" PRI_OFF_T " bytes missing)."), Map->name, Map->mapset, -dif);
  121. }
  122. return 1;
  123. }