Skip to content

AFNI/NIfTI Server

Sections
Personal tools
You are here: Home » AFNI » Documentation

Doxygen Source Code Documentation


Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Search  

list_struct.c

Go to the documentation of this file.
00001 
00002 #include "stdlib.h"
00003 #include "list_struct.h"
00004 
00005 
00006 /*----------------------------------------------------------------------
00007  *
00008  * list_struct.c            - for creating and destroying list structures
00009  * 
00010  * each list structure contains:
00011  *
00012  *   num   - the number of elements used       (the user may alter this number)
00013  *   nall  - the number of elements allocated
00014  *   list  - the 'list', the list of elements (the user may alter these values)
00015  *
00016  * each pointer list structure also contains:
00017  *
00018  *   elen  - the length of each list in the main array
00019  *
00020  * Rick Reynolds - December 6, 2004
00021  *----------------------------------------------------------------------*/
00022 
00023 
00024 /*----------------------------------------------------------------------
00025  * sample struct and prototypes:
00026  *
00027  *   I will use float for examples, noting that float may be replaced
00028  *   with any of int, short and void, also.
00029  *  
00030  *   typedef struct { int num,nall;  float *  list; } float_list;
00031  *
00032  *   int init_float_list ( float_list  * d_list, int nel );
00033  *   int init_floatp_list( floatp_list * d_list, int nel, int len );
00034  *   int free_float_list ( float_list  * d_list );
00035  *   int free_floatp_list( floatp_list * d_list );
00036  *  
00037  * brief sample function descriptions:
00038  *  
00039  *   Note that all of these functions set d_list->num to 0.  That
00040  *   variable is for the user to apply, if they wish.
00041  *  
00042  *   int init_float_list( float_list * d_list, int nel );
00043  *
00044  *       The user should pass a (useless) float_list structure.
00045  *       This function will set num to 0, nall to the input nel
00046  *       and allocate an array of length nel.
00047  *  
00048  *       This will return nel (>= 0) on success, and < 0 on failure.
00049  *  
00050  *   int init_floatp_list( floatp_list * d_list, int nel, int len );
00051  *  
00052  *       Like above, but now d_list->list will be an array of
00053  *       (float *).  Also, if len > 0, each list[i] will be
00054  *       allocated to an array of len floats.
00055  *  
00056  *   int free_float_list( float_list * d_list );
00057  *  
00058  *       This function will free d_list->list, and set num and nall to 0.
00059  *  
00060  *   int free_floatp_list( floatp_list * d_list );
00061  *  
00062  *       Like free_float_list, but before free(d_list->list), we must
00063  *       free(d_list->list[i]), for each i.
00064  *  
00065  *----------------------------------------------------------------------*/
00066 
00067 
00068 /*----------------------------------------------------------------------
00069  * init_XXXX_list:
00070  *  
00071  *   input: structure pointer and number of elements
00072  *  
00073  *   if nel <= 0, set all fields to zero (or NULL)
00074  *   if nel >  0, attempt to malloc the requested number of elements
00075  *  
00076  *   return:
00077  *       success: nel (>= 0)
00078  *       failure: < 0
00079  *----------------------------------------------------------------------*/
00080 int init_float_list( float_list * d_list, int nel )
00081 {
00082     if ( !d_list ) return -1;
00083 
00084     if ( nel <= 0 ) {
00085         d_list->num = 0;  d_list->nall = 0;  d_list->list = NULL;
00086         return 0;
00087     }
00088 
00089     d_list->list = (float *)malloc(nel * sizeof(float));   /* allocate memory */
00090 
00091     if ( d_list->list == NULL ) return -1;                 /* malloc failure  */
00092 
00093     d_list->num = 0;
00094     d_list->nall = nel;
00095 
00096     return nel;
00097 }
00098 
00099 int init_int_list( int_list * d_list, int nel )
00100 {
00101     if ( !d_list ) return -1;
00102 
00103     if ( nel <= 0 ) {
00104         d_list->num = 0;  d_list->nall = 0;  d_list->list = NULL;
00105         return 0;
00106     }
00107 
00108     d_list->list = (int *)malloc(nel * sizeof(int));       /* allocate memory */
00109 
00110     if ( d_list->list == NULL ) return -1;                 /* malloc failure  */
00111 
00112     d_list->num = 0;
00113     d_list->nall = nel;
00114 
00115     return nel;
00116 }
00117 
00118 int init_short_list( short_list * d_list, int nel )
00119 {
00120     if ( !d_list ) return -1;
00121 
00122     if ( nel <= 0 ) {
00123         d_list->num = 0;  d_list->nall = 0;  d_list->list = NULL;
00124         return 0;
00125     }
00126 
00127     d_list->list = (short *)malloc(nel * sizeof(short));   /* allocate memory */
00128 
00129     if ( d_list->list == NULL ) return -1;                 /* malloc failure  */
00130 
00131     d_list->num = 0;
00132     d_list->nall = nel;
00133 
00134     return nel;
00135 }
00136 
00137 int init_void_list( void_list * d_list, int nel )
00138 {
00139     if ( !d_list ) return -1;
00140 
00141     if ( nel <= 0 ) {
00142         d_list->num = 0;  d_list->nall = 0;  d_list->list = NULL;
00143         return 0;
00144     }
00145 
00146     /* special case, list is considered a list of bytes, so use char */
00147     d_list->list = (void *)malloc(nel * sizeof(char));     /* allocate memory */
00148 
00149     if ( d_list->list == NULL ) return -1;                 /* malloc failure  */
00150 
00151     d_list->num = 0;
00152     d_list->nall = nel;
00153 
00154     return nel;
00155 }
00156 
00157 /*----------------------------------------------------------------------
00158  * pointer lists:
00159  *
00160  * in addition to what is above, pass the list length 
00161  *
00162  * after allocating the nel pointers,
00163  * len elements will be allocated to each pointer
00164  *
00165  * the return values are the same
00166  *----------------------------------------------------------------------*/
00167 int init_floatp_list( floatp_list * d_list, int nel, int len )
00168 {
00169     int count;
00170 
00171     if ( !d_list ) return -1;
00172 
00173     /* an 'empty' structure will contain 0 and NULL field entries */
00174     if ( nel <= 0 ) {
00175         d_list->num = d_list->nall = d_list->elen = 0;
00176         d_list->list  = NULL;
00177         return 0;
00178     }
00179 
00180     d_list->list = (float **)malloc(nel * sizeof(float *));/* allocate memory */
00181 
00182     if ( d_list->list == NULL ) return -1;                 /* malloc failure  */
00183 
00184     d_list->num = 0;                                     /* none used yet   */
00185     d_list->nall = nel;                     /* number of pointers allocated */
00186     d_list->elen = len;                             /* length of each list */
00187 
00188 
00189     /* now repeat the process for each pointer, allocating 'len' elements    */
00190 
00191     /* trivial case, where the user requests no list allocation */
00192     if ( len <= 0 ){
00193         d_list->elen = 0;                   /* number of elements allocated */
00194         for ( count = 0; count < nel; count++ )
00195              d_list->list[count] = NULL;
00196         return nel;
00197     }
00198 
00199     /* general case, the user wants data, too */
00200     for ( count = 0; count < nel; count++ ){
00201         d_list->list[count] = malloc(len * sizeof(float));
00202 
00203         /* on malloc failure, free() everything else, of course */
00204         if ( d_list->list[count] == NULL ){
00205             while ( --count >= 0 ) free(d_list->list[count]);
00206             free(d_list->list);
00207             return -1;
00208         }
00209     }
00210 
00211     return nel;
00212 }
00213 
00214 int init_intp_list( intp_list * d_list, int nel, int len )
00215 {
00216     int count;
00217 
00218     if ( !d_list ) return -1;
00219 
00220     /* an 'empty' structure will contain 0 and NULL field entries */
00221     if ( nel <= 0 ) {
00222         d_list->num = d_list->nall = d_list->elen = 0;
00223         d_list->list  = NULL;
00224         return 0;
00225     }
00226 
00227     d_list->list = (int **)malloc(nel * sizeof(int *));    /* allocate memory */
00228 
00229     if ( d_list->list == NULL ) return -1;                 /* malloc failure  */
00230 
00231     d_list->num = 0;                                       /* none used yet */
00232     d_list->nall = nel;                     /* number of pointers allocated */
00233     d_list->elen = len;                             /* length of each list */
00234 
00235 
00236     /* now repeat the process for each pointer, allocating 'len' elements    */
00237 
00238     /* trivial case, where the user requests no list allocation */
00239     if ( len <= 0 ){
00240         d_list->elen = 0;                   /* number of elements allocated */
00241         for ( count = 0; count < nel; count++ )
00242              d_list->list[count] = NULL;
00243         return nel;
00244     }
00245 
00246     /* general case, the user wants data, too */
00247     for ( count = 0; count < nel; count++ ){
00248         d_list->list[count] = malloc(len * sizeof(int));
00249 
00250         /* on malloc failure, free() everything else, of course */
00251         if ( d_list->list[count] == NULL ){
00252             while ( --count >= 0 ) free(d_list->list[count]);
00253             free(d_list->list);
00254             return -1;
00255         }
00256     }
00257 
00258     return nel;
00259 }
00260 
00261 int init_shortp_list( shortp_list * d_list, int nel, int len )
00262 {
00263     int count;
00264 
00265     if ( !d_list ) return -1;
00266 
00267     /* an 'empty' structure will contain 0 and NULL field entries */
00268     if ( nel <= 0 ) {
00269         d_list->num = d_list->nall = d_list->elen = 0;
00270         d_list->list  = NULL;
00271         return 0;
00272     }
00273 
00274     d_list->list = (short **)malloc(nel * sizeof(short *));/* allocate memory */
00275 
00276     if ( d_list->list == NULL ) return -1;                 /* malloc failure  */
00277 
00278     d_list->num = 0;                                       /* none used yet */
00279     d_list->nall = nel;                     /* number of pointers allocated */
00280     d_list->elen = len;                             /* length of each list */
00281 
00282 
00283     /* now repeat the process for each pointer, allocating 'len' elements    */
00284 
00285     /* trivial case, where the user requests no list allocation */
00286     if ( len <= 0 ){
00287         d_list->elen = 0;                   /* number of elements allocated */
00288         for ( count = 0; count < nel; count++ )
00289              d_list->list[count] = NULL;
00290         return nel;
00291     }
00292 
00293     /* general case, the user wants data, too */
00294     for ( count = 0; count < nel; count++ ){
00295         d_list->list[count] = malloc(len * sizeof(short));
00296 
00297         /* on malloc failure, free() everything else, of course */
00298         if ( d_list->list[count] == NULL ){
00299             while ( --count >= 0 ) free(d_list->list[count]);
00300             free(d_list->list);
00301             return -1;
00302         }
00303     }
00304 
00305     return nel;
00306 }
00307 
00308 int init_voidp_list( voidp_list * d_list, int nel, int len )
00309 {
00310     int count;
00311 
00312     if ( !d_list ) return -1;
00313 
00314     /* an 'empty' structure will contain 0 and NULL field entries */
00315     if ( nel <= 0 ) {
00316         d_list->num = d_list->nall = d_list->elen = 0;
00317         d_list->list  = NULL;
00318         return 0;
00319     }
00320 
00321     d_list->list = (void **)malloc(nel * sizeof(void *));  /* allocate memory */
00322 
00323     if ( d_list->list == NULL ) return -1;                 /* malloc failure  */
00324 
00325     d_list->num = 0;                                       /* none used yet */
00326     d_list->nall = nel;                     /* number of pointers allocated */
00327     d_list->elen = len;                             /* length of each list */
00328 
00329 
00330     /* now repeat the process for each pointer, allocating 'len' elements    */
00331 
00332     /* trivial case, where the user requests no list allocation */
00333     if ( len <= 0 ){
00334         d_list->elen = 0;                   /* number of elements allocated */
00335         for ( count = 0; count < nel; count++ )
00336              d_list->list[count] = NULL;
00337         return nel;
00338     }
00339 
00340     /* general case, the user wants data, too */
00341     for ( count = 0; count < nel; count++ ){
00342         /* again, the special case for void * is a list of bytes */
00343         d_list->list[count] = malloc(len * sizeof(char));
00344 
00345         /* on malloc failure, free() everything else, of course */
00346         if ( d_list->list[count] == NULL ){
00347             while ( --count >= 0 ) free(d_list->list[count]);
00348             free(d_list->list);
00349             return -1;
00350         }
00351     }
00352 
00353     return nel;
00354 }
00355 
00356 /*----------------------------------------------------------------------
00357  * free_XXXX_list:
00358  *
00359  * free all lists
00360  *
00361  * return 0 on success, -1 on error
00362  *----------------------------------------------------------------------*/
00363 int free_float_list( float_list * d_list )
00364 {
00365     if ( !d_list ) return -1;
00366 
00367     if ( d_list->list ) { free(d_list->list);  d_list->list = NULL; }
00368     d_list->num = d_list->nall = 0;
00369 
00370     return 0;
00371 }
00372 
00373 int free_int_list( int_list * d_list )
00374 {
00375     if ( !d_list ) return -1;
00376 
00377     if ( d_list->list ) { free(d_list->list);  d_list->list = NULL; }
00378     d_list->num = d_list->nall = 0;
00379 
00380     return 0;
00381 }
00382 
00383 int free_short_list( short_list * d_list )
00384 {
00385     if ( !d_list ) return -1;
00386 
00387     if ( d_list->list ) { free(d_list->list);  d_list->list = NULL; }
00388     d_list->num = d_list->nall = 0;
00389 
00390     return 0;
00391 }
00392 
00393 int free_void_list( void_list * d_list )
00394 {
00395     if ( !d_list ) return -1;
00396 
00397     if ( d_list->list ) { free(d_list->list);  d_list->list = NULL; }
00398     d_list->num = d_list->nall = 0;
00399 
00400     return 0;
00401 }
00402 
00403 
00404 /*----------------------------------------------------------------------
00405  * free_XXXXp_list:
00406  *
00407  * free all sub-lists and lists
00408  *
00409  * return 0 on success, -1 on error
00410  *----------------------------------------------------------------------*/
00411 int free_floatp_list( floatp_list * d_list )
00412 {
00413     int count;
00414 
00415     if ( !d_list ) return -1;
00416 
00417     /* if nothing has been set, just clear values and return */
00418     if ( d_list->num <= 0 ){
00419         d_list->num = d_list->nall = d_list->elen = 0;
00420         d_list->list = NULL;
00421         return 0;
00422     }
00423 
00424     /* this is bad, but nothing to do */
00425     if ( !d_list->list ){
00426         d_list->num = d_list->nall = d_list->elen = 0;
00427         return -1;
00428     }
00429 
00430     /* first, free the nall lists (of length elen) */
00431 
00432     for ( count = 0; count < d_list->nall; count++ )
00433         if ( d_list->list[count] ) free(d_list->list[count]);
00434 
00435     /* now free the list and clear all values */
00436     free(d_list->list);
00437     d_list->list = NULL;
00438 
00439     d_list->num = d_list->nall = d_list->elen = 0;
00440 
00441     return 0;
00442 }
00443 
00444 int free_intp_list( intp_list * d_list )
00445 {
00446     int count;
00447 
00448     if ( !d_list ) return -1;
00449 
00450     /* if nothing has been set, just clear values and return */
00451     if ( d_list->num <= 0 ){
00452         d_list->num = d_list->nall = d_list->elen = 0;
00453         d_list->list = NULL;
00454         return 0;
00455     }
00456 
00457     /* this is bad, but nothing to do */
00458     if ( !d_list->list ){
00459         d_list->num = d_list->nall = d_list->elen = 0;
00460         return -1;
00461     }
00462 
00463     /* first, free the nall lists (of length elen) */
00464 
00465     for ( count = 0; count < d_list->nall; count++ )
00466         if ( d_list->list[count] ) free(d_list->list[count]);
00467 
00468     /* now free the list and clear all values */
00469     free(d_list->list);
00470     d_list->list = NULL;
00471 
00472     d_list->num = d_list->nall = d_list->elen = 0;
00473 
00474     return 0;
00475 }
00476 
00477 int free_shortp_list( shortp_list * d_list )
00478 {
00479     int count;
00480 
00481     if ( !d_list ) return -1;
00482 
00483     /* if nothing has been set, just clear values and return */
00484     if ( d_list->num <= 0 ){
00485         d_list->num = d_list->nall = d_list->elen = 0;
00486         d_list->list = NULL;
00487         return 0;
00488     }
00489 
00490     /* this is bad, but nothing to do */
00491     if ( !d_list->list ){
00492         d_list->num = d_list->nall = d_list->elen = 0;
00493         return -1;
00494     }
00495 
00496     /* first, free the nall lists (of length elen) */
00497 
00498     for ( count = 0; count < d_list->nall; count++ )
00499         if ( d_list->list[count] ) free(d_list->list[count]);
00500 
00501     /* now free the list and clear all values */
00502     free(d_list->list);
00503     d_list->list = NULL;
00504 
00505     d_list->num = d_list->nall = d_list->elen = 0;
00506 
00507     return 0;
00508 }
00509 
00510 int free_voidp_list( voidp_list * d_list )
00511 {
00512     int count;
00513 
00514     if ( !d_list ) return -1;
00515 
00516     /* if nothing has been set, just clear values and return */
00517     if ( d_list->num <= 0 ){
00518         d_list->num = d_list->nall = d_list->elen = 0;
00519         d_list->list = NULL;
00520         return 0;
00521     }
00522 
00523     /* this is bad, but nothing to do */
00524     if ( !d_list->list ){
00525         d_list->num = d_list->nall = d_list->elen = 0;
00526         return -1;
00527     }
00528 
00529     /* first, free the nall lists (of length elen) */
00530 
00531     for ( count = 0; count < d_list->nall; count++ )
00532         if ( d_list->list[count] ) free(d_list->list[count]);
00533 
00534     /* now free the list and clear all values */
00535     free(d_list->list);
00536     d_list->list = NULL;
00537 
00538     d_list->num = d_list->nall = d_list->elen = 0;
00539 
00540     return 0;
00541 }
00542 
 

Powered by Plone

This site conforms to the following standards: