view 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
line wrap: on
line source
/*
 btoa (binary-to-ascii) conversion.

 Read numbers of given type from the input stream until eof.
 Print each number in ascii on the stdout to the given precision.

 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[]     = "binary to ascii conversion." ;

static char *helpstr, *debugstr, *shortstr, *intstr, *floatstr, *precstr, *typestr ;

static Options option[] = {
    {   "help"      ,   "off"       ,  &helpstr     ,   "help"          ,  DEBUG },
    {   "debug"     ,   "off"       ,  &debugstr    ,   "debugging"     ,  DEBUG },
    {   "type"      ,   "short"     ,  &typestr     ,   "input data type"  ,  VAL   },
    {   "precision" ,   "3"         ,  &precstr     ,   "decimal places",  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 ) ;

    PRECISION = atoi( precstr ) ;

    otype = typeindex( "ascii" ) ;

    if ( ( itype = typeindex( typestr ) ) < 0 ) {
	fprintf( stderr, "atob: bad type [%s]\n", typestr ) ;
	exit( 1 ) ;
    }

    if (      ison( shortstr ) )  itype = 1 ;   /* compatibility */
    else if ( ison( intstr   ) )  itype = 2 ;
    else if ( ison( floatstr ) )  itype = 3 ;

    while ( readitem( &y, itype, 1, fp ) )
	writeitem( &y, otype, 1, stdout ) ;

    fclose(fp);
}