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 File Reference

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

Go to the source code of this file.


Data Structures

struct  complex
struct  rgb
struct  SER_vector

Defines

#define SERTYPE_NUM   8
#define SER_MAXNAME   256
#define QUOTE   '"'

Typedefs

typedef unsigned char byte

Enumerations

enum  SERTYPE {
  SERTYPE_byte, SERTYPE_short, SERTYPE_int, SERTYPE_float,
  SERTYPE_double, SERTYPE_complex, SERTYPE_rgb, SERTYPE_string
}

Functions

void SER_setup_stuff (void)
SER_vectorSER_new_vector (char *fname, char *typelist)
void SER_destroy_vector (SER_vector *sv)
void SER_free_vector (SER_vector *sv)
int SER_addto_vector_textmode (SER_vector *sv, int nstr, char *str)

Variables

int SERTYPE_code [SERTYPE_NUM]
int SERTYPE_initialized = 0
int SERTYPE_sizeof [SERTYPE_NUM]
const char * SERTYPE_name [SERTYPE_NUM]
const char * SERTYPE_initial [SERTYPE_NUM]

Define Documentation

#define QUOTE   '"'
 

#define SER_MAXNAME   256
 

Definition at line 38 of file thd_serialate.c.

Referenced by SER_new_vector().

#define SERTYPE_NUM   8
 

Definition at line 11 of file thd_serialate.c.

Referenced by SER_new_vector().


Typedef Documentation

typedef unsigned char byte
 

Definition at line 7 of file thd_serialate.c.

Referenced by SER_addto_vector_textmode().


Enumeration Type Documentation

enum SERTYPE
 

Enumeration values:
SERTYPE_byte 
SERTYPE_short 
SERTYPE_int 
SERTYPE_float 
SERTYPE_double 
SERTYPE_complex 
SERTYPE_rgb 
SERTYPE_string 

Definition at line 12 of file thd_serialate.c.


Function Documentation

int SER_addto_vector_textmode SER_vector   sv,
int    nstr,
char *    str
 

Definition at line 170 of file thd_serialate.c.

References rgb::b, byte, calloc, ccp, free, rgb::g, complex::i, malloc, SER_vector::numvec, rgb::r, complex::r, SERTYPE_byte, SERTYPE_complex, SERTYPE_double, SERTYPE_float, SERTYPE_int, SERTYPE_rgb, SERTYPE_short, SERTYPE_string, and SER_vector::vtype.

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 }

void SER_destroy_vector SER_vector   sv
 

Definition at line 133 of file thd_serialate.c.

References free, SER_vector::numvec, SERTYPE_string, SER_vector::vec, and SER_vector::vtype.

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 }

void SER_free_vector SER_vector   sv
 

Definition at line 159 of file thd_serialate.c.

References free, SER_vector::vec, and SER_vector::vtype.

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 }

SER_vector* SER_new_vector char *    fname,
char *    typelist
 

Definition at line 64 of file thd_serialate.c.

References AFMALL, SER_vector::fieldname, free, malloc, SER_vector::numvec, realloc, SER_MAXNAME, SER_setup_stuff(), SERTYPE_code, SERTYPE_initialized, SERTYPE_name, SERTYPE_NUM, SER_vector::vec, SER_vector::vecall, SER_vector::veclen, and SER_vector::vtype.

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 }

void SER_setup_stuff void    [static]
 

Definition at line 48 of file thd_serialate.c.

References SERTYPE_initialized, and SERTYPE_sizeof.

Referenced by SER_new_vector().

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 }

Variable Documentation

int SERTYPE_code[SERTYPE_NUM] [static]
 

Initial value:

Definition at line 18 of file thd_serialate.c.

Referenced by SER_new_vector().

const char* SERTYPE_initial[SERTYPE_NUM] [static]
 

Initial value:

 {
   "b" , "s" , "i" , "f" , "d" , "c" , "r" , "S"
}

Definition at line 34 of file thd_serialate.c.

int SERTYPE_initialized = 0 [static]
 

Definition at line 24 of file thd_serialate.c.

Referenced by SER_new_vector(), and SER_setup_stuff().

const char* SERTYPE_name[SERTYPE_NUM] [static]
 

Initial value:

 {
   "byte"      , "short"      , "int"       ,
   "float"     , "double"     , "complex"   ,
   "rgb"       , "String"
}

Definition at line 28 of file thd_serialate.c.

Referenced by SER_new_vector().

int SERTYPE_sizeof[SERTYPE_NUM] [static]
 

Definition at line 26 of file thd_serialate.c.

Referenced by SER_setup_stuff().

 

Powered by Plone

This site conforms to the following standards: