Mercurial > hg > aim92
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tools/atob.c Fri May 20 15:19:45 2011 +0100 @@ -0,0 +1,64 @@ +/* + atob (binary-to-ascii) conversion. + + Read ascii numbers from the input stream until eof. + Write each number on the stdout in the given type. + + Bugs: + 1. When converting binary floats to ascii, and then back to binary, there + will be some difference between the binary files because the ascii + floats are printed with limited precision. +*/ + + +#include <stdio.h> +#include <math.h> +#include "options.h" + + +char applic[] = "ascii to binary conversion." ; + +static char *helpstr, *debugstr, *shortstr, *intstr, *floatstr, *typestr ; + +static Options option[] = { + { "help" , "off" , &helpstr , "help" , DEBUG }, + { "debug" , "off" , &debugstr , "debugging" , DEBUG }, + { "type" , "short" , &typestr , "output data type" , VAL }, + { "short" , "off" , &shortstr , "data type" , STOG }, + { "int" , "off" , &intstr , "data type" , STOG }, + { "float" , "off" , &floatstr , "data type" , STOG }, + ( char * ) 0 } ; + + +int itype ; /* input datatype index */ +int otype ; /* output datatype index */ + +main(argc, argv) +int argc ; +char *argv[] ; +{ + FILE *fp ; + float y ; + + fp = openopts( option,argc,argv ) ; + if ( !isoff( helpstr ) ) + helpopts( helpstr, argv[0], applic, option ) ; + + itype = typeindex( "ascii" ) ; + + if ( ( otype = typeindex( typestr ) ) < 0 ) { + fprintf( stderr, "atob: bad type [%s]\n", typestr ) ; + exit( 1 ) ; + } + + if ( ison( shortstr ) ) otype = 1 ; /* compatibility */ + else if ( ison( intstr ) ) otype = 2 ; + else if ( ison( floatstr ) ) otype = 3 ; + + while ( readitem( &y, itype, 1, fp ) ) + writeitem( &y, otype, 1, stdout ) ; + + fclose(fp); +} + +