annotate tools/btoa.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 btoa (binary-to-ascii) conversion.
tomwalters@0 3
tomwalters@0 4 Read numbers of given type from the input stream until eof.
tomwalters@0 5 Print each number in ascii on the stdout to the given precision.
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[] = "binary to ascii conversion." ;
tomwalters@0 20
tomwalters@0 21 static char *helpstr, *debugstr, *shortstr, *intstr, *floatstr, *precstr, *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 , "input data type" , VAL },
tomwalters@0 27 { "precision" , "3" , &precstr , "decimal places", VAL },
tomwalters@0 28 { "short" , "off" , &shortstr , "data type" , STOG },
tomwalters@0 29 { "int" , "off" , &intstr , "data type" , STOG },
tomwalters@0 30 { "float" , "off" , &floatstr , "data type" , STOG },
tomwalters@0 31 ( char * ) 0 } ;
tomwalters@0 32
tomwalters@0 33
tomwalters@0 34 int itype ; /* input datatype index */
tomwalters@0 35 int otype ; /* output datatype index */
tomwalters@0 36
tomwalters@0 37 main(argc, argv)
tomwalters@0 38 int argc ;
tomwalters@0 39 char *argv[] ;
tomwalters@0 40 {
tomwalters@0 41 FILE *fp ;
tomwalters@0 42 float y ;
tomwalters@0 43
tomwalters@0 44 fp = openopts( option,argc,argv ) ;
tomwalters@0 45 if ( !isoff( helpstr ) )
tomwalters@0 46 helpopts( helpstr, argv[0], applic, option ) ;
tomwalters@0 47
tomwalters@0 48 PRECISION = atoi( precstr ) ;
tomwalters@0 49
tomwalters@0 50 otype = typeindex( "ascii" ) ;
tomwalters@0 51
tomwalters@0 52 if ( ( itype = typeindex( typestr ) ) < 0 ) {
tomwalters@0 53 fprintf( stderr, "atob: bad type [%s]\n", typestr ) ;
tomwalters@0 54 exit( 1 ) ;
tomwalters@0 55 }
tomwalters@0 56
tomwalters@0 57 if ( ison( shortstr ) ) itype = 1 ; /* compatibility */
tomwalters@0 58 else if ( ison( intstr ) ) itype = 2 ;
tomwalters@0 59 else if ( ison( floatstr ) ) itype = 3 ;
tomwalters@0 60
tomwalters@0 61 while ( readitem( &y, itype, 1, fp ) )
tomwalters@0 62 writeitem( &y, otype, 1, stdout ) ;
tomwalters@0 63
tomwalters@0 64 fclose(fp);
tomwalters@0 65 }
tomwalters@0 66
tomwalters@0 67