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  

niml_dtable.c

Go to the documentation of this file.
00001 #include "niml_private.h"
00002 #include <math.h>
00003 
00004 /******************************************************************/
00005 /**** Dtable: string-string pairs (two Htables) -- 15 Oct 2003 ****/
00006 /******************************************************************/
00007 
00008 /*-----------------------------------------------------------------*/
00009 /*! Create a Dtable with len slots.
00010 -------------------------------------------------------------------*/
00011 
00012 Dtable * new_Dtable( int len )
00013 {
00014    Dtable *dt ;
00015    dt = (Dtable *) calloc( 1 , sizeof(Dtable) ) ;
00016    dt->hta = new_Htable( len ) ;
00017    dt->htb = new_Htable( len ) ;
00018    return dt ;
00019 }
00020 
00021 /*-----------------------------------------------------------------*/
00022 /*! Death and destruction of a Dtable.
00023 -------------------------------------------------------------------*/
00024 
00025 void destroy_Dtable( Dtable *dt )
00026 {
00027    if( dt == NULL ) return ;
00028    Htable_set_vtkill(1) ;
00029    destroy_Htable( dt->hta ) ;
00030    destroy_Htable( dt->htb ) ;
00031    Htable_set_vtkill(0) ; ;
00032    return ;
00033 }
00034 
00035 /*-----------------------------------------------------------------*/
00036 /*! Insert string pair str_a,str_b into the Dtable.
00037     Copies of the strings are made.
00038 -------------------------------------------------------------------*/
00039 
00040 void addto_Dtable( char *str_a , char *str_b , Dtable *dt )
00041 {
00042    char *sa , *sb ;
00043    if( dt == NULL || str_a == NULL || str_b == NULL ) return ;
00044    sa = strdup(str_a) ; sb = strdup(str_b) ;
00045    addto_Htable( sa , (void *)sb , dt->hta ) ;
00046    addto_Htable( sb , (void *)sa , dt->htb ) ;
00047    return ;
00048 }
00049 
00050 /*-----------------------------------------------------------------*/
00051 
00052 char * findin_Dtable_a( char *str_a , Dtable *dt )
00053 {
00054    if( dt == NULL || str_a == NULL ) return NULL ;
00055    return (char *)findin_Htable( str_a , dt->hta ) ;
00056 }
00057 
00058 /*-----------------------------------------------------------------*/
00059 
00060 char * findin_Dtable_b( char *str_b , Dtable *dt )
00061 {
00062    if( dt == NULL || str_b == NULL ) return NULL ;
00063    return (char *)findin_Htable( str_b , dt->htb ) ;
00064 }
00065 
00066 /*-----------------------------------------------------------------*/
00067 
00068 void removefrom_Dtable_a( char *str_a , Dtable *dt )
00069 {
00070    char *str_bb , *str_aa ;
00071    if( dt == NULL ) return ;
00072    str_bb = (char *)findin_Htable( str_a , dt->hta ) ;
00073    if( str_bb == NULL ) return ;
00074    str_aa = (char *)findin_Htable( str_bb, dt->htb ) ;
00075    removefrom_Htable( str_a , dt->hta ) ;
00076    removefrom_Htable( str_bb, dt->htb ) ;
00077 
00078    /* must also remove dangling targets from each Htable */
00079 
00080    free((void *)str_bb) ; if( str_aa != NULL ) free((void *)str_aa) ;
00081    return ;
00082 }
00083 
00084 /*-----------------------------------------------------------------*/
00085 
00086 void removefrom_Dtable_b( char *str_b , Dtable *dt )
00087 {
00088    char *str_aa , *str_bb ;
00089    if( dt == NULL ) return ;
00090    str_aa = (char *)findin_Htable( str_b , dt->htb ) ;
00091    if( str_aa == NULL ) return ;
00092    str_bb = (char *)findin_Htable( str_aa, dt->hta ) ;
00093    removefrom_Htable( str_b , dt->htb ) ;
00094    removefrom_Htable( str_aa, dt->hta ) ;
00095 
00096    free((void *)str_aa) ; if( str_bb != NULL ) free((void *)str_bb) ;
00097    return ;
00098 }
00099 
00100 /*-----------------------------------------------------------------*/
00101 /* Create lists of the pointers to the strings directly inside
00102    the Dtable.  Return value is number of string pairs.  Since
00103    these are pointers INTO the Dtable, don't free() these!
00104    However, you should free(*list_a) and free(*list_b) when done.
00105 -------------------------------------------------------------------*/
00106 
00107 int listize_Dtable( Dtable *dt , char ***list_a , char ***list_b )
00108 {
00109    char **la=NULL , **lb=NULL , *sa,*sb ;
00110    int jj,kk,nn ;
00111    Htable *ht ;
00112 
00113    if( dt == NULL || list_a == NULL || list_b == NULL ) return 0 ;
00114 
00115    ht = dt->hta ;
00116 
00117    for( nn=jj=0 ; jj < ht->len ; jj++ ){
00118      if( ht->vtab[jj] == NULL ) continue ;
00119      for( kk=0 ; kk < ht->ntab[jj] ; kk++ ){
00120        sa = (char *) ht->ctab[jj][kk] ; if( sa == NULL ) continue ;
00121        sb = (char *) ht->vtab[jj][kk] ; if( sb == NULL ) continue ;
00122        la = (char **) realloc( (void *)la , sizeof(char *)*(nn+1) ) ;
00123        lb = (char **) realloc( (void *)lb , sizeof(char *)*(nn+1) ) ;
00124        la[nn] = sa ; lb[nn] = sb ; nn++ ;
00125      }
00126    }
00127    *list_a = la ; *list_b = lb ; return nn ;
00128 }
00129 
00130 /*---------------------------------------------------------------------------*/
00131 /* Create a string that encodes all of a Dtable.  free() this when done.
00132 -----------------------------------------------------------------------------*/
00133 
00134 char * Dtable_to_nimlstring( Dtable *dt , char *name )
00135 {
00136    int nn , ii ;
00137    char **la , **lb , *stout ;
00138    NI_element *nel ;
00139    NI_stream ns ;
00140 
00141    nn = listize_Dtable( dt , &la , &lb ) ;
00142    if( nn == 0 || la == NULL || lb == NULL ) return (char *)NULL ;
00143 
00144    if( name == NULL || *name == '\0' ) name = "Dtable" ;
00145 
00146    nel = NI_new_data_element( name , nn ) ;
00147    NI_add_column( nel , NI_STRING , la ) ;
00148    NI_add_column( nel , NI_STRING , lb ) ;
00149    free(la) ; free(lb) ;
00150 
00151    ns = NI_stream_open( "str:" , "w" ) ;
00152    (void) NI_write_element( ns , nel , NI_TEXT_MODE ) ;
00153    NI_free_element( nel ) ;
00154    stout = strdup( NI_stream_getbuf(ns) ) ;
00155    NI_stream_close( ns ) ;
00156    nn = strlen(stout) ;
00157    for( ii=nn-1 ; ii > 0 && isspace(stout[ii]) ; ii-- ) ; /* trailing blanks */
00158    stout[ii+1] = '\0' ;
00159    return stout ;
00160 }
00161 
00162 /*---------------------------------------------------------------------------*/
00163 
00164 Dtable * Dtable_from_nimlstring( char *nstr )
00165 {
00166    NI_stream ns ;
00167    NI_element *nel ;
00168    int nn , ii ;
00169    Dtable *dt ;
00170    char **la , **lb ;
00171 
00172    if( nstr == NULL || *nstr == '\0' ) return NULL ;
00173 
00174    /* convert string to a NIML element */
00175 
00176    ns = NI_stream_open( "str:" , "r" ) ;
00177    NI_stream_setbuf( ns , nstr ) ;
00178    nel = (NI_element *)NI_read_element( ns , 1 ) ;
00179    NI_stream_close( ns ) ;
00180    if( nel == NULL ) return NULL ;
00181 
00182    /* see if element is OK for this purpose */
00183 
00184    if( NI_element_type(nel) != NI_ELEMENT_TYPE ){
00185      NI_free_element(nel) ; return NULL ;
00186    }
00187 
00188    if( nel->vec_len    <  1         ||  /* empty element?             */
00189        nel->vec_filled <  1         ||  /* no data was filled in?     */
00190        nel->vec_num    <  2         ||  /* less than 4 columns?       */
00191        nel->vec_typ[0] != NI_STRING ||  /* must be String, String     */
00192        nel->vec_typ[1] != NI_STRING   ){
00193 
00194      NI_free_element(nel) ; return NULL ;
00195    }
00196 
00197    la = (char **) nel->vec[0] ;  /* first column of String */
00198    lb = (char **) nel->vec[1] ;  /* second column of String */
00199 
00200    nn = nel->vec_filled ;
00201    ii = rint(sqrt(2*nn+1.0l)) ;
00202    if( ii < 7 ) ii = 7 ; else if( ii%2 == 0 ) ii++ ;
00203 
00204    /* make table, insert strings */
00205 
00206    dt = new_Dtable( ii ) ;
00207    for( ii=0 ; ii < nn ; ii++ )
00208      addto_Dtable( la[ii] , lb[ii] , dt ) ;
00209 
00210    NI_free_element(nel) ; return dt ;
00211 }
 

Powered by Plone

This site conforms to the following standards: