(file) Return to patchchk.c CVS log (file) (dir) Up to [cvs] / cct / ctex / patchchk.c

File: [cvs] / cct / ctex / patchchk.c (download)
Revision: 1.2, Fri Nov 26 11:29:07 2004 UTC (5 years, 9 months ago) by zlb
Branch: MAIN
CVS Tags: T200705050300, T200604230300, T200602050300, v0_6180_3a, T200706210300, T200608190300, T200605240300, T200611170300, v0_2_9, T200708220300, T200602060300, T200806120300, T200512210300, T200511100300, T200808300300, T200608290300, T200601100300, T200601090300, T200701200300, T200601190300, T200801290300, T200705040300, T200809020300, T200603130300, T200511090300, T200609080300, v0_6180_2, v0_6180_3, T200701300300, T200911210300, T200605260300, T200604280300, T200709300300, T200611110300, T200706190300, T200803290300, T200511200300, T200610130300, T200607150300, T200602080300, T200802140300, T200511150300, T200601270300, T200907260300, T200705090300, T200512140300, T200906250300, T200707060300, T200603120300, T200608150300, T200909030300, T200710300300, T200902140300, T201001230300, T200807220300, T200802030300, T200512180300, T200511110300, T200607170300, T200603110300, T200603050300, T200802280300, T200512150300, T200512200300, T200703090300, T200605210300, T200603010300, T200611150300, T200707100300, T200511190300, T200602210300, T200809010300, T200601060300, T200706200300, T200603090300, T200603130902, T200603130900, T200801120300, v0_61_2, T200605180300, T200907250300, T200603020300, T201008070300, T200706080300, v0_61803_0, v0_61803_1, T200602040300, T200908280300, T200705060300, T200802020300, T200608100300, T200711300300, T200610040300, T200702220300, T200603130857, T200603260300, T200609130300, T200604290300, T200603111005, T200603111003, T200601070300, T200706300300, T200610190300, T200511230300, T200707170300, T200601080300, T200706060300, T200707010300, T200906200300, T200707240300, T200609151748, T200512310300, T200704220300, T200511220300, T200602190300, T200602180300, T200603100300, v0_61803_2, T200708310300, T200611200300, T200709160300, HEAD
Changes since 1.1: +38 -32 lines
appended CVS ids

/* $Id: patchchk.c,v 1.2 2004/11/26 11:29:07 zlb Exp $ */

typedef unsigned char byte;

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "common.h"

#define dvi_nop		138
#define dvi_fnt_def1	243
#define dvi_post	248
#define dvi_post_post	249

#define factor	((double)num/(double)den*1e-5/2.54*72.27*mag*0.001)

static FILE *f_dvi = NULL;

static long num, den, mag=0l;

static long 
readNumber(int size)
/* read a number of size bytes from f_dvi. Since the byte ordering in
   a DVI file is MSB first, we have to reverse the bytes read */
{
  long ll = 0;

  while (size-- > 0)
    ll = 256L * ll + (unsigned char )(fgetc(f_dvi));

  return ll;
}

int 
patchdvi_check(char *dvi_name)
/* returns TRUE if the dvi file 'dvi_name' is in the CCT format,
   i.e., it needs to be dvipatched. */
{
  int i, b;
  long fpos, ll;
  char s[1024];

  assert ( sizeof(long) >= 4 );

  if (f_dvi != NULL) {
    fclose(f_dvi);
    f_dvi=NULL;
  }

  /* append .dvi to file name when necessary */
  if (*(get_file_ext(dvi_name)) == '\0') {
    strcpy(s, dvi_name);
    strcat(s, ".dvi");
    dvi_name = s;
  }

  if ((f_dvi=fopen(dvi_name,"rb"))==NULL) {
    fprintf(stderr,"patchdvi_check: cannot open DVI file \"%s\"!\n", dvi_name);
    return 0;
  }
  setvbuf(f_dvi, NULL, _IOFBF, 32*1024);

  fseek(f_dvi, -4l, SEEK_END);
  fpos=ftell(f_dvi);
  ll = readNumber(4);
  if (ll != 0xdfdfdfdfl) {
dvi_error:
    fprintf(stderr,"patchdvi_check: not a valid dvi file!\n");
abort:
    fclose(f_dvi);
    f_dvi=NULL;
    return 0;
  }

  /* skip trailing 0xdf's */
  do {
    fseek(f_dvi,--fpos,SEEK_SET);
    i=fgetc(f_dvi);
  } while (i==0xdf);

  /* check DVI file id. */
  if (i!=2) goto dvi_error;

  /* read "post_post" command */
  fseek(f_dvi,fpos-5l,SEEK_SET);
  if (fgetc(f_dvi)!=dvi_post_post) {
    fprintf(stderr,"patchdvi_check: \"post_post\" command not found!\n");
    goto abort;
  }

  /* goto postamble, check for the "post" command byte, and read
     some important parameters from the postamble */
  fpos = readNumber(4);
  fseek(f_dvi, fpos, SEEK_SET);
  if (fgetc(f_dvi)!=dvi_post) {
    fprintf(stderr,"patchdvi_check: \"post\" command expected here!\n");
    goto abort;
  }

  fseek(f_dvi, 4l, SEEK_CUR);		/* skip location of the last page */
  num = readNumber(4);			/* read numerator */
  den = readNumber(4);			/* read denominator */
  if (!mag) mag = readNumber(4);		/* read magnification */

  /* process font definitions */
  fseek(f_dvi,fpos+29l,SEEK_SET);
  do {
    long ssize, dsize, chksum;

    if ((b=fgetc(f_dvi))==dvi_nop) continue;
    if (b<dvi_fnt_def1 || b>=dvi_fnt_def1+4) {
      if (b!=dvi_post_post) {
	fprintf(stderr,"patchdvi_check: forbidden command in the postamble\n");
	goto abort;
      }
      break;
    }
    /* read font size and font checksum */
    ll = readNumber(b-dvi_fnt_def1+1);	/* read font no. */
    chksum = readNumber(4);
    ssize = readNumber(4);
    dsize = readNumber(4);

    /* read the font name */
    i=fgetc(f_dvi); i+=fgetc(f_dvi);	/* i=length of font name string */
    fread(s,i,1,f_dvi); s[i]='\0';

#ifdef DEBUG
    fprintf(stderr, "fontno=%-4ld  design size=%-6.4lg  scale size=%-6.4lg  "
		    "name=%s\n", ll, factor*dsize, factor*ssize, s);
#endif

    /* check for CCT's special font names */
    if (!strcmp("ccdummy",s) || !strncmp("ccfnt",s,5)) {
      fclose(f_dvi);
      f_dvi=NULL;
      return 1;
    }
  } while (1);

  fclose(f_dvi);
  f_dvi=NULL;

  return 0;
}

#ifdef TEST
char *get_file_ext(char *fn)
{
    size_t l=strlen(fn);
    char *p=fn+l-1;

#ifdef UNIX
    while (p >= fn && *p != '.' && *p != '/')
#else
    while (p >= fn && *p != '.' && *p != '/' && *p!='\\' && *p != ':')
#endif
        p--;
    if (p<fn || *p!='.') p=fn+l;

    return p;
}

int main(int argc, char *argv[])
/* This program returns 0 if dvi file is CCT format, 1 otherwise */
{
    char *dvi_name;

    if (argc != 2) {
	fprintf(stderr, "Usage: %s dvi_name\n", argv[0]);
	return 0;
    }
    dvi_name = argv[1];
    
    return !patchdvi_check(dvi_name);
}
#endif

No admin address has been configured
Powered by
ViewCVS 1.0-dev