struct_alloc.c

Go to the documentation of this file.
00001 /*
00002  ****************************************************************************
00003  *
00004  * MODULE:       Vector library 
00005  *              
00006  * AUTHOR(S):    Dave Gerdes, CERL.
00007  *               Update to GRASS 5.7 Radim Blazek.
00008  *
00009  * PURPOSE:      Lower level functions for reading/writing/manipulating vectors.
00010  *
00011  * COPYRIGHT:    (C) 2001 by the GRASS Development Team
00012  *
00013  *               This program is free software under the GNU General Public
00014  *              License (>=v2). Read the file COPYING that comes with GRASS
00015  *              for details.
00016  *
00017  *****************************************************************************/
00018 
00019 #include <stdlib.h>
00020 #include <grass/Vect.h>
00021 
00022 /*  These routines all eventually call calloc() to allocate and zero
00023  **  the new space.  BUT It is not neccessarily safe to assume that
00024  **  the memory will be zero.  The next memory location asked for could
00025  **  have been previously used and not zeroed.  (e.g. compress())
00026  */
00027 
00028 /* alloc_node (map, add)
00029  ** alloc_line (map, add)
00030  ** alloc_area (map, add)
00031  ** alloc_points (map, num)
00032  ** node_alloc_line (node, add)
00033  ** area_alloc_line (node, add)
00034  **
00035  ** Allocate array space to add 'add' elements
00036  */
00037 
00038 
00039 /* allocate new node structure */
00040 P_NODE *dig_alloc_node()
00041 {
00042     P_NODE *Node;
00043 
00044     Node = (P_NODE *) G_malloc(sizeof(P_NODE));
00045     if (Node == NULL)
00046         return NULL;
00047 
00048     Node->n_lines = 0;
00049     Node->alloc_lines = 0;
00050     Node->lines = NULL;
00051     Node->angles = NULL;
00052 
00053     return (Node);
00054 }
00055 
00056 /* dig_node_alloc_line (node, add)
00057  **     allocate space in  P_node,  lines and angles arrays to add 'add' more
00058  **     lines
00059  **
00060  **  Returns   0 ok    or    -1 on error
00061  */
00062 
00063 int dig_node_alloc_line(P_NODE * node, int add)
00064 {
00065     int num;
00066     char *p;
00067 
00068     G_debug(3, "dig_node_alloc_line(): add = %d", add);
00069 
00070     num = node->n_lines + add;
00071 
00072     p = G_realloc(node->lines, num * sizeof(plus_t));
00073     if (p == NULL)
00074         return -1;
00075     node->lines = (plus_t *) p;
00076 
00077     p = G_realloc(node->angles, num * sizeof(float));
00078     if (p == NULL)
00079         return -1;
00080     node->angles = (float *)p;
00081 
00082     node->alloc_lines = num;
00083     return (0);
00084 }
00085 
00086 
00087 /* Reallocate array of pointers to nodes.
00088  *  Space for 'add' number of nodes is added.
00089  * 
00090  *  Returns: 0 success
00091  *          -1 error
00092  */
00093 int dig_alloc_nodes(struct Plus_head *Plus, int add)
00094 {
00095     int size;
00096     char *p;
00097 
00098     size = Plus->alloc_nodes + 1 + add;
00099     p = G_realloc(Plus->Node, size * sizeof(P_NODE *));
00100     if (p == NULL)
00101         return -1;
00102 
00103     Plus->Node = (P_NODE **) p;
00104     Plus->alloc_nodes = size - 1;
00105 
00106     return (0);
00107 }
00108 
00109 /* allocate new line structure */
00110 P_LINE *dig_alloc_line()
00111 {
00112     P_LINE *Line;
00113 
00114     Line = (P_LINE *) G_malloc(sizeof(P_LINE));
00115     if (Line == NULL)
00116         return NULL;
00117 
00118     return (Line);
00119 }
00120 
00121 /* Reallocate array of pointers to lines.
00122  *  Space for 'add' number of lines is added.
00123  * 
00124  *  Returns: 0 success
00125  *          -1 error
00126  */
00127 int dig_alloc_lines(struct Plus_head *Plus, int add)
00128 {
00129     int size;
00130     char *p;
00131 
00132     size = Plus->alloc_lines + 1 + add;
00133     p = G_realloc(Plus->Line, size * sizeof(P_LINE *));
00134     if (p == NULL)
00135         return -1;
00136 
00137     Plus->Line = (P_LINE **) p;
00138     Plus->alloc_lines = size - 1;
00139 
00140     return (0);
00141 }
00142 
00143 /* Reallocate array of pointers to areas.
00144  *  Space for 'add' number of areas is added.
00145  * 
00146  *  Returns: 0 success
00147  *          -1 error
00148  */
00149 int dig_alloc_areas(struct Plus_head *Plus, int add)
00150 {
00151     int size;
00152     char *p;
00153 
00154     size = Plus->alloc_areas + 1 + add;
00155     p = G_realloc(Plus->Area, size * sizeof(P_AREA *));
00156     if (p == NULL)
00157         return -1;
00158 
00159     Plus->Area = (P_AREA **) p;
00160     Plus->alloc_areas = size - 1;
00161 
00162     return (0);
00163 }
00164 
00165 /* Reallocate array of pointers to isles.
00166  *  Space for 'add' number of isles is added.
00167  * 
00168  *  Returns: 0 success
00169  *          -1 error
00170  */
00171 int dig_alloc_isles(struct Plus_head *Plus, int add)
00172 {
00173     int size;
00174     char *p;
00175 
00176     G_debug(3, "dig_alloc_isle():");
00177     size = Plus->alloc_isles + 1 + add;
00178     p = G_realloc(Plus->Isle, size * sizeof(P_ISLE *));
00179     if (p == NULL)
00180         return -1;
00181 
00182     Plus->Isle = (P_ISLE **) p;
00183     Plus->alloc_isles = size - 1;
00184 
00185     return (0);
00186 }
00187 
00188 /* allocate new area structure */
00189 P_AREA *dig_alloc_area()
00190 {
00191     P_AREA *Area;
00192 
00193     Area = (P_AREA *) G_malloc(sizeof(P_AREA));
00194     if (Area == NULL)
00195         return NULL;
00196 
00197     Area->n_lines = 0;
00198     Area->alloc_lines = 0;
00199     Area->lines = NULL;
00200 
00201     Area->alloc_isles = 0;
00202     Area->n_isles = 0;
00203     Area->isles = NULL;
00204 
00205     Area->centroid = 0;
00206 
00207     return (Area);
00208 }
00209 
00210 /* alloc new isle structure */
00211 P_ISLE *dig_alloc_isle()
00212 {
00213     P_ISLE *Isle;
00214 
00215     Isle = (P_ISLE *) G_malloc(sizeof(P_ISLE));
00216     if (Isle == NULL)
00217         return NULL;
00218 
00219     Isle->n_lines = 0;
00220     Isle->alloc_lines = 0;
00221     Isle->lines = NULL;
00222 
00223     Isle->area = 0;
00224 
00225     return (Isle);
00226 }
00227 
00228 
00229 /* allocate room for  'num'   X and Y  arrays in struct line_pnts 
00230  **   returns -1 on out of memory 
00231  */
00232 int dig_alloc_points(struct line_pnts *points, int num)
00233 {
00234     int alloced;
00235     char *p;
00236 
00237     alloced = points->alloc_points;
00238     /* alloc_space will just return if no space is needed */
00239     if (!(p =
00240           dig__alloc_space(num, &alloced, 50, (char *)points->x,
00241                            sizeof(double)))) {
00242         return (dig_out_of_memory());
00243     }
00244     points->x = (double *)p;
00245 
00246     alloced = points->alloc_points;
00247     /* alloc_space will just return if no space is needed */
00248     if (!(p =
00249           dig__alloc_space(num, &alloced, 50, (char *)points->y,
00250                            sizeof(double)))) {
00251         return (dig_out_of_memory());
00252     }
00253     points->y = (double *)p;
00254 
00255     alloced = points->alloc_points;
00256     /* alloc_space will just return if no space is needed */
00257     if (!(p =
00258           dig__alloc_space(num, &alloced, 50, (char *)points->z,
00259                            sizeof(double)))) {
00260         return (dig_out_of_memory());
00261     }
00262     points->z = (double *)p;
00263 
00264     points->alloc_points = alloced;
00265     return (0);
00266 }
00267 
00268 /* allocate room for  'num'  fields and category arrays 
00269  ** in struct line_cats 
00270  **   returns -1 on out of memory 
00271  */
00272 int dig_alloc_cats(struct line_cats *cats, int num)
00273 {
00274     int alloced;
00275     char *p;
00276 
00277     /* alloc_space will just return if no space is needed */
00278     alloced = cats->alloc_cats;
00279     if (!(p =
00280           dig__alloc_space(num, &alloced, 1, (int *)cats->field,
00281                            sizeof(int)))) {
00282         return (dig_out_of_memory());
00283     }
00284     cats->field = (int *)p;
00285 
00286     alloced = cats->alloc_cats;
00287     if (!(p =
00288           dig__alloc_space(num, &alloced, 1, (int *)cats->cat,
00289                            sizeof(int)))) {
00290         return (dig_out_of_memory());
00291     }
00292     cats->cat = (int *)p;
00293 
00294     cats->alloc_cats = alloced;
00295     return (0);
00296 }
00297 
00298 /* area_alloc_line (area, add)
00299  **     allocate space in  P_area for add new lines
00300  **
00301  **  Returns   0 ok    or    -1 on error
00302  */
00303 int dig_area_alloc_line(P_AREA * area, int add)
00304 {
00305     int num;
00306     char *p;
00307 
00308     num = area->alloc_lines + add;
00309 
00310     p = G_realloc(area->lines, num * sizeof(plus_t));
00311     if (p == NULL)
00312         return -1;
00313     area->lines = (plus_t *) p;
00314 
00315     area->alloc_lines = num;
00316 
00317     return (0);
00318 }
00319 
00320 /* area_alloc_isle (area, add)
00321  **     allocate space in  P_area for add new isles
00322  **
00323  **  Returns   0 ok    or    -1 on error
00324  */
00325 int dig_area_alloc_isle(P_AREA * area, int add)
00326 {
00327     int num;
00328     char *p;
00329 
00330     G_debug(5, "dig_area_alloc_isle(): add = %d", add);
00331     num = area->alloc_isles + add;
00332 
00333     p = G_realloc(area->isles, num * sizeof(plus_t));
00334     if (p == NULL)
00335         return -1;
00336     area->isles = (plus_t *) p;
00337 
00338     area->alloc_isles = num;
00339     return (0);
00340 }
00341 
00342 /* dig_isle_alloc_line (isle, add)
00343  **     allocate space in  P_isle for add new lines
00344  **
00345  **  Returns   0 ok    or    -1 on error
00346  */
00347 
00348 int dig_isle_alloc_line(P_ISLE * isle, int add)
00349 {
00350     int num;
00351     char *p;
00352 
00353     G_debug(3, "dig_isle_alloc_line():");
00354     num = isle->alloc_lines + add;
00355 
00356     p = G_realloc(isle->lines, num * sizeof(plus_t));
00357     if (p == NULL)
00358         return -1;
00359     isle->lines = (plus_t *) p;
00360 
00361     isle->alloc_lines = num;
00362 
00363     return (0);
00364 }
00365 
00366 
00367 
00368 /* for now just print message and return error code */
00369 int dig_out_of_memory()
00370 {
00371     fprintf(stderr, "OUT OF MEMORY!\n");
00372     return (-1);
00373 }