struct_alloc.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. Line->topo = NULL;
  105. return (Line);
  106. }
  107. /* allocate new topo structure */
  108. void *dig_alloc_topo(char type)
  109. {
  110. void *Topo = NULL;
  111. switch (type) {
  112. case GV_LINE:
  113. Topo = G_malloc(sizeof(struct P_topo_l));
  114. break;
  115. case GV_BOUNDARY:
  116. Topo = G_malloc(sizeof(struct P_topo_b));
  117. break;
  118. case GV_CENTROID:
  119. Topo = G_malloc(sizeof(struct P_topo_c));
  120. break;
  121. case GV_FACE:
  122. Topo = G_malloc(sizeof(struct P_topo_f));
  123. break;
  124. case GV_KERNEL:
  125. Topo = G_malloc(sizeof(struct P_topo_k));
  126. break;
  127. default:
  128. return NULL;
  129. }
  130. return (Topo);
  131. }
  132. /* free line structure */
  133. void dig_free_line(struct P_line *Line)
  134. {
  135. if (Line->topo)
  136. G_free(Line->topo);
  137. G_free(Line);
  138. }
  139. /* Reallocate array of pointers to lines.
  140. * Space for 'add' number of lines is added.
  141. *
  142. * Returns: 0 success
  143. * -1 error
  144. */
  145. int dig_alloc_lines(struct Plus_head *Plus, int add)
  146. {
  147. int size;
  148. char *p;
  149. size = Plus->alloc_lines + 1 + add;
  150. p = G_realloc(Plus->Line, size * sizeof(struct P_line *));
  151. if (p == NULL)
  152. return -1;
  153. Plus->Line = (struct P_line **) p;
  154. Plus->alloc_lines = size - 1;
  155. return (0);
  156. }
  157. /* Reallocate array of pointers to areas.
  158. * Space for 'add' number of areas is added.
  159. *
  160. * Returns: 0 success
  161. * -1 error
  162. */
  163. int dig_alloc_areas(struct Plus_head *Plus, int add)
  164. {
  165. int size;
  166. char *p;
  167. size = Plus->alloc_areas + 1 + add;
  168. p = G_realloc(Plus->Area, size * sizeof(struct P_area *));
  169. if (p == NULL)
  170. return -1;
  171. Plus->Area = (struct P_area **) p;
  172. Plus->alloc_areas = size - 1;
  173. return (0);
  174. }
  175. /* Reallocate array of pointers to isles.
  176. * Space for 'add' number of isles is added.
  177. *
  178. * Returns: 0 success
  179. * -1 error
  180. */
  181. int dig_alloc_isles(struct Plus_head *Plus, int add)
  182. {
  183. int size;
  184. char *p;
  185. G_debug(3, "dig_alloc_isle():");
  186. size = Plus->alloc_isles + 1 + add;
  187. p = G_realloc(Plus->Isle, size * sizeof(struct P_isle *));
  188. if (p == NULL)
  189. return -1;
  190. Plus->Isle = (struct P_isle **) p;
  191. Plus->alloc_isles = size - 1;
  192. return (0);
  193. }
  194. /* allocate new area structure */
  195. struct P_area *dig_alloc_area()
  196. {
  197. struct P_area *Area;
  198. Area = (struct P_area *) G_malloc(sizeof(struct P_area));
  199. if (Area == NULL)
  200. return NULL;
  201. Area->n_lines = 0;
  202. Area->alloc_lines = 0;
  203. Area->lines = NULL;
  204. Area->alloc_isles = 0;
  205. Area->n_isles = 0;
  206. Area->isles = NULL;
  207. Area->centroid = 0;
  208. return (Area);
  209. }
  210. /* free area structure */
  211. void dig_free_area(struct P_area *Area)
  212. {
  213. if (Area->alloc_lines > 0)
  214. free(Area->lines);
  215. if (Area->alloc_isles > 0)
  216. free(Area->isles);
  217. G_free(Area);
  218. }
  219. /* alloc new isle structure */
  220. struct P_isle *dig_alloc_isle()
  221. {
  222. struct P_isle *Isle;
  223. Isle = (struct P_isle *) G_malloc(sizeof(struct P_isle));
  224. if (Isle == NULL)
  225. return NULL;
  226. Isle->n_lines = 0;
  227. Isle->alloc_lines = 0;
  228. Isle->lines = NULL;
  229. Isle->area = 0;
  230. return (Isle);
  231. }
  232. /* free isle structure */
  233. void dig_free_isle(struct P_isle *Isle)
  234. {
  235. if (Isle->alloc_lines > 0)
  236. G_free(Isle->lines);
  237. G_free(Isle);
  238. }
  239. /* allocate room for 'num' X and Y arrays in struct line_pnts
  240. ** returns -1 on out of memory
  241. */
  242. int dig_alloc_points(struct line_pnts *points, int num)
  243. {
  244. int alloced;
  245. char *p;
  246. alloced = points->alloc_points;
  247. /* alloc_space will just return if no space is needed */
  248. if (!(p =
  249. dig__alloc_space(num, &alloced, 50, (char *)points->x,
  250. sizeof(double)))) {
  251. return (dig_out_of_memory());
  252. }
  253. points->x = (double *)p;
  254. alloced = points->alloc_points;
  255. /* alloc_space will just return if no space is needed */
  256. if (!(p =
  257. dig__alloc_space(num, &alloced, 50, (char *)points->y,
  258. sizeof(double)))) {
  259. return (dig_out_of_memory());
  260. }
  261. points->y = (double *)p;
  262. alloced = points->alloc_points;
  263. /* alloc_space will just return if no space is needed */
  264. if (!(p =
  265. dig__alloc_space(num, &alloced, 50, (char *)points->z,
  266. sizeof(double)))) {
  267. return (dig_out_of_memory());
  268. }
  269. points->z = (double *)p;
  270. points->alloc_points = alloced;
  271. return (0);
  272. }
  273. /* allocate room for 'num' fields and category arrays
  274. ** in struct line_cats
  275. ** returns -1 on out of memory
  276. */
  277. int dig_alloc_cats(struct line_cats *cats, int num)
  278. {
  279. int alloced;
  280. char *p;
  281. /* alloc_space will just return if no space is needed */
  282. alloced = cats->alloc_cats;
  283. if (!(p =
  284. dig__alloc_space(num, &alloced, 1, (int *)cats->field,
  285. sizeof(int)))) {
  286. return (dig_out_of_memory());
  287. }
  288. cats->field = (int *)p;
  289. alloced = cats->alloc_cats;
  290. if (!(p =
  291. dig__alloc_space(num, &alloced, 1, (int *)cats->cat,
  292. sizeof(int)))) {
  293. return (dig_out_of_memory());
  294. }
  295. cats->cat = (int *)p;
  296. cats->alloc_cats = alloced;
  297. return (0);
  298. }
  299. /* area_alloc_line (area, add)
  300. ** allocate space in P_area for add new lines
  301. **
  302. ** Returns 0 ok or -1 on error
  303. */
  304. int dig_area_alloc_line(struct P_area * area, int add)
  305. {
  306. int num;
  307. char *p;
  308. num = area->alloc_lines + add;
  309. p = G_realloc(area->lines, num * sizeof(plus_t));
  310. if (p == NULL)
  311. return -1;
  312. area->lines = (plus_t *) p;
  313. area->alloc_lines = num;
  314. return (0);
  315. }
  316. /* area_alloc_isle (area, add)
  317. ** allocate space in P_area for add new isles
  318. **
  319. ** Returns 0 ok or -1 on error
  320. */
  321. int dig_area_alloc_isle(struct P_area * area, int add)
  322. {
  323. int num;
  324. char *p;
  325. G_debug(5, "dig_area_alloc_isle(): add = %d", add);
  326. num = area->alloc_isles + add;
  327. p = G_realloc(area->isles, num * sizeof(plus_t));
  328. if (p == NULL)
  329. return -1;
  330. area->isles = (plus_t *) p;
  331. area->alloc_isles = num;
  332. return (0);
  333. }
  334. /* dig_isle_alloc_line (isle, add)
  335. ** allocate space in P_isle for add new lines
  336. **
  337. ** Returns 0 ok or -1 on error
  338. */
  339. int dig_isle_alloc_line(struct P_isle * isle, int add)
  340. {
  341. int num;
  342. char *p;
  343. G_debug(3, "dig_isle_alloc_line():");
  344. num = isle->alloc_lines + add;
  345. p = G_realloc(isle->lines, num * sizeof(plus_t));
  346. if (p == NULL)
  347. return -1;
  348. isle->lines = (plus_t *) p;
  349. isle->alloc_lines = num;
  350. return (0);
  351. }
  352. /* for now just print message and return error code */
  353. int dig_out_of_memory()
  354. {
  355. fprintf(stderr, "OUT OF MEMORY!\n");
  356. return (-1);
  357. }