head.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. ****************************************************************************
  3. *
  4. * MODULE: Vector library
  5. *
  6. * AUTHOR(S): Original author CERL, probably Dave Gerdes.
  7. * Update to GRASS 5.7 Radim Blazek.
  8. *
  9. * PURPOSE: Lower level functions for reading/writing/manipulating vectors.
  10. *
  11. * COPYRIGHT: (C) 2001 by the GRASS Development Team
  12. *
  13. * This program is free software under the GNU General Public
  14. * License (>=v2). Read the file COPYING that comes with GRASS
  15. * for details.
  16. *
  17. *****************************************************************************/
  18. #include <sys/types.h>
  19. #include <string.h>
  20. #include <grass/vector.h>
  21. int dig__write_head(struct Map_info *Map)
  22. {
  23. unsigned char buf[10];
  24. long length = GV_COOR_HEAD_SIZE;
  25. G_debug(1, "dig__write_head()");
  26. dig_set_cur_port(&(Map->head.port));
  27. dig_fseek(&(Map->dig_fp), 0L, 0);
  28. /* bytes 1 - 5 */
  29. buf[0] = Map->head.coor_version.major;
  30. buf[1] = Map->head.coor_version.minor;
  31. buf[2] = Map->head.coor_version.back_major;
  32. buf[3] = Map->head.coor_version.back_minor;
  33. buf[4] = Map->head.port.byte_order;
  34. if (0 >= dig__fwrite_port_C((char *)buf, 5, &(Map->dig_fp)))
  35. return (0);
  36. /* increase header size for new vectors, already set in V1_open_new_nat() */
  37. length = Map->head.head_size;
  38. /* bytes 6 - 9 : header size */
  39. if (0 >= dig__fwrite_port_L(&length, 1, &(Map->dig_fp)))
  40. return (0);
  41. /* byte 10 : dimension 2D or 3D */
  42. buf[0] = Map->head.with_z;
  43. if (0 >= dig__fwrite_port_C((char *)buf, 1, &(Map->dig_fp)))
  44. return (0);
  45. /* bytes 11 - 18 : size of coordinate file */
  46. G_debug(1, "write coor size (%"PRI_OFF_T") to head", Map->head.size);
  47. if (Map->head.head_size >= GV_COOR_HEAD_SIZE + 4) {
  48. if (Map->head.size > PORT_LONG_MAX) {
  49. /* can only happen when sizeof(off_t) == 8 */
  50. if (0 >= dig__fwrite_port_O(&(Map->head.size), 1, &(Map->dig_fp), sizeof(off_t)))
  51. return (0);
  52. }
  53. else {
  54. /* write twice to fill the space and set offset (account for sizeof(off_t) == 4) */
  55. if (0 >= dig__fwrite_port_O(&(Map->head.size), 1, &(Map->dig_fp), 4))
  56. return (0);
  57. if (0 >= dig__fwrite_port_O(&(Map->head.size), 1, &(Map->dig_fp), 4))
  58. return (0);
  59. }
  60. }
  61. else {
  62. /* old vector with shorter coor head size got modified */
  63. /* bytes 11 - 14 : size of coordinate file */
  64. if (0 >= dig__fwrite_port_O(&(Map->head.size), 1, &(Map->dig_fp), 4))
  65. return (0);
  66. }
  67. G_debug(2, "coor body offset %"PRI_OFF_T, dig_ftell(&(Map->dig_fp)));
  68. return 1;
  69. }
  70. int dig__read_head(struct Map_info *Map)
  71. {
  72. unsigned char buf[10];
  73. struct Port_info port;
  74. G_debug(2, "dig__read_head(): name = '%s'", Map->name);
  75. dig_fseek(&(Map->dig_fp), 0L, 0);
  76. /* bytes 1 - 5 */
  77. if (0 >= dig__fread_port_C((char *)buf, 5, &(Map->dig_fp)))
  78. return (0);
  79. Map->head.coor_version.major = buf[0];
  80. Map->head.coor_version.minor = buf[1];
  81. Map->head.coor_version.back_major = buf[2];
  82. Map->head.coor_version.back_minor = buf[3];
  83. Map->head.port.byte_order = buf[4];
  84. G_debug(2, "Coor header: file version %d.%d , supported from GRASS version %d.%d",
  85. Map->head.coor_version.major, Map->head.coor_version.minor,
  86. Map->head.coor_version.back_major, Map->head.coor_version.back_minor);
  87. G_debug(2, " byte order %d", Map->head.port.byte_order);
  88. /* check version numbers */
  89. if (Map->head.coor_version.major > GV_COOR_VER_MAJOR ||
  90. Map->head.coor_version.minor > GV_COOR_VER_MINOR) {
  91. /* The file was created by GRASS library with higher version than this one */
  92. if (Map->head.coor_version.back_major > GV_COOR_VER_MAJOR ||
  93. Map->head.coor_version.back_minor > GV_COOR_VER_MINOR) {
  94. /* This version of GRASS lib is lower than the oldest which can read this format */
  95. G_fatal_error
  96. ("Vector 'coor' format version %d.%d is not supported by this version of GRASS. "
  97. "Update your GRASS.", Map->head.coor_version.major,
  98. Map->head.coor_version.minor);
  99. return (-1);
  100. }
  101. G_warning
  102. ("Your GRASS version does not fully support vector format %d.%d."
  103. " Consider to upgrade GRASS.", Map->head.coor_version.major,
  104. Map->head.coor_version.minor);
  105. }
  106. dig_init_portable(&port, Map->head.port.byte_order);
  107. dig_set_cur_port(&port);
  108. /* bytes 6 - 9 : header size */
  109. if (0 >= dig__fread_port_L(&(Map->head.head_size), 1, &(Map->dig_fp)))
  110. return (0);
  111. G_debug(2, " header size %ld", Map->head.head_size);
  112. /* byte 10 : dimension 2D or 3D */
  113. if (0 >= dig__fread_port_C((char *)buf, 1, &(Map->dig_fp)))
  114. return (0);
  115. Map->head.with_z = buf[0];
  116. G_debug(2, " with_z %d", Map->head.with_z);
  117. /* Map->head.size holds stats value */
  118. if (Map->head.size > PORT_LONG_MAX && Map->head.head_size >= GV_COOR_HEAD_SIZE + 4) {
  119. /* bytes 11 - 18 : size of coordinate file */
  120. if (0 >= dig__fread_port_O(&(Map->head.size), 1, &(Map->dig_fp), sizeof(off_t)))
  121. return (0);
  122. }
  123. else {
  124. /* bytes 11 - 14 : size of coordinate file */
  125. if (0 >= dig__fread_port_O(&(Map->head.size), 1, &(Map->dig_fp), 4))
  126. return (0);
  127. }
  128. G_debug(2, " coor size %"PRI_OFF_T, Map->head.size);
  129. /* Go to end of header, file may be written by new version of GRASS with longer header */
  130. dig_fseek(&(Map->dig_fp), Map->head.head_size, SEEK_SET);
  131. return (1);
  132. }