struct_alloc.c 8.2 KB

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