tomwalters@0: /* tomwalters@0: btoa (binary-to-ascii) conversion. tomwalters@0: tomwalters@0: Read numbers of given type from the input stream until eof. tomwalters@0: Print each number in ascii on the stdout to the given precision. tomwalters@0: tomwalters@0: Bugs: tomwalters@0: 1. When converting binary floats to ascii, and then back to binary, there tomwalters@0: will be some difference between the binary files because the ascii tomwalters@0: floats are printed with limited precision. tomwalters@0: */ tomwalters@0: tomwalters@0: tomwalters@0: #include tomwalters@0: #include tomwalters@0: #include "options.h" tomwalters@0: tomwalters@0: tomwalters@0: char applic[] = "binary to ascii conversion." ; tomwalters@0: tomwalters@0: static char *helpstr, *debugstr, *shortstr, *intstr, *floatstr, *precstr, *typestr ; tomwalters@0: tomwalters@0: static Options option[] = { tomwalters@0: { "help" , "off" , &helpstr , "help" , DEBUG }, tomwalters@0: { "debug" , "off" , &debugstr , "debugging" , DEBUG }, tomwalters@0: { "type" , "short" , &typestr , "input data type" , VAL }, tomwalters@0: { "precision" , "3" , &precstr , "decimal places", VAL }, tomwalters@0: { "short" , "off" , &shortstr , "data type" , STOG }, tomwalters@0: { "int" , "off" , &intstr , "data type" , STOG }, tomwalters@0: { "float" , "off" , &floatstr , "data type" , STOG }, tomwalters@0: ( char * ) 0 } ; tomwalters@0: tomwalters@0: tomwalters@0: int itype ; /* input datatype index */ tomwalters@0: int otype ; /* output datatype index */ tomwalters@0: tomwalters@0: main(argc, argv) tomwalters@0: int argc ; tomwalters@0: char *argv[] ; tomwalters@0: { tomwalters@0: FILE *fp ; tomwalters@0: float y ; tomwalters@0: tomwalters@0: fp = openopts( option,argc,argv ) ; tomwalters@0: if ( !isoff( helpstr ) ) tomwalters@0: helpopts( helpstr, argv[0], applic, option ) ; tomwalters@0: tomwalters@0: PRECISION = atoi( precstr ) ; tomwalters@0: tomwalters@0: otype = typeindex( "ascii" ) ; tomwalters@0: tomwalters@0: if ( ( itype = typeindex( typestr ) ) < 0 ) { tomwalters@0: fprintf( stderr, "atob: bad type [%s]\n", typestr ) ; tomwalters@0: exit( 1 ) ; tomwalters@0: } tomwalters@0: tomwalters@0: if ( ison( shortstr ) ) itype = 1 ; /* compatibility */ tomwalters@0: else if ( ison( intstr ) ) itype = 2 ; tomwalters@0: else if ( ison( floatstr ) ) itype = 3 ; tomwalters@0: tomwalters@0: while ( readitem( &y, itype, 1, fp ) ) tomwalters@0: writeitem( &y, otype, 1, stdout ) ; tomwalters@0: tomwalters@0: fclose(fp); tomwalters@0: } tomwalters@0: tomwalters@0: