struct_alloc.c

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

Generated on Sun Apr 6 17:32:44 2008 for GRASS by  doxygen 1.5.5