cindex_rw.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /****************************************************************************
  2. *
  3. * MODULE: Vector library
  4. *
  5. * AUTHOR(S): Radim Blazek.
  6. *
  7. * PURPOSE: Lower level functions for reading/writing/manipulating vectors.
  8. *
  9. * COPYRIGHT: (C) 2001 by the GRASS Development Team
  10. *
  11. * This program is free software under the GNU General Public
  12. * License (>=v2). Read the file COPYING that comes with GRASS
  13. * for details.
  14. *
  15. *****************************************************************************/
  16. #include <grass/config.h>
  17. #include <stdlib.h>
  18. #include <sys/types.h>
  19. #include <string.h>
  20. #include <grass/gis.h>
  21. #include <grass/vector.h>
  22. int dig_write_cidx_head(struct gvfile * fp, struct Plus_head *plus)
  23. {
  24. int i;
  25. unsigned char buf[5];
  26. long length = 9;
  27. G_debug(3, "dig_write_cidx_head()");
  28. dig_rewind(fp);
  29. dig_set_cur_port(&(plus->cidx_port));
  30. /* Head of header */
  31. /* bytes 1 - 5 */
  32. buf[0] = GV_CIDX_VER_MAJOR;
  33. buf[1] = GV_CIDX_VER_MINOR;
  34. buf[2] = GV_CIDX_EARLIEST_MAJOR;
  35. buf[3] = GV_CIDX_EARLIEST_MINOR;
  36. buf[4] = plus->cidx_port.byte_order;
  37. if (0 >= dig__fwrite_port_C(buf, 5, fp))
  38. return (-1);
  39. /* get required offset size */
  40. if (plus->off_t_size == 0) {
  41. /* should not happen, topo is written first */
  42. if (plus->coor_size > (off_t)PORT_LONG_MAX)
  43. plus->off_t_size = 8;
  44. else
  45. plus->off_t_size = 4;
  46. }
  47. /* bytes 6 - 9 : header size */
  48. if (0 >= dig__fwrite_port_L(&length, 1, fp))
  49. return (0);
  50. /* Body of header - info about all fields */
  51. /* Number of fields */
  52. if (0 >= dig__fwrite_port_I(&(plus->n_cidx), 1, fp))
  53. return (-1);
  54. for (i = 0; i < plus->n_cidx; i++) {
  55. int t;
  56. struct Cat_index *ci;
  57. ci = &(plus->cidx[i]);
  58. G_debug(3, "cidx %d head offset: %"PRI_OFF_T, i, dig_ftell(fp));
  59. /* Field number */
  60. if (0 >= dig__fwrite_port_I(&(ci->field), 1, fp))
  61. return (-1);
  62. /* Number of categories */
  63. if (0 >= dig__fwrite_port_I(&(ci->n_cats), 1, fp))
  64. return (-1);
  65. /* Number of unique categories */
  66. if (0 >= dig__fwrite_port_I(&(ci->n_ucats), 1, fp))
  67. return (-1);
  68. /* Number of types */
  69. if (0 >= dig__fwrite_port_I(&(ci->n_types), 1, fp))
  70. return (-1);
  71. /* Types */
  72. for (t = 0; t < ci->n_types; t++) {
  73. int wtype;
  74. /* type */
  75. wtype = dig_type_to_store(ci->type[t][0]);
  76. if (0 >= dig__fwrite_port_I(&wtype, 1, fp))
  77. return (-1);
  78. /* number of items */
  79. if (0 >= dig__fwrite_port_I(&(ci->type[t][1]), 1, fp))
  80. return (-1);
  81. }
  82. /* Offset */
  83. if (0 >= dig__fwrite_port_O(&(ci->offset), 1, fp, plus->off_t_size))
  84. return (0);
  85. G_debug(3, "cidx %d offset: %"PRI_OFF_T, i, ci->offset);
  86. }
  87. G_debug(3, "cidx body offset %"PRI_OFF_T, dig_ftell(fp));
  88. return (0);
  89. }
  90. /*!
  91. \brief Read header of cidx file
  92. \param fp pointer to gvfile structure
  93. \param plus pointer to Plus_head strcuture
  94. \return 0 OK
  95. \return -1 error
  96. */
  97. int dig_read_cidx_head(struct gvfile * fp, struct Plus_head *plus)
  98. {
  99. unsigned char buf[5];
  100. int i, byte_order;
  101. dig_rewind(fp);
  102. /* bytes 1 - 5 */
  103. if (0 >= dig__fread_port_C(buf, 5, fp))
  104. return (-1);
  105. plus->cidx_Version_Major = buf[0];
  106. plus->cidx_Version_Minor = buf[1];
  107. plus->cidx_Back_Major = buf[2];
  108. plus->cidx_Back_Minor = buf[3];
  109. byte_order = buf[4];
  110. G_debug(3,
  111. "Cidx header: file version %d.%d , supported from GRASS version %d.%d",
  112. plus->cidx_Version_Major, plus->cidx_Version_Minor,
  113. plus->cidx_Back_Major, plus->cidx_Back_Minor);
  114. G_debug(3, " byte order %d", byte_order);
  115. /* check version numbers */
  116. if (plus->cidx_Version_Major > GV_CIDX_VER_MAJOR ||
  117. plus->cidx_Version_Minor > GV_CIDX_VER_MINOR) {
  118. /* The file was created by GRASS library with higher version than this one */
  119. if (plus->cidx_Back_Major > GV_CIDX_VER_MAJOR ||
  120. plus->cidx_Back_Minor > GV_CIDX_VER_MINOR) {
  121. /* This version of GRASS lib is lower than the oldest which can read this format */
  122. G_fatal_error
  123. ("Category index format version %d.%d is not supported by this release."
  124. " Try to rebuild topology or upgrade GRASS.",
  125. plus->cidx_Version_Major, plus->cidx_Version_Minor);
  126. return (-1);
  127. }
  128. G_warning
  129. ("Your GRASS version does not fully support category index format %d.%d of the vector."
  130. " Consider to rebuild topology or upgrade GRASS.",
  131. plus->cidx_Version_Major, plus->cidx_Version_Minor);
  132. }
  133. dig_init_portable(&(plus->cidx_port), byte_order);
  134. dig_set_cur_port(&(plus->cidx_port));
  135. /* bytes 6 - 9 : header size */
  136. if (0 >= dig__fread_port_L(&(plus->cidx_head_size), 1, fp))
  137. return (-1);
  138. G_debug(3, " header size %ld", plus->cidx_head_size);
  139. /* get required offset size */
  140. if (plus->off_t_size == 0) {
  141. /* should not happen, topo is opened first */
  142. if (plus->coor_size > (off_t)PORT_LONG_MAX)
  143. plus->off_t_size = 8;
  144. else
  145. plus->off_t_size = 4;
  146. }
  147. /* Body of header - info about all fields */
  148. /* Number of fields */
  149. if (0 >= dig__fread_port_I(&(plus->n_cidx), 1, fp))
  150. return (-1);
  151. /* alloc space */
  152. plus->a_cidx = plus->n_cidx;
  153. plus->cidx =
  154. (struct Cat_index *)G_malloc(plus->a_cidx * sizeof(struct Cat_index));
  155. for (i = 0; i < plus->n_cidx; i++) {
  156. int t;
  157. struct Cat_index *ci;
  158. ci = &(plus->cidx[i]);
  159. ci->cat = NULL;
  160. ci->a_cats = 0;
  161. /* Field number */
  162. if (0 >= dig__fread_port_I(&(ci->field), 1, fp))
  163. return (-1);
  164. /* Number of categories */
  165. if (0 >= dig__fread_port_I(&(ci->n_cats), 1, fp))
  166. return (-1);
  167. /* Number of unique categories */
  168. if (0 >= dig__fread_port_I(&(ci->n_ucats), 1, fp))
  169. return (-1);
  170. /* Number of types */
  171. if (0 >= dig__fread_port_I(&(ci->n_types), 1, fp))
  172. return (-1);
  173. /* Types */
  174. for (t = 0; t < ci->n_types; t++) {
  175. int rtype;
  176. /* type */
  177. if (0 >= dig__fread_port_I(&rtype, 1, fp))
  178. return (-1);
  179. ci->type[t][0] = dig_type_from_store(rtype);
  180. /* number of items */
  181. if (0 >= dig__fread_port_I(&(ci->type[t][1]), 1, fp))
  182. return (-1);
  183. }
  184. /* Offset */
  185. if (0 >= dig__fread_port_O(&(ci->offset), 1, fp, plus->off_t_size))
  186. return (0);
  187. }
  188. if (dig_fseek(fp, plus->cidx_head_size, SEEK_SET) == -1)
  189. return (-1);
  190. return (0);
  191. }
  192. /* Write spatial index */
  193. int dig_write_cidx(struct gvfile * fp, struct Plus_head *plus)
  194. {
  195. int i;
  196. dig_set_cur_port(&(plus->cidx_port));
  197. dig_rewind(fp);
  198. dig_write_cidx_head(fp, plus);
  199. /* Write category-type-id for each field */
  200. for (i = 0; i < plus->n_cidx; i++) {
  201. int j;
  202. struct Cat_index *ci;
  203. ci = &(plus->cidx[i]);
  204. ci->offset = dig_ftell(fp);
  205. /* convert type */
  206. for (j = 0; j < ci->n_cats; j++)
  207. ci->cat[j][1] = dig_type_to_store(ci->cat[j][1]);
  208. if (0 >= dig__fwrite_port_I((int *)ci->cat, 3 * ci->n_cats, fp))
  209. return (-1);
  210. /* Return back */
  211. for (j = 0; j < ci->n_cats; j++)
  212. ci->cat[j][1] = dig_type_from_store(ci->cat[j][1]);
  213. }
  214. dig_write_cidx_head(fp, plus); /* rewrite with offsets */
  215. return 0;
  216. }
  217. /*!
  218. \brief Read spatial index file
  219. \param fp pointer to gvfile structure
  220. \param[in,out] plus pointer to Plus_head structure
  221. \param head_only non-zero to read only head
  222. \return 0 OK
  223. \return 1 error
  224. */
  225. int dig_read_cidx(struct gvfile * fp, struct Plus_head *plus, int head_only)
  226. {
  227. int i;
  228. G_debug(3, "dig_read_cidx()");
  229. dig_cidx_init(plus);
  230. dig_rewind(fp);
  231. if (dig_read_cidx_head(fp, plus) == -1) {
  232. G_debug(3, "Cannot read cidx head");
  233. return 1;
  234. }
  235. if (head_only) {
  236. plus->cidx_up_to_date = 1; /* OK ? */
  237. return 0;
  238. }
  239. dig_set_cur_port(&(plus->cidx_port));
  240. /* Read category-type-id for each field */
  241. for (i = 0; i < plus->n_cidx; i++) {
  242. int j;
  243. struct Cat_index *ci;
  244. ci = &(plus->cidx[i]);
  245. ci->a_cats = ci->n_cats;
  246. ci->cat = G_malloc(ci->a_cats * 3 * sizeof(int));
  247. if (dig_fseek(fp, ci->offset, 0) == -1)
  248. return 1;
  249. if (0 >= dig__fread_port_I((int *)ci->cat, 3 * ci->n_cats, fp))
  250. return 1;
  251. /* convert type */
  252. for (j = 0; j < ci->n_cats; j++)
  253. ci->cat[j][1] = dig_type_from_store(ci->cat[j][1]);
  254. }
  255. plus->cidx_up_to_date = 1;
  256. return 0;
  257. }