/* "Patch" fix SnipIt to use a different keymap */
/* 9/1/88 - Scott Evernden */

#include <stdio.h>
#include <ctype.h>

FILE *infile, *datafile;

/* original keycode table starts like this... */
unsigned char match[] = {
    0x40, 0x81, 0xAA, 0x83, 0x84, 0x85, 0x87, 0x2A,
    0x89, 0x8A, 0x88, 0x8C, 0x38, 0x0B, 0x39, 0x3A,
    0x0A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0
};

main()
{
	long pos, ftell();
	unsigned char *mp;
	int ch, i;

	/* gonna write into this file */
	infile = fopen("SnipIt", "r+");
	if (!infile) {
		puts("Can't open \"SnipIt\"");
		exit(0);
	}

	/8 need to read this one */
	datafile = fopen("SnipIt.keys", "r");
	if (!datafile) {
		puts("Can't open \"SnipIt.keys\"");
		exit(0);
	}

	/* locate original table */
	mp = match;
	while (*mp && (ch = fgetc(infile)) != EOF) {
		if (ch != *mp)
			mp = match;
		else if (mp++ == match)
			pos = ftell(infile) - 1;
	}

	if (ch == EOF) {
		puts("Are you sure this is an original copy of \"SnipIt\"?");
		fclose(infile);
		fclose(datafile);
		exit(0);
	}

	fputs("Patching keytable...", stdout);

	fseek(infile, pos, 0L);

	for (i = 32; i < 127; i++)
		fputc(innum() & 0xFF, infile);

	fclose(infile);
	fclose(datafile);
	puts("done");
}


innum()
{
	int result;
	int ch;

	do {
		ch = fgetc(datafile);
	} while (!isxdigit(ch));

	if (ch == EOF)
		return 0;

	result = 0;
	do {
		ch -= '0';
		if (9 < ch)
			ch -= 7;
		if (15 < ch)
			ch -= 32;
		result = result * 16 + ch;
		ch = fgetc(datafile);
	} while (isxdigit(ch));

	return result;
}


