struct_alloc.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*
  2. ****************************************************************************
  3. *
  4. * MODULE: Vector library
  5. *
  6. * AUTHOR(S): Dave Gerdes, CERL.
  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 <grass/config.h>
  19. #include <stdlib.h>
  20. #include <grass/Vect.h>
  21. /* These routines all eventually call calloc() to allocate and zero
  22. ** the new space. BUT It is not neccessarily safe to assume that
  23. ** the memory will be zero. The next memory location asked for could
  24. ** have been previously used and not zeroed. (e.g. compress())
  25. */
  26. /* alloc_node (map, add)
  27. ** alloc_line (map, add)
  28. ** alloc_area (map, add)
  29. ** alloc_points (map, num)
  30. ** node_alloc_line (node, add)
  31. ** area_alloc_line (node, add)
  32. **
  33. ** Allocate array space to add 'add' elements
  34. */
  35. /* allocate new node structure */
  36. P_NODE *dig_alloc_node()
  37. {
  38. P_NODE *Node;
  39. Node = (P_NODE *) G_malloc(sizeof(P_NODE));
  40. if (Node == NULL)
  41. return NULL;
  42. Node->n_lines = 0;
  43. Node->alloc_lines = 0;
  44. Node->lines = NULL;
  45. Node->angles = NULL;
  46. return (Node);
  47. }
  48. /* free node structure */
  49. void dig_free_node(P_NODE *Node)
  50. {
  51. if (Node->alloc_lines > 0) {
  52. G_free(Node->lines);
  53. G_free(Node->angles);
  54. }
  55. G_free(Node);
  56. }
  57. /* dig_node_alloc_line (node, add)
  58. ** allocate space in P_node, lines and angles arrays to add 'add' more
  59. ** lines
  60. **
  61. ** Returns 0 ok or -1 on error
  62. */
  63. int dig_node_alloc_line(P_NODE * node, int add)
  64. {
  65. int num;
  66. char *p;
  67. G_debug(3, "dig_node_alloc_line(): add = %d", add);
  68. num = node->n_lines + add;
  69. p = G_realloc(node->lines, num * sizeof(plus_t));
  70. if (p == NULL)
  71. return -1;
  72. node->lines = (plus_t *) p;
  73. p = G_realloc(node->angles, num * sizeof(float));
  74. if (p == NULL)
  75. return -1;
  76. node->angles = (float *)p;
  77. node->alloc_lines = num;
  78. return (0);
  79. }
  80. /* Reallocate array of pointers to nodes.
  81. * Space for 'add' number of nodes is added.
  82. *
  83. * Returns: 0 success
  84. * -1 error
  85. */
  86. int dig_alloc_nodes(struct Plus_head *Plus, int add)
  87. {
  88. int size;
  89. char *p;
  90. size = Plus->alloc_nodes + 1 + add;
  91. p = G_realloc(Plus->Node, size * sizeof(P_NODE *));
  92. if (p == NULL)
  93. return -1;
  94. Plus->Node = (P_NODE **) p;
  95. Plus->alloc_nodes = size - 1;
  96. return (0);
  97. }
  98. /* allocate new line structure */
  99. P_LINE *dig_alloc_line()
  100. {
  101. P_LINE *Line;
  102. Line = (P_LINE *) G_malloc(sizeof(P_LINE));
  103. if (Line == NULL)
  104. return NULL;
  105. return (Line);
  106. }
  107. /* free line structure */
  108. void dig_free_line(P_LINE *Line)
  109. {
  110. G_free(Line);
  111. }
  112. /* Reallocate array of pointers to lines.
  113. * Space for 'add' number of lines is added.
  114. *
  115. * Returns: 0 success
  116. * -1 error
  117. */
  118. int dig_alloc_lines(struct Plus_head *Plus, int add)
  119. {
  120. int size;
  121. char *p;
  122. size = Plus->alloc_lines + 1 + add;
  123. p = G_realloc(Plus->Line, size * sizeof(P_LINE *));
  124. if (p == NULL)
  125. return -1;
  126. Plus->Line = (P_LINE **) p;
  127. Plus->alloc_lines = size - 1;
  128. return (0);
  129. }
  130. /* Reallocate array of pointers to areas.
  131. * Space for 'add' number of areas is added.
  132. *
  133. * Returns: 0 success
  134. * -1 error
  135. */
  136. int dig_alloc_areas(struct Plus_head *Plus, int add)
  137. {
  138. int size;
  139. char *p;
  140. size = Plus->alloc_areas + 1 + add;
  141. p = G_realloc(Plus->Area, size * sizeof(P_AREA *));
  142. if (p == NULL)
  143. return -1;
  144. Plus->Area = (P_AREA **) p;
  145. Plus->alloc_areas = size - 1;
  146. return (0);
  147. }
  148. /* Reallocate array of pointers to isles.
  149. * Space for 'add' number of isles is added.
  150. *
  151. * Returns: 0 success
  152. * -1 error
  153. */
  154. int dig_alloc_isles(struct Plus_head *Plus, int add)
  155. {
  156. int size;
  157. char *p;
  158. G_debug(3, "dig_alloc_isle():");
  159. size = Plus->alloc_isles + 1 + add;
  160. p = G_realloc(Plus->Isle, size * sizeof(P_ISLE *));
  161. if (p == NULL)
  162. return -1;
  163. Plus->Isle = (P_ISLE **) p;
  164. Plus->alloc_isles = size - 1;
  165. return (0);
  166. }
  167. /* allocate new area structure */
  168. P_AREA *dig_alloc_area()
  169. {
  170. P_AREA *Area;
  171. Area = (P_AREA *) G_malloc(sizeof(P_AREA));
  172. if (Area == NULL)
  173. return NULL;
  174. Area->n_lines = 0;
  175. Area->alloc_lines = 0;
  176. Area->lines = NULL;
  177. Area->alloc_isles = 0;
  178. Area->n_isles = 0;
  179. Area->isles = NULL;
  180. Area->centroid = 0;
  181. return (Area);
  182. }
  183. /* free area structure */
  184. void dig_free_area(P_AREA *Area)
  185. {
  186. if (Area->alloc_lines > 0)
  187. free(Area->lines);
  188. if (Area->alloc_isles > 0)
  189. free(Area->isles);
  190. G_free(Area);
  191. }
  192. /* alloc new isle structure */
  193. P_ISLE *dig_alloc_isle()
  194. {
  195. P_ISLE *Isle;
  196. Isle = (P_ISLE *) G_malloc(sizeof(P_ISLE));
  197. if (Isle == NULL)
  198. return NULL;
  199. Isle->n_lines = 0;
  200. Isle->alloc_lines = 0;
  201. Isle->lines = NULL;
  202. Isle->area = 0;
  203. return (Isle);
  204. }
  205. /* free isle structure */
  206. void dig_free_isle(P_ISLE *Isle)
  207. {
  208. if (Isle->alloc_lines > 0)
  209. G_free(Isle->lines);
  210. G_free(Isle);
  211. }
  212. /* allocate room for 'num' X and Y arrays in struct line_pnts
  213. ** returns -1 on out of memory
  214. */
  215. int dig_alloc_points(struct line_pnts *points, int num)
  216. {
  217. int alloced;
  218. char *p;
  219. alloced = points->alloc_points;
  220. /* alloc_space will just return if no space is needed */
  221. if (!(p =
  222. dig__alloc_space(num, &alloced, 50, (char *)points->x,
  223. sizeof(double)))) {
  224. return (dig_out_of_memory());
  225. }
  226. points->x = (double *)p;
  227. alloced = points->alloc_points;
  228. /* alloc_space will just return if no space is needed */
  229. if (!(p =
  230. dig__alloc_space(num, &alloced, 50, (char *)points->y,
  231. sizeof(double)))) {
  232. return (dig_out_of_memory());
  233. }
  234. points->y = (double *)p;
  235. alloced = points->alloc_points;
  236. /* alloc_space will just return if no space is needed */
  237. if (!(p =
  238. dig__alloc_space(num, &alloced, 50, (char *)points->z,
  239. sizeof(double)))) {
  240. return (dig_out_of_memory());
  241. }
  242. points->z = (double *)p;
  243. points->alloc_points = alloced;
  244. return (0);
  245. }
  246. /* allocate room for 'num' fields and category arrays
  247. ** in struct line_cats
  248. ** returns -1 on out of memory
  249. */
  250. int dig_alloc_cats(struct line_cats *cats, int num)
  251. {
  252. int alloced;
  253. char *p;
  254. /* alloc_space will just return if no space is needed */
  255. alloced = cats->alloc_cats;
  256. if (!(p =
  257. dig__alloc_space(num, &alloced, 1, (int *)cats->field,
  258. sizeof(int)))) {
  259. return (dig_out_of_memory());
  260. }
  261. cats->field = (int *)p;
  262. alloced = cats->alloc_cats;
  263. if (!(p =
  264. dig__alloc_space(num, &alloced, 1, (int *)cats->cat,
  265. sizeof(int)))) {
  266. return (dig_out_of_memory());
  267. }
  268. cats->cat = (int *)p;
  269. cats->alloc_cats = alloced;
  270. return (0);
  271. }
  272. /* area_alloc_line (area, add)
  273. ** allocate space in P_area for add new lines
  274. **
  275. ** Returns 0 ok or -1 on error
  276. */
  277. int dig_area_alloc_line(P_AREA * area, int add)
  278. {
  279. int num;
  280. char *p;
  281. num = area->alloc_lines + add;
  282. p = G_realloc(area->lines, num * sizeof(plus_t));
  283. if (p == NULL)
  284. return -1;
  285. area->lines = (plus_t *) p;
  286. area->alloc_lines = num;
  287. return (0);
  288. }
  289. /* area_alloc_isle (area, add)
  290. ** allocate space in P_area for add new isles
  291. **
  292. ** Returns 0 ok or -1 on error
  293. */
  294. int dig_area_alloc_isle(P_AREA * area, int add)
  295. {
  296. int num;
  297. char *p;
  298. G_debug(5, "dig_area_alloc_isle(): add = %d", add);
  299. num = area->alloc_isles + add;
  300. p = G_realloc(area->isles, num * sizeof(plus_t));
  301. if (p == NULL)
  302. return -1;
  303. area->isles = (plus_t *) p;
  304. area->alloc_isles = num;
  305. return (0);
  306. }
  307. /* dig_isle_alloc_line (isle, add)
  308. ** allocate space in P_isle for add new lines
  309. **
  310. ** Returns 0 ok or -1 on error
  311. */
  312. int dig_isle_alloc_line(P_ISLE * isle, int add)
  313. {
  314. int num;
  315. char *p;
  316. G_debug(3, "dig_isle_alloc_line():");
  317. num = isle->alloc_lines + add;
  318. p = G_realloc(isle->lines, num * sizeof(plus_t));
  319. if (p == NULL)
  320. return -1;
  321. isle->lines = (plus_t *) p;
  322. isle->alloc_lines = num;
  323. return (0);
  324. }
  325. /* for now just print message and return error code */
  326. int dig_out_of_memory()
  327. {
  328. fprintf(stderr, "OUT OF MEMORY!\n");
  329. return (-1);
  330. }