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  

2swap.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 <stdlib.h>
00010 
00011 typedef struct { unsigned char a,b ; } twobytes ;
00012 
00013 #define TEMP_FILE "Elvis.Lives"
00014 #define BUFSIZE   16000
00015 
00016 static short buf[BUFSIZE] ;
00017 
00018 int main( argc , argv )
00019    int argc ;
00020    char * argv[] ;
00021 {
00022    FILE * infil , * outfil ;
00023    int narg , nbyte , nsh , quiet = 0 , ndone ;
00024 
00025    if( argc < 2 || strncmp(argv[1],"-help",2) == 0 ){
00026      printf("Usage: 2swap [-q] file ...\n") ;
00027      printf("-- Swaps byte pairs on the files listed.\n") ;
00028      printf("   The -q option means to work quietly.\n") ;
00029      exit(0) ;
00030    }
00031 
00032    narg = 1 ;
00033    if( strncmp(argv[1],"-q",2) == 0 ){ quiet = 1 ; narg++ ; }
00034 
00035    for( ; narg < argc ; narg++ ){
00036       infil = fopen( argv[narg] , "r" ) ;
00037       if( infil == NULL ){
00038          printf("** file %s not found!\n",argv[narg]) ; fflush(stdout) ;
00039          continue ;
00040       }
00041 
00042       outfil = fopen( TEMP_FILE , "w" ) ;
00043       if( outfil == NULL ){
00044          printf("** Cannot open temporary file!\n") ;
00045          exit(1) ;
00046       }
00047 
00048       if( !quiet ){ printf("-- opened %s",argv[narg]) ; fflush(stdout) ; }
00049 
00050       ndone = 0 ;
00051       do {
00052          nsh = fread( buf , sizeof(short) , BUFSIZE , infil ) ;
00053          if( nsh <= 0 ) break ;
00054          swap2( nsh , buf ) ;
00055          fwrite( buf , sizeof(short) , nsh , outfil ) ;
00056          ndone += nsh ;
00057          if( ndone > 1000000 && ! quiet ){
00058             ndone -= 1000000 ; printf(".") ; fflush(stdout) ;
00059          }
00060       } while( nsh == BUFSIZE ) ;
00061 
00062       fsync(fileno(outfil)) ; fclose( infil ) ; fclose( outfil ) ;
00063 
00064       unlink( argv[narg] ) ;
00065       rename( TEMP_FILE , argv[narg] ) ;
00066 
00067       if( !quiet ){ printf(".\n") ; fflush(stdout) ; }
00068    }
00069    exit(0) ;
00070 }
00071 
00072 int swap2( n , ar )
00073    int n ;
00074    short * ar ;
00075 {
00076    register int ii ;
00077    register twobytes * tb = (twobytes *) ar ;
00078    register unsigned char tt ;
00079 
00080    for( ii=0 ; ii < n ; ii++ ){
00081       tt       = tb[ii].a ;
00082       tb[ii].a = tb[ii].b ;
00083       tb[ii].b = tt ;
00084    }
00085    return 0 ;
00086 }
 

Powered by Plone

This site conforms to the following standards: