struct_alloc.c 7.7 KB

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