home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / graphics-0.17 / dist-stat / atob.c next >
Encoding:
C/C++ Source or Header  |  1990-11-13  |  4.0 KB  |  214 lines

  1. /*
  2.  * $Header: /files1/home/toy/src/stat/RCS/atob.c,v 1.1 90/11/04 17:08:28 toy Exp $
  3.  *
  4.  * NAME
  5.  *    atob - ASCII to binary converter
  6.  *
  7.  * SYNOPSIS
  8.  *    atob [-o{}] [files]
  9.  *
  10.  * DESCRIPTION
  11.  *
  12.  * HISTORY
  13.  * $Log:    atob.c,v $
  14.  * Revision 1.1  90/11/04  17:08:28  toy
  15.  * Initial revision
  16.  *
  17.  */
  18.  
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <ctype.h>
  22.  
  23. #if    defined(__STDC__) || defined(STDC_HEADERS)
  24. #include <stddef.h>
  25. #include <stdlib.h>
  26. #endif
  27.  
  28. #include "gps.h"
  29.  
  30. extern int getopt ();
  31. extern int optind;
  32. extern char *optarg;
  33.  
  34. static const char RCSID[] = "@(#)$Id: atob.c,v 1.1 90/11/04 17:08:28 toy Exp $";
  35.  
  36. const char *progname;
  37.  
  38. #define    DEF_PROGNAME    "atob"
  39. #define    OPT_STRING    "H?o:"
  40. #define    OUTPUT_FORMATS    "bsifdBSI"
  41. #define    DEF_OUTPUT_FORM    's'
  42.  
  43. #if    defined(__STDC__)
  44. void
  45. help (void)
  46. #else
  47. void
  48. help ()
  49. #endif
  50. {
  51.   static const char *help_strings[] =
  52.   {
  53.     "\t-H\tThis help\n",
  54.     "\t-o o\tBinary output format:  bsifd.  The character means\n",
  55.     "\t\tbyte, short int, int, float, or double.  Use upper case\n",
  56.     "\t\tletter for unsigned version.\n",
  57.     "Read the given file or stdin for a sequence of numbers.  The\n",
  58.     "output will be the binary version of the given numbers.\n",
  59.     NULL
  60.   };
  61.  
  62.   (void) fprintf (stderr, "%s\n\n", RCSID);
  63.   (void) fprintf (stderr, "Usage:  %s [-H] [-o{%s}] [files]\n",
  64.           progname, OUTPUT_FORMATS);
  65.   print_help_strings (help_strings);
  66. }
  67.  
  68. #if    defined(__STDC__)
  69. void
  70. dump_data (int output_form)
  71. #else
  72. void
  73. dump_data (output_form)
  74.      int output_form;
  75. #endif
  76. {
  77.   char byte;
  78.   short short_val;
  79.   int int_val;
  80.   unsigned char ubyte;
  81.   unsigned short ushort_val;
  82.   unsigned int uint_val;
  83.   float float_val;
  84.   double value;
  85.  
  86.   /*
  87.    * Read each number in, convert it to the appropriate type
  88.    * and save it.
  89.    */
  90.  
  91.   while (get_number (stdin, &value) == 1)
  92.     {
  93.       switch (output_form)
  94.     {
  95.     case 'b':        /* Byte size        */
  96.       byte = (char) value;
  97.       fwrite (&byte, 1, 1, stdout);
  98.       break;
  99.     case 'B':        /* Unsigned byte    */
  100.       ubyte = (unsigned char) value;
  101.       fwrite (&ubyte, 1, 1, stdout);
  102.       break;
  103.     case 's':        /* Short int        */
  104.       short_val = (short) value;
  105.       fwrite (&short_val, sizeof (short), 1, stdout);
  106.       break;
  107.     case 'S':        /* Unsigned short    */
  108.       ushort_val = value;
  109.       fwrite (&ushort_val, sizeof (ushort_val), 1, stdout);
  110.       break;
  111.     case 'i':        /* Int            */
  112.       int_val = (int) value;
  113.       fwrite (&int_val, sizeof (int), 1, stdout);
  114.       break;
  115.     case 'I':        /* Unsigned int        */
  116.       uint_val = (unsigned int) value;
  117.       fwrite (&uint_val, sizeof (uint_val), 1, stdout);
  118.       break;
  119.     case 'f':        /* Float        */
  120.       float_val = (float) value;
  121.       fwrite (&float_val, sizeof (float), 1, stdout);
  122.       break;
  123.     case 'd':        /* Double        */
  124.       fwrite (&value, 1, sizeof (double), stdout);
  125.       break;
  126.     default:
  127.       message ("Fatal error:  unknown output form:  `%c' (%x)\n",
  128.            output_form, output_form);
  129.       exit (2);
  130.     }
  131.     }
  132. }
  133.  
  134.  
  135.  
  136. int
  137. main (argc, argv)
  138.      int argc;
  139.      char *argv[];
  140. {
  141.   char output_format;
  142.   int option;
  143.   int errcnt;
  144.  
  145.   progname = get_my_name (argv[0], DEF_PROGNAME);
  146.  
  147.  
  148.   /*
  149.    * Process options
  150.    */
  151.  
  152.   errcnt = 0;
  153.   output_format = DEF_OUTPUT_FORM;
  154.  
  155.   while ((option = getopt (argc, argv, OPT_STRING)) != -1)
  156.     {
  157.       switch (option)
  158.     {
  159.     case 'H':        /* Help            */
  160.     case '?':
  161.       help ();
  162.       exit (0);
  163.     case 'o':        /* Binary output form    */
  164.       output_format = *optarg;
  165.       break;
  166.     default:
  167.       break;
  168.     }
  169.     }
  170.  
  171.   /*
  172.    * Check options
  173.    */
  174.  
  175.   if (strchr (OUTPUT_FORMATS, output_format) == NULL)
  176.     {
  177.       message ("Unknown input format character: `%c' (0x%x)\n", output_format, (int) output_format);
  178.       errcnt++;
  179.     }
  180.  
  181.   if (errcnt > 0)
  182.     {
  183.       help ();
  184.       exit (1);
  185.     }
  186.  
  187.   /*
  188.    * Read each file and write it in the desired format.
  189.    * Use stdin if no files given.
  190.    */
  191.  
  192.   if (optind >= argc)
  193.     {
  194.       dump_data (output_format);
  195.     }
  196.   else
  197.     {
  198.       while (optind < argc)
  199.     {
  200.       if (freopen (argv[optind], "r", stdin) == NULL)
  201.         {
  202.           message ("cannot open for reading:  `%s'\n", argv[optind]);
  203.         }
  204.       else
  205.         {
  206.           dump_data (output_format);
  207.         }
  208.       optind++;
  209.     }
  210.     }
  211.  
  212.   return 0;
  213. }
  214.