struct_alloc.c 8.7 KB

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