/*																				*/
/*																				*/
/*				File:		  	getkey.c										*/
/*				Returns:	  	void											*/
/*				Parameters:	unit **FILE - 							 	*/
/*								file *char - filename				 	*/
/*								bottom **int - base of user memory	*/
/*								top *int	- top of useable memory		*/
/*								count *int - number of major keys	*/
/*								base **int - base of key storage		*/
/*																				*/


#include <stdio.h>

#include "advm.h"

void getkey (unit,file,bottom,top,count,base)
FILE  **unit;
char *file;
int **bottom;
int *top;
int *count;
int **base;

	{
	int point;
	int oldkey;
	int index;
	int key1 [18];
	int key2 [18];

	assert(*bottom < top);
	*base = *bottom;
	*count = index = point = 0;
	oldkey = -1;
	if (!(*unit = fopen(file,"rb")))
		{
		printf("Can't read keys from file: %s\n",file);
		exit(1);
		}
	for (;;)
		{
	 	if (index == 18) index = 0;
		index++;
		if (index == 1)
			{
			trace1("getkey: index = %d\n",point);
			if ((fseek(*unit, (long)128*point++, 0)) ||
				 (fread((char *)key1, sizeof(key1[0]), 18, *unit) != 18) ||
				 (fread((char *)key2, sizeof(key2[0]), 18, *unit) != 18))
					{
					printf("Can't read keys from file: %s\n",file);
					exit(1);
					}
			}
		array(key1, index-1);
		if (key1[index-1] == -1) return;
		if (key2[index-1] == oldkey) continue;
		oldkey = key2[index-1];
		(*count)++;
		*((*bottom)++) = key2[index-1];
		*((*bottom)++) = point;
		if (*bottom > top)
			{
			printf("Out of memory while storing keys from file: %s\n",file);
			exit(1);
			}
		}
	}

