annotate tools/atob.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 atob (binary-to-ascii) conversion.
tomwalters@0 3
tomwalters@0 4 Read ascii numbers from the input stream until eof.
tomwalters@0 5 Write each number on the stdout in the given type.
tomwalters@0 6
tomwalters@0 7 Bugs:
tomwalters@0 8 1. When converting binary floats to ascii, and then back to binary, there
tomwalters@0 9 will be some difference between the binary files because the ascii
tomwalters@0 10 floats are printed with limited precision.
tomwalters@0 11 */
tomwalters@0 12
tomwalters@0 13
tomwalters@0 14 #include <stdio.h>
tomwalters@0 15 #include <math.h>
tomwalters@0 16 #include "options.h"
tomwalters@0 17
tomwalters@0 18
tomwalters@0 19 char applic[] = "ascii to binary conversion." ;
tomwalters@0 20
tomwalters@0 21 static char *helpstr, *debugstr, *shortstr, *intstr, *floatstr, *typestr ;
tomwalters@0 22
tomwalters@0 23 static Options option[] = {
tomwalters@0 24 { "help" , "off" , &helpstr , "help" , DEBUG },
tomwalters@0 25 { "debug" , "off" , &debugstr , "debugging" , DEBUG },
tomwalters@0 26 { "type" , "short" , &typestr , "output data type" , VAL },
tomwalters@0 27 { "short" , "off" , &shortstr , "data type" , STOG },
tomwalters@0 28 { "int" , "off" , &intstr , "data type" , STOG },
tomwalters@0 29 { "float" , "off" , &floatstr , "data type" , STOG },
tomwalters@0 30 ( char * ) 0 } ;
tomwalters@0 31
tomwalters@0 32
tomwalters@0 33 int itype ; /* input datatype index */
tomwalters@0 34 int otype ; /* output datatype index */
tomwalters@0 35
tomwalters@0 36 main(argc, argv)
tomwalters@0 37 int argc ;
tomwalters@0 38 char *argv[] ;
tomwalters@0 39 {
tomwalters@0 40 FILE *fp ;
tomwalters@0 41 float y ;
tomwalters@0 42
tomwalters@0 43 fp = openopts( option,argc,argv ) ;
tomwalters@0 44 if ( !isoff( helpstr ) )
tomwalters@0 45 helpopts( helpstr, argv[0], applic, option ) ;
tomwalters@0 46
tomwalters@0 47 itype = typeindex( "ascii" ) ;
tomwalters@0 48
tomwalters@0 49 if ( ( otype = typeindex( typestr ) ) < 0 ) {
tomwalters@0 50 fprintf( stderr, "atob: bad type [%s]\n", typestr ) ;
tomwalters@0 51 exit( 1 ) ;
tomwalters@0 52 }
tomwalters@0 53
tomwalters@0 54 if ( ison( shortstr ) ) otype = 1 ; /* compatibility */
tomwalters@0 55 else if ( ison( intstr ) ) otype = 2 ;
tomwalters@0 56 else if ( ison( floatstr ) ) otype = 3 ;
tomwalters@0 57
tomwalters@0 58 while ( readitem( &y, itype, 1, fp ) )
tomwalters@0 59 writeitem( &y, otype, 1, stdout ) ;
tomwalters@0 60
tomwalters@0 61 fclose(fp);
tomwalters@0 62 }
tomwalters@0 63
tomwalters@0 64