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  

multivector.h File Reference

#include <ctype.h>
#include "mrilib.h"

Go to the source code of this file.


Data Structures

struct  multivector

Defines

#define MV_FLOAT   1
#define MV_STRING   2
#define MV_TYPELABEL(i)
#define MV_NVEC(m)   ((m)->nvec)
#define MV_NDIM(m)   ((m)->ndim)
#define MV_FLOAT_VEC(m, i)   ((float *)(m)->vec[(i)])
#define MV_FLOAT_VAL(m, i, j)   (((float *)(m)->vec[(i)])[(j)])
#define MV_STRING_VEC(m, i)   ((char **)(m)->vec[(i)])
#define MV_STRING_VAL(m, i, j)   (((char **)(m)->vec[(i)])[(j)])
#define MV_TYPE(m, i)   ((m)->type[(i)])
#define MV_LABEL(m, i)   (((m)->label != NULL) ? (m)->label[(i)] : NULL)
#define MV_NAME(m)   ((m)->name)
#define MV_FREE(m)   do{ multivector_free((m)); (m)=NULL; }while(0)

Functions

multivectormultivector_read (char *)
int multivector_write (char *, multivector *)
void multivector_free (multivector *mv)
void multivector_set_name (multivector *, char *)
char * MV_format_fval (float)
char * MV_format_fval2 (float, int)
 s = MV_format_fval2( fval, len); same as fval, but will attempt to keep the number len characters long. That's done by truncating digits to the right of the decimal point, if one exists.


Define Documentation

#define MV_FLOAT   1
 

Definition at line 13 of file multivector.h.

Referenced by multivector_read(), and multivector_write().

#define MV_FLOAT_VAL m,
i,
j       (((float *)(m)->vec[(i)])[(j)])
 

Definition at line 31 of file multivector.h.

#define MV_FLOAT_VEC m,
i       ((float *)(m)->vec[(i)])
 

Definition at line 30 of file multivector.h.

#define MV_FREE m       do{ multivector_free((m)); (m)=NULL; }while(0)
 

Definition at line 48 of file multivector.h.

#define MV_LABEL m,
i       (((m)->label != NULL) ? (m)->label[(i)] : NULL)
 

Definition at line 37 of file multivector.h.

#define MV_NAME m       ((m)->name)
 

Definition at line 38 of file multivector.h.

#define MV_NDIM m       ((m)->ndim)
 

Definition at line 28 of file multivector.h.

#define MV_NVEC m       ((m)->nvec)
 

Definition at line 27 of file multivector.h.

#define MV_STRING   2
 

Definition at line 14 of file multivector.h.

Referenced by multivector_read(), and multivector_write().

#define MV_STRING_VAL m,
i,
j       (((char **)(m)->vec[(i)])[(j)])
 

Definition at line 34 of file multivector.h.

#define MV_STRING_VEC m,
i       ((char **)(m)->vec[(i)])
 

Definition at line 33 of file multivector.h.

#define MV_TYPE m,
i       ((m)->type[(i)])
 

Definition at line 36 of file multivector.h.

#define MV_TYPELABEL i   
 

Value:

(((i)==MV_FLOAT)  ? "FLOAT"  :           \
                         ((i)==MV_STRING) ? "STRING" : "unknown")

Definition at line 16 of file multivector.h.


Function Documentation

void multivector_free multivector   mv
 

Definition at line 38 of file multivector.c.

References free, multivector::label, multivector::name, multivector::nvec, multivector::type, and multivector::vec.

Referenced by main(), and multivector_read().

00039 {
00040    int ii ;
00041 
00042    if( mv == NULL ) return ;
00043 
00044    if( mv->name != NULL ) free(mv->name) ;
00045    if( mv->type != NULL ) free(mv->type) ;
00046    if( mv->label != NULL )
00047       for( ii=0 ; ii < mv->nvec ; ii++ ) free(mv->label[ii]) ;
00048    if( mv->vec != NULL )
00049       for( ii=0 ; ii < mv->nvec ; ii++ ) free(mv->vec[ii]) ;
00050 
00051    free(mv) ; return ;
00052 }

multivector* multivector_read char *   
 

Definition at line 65 of file multivector.c.

References free, multivector::label, LBUF, malloc, MERR, multivector_free(), MV_FLOAT, MV_STRING, my_strequiv(), multivector::name, multivector::ndim, multivector::nvec, NVMAX, realloc, SEPCH, strtod(), typ, multivector::type, and multivector::vec.

Referenced by main().

00066 {
00067    FILE * fp ;
00068    char buf[LBUF] ;
00069    char * ptr , * pp[NVMAX] ;
00070    multivector * mv ;
00071    int ii , ll , nvec,ndim , first=0 ;
00072    float val ;
00073 
00074    /*-- sanity check --*/
00075 
00076    if( fname == NULL || fname[0] == '\0' ) return NULL ;
00077 
00078    fp = fopen( fname , "r" ) ;
00079    if( fp == NULL ){ MERR("can't open file"); return NULL; }
00080 
00081    mv = (multivector *) malloc( sizeof(multivector) ) ;
00082    nvec = ndim = mv->nvec = mv->ndim = 0 ;
00083    mv->name = strdup(fname) ;
00084    mv->type = NULL ; mv->label = NULL ; mv->vec = NULL ;
00085 
00086    /*-- read and process any header comments --*/
00087 
00088    while(1){
00089       ptr = fgets( buf , LBUF , fp ) ;
00090       if( ptr == NULL ){
00091          fclose(fp); multivector_free(mv); MERR("no data"); return NULL;
00092       }
00093 
00094       ll = strlen(buf) ;
00095       for( ii=ll-1 ; ii >= 0 ; ii-- ) if( !isspace(buf[ii]) ) break ;
00096       if( ii < 0 ) continue ;       /* was all blanks; goto next line */
00097 
00098       if( buf[0] != '#' ){ first=1; break; }   /* not a header line ==> done */
00099 
00100       ptr = strtok( buf , SEPCH ) ;
00101 
00102       /* handle #NAME */
00103 
00104       if( my_strequiv(ptr,"#NAME") ){
00105          ptr = strtok( NULL , SEPCH ) ;
00106          if( ptr != NULL ){
00107             free(mv->name) ; mv->name = strdup(ptr) ;
00108          }
00109          continue ;  /* goto next line */
00110       }
00111 
00112       /* handle #TYPE */
00113 
00114       if( my_strequiv(ptr,"#TYPE") ){
00115          int ntyp=0 , typ[NVMAX] ;
00116 
00117          if( mv->type != NULL ){
00118             fclose(fp); multivector_free(mv); MERR("second #TYPE"); return NULL;
00119          }
00120 
00121          /* scan tokens for type strings */
00122 
00123          while(1){
00124             ptr = strtok( NULL , SEPCH ) ;
00125             if( ptr == NULL ) break ;
00126 
00127             if( ntyp >= NVMAX ){
00128                fclose(fp); multivector_free(mv); MERR("oversize #TYPE"); return NULL;
00129             }
00130 
00131                  if( my_strequiv(ptr,"STRING") ) typ[ntyp++] = MV_STRING ;
00132             else if( my_strequiv(ptr,"FLOAT")  ) typ[ntyp++] = MV_FLOAT  ;
00133             else {
00134                fclose(fp); multivector_free(mv); MERR("illegal #TYPE"); return NULL;
00135             }
00136          }
00137 
00138          if( ntyp == 0 ){
00139             fclose(fp); multivector_free(mv); MERR("illegal #TYPE"); return NULL;
00140          }
00141 
00142          if( mv->nvec > 0 && ntyp != mv->nvec ){
00143             fclose(fp); multivector_free(mv); MERR("illegal #TYPE count"); return NULL;
00144          }
00145 
00146          if( mv->nvec == 0 ) nvec = mv->nvec = ntyp ;
00147          mv->type = (int *) malloc( sizeof(int) * ntyp ) ;
00148          for( ii=0 ; ii < ntyp ; ii++ ) mv->type[ii] = typ[ii] ;
00149          continue ;  /* goto next line */
00150       }
00151 
00152       /* handle #LABEL */
00153 
00154       if( my_strequiv(ptr,"#LABEL") ){
00155          int nlab=0 ; char * lab[NVMAX] ;
00156 
00157          if( mv->label != NULL ){
00158             fclose(fp); multivector_free(mv); MERR("second #LABEL"); return NULL;
00159          }
00160 
00161          /* scan tokens for label strings */
00162 
00163          while(1){
00164             ptr = strtok( NULL , SEPCH ) ;
00165             if( ptr == NULL ) break ;
00166 
00167             if( nlab >= NVMAX ){
00168                for( ii=0 ; ii < nlab ; ii++ ) free( lab[ii] ) ;
00169                fclose(fp); multivector_free(mv); MERR("oversize #LABEL"); return NULL;
00170             }
00171 
00172             lab[nlab++] = strdup(ptr) ;
00173          }
00174 
00175          if( nlab == 0 ){
00176             fclose(fp); multivector_free(mv); MERR("illegal #LABEL"); return NULL;
00177          }
00178 
00179          if( mv->nvec > 0 && nlab != mv->nvec ){
00180             for( ii=0 ; ii < nlab ; ii++ ) free( lab[ii] ) ;
00181             fclose(fp); multivector_free(mv); MERR("illegal #LABEL count"); return NULL;
00182          }
00183 
00184          if( mv->nvec == 0 ) nvec = mv->nvec = nlab ;
00185          mv->label = (char **) malloc( sizeof(char *) * nlab ) ;
00186          for( ii=0 ; ii < nlab ; ii++ ) mv->label[ii] = lab[ii] ;
00187          continue ;  /* goto next line */
00188       }
00189 
00190       /* otherwise, just ignore the line (it's a comment, maybe) */
00191 
00192    } /* end of scan over header lines */
00193 
00194    /*-- read and store data lines --*/
00195 
00196    while(1){
00197       if( !first ) ptr = fgets( buf , LBUF , fp ) ;
00198       if( ptr == NULL ) break ;        /* end of input */
00199       first = 0 ;
00200 
00201       ll = strlen(buf) ;
00202       for( ii=ll-1 ; ii >= 0 ; ii-- ) if( !isspace(buf[ii]) ) break ;
00203       if( ii < 0 ) continue ;         /* was all blanks; goto next line */
00204       if( buf[0] == '#' ) continue ;  /* a comment line; goto next line */
00205 
00206       /* extract tokens from this line */
00207 
00208       pp[0] = strtok(buf,SEPCH) ; if( pp[0] == NULL ) continue ;
00209       ll = 1 ;
00210       while(1){
00211          pp[ll] = strtok(NULL,SEPCH) ; if( pp[ll] == NULL ) break ;
00212          ll++ ;
00213       }
00214 
00215       /* check count */
00216 
00217       if( nvec == 0 ){
00218           mv->nvec = nvec = ll ;
00219           if( nvec > NVMAX ) MERR("too many columns") ;
00220       }
00221       if( ll > nvec ) ll = nvec ;
00222 
00223       /* make type, if needed */
00224 
00225       if( mv->type == NULL ){
00226          mv->type = (int *) malloc( sizeof(int) * nvec ) ;
00227          for( ii=0 ; ii < ll ; ii++ ){
00228             val = strtod( pp[ii] , &ptr ) ;
00229             if( *ptr != '\0' ) mv->type[ii] = MV_STRING ;
00230             else               mv->type[ii] = MV_FLOAT  ;
00231          }
00232          for( ; ii < nvec ; ii++ )    /* this can only happen if #LABEL  */
00233             mv->type[ii] = MV_FLOAT ; /* is used and has too many labels */
00234       }
00235 
00236       /* initialize vector space, if needed */
00237 
00238       if( mv->vec == NULL ){
00239          mv->vec = (void **) malloc( sizeof(void *) * nvec ) ;
00240          for( ii=0 ; ii < nvec ; ii++ )
00241             mv->vec[ii] = (void *) malloc( sizeof(float)*16 ) ;
00242       }
00243 
00244       /* expand vector space for new row of data,
00245          convert tokens to values and store them in this space */
00246 
00247       for( ii=0 ; ii < nvec ; ii++ ){
00248          switch( mv->type[ii] ){
00249             case MV_FLOAT:{
00250                float * fpt ;
00251                mv->vec[ii] = (void *) realloc( mv->vec[ii], sizeof(float)*(ndim+1) );
00252                fpt = (float *) mv->vec[ii] ;
00253                fpt[ndim] = (ii < ll) ? strtod( pp[ii] , NULL ) : 0.0 ;
00254             }
00255             break ;
00256 
00257             case MV_STRING:{
00258                char ** cpt ;
00259                mv->vec[ii] = (void *) realloc( mv->vec[ii], sizeof(char *)*(ndim+1) );
00260                cpt = (char **) mv->vec[ii] ;
00261                cpt[ndim] = (ii < ll) ? strdup(pp[ii]) : strdup("\0") ;
00262             }
00263             break ;
00264           }
00265       }
00266       ndim++ ;   /* just added a new element! */
00267 
00268    } /* end of processing this line */
00269 
00270    /*-- done --*/
00271 
00272    mv->ndim = ndim ; return mv ;
00273 }

void multivector_set_name multivector  ,
char *   
 

Definition at line 280 of file multivector.c.

References free, and multivector::name.

Referenced by main().

00281 {
00282    if( mv->name != NULL ){ free(mv->name); mv->name = NULL; }
00283 
00284    if( nname != NULL ) mv->name = strdup(nname) ;
00285    return ;
00286 }

int multivector_write char *   ,
multivector  
 

Definition at line 294 of file multivector.c.

References multivector::label, LBUF, MAX, MV_FLOAT, MV_fval_to_char(), MV_STRING, multivector::name, multivector::ndim, multivector::nvec, NVMAX, THD_filename_ok(), multivector::type, and multivector::vec.

Referenced by main().

00295 {
00296    int nvec,ndim , ii,kk,ll , width[NVMAX] ;
00297    char buf[LBUF] , fbuf[32] ;
00298    FILE * fp ;
00299    float * fpt ;
00300    char ** cpt ;
00301 
00302    /*-- sanity checks --*/
00303 
00304    if( !THD_filename_ok(fname) || mv == NULL ) return 0 ;
00305 
00306    nvec = mv->nvec ; ndim = mv->ndim ;
00307    if( nvec < 1 || ndim < 1 ) return 0 ;
00308 
00309    if( mv->type == NULL || mv->vec == NULL ) return 0 ;
00310 
00311    /*-- open file, write headers --*/
00312 
00313    if( strcmp(fname,"-") == 0 ){
00314       fp = stdout ;
00315    } else {
00316       fp = fopen( fname , "w" ) ; if( fp == NULL ) return 0 ;
00317    }
00318 
00319    if( mv->name != NULL ) fprintf(fp,"#NAME %s\n",mv->name) ;
00320 
00321    if( mv->label != NULL ){
00322       sprintf(buf,"#LABEL") ;
00323       for( ii=0 ; ii < nvec ; ii++ ){
00324          ll = strlen(buf) ;
00325          if( mv->label[ii] != NULL )
00326             sprintf(buf+ll," %s",mv->label[ii]) ;
00327          else
00328             sprintf(buf+ll," -none-") ;
00329       }
00330       fprintf(fp,"%s\n",buf) ;
00331    }
00332 
00333    sprintf(buf,"#TYPE") ;
00334    for( ii=0 ; ii < nvec ; ii++ ){
00335       ll = strlen(buf) ;
00336       switch( mv->type[ii] ){
00337          case MV_FLOAT:  sprintf(buf+ll," FLOAT" ) ; break ;
00338          case MV_STRING: sprintf(buf+ll," STRING") ; break ;
00339       }
00340       width[ii] = 1 ;
00341    }
00342    fprintf(fp,"%s\n",buf) ;
00343 
00344    /*-- scan vectors to determine maximum column widths --*/
00345 
00346    for( kk=0 ; kk < ndim ; kk++ ){
00347       for( ii=0 ; ii < nvec ; ii++ ){
00348          switch( mv->type[ii] ){
00349             case MV_FLOAT:
00350                fpt = (float *) mv->vec[ii] ;
00351                MV_fval_to_char( fpt[kk] , fbuf ) ; ll = strlen(fbuf) ;
00352                width[ii] = MAX( width[ii] , ll ) ;
00353             break ;
00354 
00355             case MV_STRING:
00356                cpt = (char **) mv->vec[ii] ; ll = strlen(cpt[kk]) ;
00357                width[ii] = MAX( width[ii] , ll ) ;
00358             break ;
00359          }
00360       }
00361    }
00362 
00363    /*-- write data in columns --*/
00364 
00365    for( kk=0 ; kk < ndim ; kk++ ){
00366       buf[0] = '\0' ;
00367       for( ii=0 ; ii < nvec ; ii++ ){
00368          ll = strlen(buf) ;
00369          switch( mv->type[ii] ){
00370             case MV_FLOAT:
00371                fpt = (float *) mv->vec[ii] ;
00372                MV_fval_to_char( fpt[kk] , fbuf ) ;
00373                sprintf(buf+ll," %*s",width[ii],fbuf) ;
00374             break ;
00375 
00376             case MV_STRING:
00377                cpt = (char **) mv->vec[ii] ;
00378                sprintf(buf+ll," %*s",width[ii],cpt[kk]) ;
00379             break ;
00380          }
00381       }
00382       fprintf(fp,"%s\n",buf) ;
00383    }
00384 
00385    /*-- done --*/
00386 
00387    if( fp != stdout ) fclose(fp) ;
00388    return 1 ;
00389 }

char* MV_format_fval float    fval
 

See also:
MV_format_fval2 ZSS May 28 04

Definition at line 464 of file multivector.c.

References MV_fval_to_char().

Referenced by AFNI_niml_redisplay_CB(), atr_print(), compute_node_areas(), disp_f3_point(), disp_surf_vals(), main(), redraw_graph(), SUMA_cb_AbsThresh_tb_toggled(), SUMA_cb_set_threshold_label(), SUMA_SetScaleRange(), and v2s_write_outfile_1D().

00465 {
00466    static char buf[32] ;
00467    MV_fval_to_char( fval , buf ) ;
00468    return buf ;
00469 }

char* MV_format_fval2 float    fval,
int    len
 

s = MV_format_fval2( fval, len); same as fval, but will attempt to keep the number len characters long. That's done by truncating digits to the right of the decimal point, if one exists.

See also:
MV_fval_to_char , MV_format_fval ZSS, RickR May 28 04

Definition at line 480 of file multivector.c.

References MV_fval_to_char().

Referenced by main(), SUMA_M2M_node_Info(), SUMA_TableF_SetString(), SUMA_UpdateNodeLblField(), and SUMA_UpdateNodeNodeField().

00481 {
00482    static char buf[32] ;
00483    int wid;
00484    char *pos = NULL;
00485    
00486    MV_fval_to_char( fval , buf ) ;
00487    if (len < 1) return (buf);
00488    if (strlen(buf) < len) return (buf);
00489    
00490    /* trim it down */
00491    pos = strchr (buf, '.');
00492    if (!pos) return(buf);  /* can't do no'in */
00493    wid = pos - buf;
00494    if (wid < len) buf[len] = '\0';
00495    if (buf[len-1] == '.') buf[len-1] = '\0'; /* remove trailing period */
00496    return buf ;
00497 
00498 }
 

Powered by Plone

This site conforms to the following standards: