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  

24swap.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 <stdio.h>
00008 #include <unistd.h>
00009 #include "mrilib.h"
00010 
00011 typedef struct { unsigned char a,b,c,d ; } fourbytes ;
00012 
00013 #define TEMP_FILE "Frodo.Lives"
00014 #define BUFSIZE   64000
00015 
00016 static byte bbuf[BUFSIZE] ;
00017 
00018 #define MAXPAT 256
00019 
00020 int main( int argc , char * argv[] )
00021 {
00022    FILE * infil , * outfil ;
00023    int narg , nbyte , nbyt , quiet = 0 , ndone ;
00024    int npat,ipat,jpat,ii,typ,len,num , cpat_len ;
00025    int pattype[MAXPAT] , patsize[MAXPAT] ;
00026    char * cpat = NULL ;
00027 
00028    if( argc < 2 || strncmp(argv[1],"-help",2) == 0 ){
00029      printf("Usage: 24swap [options] file ...\n"
00030             "Swaps bytes pairs and/or quadruples on the files listed.\n"
00031             "Options:\n"
00032             " -q            Operate quietly\n"
00033             " -pattern pat  'pat' determines the pattern of 2 and 4\n"
00034             "                 byte swaps.  Each element is of the form\n"
00035             "                 2xN or 4xN, where N is the number of\n"
00036             "                 bytes to swap as pairs (for 2x) or\n"
00037             "                 as quadruples (for 4x).  For 2x, N must\n"
00038             "                 be divisible by 2; for 4x, N must be\n"
00039             "                 divisible by 4.  The whole pattern is\n"
00040             "                 made up of elements separated by colons,\n"
00041             "                 as in '-pattern 4x39984:2x0'.  If bytes\n"
00042             "                 are left over after the pattern is used\n"
00043             "                 up, the pattern starts over.  However,\n"
00044             "                 if a byte count N is zero, as in the\n"
00045             "                 example below, then it means to continue\n"
00046             "                 until the end of file.\n"
00047             "\n"
00048             " N.B.: You can also use 1xN as a pattern, indicating to\n"
00049             "         skip N bytes without any swapping.\n"
00050             " N.B.: A default pattern can be stored in the Unix\n"
00051             "         environment variable AFNI_24SWAP_PATTERN.\n"
00052             "         If no -pattern option is given, the default\n"
00053             "         will be used.  If there is no default, then\n"
00054             "         nothing will be done.\n"
00055             " N.B.: If there are bytes 'left over' at the end of the file,\n"
00056             "         they are written out unswapped.  This will happen\n"
00057             "         if the file is an odd number of bytes long.\n"
00058             " N.B.: If you just want to swap pairs, see program 2swap.\n"
00059             "         For quadruples only, see program 4swap.\n"
00060             " N.B.: This program will overwrite the input file!\n"
00061             "         You might want to test it first.\n"
00062             "\n"
00063             " Example: 24swap -pat 4x8:2x0 fred\n"
00064             "          If fred contains 'abcdabcdabcdabcdabcd' on input,\n"
00065             "          then fred has    'dcbadcbabadcbadcbadc' on output.\n"
00066          ) ;
00067      exit(0) ;
00068    }
00069 
00070    machdep() ;
00071 
00072    /*-- scan arguments --*/
00073 
00074    narg = 1 ;
00075 
00076    while( narg < argc && argv[narg][0] == '-' ){
00077 
00078       if( strncmp(argv[narg],"-q",2) == 0 ){
00079          quiet = 1 ; narg++ ; continue ;
00080       }
00081 
00082       if( strncmp(argv[narg],"-pat",4) == 0 ){
00083          cpat = argv[++narg] ; narg++ ; continue ;
00084       }
00085 
00086       fprintf(stderr,"** 24swap: Unknown option %s\n",argv[narg]) ;
00087       exit(1) ;
00088    }
00089 
00090    if( narg == argc ){
00091      fprintf(stderr,"** 24swap: No input files?\n") ; exit(1) ;
00092    }
00093 
00094    if( cpat == NULL ) cpat = getenv("AFNI_24SWAP_PATTERN") ;
00095    if( cpat == NULL ){
00096      fprintf(stderr,"** 24swap: No pattern?  Exiting!\n") ; exit(1) ;
00097    }
00098    cpat_len = strlen(cpat) ;
00099    if( cpat_len < 3 ){
00100      fprintf(stderr,"** 24swap: Stupid pattern?  Exiting!\n") ; exit(1) ;
00101    }
00102 
00103    /*-- parse cpat --*/
00104 
00105    ipat = npat = 0 ; cpat_len = strlen(cpat) ;
00106    while( ipat < cpat_len ){
00107       ii = sscanf( cpat+ipat , "%dx%d%n" , &typ,&len,&num ) ;
00108       if( ii <= 0 ) break ;
00109       if( ii != 2 ){
00110          fprintf(stderr,"** 24swap: illegal pattern %s\n",cpat); exit(1);
00111       }
00112       if( len < 0 || (typ!=2 && typ!=4 && typ!=1 ) ){
00113          fprintf(stderr,"** 24swap: illegal pattern %s\n",cpat); exit(1);
00114       }
00115       if( typ == 2 && len%2 != 0 ){
00116          fprintf(stderr,"** 24swap: illegal pattern %s\n",cpat); exit(1);
00117       }
00118       if( typ == 4 && len%4 != 0 ){
00119          fprintf(stderr,"** 24swap: illegal pattern %s\n",cpat); exit(1);
00120       }
00121 
00122       pattype[npat] = typ ;
00123       patsize[npat] = len ;
00124       npat++ ; ipat += num+1 ;
00125    }
00126 
00127    if( npat < 1 ){
00128       fprintf(stderr,"** 24swap: illegal pattern %s\n",cpat); exit(1);
00129    }
00130 
00131    /*-- loop over files --*/
00132 
00133    for( ; narg < argc ; narg++ ){
00134       infil = fopen( argv[narg] , "r" ) ;
00135       if( infil == NULL ){
00136          fprintf(stderr,"** 24swap: File %s not found - skipping it!\n",argv[narg]);
00137          continue ;
00138       }
00139 
00140       if( !quiet){ printf("-- opened %s",argv[narg]) ; fflush(stdout) ; }
00141 
00142       outfil = fopen( TEMP_FILE , "w" ) ;
00143       if( outfil == NULL ){
00144          printf("** 24swap: Cannot open temporary file - exiting!\n"); exit(1);
00145       }
00146 
00147       ndone = 0 ; ipat = 0 ;  /* loop over patterns and execute them */
00148       while(1){
00149 
00150          if( patsize[ipat] == 0 ){  /* do rest of file this way */
00151             while(1){
00152                nbyt = fread( bbuf , sizeof(byte) , BUFSIZE , infil ) ;
00153                if( nbyt <= 0 ) break ;  /* end of file */
00154                switch( pattype[ipat] ){
00155                   case 1: /* nothing to do */             ; break ;
00156                   case 2: swap_twobytes ( nbyt/2 , bbuf ) ; break ;
00157                   case 4: swap_fourbytes( nbyt/4 , bbuf ) ; break ;
00158                }
00159                fwrite( bbuf , sizeof(byte) , nbyt , outfil ) ;
00160                ndone += nbyt ;
00161                if( !quiet && ndone > 1000000 ){
00162                   ndone -= 1000000 ; printf(".") ; fflush(stdout) ;
00163                }
00164             }
00165             break ; /* to end of outer loop */
00166 
00167          } else {                   /* do patsize[ipat] bytes */
00168             ii = 0 ;
00169             while(1){
00170                jpat = MIN( patsize[ipat] - ii , BUFSIZE ) ;
00171                nbyt = fread( bbuf , sizeof(byte) , jpat , infil ) ;
00172                if( nbyt <= 0 ) break ;  /* end of file */
00173                switch( pattype[ipat] ){
00174                   case 1: /* nothing to do */             ; break ;
00175                   case 2: swap_twobytes ( nbyt/2 , bbuf ) ; break ;
00176                   case 4: swap_fourbytes( nbyt/4 , bbuf ) ; break ;
00177                }
00178                fwrite( bbuf , sizeof(byte) , nbyt , outfil ) ;
00179                ndone += nbyt ;
00180                if( !quiet && ndone > 1000000 ){
00181                   ndone -= 1000000 ; printf(".") ; fflush(stdout) ;
00182                }
00183                ii += nbyt ; if( ii >= patsize[ipat] ) break ;
00184             }
00185          }
00186 
00187          if( nbyt <= 0 ) break ; /* to end of outer loop */
00188 
00189          ipat++ ; if( ipat >= npat ) ipat = 0 ;  /* continue outer loop */
00190 
00191       } /* end of outer loop */
00192 
00193       fsync(fileno(outfil)) ; fclose(infil) ; fclose(outfil) ;
00194 
00195       unlink( argv[narg] ) ;
00196       rename( TEMP_FILE , argv[narg] ) ;
00197 
00198       if( !quiet ){ printf(".\n") ; fflush(stdout) ; }
00199    }
00200    exit(0) ;
00201 }
 

Powered by Plone

This site conforms to the following standards: