#include #include #include #include #include "tiles.h" #include "strings.h" int tile_ncol; int tile_nbacks; unsigned short int (*tile_cmap)[3] = 0; unsigned char tile_pics[TILE__N][TILE_YSIZE][TILE_XSIZE]; unsigned char (*tile_backs)[TILE_YSIZE][TILE_XSIZE] = 0; static int Read(int fd, void *buf, int nb) { int left; int did; char *bp; left = nb; bp = buf; do { did = read(fd,bp,left); if (did < 0) return(-1); if (did == 0) break; left -= did; bp += did; } while (left > 0); return(nb-left); } #define Write write static int getcment(int fd, unsigned short int *ep) { unsigned char buf[6]; if (Read(fd,&buf[0],6) != 6) return(-1); ep[0] = (buf[0] << 8) | buf[1]; ep[1] = (buf[2] << 8) | buf[3]; ep[2] = (buf[4] << 8) | buf[5]; return(0); } static int putcment(int fd, unsigned short int *ep) { unsigned char buf[6]; buf[0] = ep[0] >> 8; buf[1] = ep[0] & 0xff; buf[2] = ep[1] >> 8; buf[3] = ep[1] & 0xff; buf[4] = ep[2] >> 8; buf[5] = ep[2] & 0xff; if (Write(fd,&buf[0],6) != 6) return(-1); return(0); } static int getbm_c(int fd, unsigned char (*bm)[TILE_XSIZE]) { if (Read(fd,bm,TILE_XSIZE*TILE_YSIZE) != TILE_XSIZE*TILE_YSIZE) return(-1); return(0); } static int putbm_c(int fd, unsigned char (*bm)[TILE_XSIZE]) { if (Write(fd,bm,TILE_XSIZE*TILE_YSIZE) != TILE_XSIZE*TILE_YSIZE) return(-1); return(0); } /* tiles_c would be auto [][][], except that if they are then the stack takes too big a step and we drop core. */ int tile_loaddeck(const char *path) { int errsave; int fd; unsigned char buf; int i; int ncol; int nbacks; unsigned short int (*cmap)[3]; unsigned char (*tiles_c)[TILE_YSIZE][TILE_XSIZE]; unsigned char (*backs_c)[TILE_YSIZE][TILE_XSIZE]; errno = 0; cmap = 0; tiles_c = 0; backs_c = 0; fd = open(path,O_RDONLY,0); if (fd < 0) goto err; if (Read(fd,&buf,1) != 1) goto err; ncol = buf; if (Read(fd,&buf,1) != 1) goto err; nbacks = buf; cmap = malloc(ncol*sizeof(*cmap)); if (cmap == 0) { errno = ENOMEM; goto err; } tiles_c = malloc(TILE__N*sizeof(*tiles_c)); if (tiles_c == 0) { errno = ENOMEM; goto err; } backs_c = malloc(nbacks*sizeof(*backs_c)); if (backs_c == 0) { errno = ENOMEM; goto err; } for (i=0;i= 0) close(fd); if (cmap) free(cmap); if (tiles_c) free(tiles_c); if (backs_c) free(backs_c); errno = errsave ? errsave : EIO; return(-1); } int tile_savedeck(const char *path) { int fd; unsigned char buf; int i; int errsave; off_t len; if ( (tile_ncol < 0) || (tile_ncol > 255) || (tile_nbacks < 0) || (tile_nbacks > 255) ) { errno = EINVAL; return(-1); } fd = open(path,O_RDWR|O_CREAT,0666); if (fd < 0) goto err; buf = tile_ncol; if (Write(fd,&buf,1) != 1) goto err; buf = tile_nbacks; if (Write(fd,&buf,1) != 1) goto err; for (i=0;i= 0) close(fd); errno = errsave ? errsave : EIO; return(-1); }