home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 443.lha / pcl2english_v2.0 / utils.c < prev   
C/C++ Source or Header  |  1990-12-02  |  10KB  |  431 lines

  1. /* $RCSfile: utils.c,v $  $Revision: 2.0 $ */
  2.  
  3. #include <stdio.h>
  4. #include "gdefs.h"
  5. #include "externals.h"
  6. #include "protos.h"
  7. extern FILE *ifile,*ofile;
  8.  
  9. static int begin_or_end = FALSE;    /* Variable used between 
  10.                                        print_char and print_chars */
  11.  
  12. void err_exit(int x)
  13. {
  14.     printf(err_msg[x]);
  15.     exit(x);
  16. }
  17.  
  18. void print_hex(int i)
  19. {
  20.     if ((i>=0) && (i<=9)) {
  21.         fputc(i+'0',ofile);
  22.     }
  23.     else if ((i>=10) && (i<=15)) {
  24.         fputc(i+'A'-10,ofile);
  25.     }
  26.     else printf("\nERROR: i= %d\n",i);
  27. }
  28.  
  29. void output_byte(int c)
  30. {
  31.     int i,j,k;
  32.     i=c;
  33.     j=i / 16;
  34.     k=i % 16;
  35.     print_hex(j);
  36.     print_hex(k);
  37. }
  38.  
  39. void indent(int x)
  40. {
  41.     int i;
  42.  
  43.     /* Default is to indent 4 spaces */
  44.     if (x != 0) {
  45.         for (i=0; i<x; i++) {
  46.             /* Insert favorite tabbing style */
  47.             fprintf(ofile,"    ");
  48.         }
  49.     }
  50. }
  51.  
  52. /* ROUTINE: print_esc_string
  53.  *
  54.  *    This routine prints the entire escape sequence string.  If the
  55.  *    hex option is used, a hex printout of the string is also given.
  56.  */
  57. void print_esc_string(void)
  58. {
  59.  
  60.     fprintf(ofile,"\n<Esc>%s\n", &esc_string[1]);
  61.     if (opt_x) {
  62.         int i,j;
  63.         /* Print the characters in hex */
  64.         for (i=0; i <= esc_count; i++) {
  65.             j = 0xff & esc_string[i];
  66.             output_byte(j);
  67.             fprintf(ofile," ");
  68.         }
  69.         fprintf(ofile,"\n");
  70.     }
  71. }
  72.  
  73. /* ROUTINE: print_sub_string
  74.  *
  75.  *    This routine prints part of the escape sequence string.
  76.  *    This consists of the "header" which is the first header characters
  77.  *    after the escape.  Then the string between start and stop is printed.
  78.  */
  79. void print_sub_string(int header, int start, int stop)
  80. {
  81.     int i;
  82.     char s[255];
  83.     char    *ptr_c;
  84.  
  85.     ptr_c = s;
  86.     for (i=1; i<=header; i++) {
  87.         *ptr_c++ = esc_string[i];
  88.     }
  89.     for (i=start; i<=stop; i++) {
  90.         *ptr_c++ = esc_string[i];
  91.     }
  92.     *ptr_c = 0;
  93.     indent(1);
  94.     fprintf(ofile,"<Esc>%-6s ", s);
  95. }
  96.  
  97. /* ROUTINE: get_num
  98.  *
  99.  *    Convert an alpha number sequence to an actual int.  
  100.  *
  101.  * Variables used:
  102.  *    stop_pos  -- *stop_pos contains the starting point on 
  103.  *                 entry and is updated to the new "stop point"
  104.  *                 before exiting.
  105.  *    num       -- global variable used to return the integer value.
  106.  *    fraction  -- global string used to return fractional part (if exists)
  107.  *    is_float  -- global flag to say that fractional part exists.
  108.  *    plus      -- global flag to say number started with a '+' sign.
  109.  *    minus     -- global flag to say number started with a '-' sign.
  110.  *    
  111.  * NOTES:
  112.  *    Pcl assumes a zero if no number is given.  Thus, this routine returns
  113.  *    a zero if '0' is encountered or if no digits are encountered.
  114.  *    
  115.  *    If the fractional part of a floating point number is zero, the
  116.  *    number will be treated like an int.
  117.  */
  118. void get_num(int *stop_pos, int sum)
  119. {
  120.     char c;
  121.     int  done;
  122.     int  i,j;
  123.  
  124.     i = *stop_pos;
  125.     c = esc_string[i];
  126.     plus = minus = is_float = FALSE;
  127.     if (c == '-') {
  128.         minus = TRUE;
  129.         i++;
  130.     }
  131.     else if (c == '+') {
  132.         i++;
  133.         plus = TRUE;
  134.     }
  135.     done = FALSE;
  136.     while (done == FALSE) {
  137.         c = esc_string[i];
  138.         if ((c >= '0') && (c <= '9')) {
  139.             sum *= 10;
  140.             sum += c - '0';
  141.             i++;
  142.         }
  143.         else done = TRUE;
  144.     }
  145.     if (c == '.') {
  146.         i++;
  147.         done = FALSE;
  148.         j = 0;
  149.         while (done == FALSE) {
  150.             c = esc_string[i];
  151.             if ((c >= '0') && (c <= '9')) {
  152.                 fraction[j] = c;
  153.                 i++;
  154.                 j++;
  155.             }
  156.             else done = TRUE;
  157.         }
  158.         /* Remove trailing 0's and see if anything's left */
  159.         if (j > 0) {
  160.             done = FALSE;
  161.             while (done == FALSE) {
  162.                 fraction[j] = 0;
  163.                 if ((j == 0) || (fraction[j-1] != '0')) {
  164.                     done = TRUE;
  165.                 }
  166.                 else {
  167.                     j--;
  168.                 }
  169.             }
  170.             if (j != 0) {
  171.                 is_float = TRUE;
  172.             }
  173.         }
  174.     }
  175.     *stop_pos = i;
  176.     if (minus) {
  177.         sum = -sum;
  178.     }
  179.     num = sum;
  180. }
  181.  
  182. /*
  183.  * ROUTINE: decode_esc_string
  184.  *
  185.  *   This routine provides the first level of decoding for an escape
  186.  *   string.  If it is a simple 2 character escape sequence, it is
  187.  *   handled here.  Otherwise, the appropriate lower level routine
  188.  *   is called.
  189.  */
  190. void decode_esc_string(void)
  191. {
  192.     char c;
  193.  
  194.     c = esc_string[1];
  195.     if (esc_count == 1) {
  196.         indent(1);
  197.         fprintf(ofile,"<Esc>%-6s ", &esc_string[1]);
  198.         switch (c) {
  199.             case '9':
  200.                 fprintf(ofile,
  201.                     "Reset Left and Right Margins to default settings.\n");
  202.                 break;
  203.             case '=':
  204.                 fprintf(ofile,"Half Line Feed.  Move down one-half line.\n");
  205.                 break;
  206.             case 'E':
  207.                 fprintf(ofile,"Reset to user defaults.\n");
  208.                 indent(4);
  209.                 fprintf(ofile,
  210.                     "Print buffered page.  Delete temporary fonts and macros.\n");
  211.                 break;
  212.             case 'Y':
  213.                 fprintf(ofile,"Display Functions ON.\n");
  214.                 break; 
  215.             case 'Z':
  216.                 fprintf(ofile,"Display Functions OFF.\n");
  217.                 break;
  218.             case 'z':
  219.                 fprintf(ofile,"Print Self Test.\n");
  220.                 break;
  221.             default:
  222.                 fprintf(ofile,"%s",bad_esc);
  223.                 break;
  224.         }
  225.     }
  226.     else {
  227.         switch (c) {
  228.             case '(':
  229.             case ')':
  230.                 decode_paren();
  231.                 break;
  232.             case '&':
  233.                 decode_ampersand();
  234.                 break;
  235.             case '*':
  236.                 decode_asterisk();
  237.                 break;
  238.             default:
  239.                 fprintf(ofile,"%s",bad_esc);
  240.                 break;
  241.         }
  242.     }
  243. }
  244.  
  245. /* ROUTINE: add_char
  246.  *
  247.  *    This routine places a character into a buffer when we are in
  248.  *    character mode.  The idea is to store the first (typically 10)
  249.  *    characters and last (again typically 10) characters.  Thus, if
  250.  *    we encounter a big text section, we don't have to print the whole
  251.  *    thing.  The first characters are placed in the char_start
  252.  *    array.  The last characters are placed in the char_end array which
  253.  *    is a circular buffer.
  254.  */
  255. void add_char(int c)
  256. {
  257.     if (opt_e == FALSE) {
  258.         if (char_count <= CHAR_START_LEN + CHAR_END_LEN - 1) {
  259.             char_start[char_count] = c;
  260.         }
  261.         if (char_count >= CHAR_START_LEN) {
  262.             char_end[(char_count - CHAR_START_LEN) % CHAR_END_LEN] = c;
  263.         }
  264.         char_count++;
  265.     }
  266. }
  267.  
  268. /* ROUTINE: print_extra_rasters
  269.  *
  270.  *    Raster images are printed a line at a time.  Rather than print
  271.  *    each raster escape sequence, the first is simply printed, and
  272.  *    all others that follow of the same size are counted, and the
  273.  *    additional count is printed with this routine.
  274.  */
  275. void print_extra_rasters(void)
  276. {
  277.     if ((raster_mode != NO_RASTER) && (raster_count > 0)) {
  278.         indent(1);
  279.         fprintf(ofile,"%d same size raster commands follow.\n",raster_count);
  280.     }
  281.     raster_mode = NO_RASTER;
  282.     raster_count = 0;
  283. }
  284.  
  285. /* ROUTINE: print_char
  286.  *
  287.  *    This routine prints a character that was encountered in character
  288.  *    mode.  Some special characters are expressed mnemonically between
  289.  *    brackets.  Other (typically) non-printing characters are represented
  290.  *    by their hex character codes in brackets.  The space character gets 
  291.  *    some extra special treatment.  In cases at the beginning or end of 
  292.  *    strings, where it might not be visually obvious, a <SP> is printed
  293.  *    instead.  The begin_or_end variable is used between this routine 
  294.  *    and print_chars to convey this information and is always cleared
  295.  *    on exit from this routine.
  296.  */
  297. void print_char(char c)
  298. {
  299.     char s[2];
  300.  
  301.     switch (c) {
  302.         case CARRIAGE_RETURN:
  303.             fprintf(ofile,"<CR>");
  304.             break;
  305.         case LINE_FEED:
  306.             fprintf(ofile,"<LF>");
  307.             break;
  308.         case BACK_SPACE:
  309.             fprintf(ofile,"<BS>");
  310.             break;
  311.         case TAB:
  312.             fprintf(ofile,"<TAB>");
  313.             break;
  314.         case ' ':
  315.             if (begin_or_end) {
  316.                 fprintf(ofile,"<SP>");
  317.             }
  318.             else {
  319.                 fprintf(ofile," ");
  320.             }
  321.             break;
  322.         default:
  323.             if (((c >= 32) && (c <= 127)) || ((c >= 161) && (c <= 254))) {
  324.                 s[0]=c;
  325.                 s[1]=0;
  326.                 fprintf(ofile,"%s",s);
  327.             }
  328.             else {
  329.                 /* Non-printing characters in Roman-8 character set */
  330.                 /* Hopefully one won't see much of these */
  331.                 fprintf(ofile,"<0x%x>",0xff & c);
  332.             }
  333.             break;
  334.     }
  335.     begin_or_end = FALSE;
  336. }
  337.  
  338. /*
  339.  * ROUTINE: print_chars
  340.  *     Prints the first (10) characters and last (10) characters of text 
  341.  *
  342.  * NOTES:
  343.  *     Uses the following forms:
  344.  *        <=20 chars "123456789ab"  
  345.  *         >20 chars "123456789a ... 123456789a"  
  346.  *     Will also print additional line info when lines have been skipped.
  347.  *     Special characters will come out between <>'s.  If you are unsure
  348.  *     if <>'s were in your text, count the number of characters printed.
  349.  *     Spaces on "ends" of text will come out as <SP>.
  350.  *     The actual number of characters printed depends on CHAR_START_LEN
  351.  *     and CHAR_END_LEN.
  352.  *     Will print the hex equivalents if -x option used.
  353.  */
  354. void print_chars(void)
  355. {
  356.     int i,j,k;
  357.  
  358.     print_extra_rasters();
  359.     if (opt_e == FALSE) {
  360.         if (char_count > 0) {
  361.             fprintf(ofile,"\n");
  362.             if (char_count <= CHAR_START_LEN + CHAR_END_LEN) {
  363.                 /* Only print (some of the) start characters */
  364.                 begin_or_end = TRUE;
  365.                 for (i=0; i<char_count; i++) {
  366.                     if (i == char_count-1) {
  367.                         begin_or_end = TRUE;
  368.                     }
  369.                     print_char(char_start[i]);
  370.                 }
  371.                 if (opt_x) {
  372.                     fprintf(ofile,"\n");
  373.                     for (i=0; i<char_count; i++) {
  374.                         j = 0xff & char_start[i];
  375.                         output_byte(j);
  376.                         fprintf(ofile," ");
  377.                     }
  378.                 }
  379.             }
  380.             else { 
  381.                 /* "End" array did "wrap around" */
  382.                 /* Move the "end" characters to end of "start" array */
  383.                 j = k = (char_count - CHAR_START_LEN) % CHAR_END_LEN;
  384.                 for (i=CHAR_START_LEN; k<CHAR_END_LEN; i++, k++) {
  385.                     char_start[i] = char_end[k];
  386.                 }
  387.                 for (k=0; k<j; i++, k++) {
  388.                     char_start[i] = char_end[k];
  389.                 }
  390.                 begin_or_end = TRUE;
  391.                 /* Print the first set */
  392.                 for (i=0; i<CHAR_START_LEN; i++) {
  393.                     if (i == CHAR_START_LEN-1) {
  394.                         begin_or_end = TRUE;
  395.                     }
  396.                     print_char(char_start[i]);
  397.                 }
  398.                 fprintf(ofile," ... ");
  399.                 begin_or_end = TRUE;
  400.                 /* Print the second set */
  401.                 for (i=CHAR_START_LEN; i<CHAR_START_LEN+CHAR_END_LEN; i++) {
  402.                     if (i == CHAR_START_LEN+CHAR_END_LEN-1) {
  403.                         begin_or_end = TRUE;
  404.                     }
  405.                     print_char(char_start[i]);
  406.                 }
  407.                 if (opt_x) {
  408.                     fprintf(ofile,"\n");
  409.                     for (i=0; i<CHAR_START_LEN; i++) {
  410.                         j = 0xff & char_start[i];
  411.                         output_byte(j);
  412.                         fprintf(ofile," ");
  413.                     }
  414.                     fprintf(ofile," ... ");
  415.                     for (i=CHAR_START_LEN; i<CHAR_START_LEN+CHAR_END_LEN; i++) {
  416.                         j = 0xff & char_start[i];
  417.                         output_byte(j);
  418.                         fprintf(ofile," ");
  419.                     }
  420.                 }
  421.             }
  422.             fprintf(ofile,"\n");
  423.             if (num_lines > 0) {
  424.                 indent(1);
  425.                 fprintf(ofile,"Added %d more lines.\n", num_lines);
  426.             }
  427.         }
  428.     }
  429.     num_lines = char_count = 0;
  430. }
  431.