struct_alloc.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. /* allocate new node structure */
  35. P_NODE *dig_alloc_node()
  36. {
  37. P_NODE *Node;
  38. Node = (P_NODE *) malloc(sizeof(P_NODE));
  39. if (Node == NULL)
  40. return NULL;
  41. Node->n_lines = 0;
  42. Node->alloc_lines = 0;
  43. Node->lines = NULL;
  44. Node->angles = NULL;
  45. return (Node);
  46. }
  47. /* dig_node_alloc_line (node, add)
  48. ** allocate space in P_node, lines and angles arrays to add 'add' more
  49. ** lines
  50. **
  51. ** Returns 0 ok or -1 on error
  52. */
  53. int dig_node_alloc_line(P_NODE * node, int add)
  54. {
  55. int num;
  56. char *p;
  57. G_debug(3, "dig_node_alloc_line(): add = %d", add);
  58. num = node->n_lines + add;
  59. p = realloc(node->lines, num * sizeof(plus_t));
  60. if (p == NULL)
  61. return -1;
  62. node->lines = (plus_t *) p;
  63. p = realloc(node->angles, num * sizeof(float));
  64. if (p == NULL)
  65. return -1;
  66. node->angles = (float *)p;
  67. node->alloc_lines = num;
  68. return (0);
  69. }
  70. /* Reallocate array of pointers to nodes.
  71. * Space for 'add' number of nodes is added.
  72. *
  73. * Returns: 0 success
  74. * -1 error
  75. */
  76. int dig_alloc_nodes(struct Plus_head *Plus, int add)
  77. {
  78. int size;
  79. char *p;
  80. size = Plus->alloc_nodes + 1 + add;
  81. p = realloc(Plus->Node, size * sizeof(P_NODE *));
  82. if (p == NULL)
  83. return -1;
  84. Plus->Node = (P_NODE **) p;
  85. Plus->alloc_nodes = size - 1;
  86. return (0);
  87. }
  88. /* allocate new line structure */
  89. P_LINE *dig_alloc_line()
  90. {
  91. P_LINE *Line;
  92. Line = (P_LINE *) malloc(sizeof(P_LINE));
  93. if (Line == NULL)
  94. return NULL;
  95. return (Line);
  96. }
  97. /* Reallocate array of pointers to lines.
  98. * Space for 'add' number of lines is added.
  99. *
  100. * Returns: 0 success
  101. * -1 error
  102. */
  103. int dig_alloc_lines(struct Plus_head *Plus, int add)
  104. {
  105. int size;
  106. char *p;
  107. size = Plus->alloc_lines + 1 + add;
  108. p = realloc(Plus->Line, size * sizeof(P_LINE *));
  109. if (p == NULL)
  110. return -1;
  111. Plus->Line = (P_LINE **) p;
  112. Plus->alloc_lines = size - 1;
  113. return (0);
  114. }
  115. /* Reallocate array of pointers to areas.
  116. * Space for 'add' number of areas is added.
  117. *
  118. * Returns: 0 success
  119. * -1 error
  120. */
  121. int dig_alloc_areas(struct Plus_head *Plus, int add)
  122. {
  123. int size;
  124. char *p;
  125. size = Plus->alloc_areas + 1 + add;
  126. p = realloc(Plus->Area, size * sizeof(P_AREA *));
  127. if (p == NULL)
  128. return -1;
  129. Plus->Area = (P_AREA **) p;
  130. Plus->alloc_areas = size - 1;
  131. return (0);
  132. }
  133. /* Reallocate array of pointers to isles.
  134. * Space for 'add' number of isles is added.
  135. *
  136. * Returns: 0 success
  137. * -1 error
  138. */
  139. int dig_alloc_isles(struct Plus_head *Plus, int add)
  140. {
  141. int size;
  142. char *p;
  143. G_debug(3, "dig_alloc_isle():");
  144. size = Plus->alloc_isles + 1 + add;
  145. p = realloc(Plus->Isle, size * sizeof(P_ISLE *));
  146. if (p == NULL)
  147. return -1;
  148. Plus->Isle = (P_ISLE **) p;
  149. Plus->alloc_isles = size - 1;
  150. return (0);
  151. }
  152. /* allocate new area structure */
  153. P_AREA *dig_alloc_area()
  154. {
  155. P_AREA *Area;
  156. Area = (P_AREA *) malloc(sizeof(P_AREA));
  157. if (Area == NULL)
  158. return NULL;
  159. Area->n_lines = 0;
  160. Area->alloc_lines = 0;
  161. Area->lines = NULL;
  162. Area->alloc_isles = 0;
  163. Area->n_isles = 0;
  164. Area->isles = NULL;
  165. Area->centroid = 0;
  166. return (Area);
  167. }
  168. /* alloc new isle structure */
  169. P_ISLE *dig_alloc_isle()
  170. {
  171. P_ISLE *Isle;
  172. Isle = (P_ISLE *) malloc(sizeof(P_ISLE));
  173. if (Isle == NULL)
  174. return NULL;
  175. Isle->n_lines = 0;
  176. Isle->alloc_lines = 0;
  177. Isle->lines = NULL;
  178. Isle->area = 0;
  179. return (Isle);
  180. }
  181. /* allocate room for 'num' X and Y arrays in struct line_pnts
  182. ** returns -1 on out of memory
  183. */
  184. int dig_alloc_points(struct line_pnts *points, int num)
  185. {
  186. int alloced;
  187. char *p;
  188. alloced = points->alloc_points;
  189. /* alloc_space will just return if no space is needed */
  190. if (!(p =
  191. dig__alloc_space(num, &alloced, 50, (char *)points->x,
  192. sizeof(double)))) {
  193. return (dig_out_of_memory());
  194. }
  195. points->x = (double *)p;
  196. alloced = points->alloc_points;
  197. /* alloc_space will just return if no space is needed */
  198. if (!(p =
  199. dig__alloc_space(num, &alloced, 50, (char *)points->y,
  200. sizeof(double)))) {
  201. return (dig_out_of_memory());
  202. }
  203. points->y = (double *)p;
  204. alloced = points->alloc_points;
  205. /* alloc_space will just return if no space is needed */
  206. if (!(p =
  207. dig__alloc_space(num, &alloced, 50, (char *)points->z,
  208. sizeof(double)))) {
  209. return (dig_out_of_memory());
  210. }
  211. points->z = (double *)p;
  212. points->alloc_points = alloced;
  213. return (0);
  214. }
  215. /* allocate room for 'num' fields and category arrays
  216. ** in struct line_cats
  217. ** returns -1 on out of memory
  218. */
  219. int dig_alloc_cats(struct line_cats *cats, int num)
  220. {
  221. int alloced;
  222. char *p;
  223. /* alloc_space will just return if no space is needed */
  224. alloced = cats->alloc_cats;
  225. if (!(p =
  226. dig__alloc_space(num, &alloced, 1, (int *)cats->field,
  227. sizeof(int)))) {
  228. return (dig_out_of_memory());
  229. }
  230. cats->field = (int *)p;
  231. alloced = cats->alloc_cats;
  232. if (!(p =
  233. dig__alloc_space(num, &alloced, 1, (int *)cats->cat,
  234. sizeof(int)))) {
  235. return (dig_out_of_memory());
  236. }
  237. cats->cat = (int *)p;
  238. cats->alloc_cats = alloced;
  239. return (0);
  240. }
  241. /* area_alloc_line (area, add)
  242. ** allocate space in P_area for add new lines
  243. **
  244. ** Returns 0 ok or -1 on error
  245. */
  246. int dig_area_alloc_line(P_AREA * area, int add)
  247. {
  248. int num;
  249. char *p;
  250. num = area->alloc_lines + add;
  251. p = realloc(area->lines, num * sizeof(plus_t));
  252. if (p == NULL)
  253. return -1;
  254. area->lines = (plus_t *) p;
  255. area->alloc_lines = num;
  256. return (0);
  257. }
  258. /* area_alloc_isle (area, add)
  259. ** allocate space in P_area for add new isles
  260. **
  261. ** Returns 0 ok or -1 on error
  262. */
  263. int dig_area_alloc_isle(P_AREA * area, int add)
  264. {
  265. int num;
  266. char *p;
  267. G_debug(5, "dig_area_alloc_isle(): add = %d", add);
  268. num = area->alloc_isles + add;
  269. p = realloc(area->isles, num * sizeof(plus_t));
  270. if (p == NULL)
  271. return -1;
  272. area->isles = (plus_t *) p;
  273. area->alloc_isles = num;
  274. return (0);
  275. }
  276. /* dig_isle_alloc_line (isle, add)
  277. ** allocate space in P_isle for add new lines
  278. **
  279. ** Returns 0 ok or -1 on error
  280. */
  281. int dig_isle_alloc_line(P_ISLE * isle, int add)
  282. {
  283. int num;
  284. char *p;
  285. G_debug(3, "dig_isle_alloc_line():");
  286. num = isle->alloc_lines + add;
  287. p = realloc(isle->lines, num * sizeof(plus_t));
  288. if (p == NULL)
  289. return -1;
  290. isle->lines = (plus_t *) p;
  291. isle->alloc_lines = num;
  292. return (0);
  293. }
  294. /* for now just print message and return error code */
  295. int dig_out_of_memory()
  296. {
  297. fprintf(stderr, "OUT OF MEMORY!\n");
  298. return (-1);
  299. }