2
****************************************************************************
4
* MODULE: Vector library
6
* AUTHOR(S): Dave Gerdes, CERL.
7
* Update to GRASS 5.7 Radim Blazek.
9
* PURPOSE: Lower level functions for reading/writing/manipulating vectors.
11
* COPYRIGHT: (C) 2001 by the GRASS Development Team
13
* This program is free software under the GNU General Public
14
* License (>=v2). Read the file COPYING that comes with GRASS
17
*****************************************************************************/
2
\file lib/vector/diglib/struct_alloc.c
4
\brief Vector library - allocate and zero array space (lower level functions)
6
Lower level functions for reading/writing/manipulating vectors.
8
These routines all eventually call calloc() to allocate and zero the
9
new space. BUT It is not neccessarily safe to assume that the memory
10
will be zero. The next memory location asked for could have been
11
previously used and not zeroed. (e.g. compress()).
13
This program is free software under the GNU General Public License
14
(>=v2). Read the file COPYING that comes with GRASS for details.
16
\author CERL (probably Dave Gerdes)
19
20
#include <stdlib.h>
20
#include <grass/Vect.h>
22
/* These routines all eventually call calloc() to allocate and zero
23
** the new space. BUT It is not neccessarily safe to assume that
24
** the memory will be zero. The next memory location asked for could
25
** have been previously used and not zeroed. (e.g. compress())
28
/* alloc_node (map, add)
29
** alloc_line (map, add)
30
** alloc_area (map, add)
31
** alloc_points (map, num)
32
** node_alloc_line (node, add)
33
** area_alloc_line (node, add)
35
** Allocate array space to add 'add' elements
39
/* allocate new node structure */
40
P_NODE *dig_alloc_node()
21
#include <grass/vector.h>
22
#include <grass/glocale.h>
25
\brief Allocate new node structure
27
\return pointer to allocated P_node struct
30
struct P_node *dig_alloc_node()
44
Node = (P_NODE *) G_malloc(sizeof(P_NODE));
34
Node = (struct P_node *) G_malloc(sizeof(struct P_node));
49
Node->alloc_lines = 0;
56
/* dig_node_alloc_line (node, add)
57
** allocate space in P_node, lines and angles arrays to add 'add' more
60
** Returns 0 ok or -1 on error
63
int dig_node_alloc_line(P_NODE * node, int add)
38
G_zero(Node, sizeof(struct P_node));
44
\brief Free node structure
46
\param Node pointer to P_node struct to be freed
48
void dig_free_node(struct P_node *Node)
50
if (Node->alloc_lines > 0) {
59
\brief Allocate space in P_node struct
61
Lines and angles arrays to add 'add' more lines
63
\param node pointer to P_node struct
64
\param add number lines to be added
69
int dig_node_alloc_line(struct P_node * node, int add)
72
78
p = G_realloc(node->lines, num * sizeof(plus_t));
75
81
node->lines = (plus_t *) p;
77
83
p = G_realloc(node->angles, num * sizeof(float));
80
86
node->angles = (float *)p;
82
88
node->alloc_lines = num;
94
\brief Reallocate array of pointers to nodes
96
\param Plus pointer to Plus_head structure
97
\param add number of nodes to be added
87
/* Reallocate array of pointers to nodes.
88
* Space for 'add' number of nodes is added.
93
102
int dig_alloc_nodes(struct Plus_head *Plus, int add)
98
107
size = Plus->alloc_nodes + 1 + add;
99
p = G_realloc(Plus->Node, size * sizeof(P_NODE *));
108
p = G_realloc(Plus->Node, size * sizeof(struct P_node *));
103
Plus->Node = (P_NODE **) p;
112
Plus->Node = (struct P_node **) p;
104
113
Plus->alloc_nodes = size - 1;
109
/* allocate new line structure */
110
P_LINE *dig_alloc_line()
119
\brief Allocate new line structure
121
\return pointer to allocated P_node struct
122
\return NULL on error
124
struct P_line *dig_alloc_line()
114
Line = (P_LINE *) G_malloc(sizeof(P_LINE));
128
Line = (struct P_line *) G_malloc(sizeof(struct P_line));
115
129
if (Line == NULL)
121
/* Reallocate array of pointers to lines.
122
* Space for 'add' number of lines is added.
132
G_zero(Line, sizeof(struct P_line));
138
\brief Allocate new topo struct
140
\param type to of struct to allocate
142
void *dig_alloc_topo(char type)
148
Topo = G_malloc(sizeof(struct P_topo_l));
151
Topo = G_malloc(sizeof(struct P_topo_b));
154
Topo = G_malloc(sizeof(struct P_topo_c));
157
Topo = G_malloc(sizeof(struct P_topo_f));
160
Topo = G_malloc(sizeof(struct P_topo_k));
170
\brief Free line structure
172
\param pointer to P_line struct to be freed
174
void dig_free_line(struct P_line *Line)
182
\brief Reallocate array of pointers to lines.
184
\param Plus pointer to Plus_head structure
185
\param add space for 'add' number of lines is added.
127
190
int dig_alloc_lines(struct Plus_head *Plus, int add)
132
195
size = Plus->alloc_lines + 1 + add;
133
p = G_realloc(Plus->Line, size * sizeof(P_LINE *));
196
p = G_realloc(Plus->Line, size * sizeof(struct P_line *));
137
Plus->Line = (P_LINE **) p;
200
Plus->Line = (struct P_line **) p;
138
201
Plus->alloc_lines = size - 1;
143
/* Reallocate array of pointers to areas.
144
* Space for 'add' number of areas is added.
207
\brief Reallocate array of pointers to areas.
209
\param Plus pointer to Plus_head structure
210
\param add space for 'add' number of areas is added
149
215
int dig_alloc_areas(struct Plus_head *Plus, int add)
154
220
size = Plus->alloc_areas + 1 + add;
155
p = G_realloc(Plus->Area, size * sizeof(P_AREA *));
221
p = G_realloc(Plus->Area, size * sizeof(struct P_area *));
159
Plus->Area = (P_AREA **) p;
225
Plus->Area = (struct P_area **) p;
160
226
Plus->alloc_areas = size - 1;
165
/* Reallocate array of pointers to isles.
166
* Space for 'add' number of isles is added.
232
\brief Reallocate array of pointers to isles
234
\param Plus pointer to Plus_head structure
235
\param add space for 'add' number of isles is added.
171
240
int dig_alloc_isles(struct Plus_head *Plus, int add)
176
245
G_debug(3, "dig_alloc_isle():");
177
246
size = Plus->alloc_isles + 1 + add;
178
p = G_realloc(Plus->Isle, size * sizeof(P_ISLE *));
247
p = G_realloc(Plus->Isle, size * sizeof(struct P_isle *));
182
Plus->Isle = (P_ISLE **) p;
251
Plus->Isle = (struct P_isle **) p;
183
252
Plus->alloc_isles = size - 1;
188
/* allocate new area structure */
189
P_AREA *dig_alloc_area()
258
\brief Allocate new area structure
260
\return pointer to allocated P_area struct
261
\return NULL on error
263
struct P_area *dig_alloc_area()
193
Area = (P_AREA *) G_malloc(sizeof(P_AREA));
267
Area = (struct P_area *) G_malloc(sizeof(struct P_area));
194
268
if (Area == NULL)
198
Area->alloc_lines = 0;
201
Area->alloc_isles = 0;
210
/* alloc new isle structure */
211
P_ISLE *dig_alloc_isle()
215
Isle = (P_ISLE *) G_malloc(sizeof(P_ISLE));
271
G_zero(Area, sizeof(struct P_area));
277
\brief Free area structure
279
\param Area pointer to P_area struct to be freed
281
void dig_free_area(struct P_area *Area)
283
if (Area->alloc_lines > 0)
286
if (Area->alloc_isles > 0)
293
\brief Allocate new isle structure
295
\return pointer to allocated P_isle struct
296
\return NULL on error
298
struct P_isle *dig_alloc_isle()
302
Isle = (struct P_isle *) G_malloc(sizeof(struct P_isle));
216
303
if (Isle == NULL)
220
Isle->alloc_lines = 0;
229
/* allocate room for 'num' X and Y arrays in struct line_pnts
230
** returns -1 on out of memory
306
G_zero(Isle, sizeof(struct P_isle));
312
\brief Free isle structure
314
\param Isle pointer to P_isle struct to be freed
316
void dig_free_isle(struct P_isle *Isle)
318
if (Isle->alloc_lines > 0)
325
\brief allocate room for 'num' X and Y arrays in struct line_pnts
327
\param points pointer to line_pnts struct
328
\param num number of points
331
\return returns -1 on out of memory
232
333
int dig_alloc_points(struct line_pnts *points, int num)
237
338
alloced = points->alloc_points;
238
339
/* alloc_space will just return if no space is needed */
240
dig__alloc_space(num, &alloced, 50, (char *)points->x,
242
return (dig_out_of_memory());
341
dig__alloc_space(num, &alloced, 50, (char *)points->x,
343
return (dig_out_of_memory());
244
345
points->x = (double *)p;
246
347
alloced = points->alloc_points;
247
348
/* alloc_space will just return if no space is needed */
249
dig__alloc_space(num, &alloced, 50, (char *)points->y,
251
return (dig_out_of_memory());
350
dig__alloc_space(num, &alloced, 50, (char *)points->y,
352
return (dig_out_of_memory());
253
354
points->y = (double *)p;
255
356
alloced = points->alloc_points;
256
357
/* alloc_space will just return if no space is needed */
258
dig__alloc_space(num, &alloced, 50, (char *)points->z,
260
return (dig_out_of_memory());
359
dig__alloc_space(num, &alloced, 50, (char *)points->z,
361
return (dig_out_of_memory());
262
363
points->z = (double *)p;
264
365
points->alloc_points = alloced;
268
/* allocate room for 'num' fields and category arrays
269
** in struct line_cats
270
** returns -1 on out of memory
371
\brief Allocate room for 'num' fields and category arrays
374
\param cats pointer to line_cats struct
375
\param num number of cats
378
\return returns -1 on out of memory
272
380
int dig_alloc_cats(struct line_cats *cats, int num)
277
385
/* alloc_space will just return if no space is needed */
278
386
alloced = cats->alloc_cats;
280
dig__alloc_space(num, &alloced, 1, (int *)cats->field,
282
return (dig_out_of_memory());
388
dig__alloc_space(num, &alloced, 1, (int *)cats->field,
390
return dig_out_of_memory();
284
392
cats->field = (int *)p;
286
394
alloced = cats->alloc_cats;
288
dig__alloc_space(num, &alloced, 1, (int *)cats->cat,
290
return (dig_out_of_memory());
396
dig__alloc_space(num, &alloced, 1, (int *)cats->cat,
398
return dig_out_of_memory();
292
400
cats->cat = (int *)p;
294
402
cats->alloc_cats = alloced;
298
/* area_alloc_line (area, add)
299
** allocate space in P_area for add new lines
301
** Returns 0 ok or -1 on error
408
\brief allocate space in P_area for add new lines
410
\param area pointer to P_area struct
411
\param add number of lines to be added
303
int dig_area_alloc_line(P_AREA * area, int add)
416
int dig_area_alloc_line(struct P_area * area, int add)
333
450
p = G_realloc(area->isles, num * sizeof(plus_t));
336
453
area->isles = (plus_t *) p;
338
455
area->alloc_isles = num;
342
/* dig_isle_alloc_line (isle, add)
343
** allocate space in P_isle for add new lines
345
** Returns 0 ok or -1 on error
348
int dig_isle_alloc_line(P_ISLE * isle, int add)
460
\brief Allocate space in P_isle for add new lines
462
\param isle pointer to P_area struct
463
\param add number of isle to be added
468
int dig_isle_alloc_line(struct P_isle * isle, int add)