home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / fax-3.2.1 / cmd / faxps / cvt.c next >
Encoding:
C/C++ Source or Header  |  1992-07-31  |  2.6 KB  |  115 lines

  1. /*
  2.   This file is part of the NetFax system.
  3.  
  4.   (c) Copyright 1989 by David M. Siegel. 
  5.       All rights reserved.
  6.  
  7.     This program is free software; you can redistribute it and/or modify
  8.     it under the terms of the GNU General Public License as published by
  9.     the Free Software Foundation.
  10.  
  11.     This program is distributed in the hope that it will be useful, 
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of 
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with this program; if not, write to the Free Software
  18.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20.  
  21. #include <stdio.h>
  22. #include <unistd.h>
  23. #include <sys/param.h>
  24.  
  25. #include "../../lib/libfax/libfax.h"
  26.  
  27. /*
  28.  * Count the number of pages that the postscript converter generated.
  29.  */
  30. static int count_pages(base)
  31.      char *base;
  32. {
  33.     int pages = 1;
  34.  
  35.     for (;;) {
  36.     char filename[MAXPATHLEN];
  37.     sprintf(filename, "%s.g3.%d", base, pages);
  38.     if (access(filename, R_OK) != 0)
  39.       return (pages-1);
  40.     pages++;
  41.     }
  42. }
  43.  
  44.  
  45. /*
  46.  * Convert a postscript file to a set of G3 format files, using the digifax
  47.  * driver in Ghostscript.
  48.  */
  49. int cvt_postscript_to_g3(in_file, out_base)
  50.      char *in_file;
  51.      char *out_base;
  52. {
  53.  
  54.     char program[128];
  55.     FILE *fp;
  56.  
  57.     /*
  58.      * Open ghostscript converter.
  59.      */
  60.     sprintf(program, "%s -sOUTPUTFILE=%s.g3.%%d %s", PS_PROG, out_base, in_file);
  61.     if ((fp = popen(program, "w")) == NULL)
  62.       return (-1);
  63.     
  64.     /*
  65.      * We`re all done! That was simple.
  66.      */
  67.     fprintf(fp, "quit\n");
  68.  
  69.     /*
  70.      * All done, wait for the conversion to complete.
  71.      */
  72.     pclose(fp);
  73.  
  74.     /*
  75.      * Now count the number of pages that ghostscript wrote out.
  76.      */
  77.     return (count_pages(out_base));
  78.  
  79. }
  80.  
  81.  
  82.  
  83.  
  84. int cvt_coversheet_to_g3(out_base, recipient, sender, sender_fax, pages)
  85.      char *out_base;
  86.      char *recipient;
  87.      char *sender;
  88.      char *sender_fax;
  89.      int pages;
  90. {
  91.     char ps_file[MAXPATHLEN];
  92.     FILE *fp;
  93.  
  94.     sprintf(ps_file, "%s.ps", out_base);
  95.     if ((fp = fopen(ps_file, "w+")) == NULL)
  96.       return (-1);
  97.     
  98.     fprintf(fp, "/recipient (%s) def\n", recipient);
  99.     fprintf(fp, "/sender (%s) def\n", sender);
  100.     fprintf(fp, "/returnfaxnum (%s) def\n", sender_fax);
  101.     fprintf(fp, "/pages (%d) def\n", pages+1);
  102.     fprintf(fp, "(%s) run\n", PS_TO_COVER_PROG);
  103.  
  104.     fclose(fp);
  105.  
  106.     if (cvt_postscript_to_g3(ps_file, out_base) < 0) {
  107.     unlink(ps_file);
  108.     return (-1);
  109.     }
  110.  
  111.     unlink(ps_file);
  112.  
  113.     return (0);
  114. }
  115.