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

#include "NLfit_model.h"

Go to the source code of this file.


Defines

#define EMPTY_STRING   "\0"
#define NL_DEBUG   0

Functions

NLFIT_MODEL_arrayNLFIT_get_all_MODELs (char *dname)
NLFIT_MODELNLFIT_read_MODEL (char *fname)
NLFIT_MODEL_arrayNLFIT_get_many_MODELs (void)

Define Documentation

#define EMPTY_STRING   "\0"
 

Definition at line 27 of file NLfit_model.c.

#define NL_DEBUG   0
 

Definition at line 28 of file NLfit_model.c.


Function Documentation

NLFIT_MODEL_array* NLFIT_get_all_MODELs char *    dname
 

Definition at line 37 of file NLfit_model.c.

References ADDTO_MODEL_ARRAY, THD_string_array::ar, DESTROY_MODEL_ARRAY, DESTROY_SARR, DYNAMIC_suffix, INIT_MODEL_ARRAY, NLFIT_read_MODEL(), THD_string_array::num, NLFIT_MODEL_array::num, THD_extract_regular_files(), THD_get_all_filenames(), and THD_is_directory().

Referenced by NLFIT_get_many_MODELs().

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 }

NLFIT_MODEL_array* NLFIT_get_many_MODELs void   
 

Definition at line 211 of file NLfit_model.c.

References ADDTO_MODEL_ARRAY, ADDTO_SARR, THD_string_array::ar, DESTROY_MODEL_ARRAY, DESTROY_SARR, FREE_MODEL_ARRAY, INIT_MODEL_ARRAY, INIT_SARR, NLFIT_MODEL_array::modar, my_getenv(), myXtFree, NLFIT_get_all_MODELs(), THD_string_array::num, NLFIT_MODEL_array::num, THD_equiv_files(), THD_is_directory(), THD_MAX_NAME, and XtMalloc.

Referenced by get_options(), and PLUGIN_init().

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 }

NLFIT_MODEL* NLFIT_read_MODEL char *    fname
 

Definition at line 116 of file NLfit_model.c.

References DYNAMIC_CLOSE, DYNAMIC_ERROR_STRING, DYNAMIC_OPEN, DYNAMIC_SYMBOL, NLFIT_MODEL::interface, ISVALID_DYNAMIC_handle, MODEL_interface::label, NLFIT_MODEL::libhandle, NLFIT_MODEL::libinit_func, NLFIT_MODEL::libname, MAX_MODEL_NAME, MCW_strncpy, myXtFree, NLFIT_MODEL_TYPE, THD_is_file(), NLFIT_MODEL::type, vptr_func, and XtMalloc.

Referenced by NLFIT_get_all_MODELs().

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 }
 

Powered by Plone

This site conforms to the following standards: