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