home *** CD-ROM | disk | FTP | other *** search
/ Dream 49 / Amiga_Dream_49.iso / atari / texte / gs353b.zoo / help / hlptops.c < prev    next >
C/C++ Source or Header  |  1995-02-01  |  4KB  |  180 lines

  1. /*
  2.  *    Convert Atari Ghostscript .hlp files to Postscript
  3.  *    Tim Gallivan, 1994.
  4.  *
  5.  *    The /width and /height printed by this program depend
  6.  *    critically on the contents of the file procs.ps. I
  7.  *    don't suggest changing that file or this one unless
  8.  *    you know enough PostScript to fix things up.
  9.  */
  10.  
  11. /*     Usage: "hlptops file1.hlp [file2.hlp ...]"        */
  12.  
  13. #define MAXLEN         (81)
  14. #define MARGINS        (20)
  15. #define CHARHEIGHT    (20)
  16. #define HEADERHEIGHT    (4 * CHARHEIGHT + 15)
  17.  
  18. #define ERROR1     "hlptops usage: hlptops file1.hlp [file2.hlp ...]"
  19. #define ERROR2    "hlptops: %s does not appear to be a Ghostscript help file.\n"
  20.  
  21. #define HEADER1 "%!PS-Adobe-\n\
  22. \n\
  23. currentdict /procsdef known not {(procs.ps) runlibfile} if\n"
  24.  
  25. #define HEADER2 "/height 000 def \n\
  26. setsize\n\n"
  27.  
  28. #define TRAILER    "\nfooter showpage restoresize\n"
  29.  
  30. #include <stdio.h>
  31. #include <string.h>
  32.  
  33. main(int argc, char **argv)
  34. {
  35.     FILE *hlpfile, *psfile;
  36.  
  37.     char infile[MAXLEN], outfile[MAXLEN], line[MAXLEN], temp[MAXLEN], *ptr;
  38.  
  39.     int i, j, k, length, longest, linecount, width, height;
  40.  
  41.     fpos_t widthpos, heightpos;
  42.  
  43.     if (argc < 1) {        /* check for no input */
  44.     puts(ERROR1);
  45.     exit(-1);
  46.     }
  47.  
  48.     /* Process each file in the argument list. */
  49.  
  50.     for (i=1; i < argc; i++, linecount=0) {
  51.  
  52.     /* See if it looks like a Ghostscript hlp file. */
  53.  
  54.     if ((ptr = strstr(argv[i], "hlp")) == NULL) {
  55.         printf(ERROR2, argv[i]);
  56.         break;
  57.     }
  58.     
  59.     /* Copy the input file name and open it. */
  60.  
  61.     strcpy(infile, argv[i]);
  62.     if ((hlpfile = fopen(infile, "r")) == NULL) {
  63.         printf("hlptops: Unable to open %s!\n", infile);
  64.         continue;
  65.     }
  66.  
  67.     /* Construct the output file name and open it. */
  68.  
  69.     *ptr = '\0';
  70.     strcpy(outfile , argv[i]);
  71.     strcat(outfile, "ps");
  72.     if ((psfile = fopen(outfile, "w")) == NULL) {
  73.         printf("hlptops: Unable to open %s!\n", outfile);
  74.         fclose(psfile);
  75.         continue;
  76.     }
  77.  
  78.     /* Write the PostScript header in the output file. */
  79.  
  80.     fputs(HEADER1, psfile);
  81.     fgetpos(psfile, &widthpos);    /* get position of /width */
  82.     fputs("/width 000 def \n", psfile);
  83.     fgetpos(psfile, &heightpos);    /* get position of /height */
  84.     fputs(HEADER2, psfile);
  85.  
  86.     /* skip any blank lines at the beginning of the hlp file */
  87.  
  88.     do {
  89.         if ((ptr = fgets(line, MAXLEN, hlpfile)) == NULL) {
  90.         printf("hlptops: Unexpected end of file in %s\n", infile);
  91.         goto endloop;
  92.         }
  93.  
  94.         line[strlen(line)-1] = '\0';    /* get rid of \n */
  95.     }
  96.     while (strlen(line) == 0);
  97.  
  98.     /* Use the first non-empty line as the header. */
  99.     /* skip leading spaces */
  100.  
  101.     ++linecount;
  102.  
  103.     /* Escape any parentheses or backslashes that occur. */
  104.  
  105.     if (strchr(line, '(') || strchr(line, ')') || strchr(line, '\\')) {
  106.         for(j=0, k=0; line[j] != NULL; j++, k++) {
  107.         if (line[j] == '(' || line[j] == ')' || line[j] == '\\') {
  108.             temp[k] = '\\';
  109.             ++k;
  110.         }
  111.         temp[k] = line[j];
  112.         }
  113.         temp[k] = '\0';
  114.         strcpy(line, temp);
  115.     }
  116.  
  117.     longest = strlen(line);
  118.  
  119.     fprintf(psfile, "(%s) header\n\n", line+strspn(line, " "));
  120.     
  121.     /* Convert the remainder of the file. */
  122.  
  123.     while ((ptr = fgets((char *)line, MAXLEN, hlpfile)) != NULL) {
  124.         length = strlen(line) - 1;
  125.         line[length] = '\0';    /* get rid of \n */
  126.         ++linecount;
  127.         /* Escape any parentheses or backslashes that occur. */
  128.  
  129.         if (strchr(line, '(') || strchr(line, ')') || strchr(line, '\\')) {
  130.         for(j=0, k=0; line[j] != NULL; j++, k++) {
  131.             if (line[j] == '(' || line[j] == ')' || line[j] == '\\') {
  132.             temp[k] = '\\';
  133.             ++k;
  134.             }
  135.             temp[k] = line[j];
  136.         }
  137.         temp[k] = '\0';
  138.         strcpy(line, temp);
  139.         }
  140.  
  141.         if (strlen(line) == 0) {
  142.         if (linecount != 2) {        /* no return after title */
  143.             fputs("return\n\n", psfile);
  144.         }
  145.         }
  146.         else {
  147.         fprintf(psfile, "(%s) jnl\n", line);
  148.         }
  149.  
  150.         if (length > longest) longest = length;
  151.  
  152.     }
  153.  
  154.     endloop:
  155.  
  156.     /* Write the trailer in the output file. */
  157.  
  158.     fputs(TRAILER, psfile); 
  159.  
  160.     /* Assume the average character width is .41 * height */
  161.  
  162.     width = (int)(longest * CHARHEIGHT * .41 + 2 * MARGINS);
  163.  
  164.     fsetpos(psfile, &widthpos);    /* move back to set /width */
  165.     fprintf(psfile, "/width %d def ", width); 
  166.  
  167.     height = linecount * CHARHEIGHT + HEADERHEIGHT;
  168.  
  169.     fsetpos(psfile, &heightpos);    /* move back to set /height */
  170.     fprintf(psfile, "/height %d def ", height); 
  171.  
  172.     /* Close both files and move to next input file. */
  173.  
  174.     fclose(hlpfile);
  175.     fclose(psfile);
  176.  
  177.     }
  178.  
  179. }
  180.