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  

thd_serialate.c

Go to the documentation of this file.
00001 
00002 #include <stdlib.h>
00003 #include <stdio.h>
00004 #include <string.h>
00005 #include <ctype.h>
00006 
00007 typedef unsigned char           byte ;
00008 typedef struct { byte r,g,b ; } rgb ;
00009 typedef struct { float r,i ; }  complex ;
00010 
00011 #define SERTYPE_NUM 8
00012 typedef enum {
00013    SERTYPE_byte  , SERTYPE_short  , SERTYPE_int  ,
00014    SERTYPE_float , SERTYPE_double , SERTYPE_complex ,
00015    SERTYPE_rgb   , SERTYPE_string
00016 } SERTYPE ;
00017 
00018 static int SERTYPE_code[SERTYPE_NUM] = {
00019    SERTYPE_byte  , SERTYPE_short  , SERTYPE_int  ,
00020    SERTYPE_float , SERTYPE_double , SERTYPE_complex ,
00021    SERTYPE_rgb   , SERTYPE_string
00022 } ;
00023 
00024 static int SERTYPE_initialized = 0 ;
00025 
00026 static int SERTYPE_sizeof[SERTYPE_NUM] ;
00027 
00028 static const char *SERTYPE_name[SERTYPE_NUM] = {
00029    "byte"      , "short"      , "int"       ,
00030    "float"     , "double"     , "complex"   ,
00031    "rgb"       , "String"
00032 } ;
00033 
00034 static const char *SERTYPE_initial[SERTYPE_NUM] = {
00035    "b" , "s" , "i" , "f" , "d" , "c" , "r" , "S"
00036 } ;
00037 
00038 #define SER_MAXNAME 256
00039 typedef struct {
00040   char fieldname[SER_MAXNAME] ;
00041   int numvec , veclen , vecall ;
00042   int *vtype ;
00043   void **vec ;
00044 } SER_vector ;
00045 
00046 /*--------------------------------------------------------------------*/
00047 
00048 static void SER_setup_stuff(void)
00049 {
00050    SERTYPE_sizeof[0] = sizeof(byte) ;
00051    SERTYPE_sizeof[1] = sizeof(short) ;
00052    SERTYPE_sizeof[2] = sizeof(int) ;
00053    SERTYPE_sizeof[3] = sizeof(float) ;
00054    SERTYPE_sizeof[4] = sizeof(double) ;
00055    SERTYPE_sizeof[5] = sizeof(complex) ;
00056    SERTYPE_sizeof[6] = sizeof(rgb) ;
00057    SERTYPE_sizeof[7] = sizeof(char *) ;
00058 
00059    SERTYPE_initialized = 1 ;
00060 }
00061 
00062 /*--------------------------------------------------------------------*/
00063 
00064 SER_vector * SER_new_vector( char *fname , char *typelist )
00065 {
00066    SER_vector *sv ;
00067    int ntyp , ii , ntl , tpos , jj , *vtyp=NULL ;
00068    char *tlist , tname[SER_MAXNAME] ;
00069 
00070    if( typelist == NULL ) return NULL ;    /* bad */
00071 
00072    if( !SERTYPE_initialized ) SER_setup_stuff() ;
00073 
00074    sv = AFMALL( SER_vector, sizeof(SER_vector)) ;
00075 
00076    /*-- set fieldname --*/
00077 
00078    if( fname == NULL ){                    /* not good, but OK */
00079      sv->fieldname[0] = '\0' ;
00080    } else {
00081      strncpy(sv->fieldname,fname,SER_MAXNAME) ;
00082      sv->fieldname[SER_MAXNAME-1] = '\0' ;
00083    }
00084 
00085    /*-- copy and mangle type list --*/
00086 
00087    tlist = strdup(typelist) ;
00088    ntl = strlen(tlist) ;                       /* replace separators */
00089    for( ii=0 ; ii < ntl ; ii++ )               /* with blanks */
00090       if( tlist[ii] == ',' || tlist[ii] == ':' ||
00091           tlist[ii] == ';' || tlist[ii] == '+' ||
00092           tlist[ii] == '-' || tlist[ii] == '/'   ) tlist[ii] = ' ' ;
00093 
00094    /*-- scan mangled type list and get type codes --*/
00095 
00096    tpos = ntyp = 0 ;
00097    vtyp = (int *) malloc(sizeof(int)) ;
00098 
00099    do{
00100       ii = sscanf( tlist+tpos , "%s%n" , tname , &jj ) ;    /* next name */
00101       if( ii < 1 ) break ;                                /* end of work */
00102       tpos += jj ;                            /* char after last scanned */
00103 
00104       ii = strlen(tname) ;                     /* find name in type list */
00105       for( jj=0 ; jj < SERTYPE_NUM ; jj++ )
00106          if( strncmp(SERTYPE_name[jj],tname,ii) == 0 ) break ;
00107 
00108       if( jj == SERTYPE_NUM ){ free(tlist); free(vtyp); return NULL; }
00109 
00110       ntyp++ ;                                           /* add new type */
00111       vtyp = (int *) realloc( vtyp , sizeof(int)*ntyp ) ;
00112       vtyp[ntyp-1] = SERTYPE_code[jj] ;
00113    } while( tpos < ntl ) ;
00114 
00115    free(tlist) ;                              /* don't need copy no more */
00116 
00117    if( ntyp < 1 ){ free(vtyp); return NULL; }            /* got no types */
00118 
00119    /*-- now can setup rest of vector --*/
00120 
00121    sv->numvec = ntyp ;
00122    sv->veclen = 0 ;
00123    sv->vecall = 0 ;
00124    sv->vtype  = vtyp ;
00125    sv->vec    = (void **) malloc( sizeof(void *)*ntyp ) ;
00126    for( ii=0 ; ii < ntyp ; ii++ ) sv->vec[ii] = NULL ;
00127 
00128    return sv ;
00129 }
00130 
00131 /*--------------------------------------------------------------------*/
00132 
00133 void SER_destroy_vector( SER_vector *sv )
00134 {
00135    int ii , jj ;
00136 
00137    if( sv == NULL ) return ;
00138 
00139    if( sv->vec != NULL ){                    /* destroy each sub-vector */
00140      for( ii=0 ; ii < sv->numvec ; ii++ ){
00141         if( sv->vec[ii] != NULL ){
00142            if( sv->vtype[ii] == SERTYPE_string ){       /* if strings,  */
00143               char **SSp = (char **) sv->vec[ii] ;      /* must destroy */
00144               for( jj=0 ; jj < sv->numvec ; jj++ )      /* each string  */
00145                  if( SSp[jj] != NULL ) free(SSp[jj]) ;
00146            }
00147            free(sv->vec[ii]) ;
00148         }
00149      }
00150      free(sv->vec) ;
00151    }
00152 
00153    if( sv->vtype != NULL ) free(sv->vtype) ;
00154    free(sv) ;
00155 }
00156 
00157 /*--------------------------------------------------------------------*/
00158 
00159 void SER_free_vector( SER_vector *sv )
00160 {
00161    int ii ;
00162    if( sv == NULL ) return ;
00163    if( sv->vtype != NULL ) free(sv->vtype) ;
00164    if( sv->vec != NULL ) free(sv->vec) ;
00165    free(sv) ;
00166 }
00167 
00168 /*--------------------------------------------------------------------*/
00169 
00170 int SER_addto_vector_textmode( SER_vector *sv , int nstr , char *str )
00171 {
00172    int jj , err=0 , spos=0 , snum , ss ;
00173    void **val ;
00174    byte         *bbp ;
00175    short        *ssp ;
00176    int     ii , *iip ;
00177    float   ff , *ffp ;
00178    double  dd , *ddp ;
00179    complex cc , *ccp ;
00180    rgb          *rrp ; int rr,gg,bb ;
00181    char   *SS ,**SSp ;
00182 
00183    if( sv  == NULL ||
00184        str == NULL || str[0] == '\0' || nstr < sv->numvec ) return 0 ;
00185 
00186    /*-- loop over sub-vectors --*/
00187 
00188    val = (void **) calloc( sizeof(void *) , sv->numvec ) ;
00189 
00190    for( jj=0 ; jj < sv->numvec && spos < nstr ; jj++ ){
00191 
00192       switch( sv->vtype[jj] ){
00193 
00194          case SERTYPE_byte:
00195             ss = sscanf(str+spos,"%d%n",&ii,&snum) ;
00196             if( ss < 1 ){ err++ ; break ; }
00197             spos += snum ;
00198             bbp = (byte *) malloc( sizeof(byte) ) ; *bbp = (byte) ii ;
00199             val[jj] = (void *) bbp ;
00200          break ;
00201 
00202          case SERTYPE_short:
00203             ss = sscanf(str+spos,"%d%n",&ii,&snum) ;
00204             if( ss < 1 ){ err++ ; break ; }
00205             spos += snum ;
00206             ssp = (short *) malloc( sizeof(short) ) ; *ssp = (short) ii ;
00207             val[jj] = (void *) ssp ;
00208          break ;
00209 
00210          case SERTYPE_int:
00211             ss = sscanf(str+spos,"%d%n",&ii,&snum) ;
00212             if( ss < 1 ){ err++ ; break ; }
00213             spos += snum ;
00214             iip = (int *) malloc( sizeof(int) ) ; *iip = ii ;
00215             val[jj] = (void *) iip ;
00216          break ;
00217 
00218          case SERTYPE_float:
00219             ss = sscanf(str+spos,"%f%n",&ff,&snum) ;
00220             if( ss < 1 ){ err++ ; break ; }
00221             spos += snum ;
00222             ffp = (float *) malloc( sizeof(float) ) ; *ffp = ff ;
00223             val[jj] = (void *) ffp ;
00224          break ;
00225 
00226          case SERTYPE_double:
00227             ss = sscanf(str+spos,"%lf%n",&dd,&snum) ;
00228             if( ss < 1 ){ err++ ; break ; }
00229             spos += snum ;
00230             ddp = (double *) malloc( sizeof(double) ) ; *ddp = dd ;
00231             val[jj] = (void *) ddp ;
00232          break ;
00233 
00234          case SERTYPE_complex:
00235             ss = sscanf(str+spos,"%f%f%n",&(cc.r),&(cc.i),&snum) ;
00236             if( ss < 2 ){ err++ ; break ; }
00237             spos += snum ;
00238             ccp = (complex *) malloc( sizeof(complex) ) ; *ccp = cc ;
00239             val[jj] = (void *) ccp ;
00240          break ;
00241 
00242          case SERTYPE_rgb:
00243             ss = sscanf(str+spos,"%d%d%d%n",&rr,&gg,&bb,&snum) ;
00244             if( ss < 2 ){ err++ ; break ; }
00245             spos += snum ;
00246             rrp = (rgb *) malloc( sizeof(rgb) ) ;
00247             rrp->r = (byte)rr ; rrp->g = (byte)gg ; rrp->b = (byte)bb ;
00248             val[jj] = (void *) rrp ;
00249          break ;
00250 
00251 #define QUOTE '"'  /* string quoting character */
00252 
00253          case SERTYPE_string:{
00254             int quote=0 , kk,mm , ll ;
00255             for( kk=spos ; kk < nstr && iswhite(str[kk]) ; kk++ ) ; /* nada */
00256             if( kk == nstr ){ err++ ; break ; }
00257             if( str[kk] == QUOTE ){ quote=1; kk++; if(kk==nstr){err++;break;} }
00258             if( quote ){
00259                mm = kk ;
00260                while( mm < nstr ){
00261                   if( str[mm] == QUOTE && str[mm-1] != '\\' ) break ;
00262                   mm++ ;
00263                }
00264             } else {
00265                for( mm=kk ; mm < nstr && !iswhite(str[kk]) ; mm++ ) ; /* nada */
00266             }
00267             /* string runs from str[kk] to str[mm-1] */
00268             spos = mm+1 ;
00269             ll   = mm-kk ;
00270          }
00271          break ;
00272 
00273       }
00274    }
00275 
00276    if( jj < sv->numvec || err > 0 ){
00277       for( jj=0 ; jj < sv->numvec ; jj++ )
00278          if( val[jj] != NULL ) free(val[jj]) ;
00279       free(val) ; return 0 ;
00280    }
00281 }
 

Powered by Plone

This site conforms to the following standards: