home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / t / t1utils1.zip / T1ASCII.C < prev    next >
C/C++ Source or Header  |  1992-06-12  |  4KB  |  189 lines

  1. /* t1ascii
  2.  *
  3.  * This program takes an Adobe Type-1 font program in binary (PFB) format and
  4.  * converts it to ASCII (PFA) format.
  5.  *
  6.  * Copyright (c) 1992 by I. Lee Hetherington, all rights reserved.
  7.  *
  8.  * Permission is hereby granted to use, modify, and distribute this program
  9.  * for any purpose provided this copyright notice and the one below remain
  10.  * intact.
  11.  *
  12.  * I. Lee Hetherington (ilh@lcs.mit.edu)
  13.  *
  14.  * $Log:        t1ascii.c,v $
  15.  * Revision 1.1  92/05/22  11:47:24  ilh
  16.  * initial version
  17.  *
  18.  * Ported to Microsoft C/C++ Compiler and MS-DOS operating system by
  19.  * Kai-Uwe Herbing (herbing@netmbx.netmbx.de) on June 12, 1992. Code
  20.  * specific to the MS-DOS version is encapsulated with #ifdef _MSDOS
  21.  * ... #endif, where _MSDOS is an identifier, which is automatically
  22.  * defined, if you compile with the Microsoft C/C++ Compiler.
  23.  *
  24.  */
  25.  
  26. #ifndef lint
  27. static char rcsid[] =
  28.   "@(#) $Id: t1ascii.c,v 1.1 92/05/22 11:47:24 ilh Exp Locker: ilh $";
  29. static char copyright[] =
  30.   "@(#) Copyright (c) 1992 by I. Lee Hetherington, all rights reserved.";
  31. #ifdef _MSDOS
  32. static char portnotice[] =
  33.   "@(#) Ported to MS-DOS by Kai-Uwe Herbing (herbing@netmbx.netmbx.de).";
  34. #endif
  35. #endif
  36.  
  37. /* Note: this is ANSI C. */
  38.  
  39. #ifdef _MSDOS
  40.   #include <fcntl.h>
  41.   #include <io.h>
  42. #endif
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <string.h>
  46. #include <ctype.h>
  47. #include <limits.h>
  48.  
  49. /* int32 must be at least 32-bit */
  50. #if MAX_INT >= 0x7FFFFFFFUL
  51. typedef int int32;
  52. #else
  53. typedef long int32;
  54. #endif
  55.  
  56. #define MARKER   128
  57. #define ASCII    1
  58. #define BINARY   2
  59. #define DONE     3
  60.  
  61. static FILE *ifp = stdin;
  62. static FILE *ofp = stdout;
  63.  
  64. /* This function reads a four-byte block length. */
  65.  
  66. static int32 read_length()
  67. {
  68.   int32 length;
  69.  
  70.   length = (int32) (fgetc(ifp) & 0xff);
  71.   length |= (int32) (fgetc(ifp) & 0xff) << 8;
  72.   length |= (int32) (fgetc(ifp) & 0xff) << 16;
  73.   length |= (int32) (fgetc(ifp) & 0xff) << 24;
  74.  
  75.   return length;
  76. }
  77.  
  78. /* This function outputs a single byte in hexadecimal.  It limits hexadecimal
  79.    output to 64 columns. */
  80.  
  81. static void output_hex(int b)
  82. {
  83.   static char *hexchar = "0123456789ABCDEF";
  84.   static int hexcol = 0;
  85.  
  86.   /* trim hexadecimal lines to 64 columns */
  87.   if (hexcol >= 64) {
  88.     fputc('\n', ofp);
  89.     hexcol = 0;
  90.   }
  91.   fputc(hexchar[(b >> 4) & 0xf], ofp);
  92.   fputc(hexchar[b & 0xf], ofp);
  93.   hexcol += 2;
  94. }
  95.  
  96. static void usage()
  97. {
  98.   fprintf(stderr,
  99.           "usage: t1ascii [input [output]]\n");
  100.   exit(1);
  101. }
  102.  
  103. static void print_banner()
  104. {
  105.   static char rcs_revision[] = "$Revision: 1.1 $";
  106.   static char revision[20];
  107.  
  108.   if (sscanf(rcs_revision, "$Revision: %19s", revision) != 1)
  109.     revision[0] = '\0';
  110.   fprintf(stderr, "This is t1ascii %s.\n", revision);
  111. }
  112.  
  113. int main(int argc, char **argv)
  114. {
  115.   int32 length;
  116.   int c, block = 1, last_type = ASCII;
  117.  
  118.   print_banner();
  119.  
  120.   if (argc > 3)
  121.     usage();
  122.  
  123.   /* possibly open input & output files */
  124.   if (argc >= 2) {
  125.     ifp = fopen(argv[1], "r");
  126.     if (!ifp) {
  127.       fprintf(stderr, "error: cannot open %s for reading\n", argv[1]);
  128.       exit(1);
  129.     }
  130.   }
  131.   if (argc == 3) {
  132.     ofp = fopen(argv[2], "w");
  133.     if (!ofp) {
  134.       fprintf(stderr, "error: cannot open %s for writing\n", argv[2]);
  135.       exit(1);
  136.     }
  137.   }
  138.  
  139.   #ifdef _MSDOS
  140.     /* As we are processing a PFB (binary) input */
  141.     /* file, we must set its file mode to binary. */
  142.     _setmode(_fileno(ifp), _O_BINARY);
  143.   #endif
  144.  
  145.   /* main loop through blocks */
  146.  
  147.   for (;;) {
  148.     c = fgetc(ifp);
  149.     if (c == EOF) {
  150.       break;
  151.     }
  152.     if (c != MARKER) {
  153.       fprintf(stderr,
  154.               "error:  missing marker (128) at beginning of block %d",
  155.               block);
  156.       exit(1);
  157.     }
  158.     switch (c = fgetc(ifp)) {
  159.     case ASCII:
  160.       if (last_type != ASCII)
  161.         fputc('\n', ofp);
  162.       last_type = ASCII;
  163.       for (length = read_length(); length > 0; length--)
  164.         if ((c = fgetc(ifp)) == '\r')
  165.           fputc('\n', ofp);
  166.         else
  167.           fputc(c, ofp);
  168.       break;
  169.     case BINARY:
  170.       last_type = BINARY;
  171.       for (length = read_length(); length > 0; length--)
  172.         output_hex(fgetc(ifp));
  173.       break;
  174.     case DONE:
  175.       /* nothing to be done --- will exit at top of loop with EOF */
  176.       break;
  177.     default:
  178.       fprintf(stderr, "error: bad block type %d in block %d\n",
  179.               c, block);
  180.       break;
  181.     }
  182.     block++;
  183.   }
  184.   fclose(ifp);
  185.   fclose(ofp);
  186.  
  187.   return 0;
  188. }
  189.