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  

NLfit_model.c

Go to the documentation of this file.
00001 /*****************************************************************************
00002    Major portions of this software are copyrighted by the Medical College
00003    of Wisconsin, 1994-2000, and are released under the Gnu General Public
00004    License, Version 2.  See the file README.Copyright for details.
00005 ******************************************************************************/
00006 
00007 /*
00008   This file contains routines to open and initialize models. The interface 
00009   with the signal and noise models is accomplished using dynamic libraries.
00010   This file was adapted from afni_plugin.c.
00011   
00012   File:     NLfit_model.c
00013   Author:   B. Douglas Ward
00014   Date:     23 June 1997
00015 
00016 */
00017 
00018 /*---------------------------------------------------------------------------*/
00019 
00020 #include "NLfit_model.h"
00021 
00022 /*----- Compile this only if plugins are properly enabled in machdep.h -----*/
00023 #ifndef ALLOW_PLUGINS
00024 #  error "Plugins not properly set up -- see machdep.h"
00025 #endif
00026 
00027 #define EMPTY_STRING "\0"
00028 #define NL_DEBUG 0
00029 
00030 
00031 /*---------------------------------------------------------------------------*/
00032 /*
00033    Routine to read in all MODELs found in a given directory
00034 */
00035 
00036 
00037 NLFIT_MODEL_array * NLFIT_get_all_MODELs( char * dname )
00038 {
00039    THD_string_array * flist , * rlist ;
00040    int ir , ii ;
00041    char * fname , * suff ;
00042    NLFIT_MODEL_array * outar ;
00043    NLFIT_MODEL       * model ;
00044 
00045    /*----- sanity check and initialize -----*/
00046 
00047    if( dname == NULL || strlen(dname) == 0 )  return (NULL) ;
00048    if( ! THD_is_directory(dname) )            return (NULL) ;
00049 
00050    INIT_MODEL_ARRAY( outar ) ;
00051 
00052    if (NL_DEBUG)
00053      { 
00054        char str[256] ; 
00055        sprintf (str,"scanning directory %s \n",dname) ; 
00056        printf (str) ; 
00057      }
00058 
00059    /*----- find all filenames -----*/
00060 
00061    flist = THD_get_all_filenames( dname ) ;
00062    if( flist == NULL || flist->num <= 0 ){
00063       DESTROY_SARR(flist) ;
00064       DESTROY_MODEL_ARRAY(outar) ;
00065       return (NULL) ;
00066    }
00067 
00068    rlist = THD_extract_regular_files( flist ) ;
00069    DESTROY_SARR(flist) ;
00070    if( rlist == NULL || rlist->num <= 0 ){
00071       DESTROY_SARR(rlist) ;
00072       DESTROY_MODEL_ARRAY(outar) ;
00073       return (NULL) ;
00074    }
00075 
00076   if (NL_DEBUG)
00077     { 
00078       char str[256] ; 
00079       sprintf(str,"%d files to scan \n",rlist->num) ; 
00080       printf (str) ; 
00081     }
00082 
00083 
00084    /*----- scan thru and find all filenames ending in DYNAMIC_suffix -----*/
00085 
00086    for( ir=0 ; ir < rlist->num ; ir++ ){
00087       fname = rlist->ar[ir] ; if( fname == NULL ) continue ;
00088       if (strstr(fname, "model") == NULL)  continue;
00089 
00090       suff = strstr(fname,DYNAMIC_suffix) ;
00091       if( suff != NULL  &&  strlen(suff) == strlen(DYNAMIC_suffix)){
00092          model  = NLFIT_read_MODEL( fname ) ;
00093          if( model != NULL ) ADDTO_MODEL_ARRAY( outar , model ) ;
00094       }
00095    }
00096 
00097   if (NL_DEBUG)
00098     { 
00099       char str[256] ;
00100       sprintf (str,"directory %s has %d MODELs \n",dname,outar->num) ; 
00101       printf (str) ; 
00102     }
00103 
00104 
00105    DESTROY_SARR(rlist) ;
00106    if( outar->num == 0 ) DESTROY_MODEL_ARRAY(outar) ;
00107    return (outar) ;
00108 }
00109 
00110 
00111 /*---------------------------------------------------------------------------*/
00112 /*
00113    Routine to open and initialize a single MODEL
00114 */
00115 
00116 NLFIT_MODEL * NLFIT_read_MODEL( char * fname )
00117 {
00118    NLFIT_MODEL * model ;
00119    static int firsterr=1 ;
00120 
00121    /*----- sanity checks -----*/
00122 
00123    if( fname == NULL || strlen(fname) == 0 )  return (NULL) ;
00124    if( ! THD_is_file(fname) )                 return (NULL) ;
00125 
00126    /*----- make space for new MODEL -----*/
00127 
00128    model = (NLFIT_MODEL *) XtMalloc( sizeof(NLFIT_MODEL) ) ;
00129    model->type = NLFIT_MODEL_TYPE ;
00130 
00131    /*----- copy name into model structure -----*/
00132 
00133    MCW_strncpy( model->libname , fname , MAX_MODEL_NAME ) ;
00134 
00135    /*----- open the library (we hope) -----*/
00136 
00137    DYNAMIC_OPEN( fname , model->libhandle ) ;
00138    if( ! ISVALID_DYNAMIC_handle( model->libhandle ) ){
00139       char *er ;
00140       if( firsterr ){ fprintf(stderr,"\n"); firsterr=0; }
00141       fprintf (stderr,"failed to open library %s ",fname); 
00142       er = (char *)DYNAMIC_ERROR_STRING ;
00143       if( er != NULL ) fprintf(stderr," -- %s\n",er) ;
00144       else             fprintf(stderr,"\n") ;
00145       myXtFree(model) ;
00146       return (NULL) ;
00147    }
00148 
00149    if (NL_DEBUG)
00150      { 
00151        char str[256] ;
00152        sprintf (str,"opened library %s with handle %p \n" , 
00153                fname,model->libhandle ) ;
00154        printf (str) ; 
00155      }
00156 
00157 
00158    /*----- find the required symbols -----*/
00159    /*..... 13 Sep 2001: add _ for Mac OS X [RWCox] .....*/
00160    /*..... 30 Oct 2003: remove it for OS X 10.3    .....*/
00161 
00162 #ifndef NEED_UNDERSCORE
00163    DYNAMIC_SYMBOL(model->libhandle, "initialize_model" , 
00164                   model->libinit_func );
00165 #else
00166    DYNAMIC_SYMBOL(model->libhandle, "_initialize_model" , 
00167                   model->libinit_func );
00168 #endif
00169 
00170    /*----- if symbols not found, complain and kill this MODEL -----*/
00171 
00172    if( model->libinit_func == (vptr_func *) NULL ){
00173       char *er = (char *)DYNAMIC_ERROR_STRING ;
00174       if( firsterr ){ fprintf(stderr,"\n"); firsterr=0; }
00175       fprintf(stderr,"model %s lacks initialize_model() function\n",fname) ;
00176       if( er != NULL ) fprintf(stderr," -- %s\n",er) ;
00177       DYNAMIC_CLOSE( model->libhandle ) ;
00178       myXtFree(model) ;
00179       return (NULL) ;
00180    }
00181 
00182    /*----- create interface(s) by calling initialization function -----*/
00183 
00184    model->interface = (MODEL_interface *) model->libinit_func() ;
00185    if( model->interface == NULL ) 
00186      {
00187        DYNAMIC_CLOSE( model->libhandle ) ;
00188        myXtFree(model) ;
00189        return (NULL) ;
00190      }
00191 
00192    if (NL_DEBUG)
00193      { 
00194        char str[256] ;
00195        sprintf (str,"Interface created for %s model\n",
00196                 model->interface->label) ; 
00197        printf (str) ; 
00198      }
00199 
00200    /*----- done -----*/
00201 
00202    return (model) ;
00203 }
00204 
00205 
00206 /*---------------------------------------------------------------------------*/
00207 /*
00208    Routine to read in all MODELs in the desired list of directories
00209 */
00210 
00211 NLFIT_MODEL_array * NLFIT_get_many_MODELs(void)
00212 {
00213    char * epath , * elocal , * eee ;
00214    char ename[THD_MAX_NAME] , efake[]="/usr/local/lib/afni:./" ;
00215    NLFIT_MODEL_array * outar , * tmpar ;
00216    int epos , ll , ii , id ;
00217    THD_string_array *qlist ;  /* 02 Feb 2002 */
00218 
00219    /*----- sanity checks -----*/
00220 
00221    epath = my_getenv("AFNI_MODELPATH") ;     /* get the path list to read from */
00222 
00223    if( epath == NULL )
00224       epath = my_getenv("AFNI_PLUGINPATH") ; /* try another name? */
00225 
00226    if( epath == NULL )
00227       epath = my_getenv("PATH") ;             /* try another name? */
00228 
00229    if( epath == NULL ) epath = efake ;     /* put in a fake path instead? */
00230 
00231    /*----- copy path list into local memory -----*/
00232 
00233    ll = strlen(epath) ;
00234    elocal = (char *) XtMalloc( sizeof(char) * (ll+2) ) ;
00235 
00236    /*----- put a blank at the end -----*/
00237 
00238    strcpy( elocal , epath ) ; elocal[ll] = ' ' ; elocal[ll+1] = '\0' ;
00239 
00240    /*----- replace colons with blanks -----*/
00241 
00242    for( ii=0 ; ii < ll ; ii++ )
00243       if( elocal[ii] == ':' ) elocal[ii] = ' ' ;
00244 
00245    if (NL_DEBUG)
00246      { 
00247        printf ("paths to be searched for MODELs follow:") ;
00248        printf("%s\n",elocal) ; 
00249        fflush(stdout) ; 
00250      }
00251 
00252 
00253    INIT_SARR(qlist) ; /* 02 Feb 2002: list of searched directories */
00254 
00255    /*----- extract blank delimited strings;
00256            use as directory names to get libraries -----*/
00257 
00258    INIT_MODEL_ARRAY( outar ) ;
00259    epos = 0 ;
00260 
00261    do{
00262       ii = sscanf( elocal+epos , "%s%n" , ename , &id ) ; /* next substring */
00263       if( ii < 1 || id < 1 ) break ;                     /* none --> end of work */
00264       epos += id ;                               /* char after last scanned */
00265 
00266       if( !THD_is_directory(ename) ) continue ;  /* 21 May 2002 - rcr */
00267 
00268       /* 02 Feb 2002: check if ename has already been checked */
00269 
00270       for( ii=0 ; ii < qlist->num ; ii++ )
00271          if( THD_equiv_files(qlist->ar[ii],ename) ) break ;
00272       if( ii < qlist->num ) continue ;
00273       ADDTO_SARR(qlist,ename) ;
00274 
00275       ii = strlen(ename) ;                           /* make sure name has */
00276       if( ename[ii-1] != '/' ){                     /* a trailing '/' on it */
00277         ename[ii]  = '/' ; ename[ii+1] = '\0' ; 
00278       }
00279 
00280       tmpar = NLFIT_get_all_MODELs( ename ) ;        /* read this directory */
00281       if( tmpar != NULL ){
00282          for( ii=0 ; ii < tmpar->num ; ii++ )     /* move results to output */
00283             ADDTO_MODEL_ARRAY( outar , tmpar->modar[ii] ) ;
00284 
00285          FREE_MODEL_ARRAY(tmpar) ;                      /* toss temp array */
00286       }
00287    } while( epos < ll ) ;  /* scan until 'epos' is after end of epath */
00288 
00289    myXtFree(elocal) ;
00290 
00291    if (NL_DEBUG)
00292      { 
00293        char str[256] ; 
00294        sprintf (str,"found %d MODELs \n",outar->num) ; 
00295        printf (str) ; 
00296      }
00297 
00298    if( outar->num == 0 ) DESTROY_MODEL_ARRAY(outar) ;
00299 
00300    DESTROY_SARR(qlist) ; /* 02 Feb 2002 */
00301    return (outar) ;
00302 }
00303 
00304 
 

Powered by Plone

This site conforms to the following standards: