1 /*****************************************************************************
2    Major portions of this software are copyrighted by the Medical College
3    of Wisconsin, 1994-2000, and are released under the Gnu General Public
4    License, Version 2.  See the file README.Copyright for details.
5 ******************************************************************************/
6 
7 #include "afni.h"
8 
9 #ifndef ALLOW_PLUGINS
10 #  error "Plugins not properly set up -- see machdep.h"
11 #endif
12 
13 /***********************************************************************
14   Simple plugin to rename a dataset from within AFNI.
15 
16   May 1998: modified to work with compressed .BRIK names.
17 ************************************************************************/
18 
19 char * RENAME_main( PLUGIN_interface * ) ;
20 
21 static char helpstring[] =
22    " Purpose: Renaming a dataset from within AFNI\n"
23    " Inputs:\n"
24    " Dataset   = A dataset in the current session.\n"
25    " Prefix    = New filename prefix.\n"
26    " N.B.: All views containing this dataset's\n"
27    "       children and/or parents will be affected\n"
28    "       affected by this operation."
29 ;
30 
31 /***********************************************************************
32    Set up the interface to the user
33 ************************************************************************/
34 
35 
36 DEFINE_PLUGIN_PROTOTYPE
37 
PLUGIN_init(int ncall)38 PLUGIN_interface * PLUGIN_init( int ncall )
39 {
40    PLUGIN_interface * plint ;
41 
42    if( ncall > 0 ) return NULL ;  /* only one interface */
43 
44    CHECK_IF_ALLOWED("DATASETRENAME","Dataset Rename") ;  /* 30 Sep 2016 */
45 
46    /*-- set titles and call point --*/
47 
48    plint = PLUTO_new_interface( "Dataset Rename" , "Dataset Renaming" , helpstring ,
49                                  PLUGIN_CALL_VIA_MENU , RENAME_main  ) ;
50 
51    PLUTO_add_hint( plint , "Rename a Dataset" ) ;
52 
53    PLUTO_set_sequence( plint , "A:afnicontrol:dset" ) ;
54 
55    PLUTO_set_runlabels( plint , "Rename+Keep" , "Rename+Close" ) ;  /* 04 Nov 2003 */
56 
57    /*-- first line of input: Dataset --*/
58 
59    PLUTO_add_option( plint , "Input" , "Input" , TRUE ) ;
60    PLUTO_add_dataset(plint , "Dataset" ,
61                                     ANAT_ALL_MASK , FUNC_ALL_MASK ,
62                                     WARP_ON_DEMAND_MASK | DIMEN_ALL_MASK |
63                                     SESSION_ALL_MASK    | BRICK_ALLTYPE_MASK ) ;
64 
65    /*-- second line of input: Prefix for output dataset --*/
66 
67    PLUTO_add_option( plint , "Output" , "Output" , TRUE ) ;
68    PLUTO_add_string( plint , "Prefix" , 0,NULL , 19 ) ;
69 
70    return plint ;
71 }
72 
73 /***************************************************************************
74   Main routine for this plugin (will be called from AFNI).
75 ****************************************************************************/
76 
RENAME_main(PLUGIN_interface * plint)77 char * RENAME_main( PLUGIN_interface * plint )
78 {
79    char * new_prefix ;
80    MCW_idcode * idc ;
81    THD_3dim_dataset * dset ;
82    char * old_header_name , * old_brick_name ;
83    THD_slist_find find ;
84    THD_session * ss ;
85    int iss , id , ivv , ierr , mm ;
86 
87    /*--------------------------------------------------------------------*/
88    /*----- Check inputs from AFNI to see if they are reasonable-ish -----*/
89 
90    if( plint == NULL )
91       return "***********************\n"
92              "RENAME_main: NULL input\n"
93              "***********************"  ;
94 
95    PLUTO_next_option(plint) ;
96    idc  = PLUTO_get_idcode(plint) ;
97    dset = PLUTO_find_dset(idc) ;
98    if( dset == NULL )
99       return "******************************\n"
100              "RENAME_main:bad input dataset\n"
101              "******************************"  ;
102 
103    PLUTO_next_option(plint) ;
104    new_prefix = PLUTO_get_string(plint) ;
105    if( ! PLUTO_prefix_ok(new_prefix) )
106       return "***********************\n"
107              "RENAME_main:bad prefix\n"
108              "***********************"  ;
109 
110    /*------------------------------------------------------*/
111    /*---------- At this point, the inputs are OK ----------*/
112 
113    /*-- find this dataset in the AFNI library --*/
114 
115    find = THD_dset_in_sessionlist( FIND_IDCODE, idc, GLOBAL_library.sslist, -1 ) ;
116    iss  = find.sess_index ;
117    ss   = GLOBAL_library.sslist->ssar[iss] ;
118    id = find.dset_index ;
119 
120    /*-- for each element of this row,
121         change its internal names and, if needed, filenames on disk --*/
122 
123    ierr = 0 ;
124 
125    for( ivv=FIRST_VIEW_TYPE ; ivv <= LAST_VIEW_TYPE ; ivv++ ){
126 
127       dset = GET_SESSION_DSET(ss, id, ivv);
128 
129       if( ! ISVALID_3DIM_DATASET(dset) ) continue ;  /* skip this one */
130 
131       /*-- copy the old filenames --*/
132 
133       old_header_name = XtNewString( dset->dblk->diskptr->header_name ) ;
134       old_brick_name  = XtNewString( dset->dblk->diskptr->brick_name  ) ;
135 
136       /*-- initialize the new filenames inside the dataset --*/
137 
138       EDIT_dset_items( dset , ADN_prefix , new_prefix , ADN_none ) ;
139 
140       /*-- rename the old files to the new files, if they exist on disk --*/
141 
142       if( THD_is_file(old_header_name) )
143          ierr += rename( old_header_name , dset->dblk->diskptr->header_name ) ;
144 
145       /* May 1998: fix .BRIK rename to allow for compression */
146 #if 0
147       if( THD_is_file(old_brick_name) )
148          ierr += rename( old_brick_name , dset->dblk->diskptr->brick_name ) ;
149 #else
150       mm = COMPRESS_filecode(old_brick_name) ;
151       if( mm != COMPRESS_NOFILE ){
152         char * old_name = COMPRESS_add_suffix(old_brick_name,mm) ;
153         char * new_name = COMPRESS_add_suffix(dset->dblk->diskptr->brick_name,mm) ;
154         ierr += rename( old_name , new_name ) ;
155         free(old_name) ; free(new_name) ;
156       }
157 #endif
158 
159       XtFree(old_header_name) ; XtFree(old_brick_name) ;
160    }
161 
162    /*-- clean up AFNI --*/
163 
164    PLUTO_fixup_names() ;
165 
166    /*-- done --*/
167 
168    if( ierr ) return "***********************************************\n"
169                      "RENAME_main: some file rename operations failed\n"
170                      "***********************************************"  ;
171 
172    return NULL ;
173 }
174