#include #include #include #include #include "cards.h" extern const char *__progname; typedef struct ops OPS; struct ops { void (*save_cmap)(int, unsigned short int (*)[3]); void (*save_bw)(void); void (*save_col)(int); } ; #define SPLITOPS(name) {\ &save_cmap_##name, \ &save_bw_##name, \ &save_col_##name, \ } static char *deckdir; static char *path; static char *tmp; /* last char of cvec is used for nonexistent colors */ static char cvec_[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ?"; #define MAXCOLORS ((sizeof(cvec_)/sizeof(cvec_[0]))-2) static char *cvec; static const char card_vals[] = "A23456789TJQK"; static const char card_suit[] = "CDHSRB"; static const char *card_xdeck[] = { "j" }; static void alloctemp(void) { int len; len = strlen(deckdir); path = malloc(len+64); tmp = path + len; strcpy(path,deckdir); *tmp++ = '/'; } static FILE *xfopen(const char *path, const char *mode) { FILE *rv; rv = fopen(path,mode); if (rv) return(rv); fprintf(stderr,"%s: can't open %s for %s\n",__progname,path,('w'==*mode)?"write":"read"); exit(1); } static void nlscan(FILE *f) { int c; while (1) { c = getc(f); if (c == '\n') break; if (c == EOF) { fprintf(stderr,"%s: premature EOF on %s\n",__progname,path); exit(1); } } } static void make_bw(void) { FILE *f; int x; int y; int c; int b; f = xfopen(path,"r"); for (y=0;y= i) { i += 8; cvec = realloc(cvec,i+1); colors = realloc(colors,i*sizeof(*colors)); } cvec[nc] = c; if (fscanf(f,"%x%x%x",&r,&g,&b) != 3) break; colors[nc][0] = r; colors[nc][1] = g; colors[nc][2] = b; nc ++; nlscan(f); } fclose(f); cvec[nc] = '\0'; if (nc < 1) { fprintf(stderr,"%s: no colors\n",__progname); exit(1); } nb = 0; while (1) { sprintf(tmp,"bb%d",nb+1); if (access(path,R_OK) < 0) break; nb ++; } if (nb < 1) { fprintf(stderr,"%s: no backs\n",__progname); exit(1); } putchar(nc); putchar(nb); for (i=0;i>8); putchar(colors[i][c]&0xff); } } for (i=0;i MAXCOLORS) { fprintf(stderr,"%s: can't handle decks with more than %d colors\n",__progname,(int)MAXCOLORS); exit(1); } colours = malloc(ncol*sizeof(*colours)); for (i=0;isave_cmap)(ncol,colours); free(colours); for (i=0;isave_bw)(); } sprintf(tmp,"bb%d",i+1); unlink(path); for (i=0;isave_bw)(); } for (;isave_bw)(); } for (i=0;isave_col)(ncol); } sprintf(tmp,"cb%d",i+1); unlink(path); for (i=0;isave_col)(ncol); } for (;isave_col)(ncol); } } static void save_cmap_text(int ncol, unsigned short int (*cols)[3]) { int i; FILE *f; strcpy(tmp,"c"); f = xfopen(path,"w"); for (i=0;i>= 1; } putc('\n',f); } fclose(f); } static void save_col_text(int ncol) { FILE *f; int x; int y; int c; f = xfopen(path,"w"); for (y=0;y=ncol;i--) { ppmcm_cols[i][0] = 0; ppmcm_cols[i][1] = 0; ppmcm_cols[i][2] = 0; } for (i=ncol-1;i>=0;i--) { ppmcm_cols[i][0] = cols[i][0] >> 8; ppmcm_cols[i][1] = cols[i][1] >> 8; ppmcm_cols[i][2] = cols[i][2] >> 8; } } static void save_bw_ppm(void) { FILE *f; int x; int y; int c; int b; f = xfopen(path,"w"); fprintf(f,"P1\n%d %d\n",CARD_XSIZE,CARD_YSIZE); for (y=0;y>= 1; } putc('\n',f); } fclose(f); } static void save_col_ppm(int ncol) { FILE *f; int x; int y; int c; f = xfopen(path,"w"); fprintf(f,"P3\n%d %d\n255\n",CARD_XSIZE,CARD_YSIZE); for (y=0;y