Main Page   Modules   Data Structures   File List   Data Fields   Globals  

C_libs/gen/dyn_alloc.c

Go to the documentation of this file.
00001 /* === Doxygen File Comment ============================== */
00010 /* ======================================================= */
00011 
00012 #include <stdio.h>
00013 #include <alloc.h>
00014 #include <malloc.h>
00015 #include <error_codes.h>
00016 
00017 /* --- Doxygen Function Comment -------------------------- */
00021                 /* @{ ***** Starting group1 ***** */
00029 /* ------------------------------------------------------- */
00030 
00031 int
00032 alloc2d(void ***pt_tabl, int nrow, int ncol, short datatype)
00033 {
00034         int nc = ncol, nr = nrow;
00035         short **pt_short;
00036         long **pt_long;
00037         float **pt_float;
00038         double **pt_double;
00039         char **pt_char;
00040         long i;
00041 
00042         switch(datatype) {
00043 
00044         case CHAR :
00045                 *(char ***)pt_tabl = (char **)malloc(nr*sizeof(char *));
00046                 pt_char = *(char ***)pt_tabl;
00047                 if (pt_char == NULL) return(ERR_ALLOC);
00048                 pt_char[0] = (char *)malloc(nr*nc*sizeof(char));
00049                 if (pt_char[0] == NULL) return(ERR_ALLOC);
00050                 for (i=1;i<nr;i++)
00051                         pt_char[i] = pt_char[i-1] + nc;
00052         break;  
00053 
00054         case SHORT :
00055                 *(short ***)pt_tabl = (short **)malloc(nr*sizeof(short *));
00056                 pt_short = *(short ***)pt_tabl;
00057                 if (pt_short == NULL) return(ERR_ALLOC);
00058                 pt_short[0] = (short *)malloc(nr*nc*sizeof(short));
00059                 if (pt_short[0] == NULL) return(ERR_ALLOC);
00060                 for (i=1;i<nr;i++)
00061                         pt_short[i] = pt_short[i-1] + nc;
00062         break;  
00063              
00064         case INT :
00065         case LONG :
00066                 *(long ***)pt_tabl = (long **)malloc(nr*sizeof(long *));
00067                 pt_long = *(long ***)pt_tabl;
00068                 if (pt_long == NULL) return(ERR_ALLOC);
00069                 pt_long[0] = (long *)malloc(nr*nc*sizeof(long));
00070                 if (pt_long[0] == NULL) return(ERR_ALLOC);
00071                 for (i=1;i<nr;i++)
00072                         pt_long[i] = pt_long[i-1] + nc;
00073         break;  
00074             
00075         case FLOAT :
00076                 *(float ***)pt_tabl = (float **)malloc(nr*sizeof(float *));
00077                 pt_float = *(float ***)pt_tabl;
00078                 if (pt_float == NULL) return(ERR_ALLOC);
00079                 pt_float[0] = (float *)malloc(nr*nc*sizeof(float));
00080                 if (pt_float[0] == NULL) return(ERR_ALLOC);
00081                 for (i=1;i<nr;i++)
00082                         pt_float[i] = pt_float[i-1] + nc;
00083         break;  
00084            
00085         case DOUBLE :
00086                 *(double ***)pt_tabl = (double **)malloc(nr*sizeof(double *));
00087                 pt_double = *(double ***)pt_tabl;
00088                 if (pt_double == NULL) return(ERR_ALLOC);
00089                 pt_double[0] = (double *)malloc(nr*nc*sizeof(double));
00090                 if (pt_double[0] == NULL) return(ERR_ALLOC);
00091                 for (i=1;i<nr;i++)
00092                         pt_double[i] = pt_double[i-1] + nc;
00093         break;  
00094 
00095         default :
00096                 return(ERR_BAD_TYPE);           /* unkwnown data type */
00097 
00098         }
00099         return(0);
00100 }
00101 
00102 /* --- Doxygen Function Comment -------------------------- */
00109 /* ------------------------------------------------------- */
00110 
00111 int 
00112 free2d(void ***pt_tabl, short datatype)
00113 
00114 {
00115         short **pt_short;
00116         long **pt_long;
00117         float **pt_float;
00118         double **pt_double;
00119         char **pt_char;
00120 
00121         switch(datatype) {
00122                     
00123         case CHAR :
00124                 pt_char = *(char ***)pt_tabl;
00125                 if (pt_char == NULL) return(ERR_FREE);
00126                 if (pt_char[0] == NULL) return(ERR_FREE);
00127                 free((char *)pt_char[0]);
00128                 free((char *)pt_char);
00129         break;  
00130                            
00131         case SHORT :
00132                 pt_short = *(short ***)pt_tabl;
00133                 if (pt_short == NULL) return(ERR_FREE);
00134                 if (pt_short[0] == NULL) return(ERR_FREE);
00135                 free((char *)pt_short[0]);
00136                 free((char *)pt_short);
00137         break;  
00138                           
00139         case INT :
00140         case LONG :
00141                 pt_long = *(long ***)pt_tabl;
00142                 if (pt_long == NULL) return(ERR_FREE);
00143                 if (pt_long[0] == NULL) return(ERR_FREE);
00144                 free((char *)pt_long[0]);
00145                 free((char *)pt_long);
00146         break;  
00147          
00148         case FLOAT :
00149                 pt_float = *(float ***)pt_tabl;
00150                 if (pt_float == NULL) return(ERR_FREE);
00151                 if (pt_float[0] == NULL) return(ERR_FREE);
00152                 free((char *)pt_float[0]);
00153                 free((char *)pt_float);
00154         break;  
00155         
00156         case DOUBLE :
00157                 pt_double = *(double ***)pt_tabl;
00158                 if (pt_double == NULL) return(ERR_FREE);
00159                 if (pt_double[0] == NULL) return(ERR_FREE);
00160                 free((char *)pt_double[0]);
00161                 free((char *)pt_double);
00162         break;  
00163 
00164         default :
00165                 return(ERR_BAD_TYPE);                /* unkwnown data type */
00166 
00167         }
00168         return(0);
00169 }
00170 

Generated on Tue Jul 1 17:35:44 2003 for IFU_C_iolibs by doxygen1.3