plus.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /**
  2. * \file plus.c
  3. *
  4. * \brief Vector library - update topo structure (lower level functions)
  5. *
  6. * Lower level functions for reading/writing/manipulating vectors.
  7. *
  8. * This program is free software under the GNU General Public License
  9. * (>=v2). Read the file COPYING that comes with GRASS for details.
  10. *
  11. * \author CERL (probably Dave Gerdes), Radim Blazek
  12. *
  13. * \date 2001-2006
  14. */
  15. #include <sys/types.h>
  16. #include <unistd.h>
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <grass/vector.h>
  20. #include <grass/glocale.h>
  21. /*!
  22. * \brief Initialize Plus_head structure
  23. *
  24. * \param[in,out] Plus pointer to Plus_head structure
  25. *
  26. * \return 1
  27. */
  28. int dig_init_plus(struct Plus_head *Plus)
  29. {
  30. G_debug(3, "dig_init_plus()");
  31. Plus->Version_Major = 0;
  32. Plus->Version_Minor = 0;
  33. Plus->Back_Major = 0;
  34. Plus->Back_Minor = 0;
  35. Plus->off_t_size = 0;
  36. Plus->box.N = 0;
  37. Plus->box.S = 0;
  38. Plus->box.E = 0;
  39. Plus->box.W = 0;
  40. Plus->box.T = 0;
  41. Plus->box.B = 0;
  42. Plus->built = GV_BUILD_NONE;
  43. Plus->Node = NULL;
  44. Plus->Line = NULL;
  45. Plus->Area = NULL;
  46. Plus->Isle = NULL;
  47. Plus->n_nodes = 0;
  48. Plus->n_edges = 0;
  49. Plus->n_lines = 0;
  50. Plus->n_areas = 0;
  51. Plus->n_isles = 0;
  52. Plus->n_volumes = 0;
  53. Plus->n_holes = 0;
  54. Plus->alloc_nodes = 0;
  55. Plus->alloc_edges = 0;
  56. Plus->alloc_lines = 0;
  57. Plus->alloc_areas = 0;
  58. Plus->alloc_isles = 0;
  59. Plus->alloc_volumes = 0;
  60. Plus->alloc_holes = 0;
  61. Plus->n_plines = 0;
  62. Plus->n_llines = 0;
  63. Plus->n_blines = 0;
  64. Plus->n_clines = 0;
  65. Plus->n_flines = 0;
  66. Plus->n_klines = 0;
  67. Plus->Node_offset = 0L;
  68. Plus->Line_offset = 0L;
  69. Plus->Area_offset = 0L;
  70. Plus->Isle_offset = 0L;
  71. Plus->Volume_offset = 0L;
  72. Plus->Hole_offset = 0L;
  73. Plus->Node_spidx_offset = 0L;
  74. Plus->Line_spidx_offset = 0L;
  75. Plus->Area_spidx_offset = 0L;
  76. Plus->Isle_spidx_offset = 0L;
  77. Plus->Face_spidx_offset = 0L;
  78. Plus->Volume_spidx_offset = 0L;
  79. Plus->Hole_spidx_offset = 0L;
  80. dig_spidx_init(Plus);
  81. dig_cidx_init(Plus);
  82. return 1;
  83. }
  84. /*!
  85. * \brief Free Plus->Node structure
  86. *
  87. * \param[in] Plus pointer to Plus_head structure
  88. */
  89. void dig_free_plus_nodes(struct Plus_head *Plus)
  90. {
  91. int i;
  92. struct P_node *Node;
  93. G_debug(2, "dig_free_plus_nodes()");
  94. /* Nodes */
  95. if (Plus->Node) { /* it may be that header only is loaded */
  96. for (i = 1; i <= Plus->n_nodes; i++) {
  97. Node = Plus->Node[i];
  98. if (Node == NULL)
  99. continue;
  100. dig_free_node(Node);
  101. }
  102. G_free(Plus->Node);
  103. }
  104. Plus->Node = NULL;
  105. Plus->n_nodes = 0;
  106. Plus->alloc_nodes = 0;
  107. }
  108. /*!
  109. * \brief Free Plus->Line structure
  110. *
  111. * \param[in] Plus pointer to Plus_head structure
  112. */
  113. void dig_free_plus_lines(struct Plus_head *Plus)
  114. {
  115. int i;
  116. struct P_line *Line;
  117. G_debug(2, "dig_free_plus_lines()");
  118. /* Lines */
  119. if (Plus->Line) { /* it may be that header only is loaded */
  120. for (i = 1; i <= Plus->n_lines; i++) {
  121. Line = Plus->Line[i];
  122. if (Line == NULL)
  123. continue;
  124. dig_free_line(Line);
  125. }
  126. G_free(Plus->Line);
  127. }
  128. Plus->Line = NULL;
  129. Plus->n_lines = 0;
  130. Plus->alloc_lines = 0;
  131. Plus->n_plines = 0;
  132. Plus->n_llines = 0;
  133. Plus->n_blines = 0;
  134. Plus->n_clines = 0;
  135. Plus->n_flines = 0;
  136. Plus->n_klines = 0;
  137. }
  138. /*!
  139. * \brief Free Plus->Area structure
  140. *
  141. * \param[in] Plus pointer to Plus_head structure
  142. */
  143. void dig_free_plus_areas(struct Plus_head *Plus)
  144. {
  145. int i;
  146. struct P_area *Area;
  147. G_debug(2, "dig_free_plus_areas()");
  148. /* Areas */
  149. if (Plus->Area) { /* it may be that header only is loaded */
  150. for (i = 1; i <= Plus->n_areas; i++) {
  151. Area = Plus->Area[i];
  152. if (Area == NULL)
  153. continue;
  154. dig_free_area(Area);
  155. }
  156. G_free(Plus->Area);
  157. }
  158. Plus->Area = NULL;
  159. Plus->n_areas = 0;
  160. Plus->alloc_areas = 0;
  161. }
  162. /*!
  163. * \brief Free Plus->Isle structure
  164. *
  165. * \param[in] Plus pointer to Plus_head structure
  166. */
  167. void dig_free_plus_isles(struct Plus_head *Plus)
  168. {
  169. int i;
  170. struct P_isle *Isle;
  171. G_debug(2, "dig_free_plus_isles()");
  172. /* Isles */
  173. if (Plus->Isle) { /* it may be that header only is loaded */
  174. for (i = 1; i <= Plus->n_isles; i++) {
  175. Isle = Plus->Isle[i];
  176. if (Isle == NULL)
  177. continue;
  178. dig_free_isle(Isle);
  179. }
  180. G_free(Plus->Isle);
  181. }
  182. Plus->Isle = NULL;
  183. Plus->n_isles = 0;
  184. Plus->alloc_isles = 0;
  185. }
  186. /*!
  187. * \brief Free Plus structure.
  188. *
  189. * Structure is not inited and dig_init_plus() should follow.
  190. *
  191. * \param[in] Plus pointer to Plus_head structure
  192. */
  193. void dig_free_plus(struct Plus_head *Plus)
  194. {
  195. G_debug(2, "dig_free_plus()");
  196. dig_free_plus_nodes(Plus);
  197. dig_free_plus_lines(Plus);
  198. dig_free_plus_areas(Plus);
  199. dig_free_plus_isles(Plus);
  200. dig_cidx_free(Plus);
  201. }
  202. /*!
  203. * \brief Reads topo file to topo structure.
  204. *
  205. * \param[in,out] Plus pointer to Plus_head structure
  206. * \param[in] plus topo file
  207. * \param[in] head_only read only head
  208. *
  209. * \return 1 on success
  210. * \return 0 on error
  211. */
  212. int dig_load_plus(struct Plus_head *Plus, struct gvfile * plus, int head_only)
  213. {
  214. int i;
  215. G_debug(1, "dig_load_plus()");
  216. /* TODO
  217. if (do_checks)
  218. dig_do_file_checks (map, map->plus_file, map->digit_file);
  219. */
  220. /* free and init old */
  221. dig_init_plus(Plus);
  222. /* Now let's begin reading the Plus file nodes, lines, areas and isles */
  223. if (dig_Rd_Plus_head(plus, Plus) == -1)
  224. return 0;
  225. if (head_only)
  226. return 1;
  227. dig_set_cur_port(&(Plus->port));
  228. /* Nodes */
  229. if (dig_fseek(plus, Plus->Node_offset, 0) == -1)
  230. G_fatal_error(_("Unable read topology for nodes"));
  231. dig_alloc_nodes(Plus, Plus->n_nodes);
  232. for (i = 1; i <= Plus->n_nodes; i++) {
  233. if (dig_Rd_P_node(Plus, i, plus) == -1)
  234. G_fatal_error(_("Unable to read topology for node %d"), i);
  235. }
  236. /* Lines */
  237. if (dig_fseek(plus, Plus->Line_offset, 0) == -1)
  238. G_fatal_error(_("Unable read topology for lines"));
  239. dig_alloc_lines(Plus, Plus->n_lines);
  240. for (i = 1; i <= Plus->n_lines; i++) {
  241. if (dig_Rd_P_line(Plus, i, plus) == -1)
  242. G_fatal_error(_("Unable to read topology for line %d"), i);
  243. }
  244. /* Areas */
  245. if (dig_fseek(plus, Plus->Area_offset, 0) == -1)
  246. G_fatal_error(_("Unable to read topo for areas"));
  247. dig_alloc_areas(Plus, Plus->n_areas);
  248. for (i = 1; i <= Plus->n_areas; i++) {
  249. if (dig_Rd_P_area(Plus, i, plus) == -1)
  250. G_fatal_error(_("Unable read topology for area %d"), i);
  251. }
  252. /* Isles */
  253. if (dig_fseek(plus, Plus->Isle_offset, 0) == -1)
  254. G_fatal_error(_("Unable to read topology for isles"));
  255. dig_alloc_isles(Plus, Plus->n_isles);
  256. for (i = 1; i <= Plus->n_isles; i++) {
  257. if (dig_Rd_P_isle(Plus, i, plus) == -1)
  258. G_fatal_error(_("Unable to read topology for isle %d"), i);
  259. }
  260. return (1);
  261. }
  262. /*!
  263. * \brief Writes topo structure to topo file
  264. *
  265. * \param[in,out] fp_plus topo file
  266. * \param[in] Plus pointer to Plus_head structure
  267. *
  268. * \return 0 on success
  269. * \return -1 on error
  270. */
  271. int dig_write_plus_file(struct gvfile * fp_plus, struct Plus_head *Plus)
  272. {
  273. dig_set_cur_port(&(Plus->port));
  274. dig_rewind(fp_plus);
  275. if (dig_Wr_Plus_head(fp_plus, Plus) < 0) {
  276. G_warning(_("Unable to write head to plus file"));
  277. return (-1);
  278. }
  279. if (dig_write_nodes(fp_plus, Plus) < 0) {
  280. G_warning(_("Unable to write nodes to plus file"));
  281. return (-1);
  282. }
  283. if (dig_write_lines(fp_plus, Plus) < 0) {
  284. G_warning(_("Unable to write lines to plus file"));
  285. return (-1);
  286. }
  287. if (dig_write_areas(fp_plus, Plus) < 0) {
  288. G_warning(_("Unable to write areas to plus file"));
  289. return (-1);
  290. }
  291. if (dig_write_isles(fp_plus, Plus) < 0) {
  292. G_warning(_("Unable to write isles to plus file"));
  293. return (-1);
  294. }
  295. dig_rewind(fp_plus);
  296. if (dig_Wr_Plus_head(fp_plus, Plus) < 0) {
  297. G_warning(_("Unable to write head to plus file"));
  298. return (-1);
  299. }
  300. dig_fflush(fp_plus);
  301. return (0);
  302. } /* write_plus_file() */
  303. /*!
  304. * \brief Writes topo structure (nodes) to topo file
  305. *
  306. * \param[in,out] fp_plus topo file
  307. * \param[in] Plus pointer to Plus_head structure
  308. *
  309. * \return 0 on success
  310. * \return -1 on error
  311. */
  312. int dig_write_nodes(struct gvfile * plus, struct Plus_head *Plus)
  313. {
  314. int i;
  315. Plus->Node_offset = dig_ftell(plus);
  316. for (i = 1; i <= Plus->n_nodes; i++) {
  317. if (dig_Wr_P_node(Plus, i, plus) < 0)
  318. return (-1);
  319. }
  320. return (0);
  321. } /* write_nodes() */
  322. /*!
  323. * \brief Writes topo structure (lines) to topo file
  324. *
  325. * \param[in,out] fp_plus topo file
  326. * \param[in] Plus pointer to Plus_head structure
  327. *
  328. * \return 0 on success
  329. * \return -1 on error
  330. */
  331. int dig_write_lines(struct gvfile * plus, struct Plus_head *Plus)
  332. {
  333. int i;
  334. Plus->Line_offset = dig_ftell(plus);
  335. for (i = 1; i <= Plus->n_lines; i++) {
  336. if (dig_Wr_P_line(Plus, i, plus) < 0)
  337. return (-1);
  338. }
  339. return (0);
  340. } /* write_line() */
  341. /*!
  342. * \brief Writes topo structure (areas) to topo file
  343. *
  344. * \param[in,out] fp_plus topo file
  345. * \param[in] Plus pointer to Plus_head structure
  346. *
  347. * \return 0 on success
  348. * \return -1 on error
  349. */
  350. int dig_write_areas(struct gvfile * plus, struct Plus_head *Plus)
  351. {
  352. int i;
  353. Plus->Area_offset = dig_ftell(plus);
  354. for (i = 1; i <= Plus->n_areas; i++) {
  355. if (dig_Wr_P_area(Plus, i, plus) < 0)
  356. return (-1);
  357. }
  358. return (0);
  359. } /* write_areas() */
  360. /*!
  361. * \brief Writes topo structure (isles) to topo file
  362. *
  363. * \param[in,out] fp_plus topo file
  364. * \param[in] Plus pointer to Plus_head structure
  365. *
  366. * \return 0 on success
  367. * \return -1 on error
  368. */
  369. int dig_write_isles(struct gvfile * plus, struct Plus_head *Plus)
  370. {
  371. int i;
  372. Plus->Isle_offset = dig_ftell(plus);
  373. for (i = 1; i <= Plus->n_isles; i++) {
  374. if (dig_Wr_P_isle(Plus, i, plus) < 0)
  375. return (-1);
  376. }
  377. return (0);
  378. } /* write_isles() */