#include #include #include #include extern const char *__progname; static int npuzzles = 0; static char **filenames = 0; static char ochars[16] = "0123456789ABCDEF"; static char (*puzzles)[16][16]; static int load_puzzle(const char *name, char (*puzzle)[16][16]) { int y; int x; int ch; FILE *f; f = fopen(name,"r"); if (f == 0) { fprintf(stderr,"%s: %s: %s\n",__progname,name,strerror(errno)); return(1); } for (y=0;y<16;y++) { for (x=0;x<16;x++) { while <"cell"> (1) { ch = getc(f); if (ch == EOF) { fprintf(stderr,"%s: %s: EOF in mid-puzzle\n",__progname,name); fclose(f); return(1); } switch (ch) { case '.': ch = 0; break <"cell">; case '0': ch = 1; break <"cell">; case '1': ch = 2; break <"cell">; case '2': ch = 3; break <"cell">; case '3': ch = 4; break <"cell">; case '4': ch = 5; break <"cell">; case '5': ch = 6; break <"cell">; case '6': ch = 7; break <"cell">; case '7': ch = 8; break <"cell">; case '8': ch = 9; break <"cell">; case '9': ch = 10; break <"cell">; case 'A': ch = 11; break <"cell">; case 'B': ch = 12; break <"cell">; case 'C': ch = 13; break <"cell">; case 'D': ch = 14; break <"cell">; case 'E': ch = 15; break <"cell">; case 'F': ch = 16; break <"cell">; } } puzzle[0][x][y] = ch; } } fclose(f); return(0); } static void load_puzzles(void) { int errs; int i; puzzles = malloc(npuzzles*sizeof(*puzzles)); errs = 0; for (i=0;i 16)) abort(); return(ochars[f-1]); } static void genplot(double atx, double aty, int inx) { int ix; int iy; int i; int j; int x; int y; int c; /* Puzzles are placed 420 (17.5*24) from one another; atx=.5 -> [ix..ix+384] is centred around 306 (ix=114); .5*420+k=114 gives k=-96. */ ix = (atx * 420) - 96; /* Puzzles are placed 420 (17.5*24) from one another; aty=.5 -> [iy..iy-384] is centred around 396 (iy=588); k-.5*420=588 gives k=798. */ iy = 798 - (aty * 420); for (i=0;i<16;i+=4) { for (j=1;j<=3;j++) { printf("%d %d drmt %d %d drlt\n", ix+((i+j)*24),iy,ix+((i+j)*24),iy-384); printf("%d %d drmt %d %d drlt\n", ix,iy-((i+j)*24),ix+384,iy-((i+j)*24)); } } printf(".1 setlinewidth stroke\n"); printf("%d %d drmt %d %d drlt %d %d drlt %d %d drlt closepath\n", ix,iy,ix+384,iy,ix+384,iy-384,ix,iy-384); for (j=1;j<=3;j++) { printf("%d %d drmt %d %d drlt %d %d drmt %d %d drlt\n", ix+(j*96),iy,ix+(j*96),iy-384,ix,iy-(j*96),ix+384,iy-(j*96)); } printf(".75 setlinewidth stroke\n"); for (x=0;x<16;x++) { for (y=0;y<16;y++) { c = puzzles[inx][x][y]; if (c == 0) continue; printf("%d %d drmt (%c) cs\n",ix+(x*24)+12,iy-((y*24)+19),och(c)); } } } static void pagetail(void) { printf("showpage\n"); } static void jobtail(void) { } static void page1(int inx) { pagehead(); genplot(.5,.5,inx); pagetail(); } static void page2(int inx) { pagehead(); genplot(.5,0,inx); genplot(.5,1,inx+1); pagetail(); } static void plot_them(void) { int left; int inx; jobhead(); left = npuzzles; inx = 0; while (left >= 2) { page2(inx); inx += 2; left -= 2; } switch (left) { case 0: break; case 1: page1(inx); break; } jobtail(); } static void handleargs(int ac, char **av) { int skip; int errs; skip = 0; errs = 0; for (ac--,av++;ac;ac--,av++) { if (skip > 0) { skip --; continue; } if (**av != '-') { npuzzles ++; filenames = realloc(filenames,npuzzles*sizeof(*filenames)); filenames[npuzzles-1] = *av; continue; } if (0) { needarg:; fprintf(stderr,"%s: %s needs a following argument\n",__progname,*av); errs = 1; continue; } #define WANTARG() do { if (++skip >= ac) goto needarg; } while (0) if (!strcmp(*av,"-chars")) { WANTARG(); if (strlen(av[skip]) == 16) { bcopy(av[skip],&ochars[0],16); } else { fprintf(stderr,"%s: -chars string must be 16 chars long\n",__progname); errs = 1; } continue; } #undef WANTARG fprintf(stderr,"%s: unrecognized option `%s'\n",__progname,*av); errs = 1; } if (errs) exit(1); } int main(int, char **); int main(int ac, char **av) { handleargs(ac,av); load_puzzles(); plot_them(); exit(0); }