/* Generated by tgr */

#import "FaxView.h"
#include <string.h>

@implementation FaxView

extern char *faxname ;
static int maxpage ;
+ new {
   static NXRect fr = { { 0.0, 0.0 } , { 10.0, 400.0 } } ;

   self = [[super new] initFrame:&fr] ;
   return self ;
}

- faxPSCode:sender {
   return [super faxPSCode:sender] ;
}
static FILE *f ;
#define LINELEN (400)
static char line[LINELEN] ;
- beginPrologueBBox:(const NXRect *)boundingBox creationDate:(const char *)date
   createdBy:(const char *)anApplication fonts:(const char *)fontNames
   forWhom:(const char *)user pages:(int)numPages title:(const char *)aTitle {
   int i ;
   int numtopass = 0 ;
   char *tp ;
   char tbuf[20000] ;
   char *ignored[100] ;
   extern void error() ;
   extern char *strcpy() ;
   NXRect bb ;

   tp = tbuf ;
   if (boundingBox)
      bb = *boundingBox ;
   f = fopen(faxname, "r") ;
   if (f == 0) {
      [super beginPrologueBBox:boundingBox creationDate:date
         createdBy:anApplication fonts:fontNames
         forWhom:user pages:numPages title:aTitle] ;
      return self ;
   }
   fgets(line, LINELEN, f) ;
   while (fgets(line, LINELEN, f)) {
      if (strncmp(line, "%%EndComments", 13)==0)
         break ;
      if (strlen(line) + tp > tbuf + 19990)
         error("! too many header comments?") ;
      strcpy(tp, line) ;
      if (strncmp(line, "%%Creator: ", 11) == 0) {
         anApplication = tp + 11 ;
      } else if (strncmp(line, "%%Title: ", 9) == 0) {
         aTitle = tp + 9 ;
      } else if (strncmp(line, "%%CreationDate: ", 16) == 0) {
         date = tp + 16 ;
      } else if (strncmp(line, "%%For: ", 7) == 0) {
         user = tp + 7 ;
      } else if (strncmp(line, "%%Pages: ", 9) == 0) {
         int nump ;
         sscanf(tp + 9, "%d", &nump) ;
         numPages = nump ;
         maxpage = nump ;
      } else if (strncmp(line, "%%BoundingBox: ", 15) == 0) {
         int llx, lly, urx, ury ;
         sscanf(tp + 15, "%d %d %d %d", &llx, &lly, &urx, &ury) ;
         bb.origin.x = llx ;
         bb.origin.y = lly ;
         bb.size.width = urx - llx ;
         bb.size.height = ury - lly ;
         boundingBox = &bb ;
      } else {
         ignored[numtopass++] = tp ;
      }
      tp = tp + strlen(tp) - 1 ;
      *tp++ = 0 ;
   }
   [super beginPrologueBBox:boundingBox creationDate:date
      createdBy:anApplication fonts:(const char *)""
      forWhom:user pages:numPages title:aTitle] ;
   [super endHeaderComments] ;
   for (i=0; i<numtopass; i++)
      DPSPrintf(DPSGetCurrentContext(), "%s\n", ignored[i]) ;
   while (fgets(line, LINELEN, f)) {
/* we're at the `real' trailer if we hit the %%Trailer comment with
   less than 100 bytes to go.  This is a kludge but it works for dvips
   output, which is the only output we care about.  Note that this code
   will seek for every %%Trailer seen, but that shouldn't be too often. */
      if (strncmp(line, "%%Trailer", 9) == 0) {
         char dummy[100] ;
         long t = ftell(f) ;
         if (fread(dummy, 1, 100, f) < 100) {
            fseek(f, t, 0) ;
            break ;
         }
         fseek(f, t, 0) ;
      }
      DPSPrintf(DPSGetCurrentContext(), "%s", line) ;
   }
/*
 *   Now we disable the coversheet page number, after printing our own.
 *   Serious kludge! 
   DPSPrintf(DPSGetCurrentContext(), "%%%%Page: Coversheet %d\n%%%%%%",
                      numPages+1) ; */
   return self ;
}
- endHeaderComments {
   return self ;
}

- endPrologue {
   return self ;
}
- beginSetup {
   return self ;
}
- endSetup {
   return self ;
}
- beginPageSetupRect:(const NXRect *)ar placement:(const NXPoint *)loc {
   return self ;
}
- endPageSetup {
   return self ;
}
- beginPage:(int)ord label:(const char *)as bBox:(const NXRect *)pr
    fonts:(const char *)fn {
   return self ;
}
- endPage {
   return self ;
}
- beginTrailer {
   if (f) {
      DPSPrintf(DPSGetCurrentContext(), "%s", line) ;
      while (fgets(line, LINELEN, f))
         DPSPrintf(DPSGetCurrentContext(), "%s", line) ;
      fclose(f) ;
      f = 0 ;
   }
   return self ;
}
- endTrailer {
   return self ;
}
- copyPSCodeInside:(const NXRect *)rect to:(NXStream *)stream {
   return self ;
}

- (BOOL)knowsPagesFirst:(int *)fpn last:(int *)lpn {
   return YES ;
}
- (BOOL)getRect:(NXRect *)theRect forPage:(int)page {
   if (page > maxpage)
      return NO ;
   theRect->origin.x = 0 ;
   theRect->size.width = 10.0 ;
   theRect->origin.y = 400.0 * (page - 1) / (maxpage + 1) ;
   theRect->size.height = 400.0 / (maxpage + 1) ;
   return YES ;
}

@end
