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  

3drename.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 "mrilib.h"
00008 
00009 #undef DEBUG
00010 
00011 static int THD_rename_dataset_files( char * , char * , int ) ;
00012 
00013 int main( int argc , char * argv[] )
00014 {
00015    int nopt=1 , old_view , new_view , vbot,vtop ;
00016    char * old_name , * new_name ;
00017    char old_prefix[THD_MAX_PREFIX] , new_prefix[THD_MAX_PREFIX] ;
00018    int ii , old_len , new_len ;
00019 
00020    if( argc != 3 || strcmp(argv[1],"-help") == 0 ){
00021       printf(
00022        "Usage 1: 3drename old_prefix new_prefix\n"
00023        "  Will rename all datasets using the old_prefix to use the new_prefix;\n"
00024        "    3drename fred ethel\n"
00025        "  will change fred+orig.HEAD    to ethel+orig.HEAD\n"
00026        "              fred+orig.BRIK    to ethel+orig.BRIK\n"
00027        "              fred+tlrc.HEAD    to ethel+tlrc.HEAD\n"
00028        "              fred+tlrc.BRIK.gz to ethel+tlrc.BRIK.gz\n"
00029        "\n"
00030        "Usage 2: 3drename old_prefix+view new_prefix\n"
00031        "  Will rename only the dataset with the given view (orig, acpc, tlrc).\n"
00032       ) ;
00033       exit(0) ;
00034    }
00035 
00036    machdep() ;
00037 
00038    /* input arguments */
00039 
00040    old_name = argv[nopt++] ; old_len = strlen(old_name) ;
00041    new_name = argv[nopt++] ; new_len = strlen(new_name) ;
00042 
00043    if( old_len < 1 || old_len > THD_MAX_PREFIX || !THD_filename_pure(old_name) ){
00044       fprintf(stderr,"** Illegal old dataset name!\n") ; exit(1) ;
00045    }
00046    if( new_len < 1 || new_len > THD_MAX_PREFIX || !THD_filename_pure(new_name) ){
00047       fprintf(stderr,"** Illegal new dataset name!\n") ; exit(1) ;
00048    }
00049 
00050    /* disallow operation on MINC or ANALYZE files */
00051 
00052    if( STRING_HAS_SUFFIX(old_name,".mnc") ){
00053       fprintf(stderr,"** Old dataset name can't be a MINC file!\n"); exit(1);
00054    }
00055    if( STRING_HAS_SUFFIX(new_name,".mnc") ){
00056       fprintf(stderr,"** New dataset name can't be a MINC file!\n"); exit(1);
00057    }
00058    if( STRING_HAS_SUFFIX(old_name,".hdr") ){
00059       fprintf(stderr,"** Old dataset name can't be an ANALYZE file!\n"); exit(1);
00060    }
00061    if( STRING_HAS_SUFFIX(new_name,".hdr") ){
00062       fprintf(stderr,"** New dataset name can't be an ANALYZE file!\n"); exit(1);
00063    }
00064    if( STRING_HAS_SUFFIX(old_name,".svl") ){
00065       fprintf(stderr,"** Old dataset name can't be an CTF-SAM file!\n"); exit(1);
00066    }
00067    if( STRING_HAS_SUFFIX(new_name,".svl") ){
00068       fprintf(stderr,"** New dataset name can't be an CTF-SAM file!\n"); exit(1);
00069    }
00070    if( STRING_HAS_SUFFIX(old_name,".nii") || STRING_HAS_SUFFIX(old_name,".nii.gz") ){
00071       fprintf(stderr,"** Old dataset name can't be a NIfTI-1 file!\n"); exit(1);
00072    }
00073    if( STRING_HAS_SUFFIX(new_name,".nii") || STRING_HAS_SUFFIX(old_name,".nii.gz") ){
00074       fprintf(stderr,"** New dataset name can't be a NIfTI-1 file!\n"); exit(1);
00075    }
00076 
00077    /* check old_name for a +view suffix somewhere */
00078 
00079    MCW_strncpy(old_prefix,old_name,THD_MAX_PREFIX) ;
00080    if( strstr(old_name,"+") == NULL ){
00081       old_view = ILLEGAL_TYPE ;               /* no +view ==> do all views */
00082    } else {
00083       char * qq ;
00084       for( qq=old_name+old_len ; *qq != '+' ; qq-- ) ; /* find last '+' */
00085       qq++ ;                                           /* point to view */
00086       for( old_view=0 ; old_view <= LAST_VIEW_TYPE ; old_view++ )
00087          if( strncmp(qq,VIEW_codestr[old_view],
00088                         strlen(VIEW_codestr[old_view])) == 0 ) break ;
00089 
00090       if( old_view > LAST_VIEW_TYPE ){
00091          old_view = ILLEGAL_TYPE ;               /* the '+' is a fake! */
00092       } else {
00093          old_prefix[ (qq-old_name)-1 ] = '\0' ;  /* truncate prefix */
00094       }
00095    }
00096 #ifdef DEBUG
00097 fprintf(stderr,"++ old_view = %d\n",old_view) ;
00098 #endif
00099 
00100    /* check new_name for a +view suffix, also */
00101 
00102    MCW_strncpy(new_prefix,new_name,THD_MAX_PREFIX) ;
00103    if( strstr(new_name,"+") == NULL ){
00104       new_view = ILLEGAL_TYPE ;               /* no +view */
00105    } else {
00106       char * qq ;
00107       for( qq=new_name+new_len ; *qq != '+' ; qq-- ) ; /* find last '+' */
00108       qq++ ;                                           /* point to view */
00109       for( new_view=0 ; new_view <= LAST_VIEW_TYPE ; new_view++ )
00110          if( strncmp(qq,VIEW_codestr[new_view],
00111                         strlen(VIEW_codestr[new_view])) == 0 ) break ;
00112 
00113       if( new_view > LAST_VIEW_TYPE ){
00114          new_view = ILLEGAL_TYPE ;               /* the '+' is a fake! */
00115       } else {
00116          new_prefix[ (qq-new_name)-1 ] = '\0' ;  /* truncate prefix */
00117       }
00118    }
00119 #ifdef DEBUG
00120 fprintf(stderr,"++ new_view = %d\n",new_view) ;
00121 #endif
00122 
00123    /* of course, we don't actually use the +view suffix on the output */
00124 
00125    if( new_view >= 0 ){
00126       fprintf(stderr,"++ Warning: ignoring +%s on new_prefix.\n",
00127               VIEW_codestr[new_view]) ;
00128       new_view = ILLEGAL_TYPE ;
00129    }
00130 
00131    if( old_view >= 0 ){
00132       vbot = vtop = old_view ;
00133    } else {
00134       vbot = 0 ; vtop = LAST_VIEW_TYPE ;
00135    }
00136 
00137    for( ii=vbot ; ii <= vtop ; ii++ )
00138       (void) THD_rename_dataset_files( old_prefix , new_prefix , ii ) ;
00139 
00140    exit(0) ;
00141 }
00142 
00143 /*------------------------------------------------------------------------*/
00144 
00145 int THD_rename_dataset_files( char * old_prefix , char * new_prefix , int view )
00146 {
00147    char old_headname[THD_MAX_NAME] , old_brikname[THD_MAX_NAME] ;
00148    char new_headname[THD_MAX_NAME] , new_brikname[THD_MAX_NAME] ;
00149    char * old_bname=NULL ;
00150    int brick_ccode , val ;
00151 
00152    if( old_prefix == NULL || old_prefix[0] == '\0' ||
00153        new_prefix == NULL || new_prefix[0] == '\0' ||
00154        view < 0           || view > LAST_VIEW_TYPE   ){
00155 
00156       fprintf(stderr,"** THD_rename_dataset_files: illegal inputs!\n") ;
00157       return 0 ;
00158    }
00159 
00160 #ifdef DEBUG
00161 fprintf(stderr,"++ THD_rename_dataset_files: %s %s %d\n",old_prefix,new_prefix,view);
00162 #endif
00163 
00164    /*-- create the old .HEAD filename --*/
00165 
00166    sprintf( old_headname , "%s+%s.%s" ,
00167             old_prefix , VIEW_codestr[view] , DATASET_HEADER_SUFFIX ) ;
00168 
00169 #ifdef DEBUG
00170 fprintf(stderr,"++ THD_rename_dataset_files: old_headname=%s\n",old_headname) ;
00171 #endif
00172 
00173    if( !THD_is_file(old_headname) ){
00174       fprintf(stderr,"** THD_rename_dataset_files: old header %s doesn't exist!\n",
00175               old_headname) ;
00176       return 0 ;
00177    }
00178 
00179    /*-- create the new .HEAD filename --*/
00180 
00181    sprintf( new_headname , "%s+%s.%s" ,
00182             new_prefix , VIEW_codestr[view] , DATASET_HEADER_SUFFIX ) ;
00183 
00184 #ifdef DEBUG
00185 fprintf(stderr,"++ THD_rename_dataset_files: new_headname=%s\n",new_headname) ;
00186 #endif
00187 
00188    if( THD_is_file(new_headname) ){
00189       fprintf(stderr,"** THD_rename_dataset_files: new header %s already exists!\n",
00190               new_headname) ;
00191       return 0 ;
00192    }
00193 
00194    /*-- create the old .BRIK filename --*/
00195 
00196    sprintf( old_brikname , "%s+%s.%s" ,
00197             old_prefix , VIEW_codestr[view] , DATASET_BRICK_SUFFIX ) ;
00198 
00199 #ifdef DEBUG
00200 fprintf(stderr,"++ THD_rename_dataset_files: old_brikname=%s\n",old_brikname) ;
00201 #endif
00202 
00203    /*-- however, the .BRIK might have a compression suffix on it,
00204         which affects both its old name and its new name         --*/
00205 
00206    brick_ccode = COMPRESS_filecode(old_brikname) ;
00207    if( brick_ccode == COMPRESS_NOFILE ){
00208       fprintf(stderr,"++ THD_rename_dataset_files: old brick %s doesn't exist.\n",
00209               old_brikname) ;
00210       new_brikname[0] = '\0' ;  /* flag to do nothing */
00211    } else {
00212       old_bname = COMPRESS_filename( old_brikname ) ;
00213       if( old_bname == NULL ){  /* should not happen */
00214          fprintf(stderr,"** THD_rename_dataset_files: COMPRESS_filename fails!\n") ;
00215          return 0 ;
00216       }
00217       sprintf( new_brikname , "%s+%s.%s" ,
00218                new_prefix , VIEW_codestr[view] , DATASET_BRICK_SUFFIX ) ;
00219       if( brick_ccode >= 0 )
00220          strcat(new_brikname,COMPRESS_suffix[brick_ccode]) ;
00221 
00222 #ifdef DEBUG
00223 fprintf(stderr,"++ THD_rename_dataset_files: new_brikname=%s\n",new_brikname) ;
00224 #endif
00225 
00226       if( THD_is_file(new_brikname) ){
00227          fprintf(stderr,"** THD_rename_dataset_files: new brick %s already exists!\n",
00228                  new_brikname) ;
00229          if( old_bname != NULL ) free(old_bname) ;
00230          return 0 ;
00231       }
00232    }
00233 
00234    /*-- actually do the renaming --*/
00235 
00236    fprintf(stderr,"++ THD_rename_dataset_files: rename %s -> %s\n",
00237            old_headname,new_headname) ;
00238    val = rename( old_headname , new_headname ) ;
00239 
00240    if( val != 0 ){
00241       perror("** THD_rename_dataset_files: rename() failed") ;
00242       if( old_bname != NULL ) free(old_bname) ;
00243       return 0 ;
00244    }
00245 
00246    if( new_brikname[0] != '\0' && old_bname != NULL ){
00247       fprintf(stderr,"++ THD_rename_dataset_files: rename %s -> %s\n",
00248               old_bname,new_brikname) ;
00249       val = rename( old_bname , new_brikname ) ;
00250       free(old_bname) ;
00251 
00252       if( val != 0 ){
00253          perror("** THD_rename_dataset_files: rename() failed") ;
00254          return 0 ;
00255       }
00256    }
00257 
00258    return 1 ;
00259 }
 

Powered by Plone

This site conforms to the following standards: