annotate tools/swab.c @ 0:5242703e91d3 tip

Initial checkin for AIM92 aimR8.2 (last updated May 1997).
author tomwalters
date Fri, 20 May 2011 15:19:45 +0100
parents
children
rev   line source
tomwalters@0 1 /*
tomwalters@0 2 swab.c swap bytes
tomwalters@0 3 ------
tomwalters@0 4
tomwalters@0 5 Read and write binary shorts (2-bytes).
tomwalters@0 6 With no filename arguments, data is expected on the stdin, and the
tomwalters@0 7 result is written on the stdout. Otherwise each given filename is
tomwalters@0 8 processed in turn. When the `output' option is "off" output overwrites the
tomwalters@0 9 respective input file. (The respective output for the stdin is the stdout).
tomwalters@0 10 Otherwise all results are written to the one given output file
tomwalters@0 11 (which is the stdout by default).
tomwalters@0 12
tomwalters@0 13 */
tomwalters@0 14
tomwalters@0 15 #include <stdio.h>
tomwalters@0 16 #include <math.h>
tomwalters@0 17 #include "options.h"
tomwalters@0 18
tomwalters@0 19
tomwalters@0 20 char applic[] = "Swap byte order of binary 2-byte data." ;
tomwalters@0 21
tomwalters@0 22 static char *helpstr, *outstr, *sizestr ;
tomwalters@0 23
tomwalters@0 24 static Options option[] = {
tomwalters@0 25 { "help" , "off" , &helpstr , "help" , DEBUG },
tomwalters@0 26 { "output" , "stdout" , &outstr , "output filename (off = overwrite input)" , VAL },
tomwalters@0 27 { "SIZE" , "262144" , &sizestr , "buffer size in bytes" , SVAL },
tomwalters@0 28 ( char * ) 0 } ;
tomwalters@0 29
tomwalters@0 30
tomwalters@0 31 int SIZE ; /* size of temp buffer for stdin data */
tomwalters@0 32
tomwalters@0 33 main(argc, argv)
tomwalters@0 34 int argc ;
tomwalters@0 35 char *argv[] ;
tomwalters@0 36 {
tomwalters@0 37 FILE *ifp, *ofp ;
tomwalters@0 38 int i, j, n, doOpen, doCreate ;
tomwalters@0 39 char *data, *Y ;
tomwalters@0 40
tomwalters@0 41 i = getopts( option, argc, argv ) ;
tomwalters@0 42 if ( !isoff( helpstr ) )
tomwalters@0 43 helpopts( helpstr, argv[0], applic, option ) ;
tomwalters@0 44
tomwalters@0 45 SIZE = atoi( sizestr ) ;
tomwalters@0 46
tomwalters@0 47 doOpen = doCreate = 0 ;
tomwalters@0 48
tomwalters@0 49 if ( i == 0 ) ifp = stdin ;
tomwalters@0 50 else doOpen++ ;
tomwalters@0 51
tomwalters@0 52 if ( isoff( outstr ) ) {
tomwalters@0 53 if ( i == 0 ) ofp = stdout ; /* o/p = i/p which is stdin */
tomwalters@0 54 else doCreate++ ; /* o/p = i/p for current file */
tomwalters@0 55 }
tomwalters@0 56 else if ( isstr( outstr, "stdout") ) ofp = stdout ;
tomwalters@0 57 else if ( ( ofp = fopen( outstr, "w" ) ) == (FILE *)0 ) {
tomwalters@0 58 fprintf( stderr,"swab: can't create %s\n", outstr ) ;
tomwalters@0 59 exit( 1 ) ;
tomwalters@0 60 }
tomwalters@0 61
tomwalters@0 62 Y = (char *)malloc( sizeof( short ) ) ;
tomwalters@0 63 if ( ( data = (char *)malloc( SIZE ) ) == NULL ) {
tomwalters@0 64 fprintf( stderr, "malloc out of space\n" ) ;
tomwalters@0 65 exit( 1 ) ;
tomwalters@0 66 }
tomwalters@0 67
tomwalters@0 68 do {
tomwalters@0 69
tomwalters@0 70 if ( doOpen && ( ifp = fopen( argv[argc-i], "r" ) ) == (FILE *)0 ) {
tomwalters@0 71 fprintf( stderr,"swab: can't open %s\n", argv[argc-i] ) ;
tomwalters@0 72 exit( 1 ) ;
tomwalters@0 73 }
tomwalters@0 74
tomwalters@0 75 for ( n = 0 ; n < SIZE && fread( &data[n], sizeof(short), 1, ifp ) ; n += 2 )
tomwalters@0 76 ;
tomwalters@0 77 if ( n == SIZE ) fprintf( stderr, "warning: buffer out of space, increase SIZE\n" ) ;
tomwalters@0 78
tomwalters@0 79 if ( doOpen ) fclose( ifp ) ;
tomwalters@0 80
tomwalters@0 81 if ( doCreate && ( ofp = fopen( argv[argc-i], "w" ) ) == (FILE *)0 ) {
tomwalters@0 82 fprintf( stderr,"swab: can't create %s\n", argv[argc-i] ) ;
tomwalters@0 83 exit( 1 ) ;
tomwalters@0 84 }
tomwalters@0 85
tomwalters@0 86 for ( j = 0 ; j < n ; j += 2 ) {
tomwalters@0 87 *Y = data[j+1] ;
tomwalters@0 88 *(Y+1) = data[j] ;
tomwalters@0 89 fwrite( Y, sizeof(short), 1, ofp );
tomwalters@0 90 }
tomwalters@0 91
tomwalters@0 92 if ( doCreate ) fclose( ofp ) ;
tomwalters@0 93
tomwalters@0 94 } while ( --i > 0 ) ;
tomwalters@0 95
tomwalters@0 96 }
tomwalters@0 97