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