home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 700-799 / ff732.lha / pstools / t1utils / t1sourcecode / t1ascii.c < prev    next >
C/C++ Source or Header  |  1992-09-26  |  3KB  |  160 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.  */
  19.  
  20. #ifndef lint
  21. static char rcsid[] =
  22.   "@(#) $Id: t1ascii.c,v 1.1 92/05/22 11:47:24 ilh Exp $";
  23. static char copyright[] =
  24.   "@(#) Copyright (c) 1992 by I. Lee Hetherington, all rights reserved.";
  25. #endif
  26.  
  27. /* Note: this is ANSI C. */
  28.  
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <ctype.h>
  33.  
  34. #define MARKER   128
  35. #define ASCII    1
  36. #define BINARY   2
  37. #define DONE     3
  38.  
  39. static FILE *ifp = stdin;
  40. static FILE *ofp = stdout;
  41.  
  42. /* This function reads a four-byte block length. */
  43.  
  44. static int read_length()
  45. {
  46.   int length;
  47.  
  48.   length = fgetc(ifp);
  49.   length |= fgetc(ifp) << 8;
  50.   length |= fgetc(ifp) << 16;
  51.   length |= fgetc(ifp) << 24;
  52.  
  53.   return length;
  54. }
  55.  
  56. /* This function outputs a single byte in hexadecimal.  It limits hexadecimal
  57.    output to 64 columns. */
  58.  
  59. static void output_hex(int b)
  60. {
  61.   static char *hexchar = "0123456789ABCDEF";
  62.   static int hexcol = 0;
  63.  
  64.   /* trim hexadecimal lines to 64 columns */
  65.   if (hexcol >= 64) {
  66.     fputc('\n', ofp);
  67.     hexcol = 0;
  68.   }
  69.   fputc(hexchar[(b >> 4) & 0xf], ofp);
  70.   fputc(hexchar[b & 0xf], ofp);
  71.   hexcol += 2;
  72. }
  73.  
  74. static void usage()
  75. {
  76.   fprintf(stderr,
  77.       "usage: t1ascii [input [output]]\n");
  78.   exit(1);
  79. }
  80.  
  81. static void print_banner()
  82. {
  83.   static char rcs_revision[] = "$Revision: 1.1 $";
  84.   static char revision[20];
  85.  
  86.   if (sscanf(rcs_revision, "$Revision: %19s", revision) != 1)
  87.     revision[0] = '\0';
  88.   fprintf(stderr, "This is t1ascii %s.\n", revision);
  89. }
  90.  
  91. int main(int argc, char **argv)
  92. {
  93.   int c, block = 1, length, last_type = ASCII;
  94.  
  95.   print_banner();
  96.  
  97.   if (argc > 3)
  98.     usage();
  99.  
  100.   /* possibly open input & output files */
  101.   if (argc >= 2) {
  102.     ifp = fopen(argv[1], "r");
  103.     if (!ifp) {
  104.       fprintf(stderr, "error: cannot open %s for reading\n", argv[1]);
  105.       exit(1);
  106.     }
  107.   }
  108.   if (argc == 3) {
  109.     ofp = fopen(argv[2], "w");
  110.     if (!ofp) {
  111.       fprintf(stderr, "error: cannot open %s for writing\n", argv[2]);
  112.       exit(1);
  113.     }
  114.   }
  115.  
  116.   /* main loop through blocks */
  117.  
  118.   for (;;) {
  119.     c = fgetc(ifp);
  120.     if (c == EOF) {
  121.       break;
  122.     }
  123.     if (c != MARKER) {
  124.       fprintf(stderr,
  125.           "error:  missing marker (128) at beginning of block %d",
  126.           block);
  127.       exit(1);
  128.     }
  129.     switch (c = fgetc(ifp)) {
  130.     case ASCII:
  131.       if (last_type != ASCII)
  132.     fputc('\n', ofp);
  133.       last_type = ASCII;
  134.       for (length = read_length(); length > 0; length--)
  135.     if ((c = fgetc(ifp)) == '\r')
  136.       fputc('\n', ofp);
  137.     else
  138.       fputc(c, ofp);
  139.       break;
  140.     case BINARY:
  141.       last_type = BINARY;
  142.       for (length = read_length(); length > 0; length--)
  143.     output_hex(fgetc(ifp));      
  144.       break;
  145.     case DONE:
  146.       /* nothing to be done --- will exit at top of loop with EOF */
  147.       break;
  148.     default:
  149.       fprintf(stderr, "error: bad block type %d in block %d\n",
  150.           c, block);
  151.       break;
  152.     }
  153.     block++;
  154.   }
  155.   fclose(ifp);
  156.   fclose(ofp);
  157.  
  158.   return 0;
  159. }
  160.