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  

plug_rename.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 #include "afni.h"
00008 
00009 #ifndef ALLOW_PLUGINS
00010 #  error "Plugins not properly set up -- see machdep.h"
00011 #endif
00012 
00013 /***********************************************************************
00014   Simple plugin to rename a dataset from within AFNI.
00015 
00016   May 1998: modified to work with compressed .BRIK names.
00017 ************************************************************************/
00018 
00019 char * RENAME_main( PLUGIN_interface * ) ;
00020 
00021 static char helpstring[] =
00022    " Purpose: Renaming a dataset from within AFNI\n"
00023    " Inputs:\n"
00024    " Dataset   = A dataset in the current session.\n"
00025    " Prefix    = New filename prefix.\n"
00026    " N.B.: All views containing this dataset's\n"
00027    "       children and/or parents will be affected\n"
00028    "       affected by this operation."
00029 ;
00030 
00031 /***********************************************************************
00032    Set up the interface to the user
00033 ************************************************************************/
00034 
00035 
00036 DEFINE_PLUGIN_PROTOTYPE
00037 
00038 PLUGIN_interface * PLUGIN_init( int ncall )
00039 {
00040    PLUGIN_interface * plint ;
00041 
00042    if( ncall > 0 ) return NULL ;  /* only one interface */
00043 
00044    /*-- set titles and call point --*/
00045 
00046    plint = PLUTO_new_interface( "Dataset Rename" , "Dataset Renaming" , helpstring ,
00047                                  PLUGIN_CALL_VIA_MENU , RENAME_main  ) ;
00048 
00049    PLUTO_add_hint( plint , "Rename a Dataset" ) ;
00050 
00051    PLUTO_set_sequence( plint , "A:afnicontrol:dset" ) ;
00052 
00053    PLUTO_set_runlabels( plint , "Rename+Keep" , "Rename+Close" ) ;  /* 04 Nov 2003 */
00054 
00055    /*-- first line of input: Dataset --*/
00056 
00057    PLUTO_add_option( plint , "Input" , "Input" , TRUE ) ;
00058    PLUTO_add_dataset(plint , "Dataset" ,
00059                                     ANAT_ALL_MASK , FUNC_ALL_MASK ,
00060                                     WARP_ON_DEMAND_MASK | DIMEN_ALL_MASK |
00061                                     SESSION_ALL_MASK    | BRICK_ALLTYPE_MASK ) ;
00062 
00063    /*-- second line of input: Prefix for output dataset --*/
00064 
00065    PLUTO_add_option( plint , "Output" , "Output" , TRUE ) ;
00066    PLUTO_add_string( plint , "Prefix" , 0,NULL , 19 ) ;
00067 
00068    return plint ;
00069 }
00070 
00071 /***************************************************************************
00072   Main routine for this plugin (will be called from AFNI).
00073 ****************************************************************************/
00074 
00075 char * RENAME_main( PLUGIN_interface * plint )
00076 {
00077    char * new_prefix ;
00078    MCW_idcode * idc ;
00079    THD_3dim_dataset * dset ;
00080    THD_3dim_dataset ** session_row ;
00081    char * old_header_name , * old_brick_name ;
00082    THD_slist_find find ;
00083    THD_session * ss ;
00084    int iss , id , ivv , ierr , mm ;
00085 
00086    /*--------------------------------------------------------------------*/
00087    /*----- Check inputs from AFNI to see if they are reasonable-ish -----*/
00088 
00089    if( plint == NULL )
00090       return "***********************\n"
00091              "RENAME_main: NULL input\n"
00092              "***********************"  ;
00093 
00094    PLUTO_next_option(plint) ;
00095    idc  = PLUTO_get_idcode(plint) ;
00096    dset = PLUTO_find_dset(idc) ;
00097    if( dset == NULL )
00098       return "******************************\n"
00099              "RENAME_main:bad input dataset\n"
00100              "******************************"  ;
00101 
00102    PLUTO_next_option(plint) ;
00103    new_prefix = PLUTO_get_string(plint) ;
00104    if( ! PLUTO_prefix_ok(new_prefix) )
00105       return "***********************\n"
00106              "RENAME_main:bad prefix\n"
00107              "***********************"  ;
00108 
00109    /*------------------------------------------------------*/
00110    /*---------- At this point, the inputs are OK ----------*/
00111 
00112    /*-- find this dataset in the AFNI library --*/
00113 
00114    find = THD_dset_in_sessionlist( FIND_IDCODE, idc, GLOBAL_library.sslist, -1 ) ;
00115    iss  = find.sess_index ;
00116    ss   = GLOBAL_library.sslist->ssar[iss] ;
00117 
00118    /*-- set up session_row to point to all the associated datasets --*/
00119 
00120    id = find.dset_index ;
00121    session_row = ss->dsset[id] ;
00122 
00123    /*-- for each element of this row,
00124         change its internal names and, if needed, filenames on disk --*/
00125 
00126    ierr = 0 ;
00127 
00128    for( ivv=FIRST_VIEW_TYPE ; ivv <= LAST_VIEW_TYPE ; ivv++ ){
00129 
00130       dset = session_row[ivv] ;
00131       if( ! ISVALID_3DIM_DATASET(dset) ) continue ;  /* skip this one */
00132 
00133       /*-- copy the old filenames --*/
00134 
00135       old_header_name = XtNewString( dset->dblk->diskptr->header_name ) ;
00136       old_brick_name  = XtNewString( dset->dblk->diskptr->brick_name  ) ;
00137 
00138       /*-- initialize the new filenames inside the dataset --*/
00139 
00140       EDIT_dset_items( dset , ADN_prefix , new_prefix , ADN_none ) ;
00141 
00142       /*-- rename the old files to the new files, if they exist on disk --*/
00143 
00144       if( THD_is_file(old_header_name) )
00145          ierr += rename( old_header_name , dset->dblk->diskptr->header_name ) ;
00146 
00147       /* May 1998: fix .BRIK rename to allow for compression */
00148 #if 0
00149       if( THD_is_file(old_brick_name) )
00150          ierr += rename( old_brick_name , dset->dblk->diskptr->brick_name ) ;
00151 #else
00152       mm = COMPRESS_filecode(old_brick_name) ;
00153       if( mm != COMPRESS_NOFILE ){
00154         char * old_name = COMPRESS_add_suffix(old_brick_name,mm) ;
00155         char * new_name = COMPRESS_add_suffix(dset->dblk->diskptr->brick_name,mm) ;
00156         ierr += rename( old_name , new_name ) ;
00157         free(old_name) ; free(new_name) ;
00158       }
00159 #endif
00160 
00161       XtFree(old_header_name) ; XtFree(old_brick_name) ;
00162    }
00163 
00164    /*-- clean up AFNI --*/
00165 
00166    PLUTO_fixup_names() ;
00167 
00168    /*-- done --*/
00169 
00170    if( ierr ) return "***********************************************\n"
00171                      "RENAME_main: some file rename operations failed\n"
00172                      "***********************************************"  ;
00173 
00174    return NULL ;
00175 }
 

Powered by Plone

This site conforms to the following standards: