/*
 * bsearch.c
 *
 *  Created on: 27-apr-2009
 *      Author: cattaneo
 */


#include <ourhdr.h>
#include "myheader.h"

Record *binSearch(char * nome)
{
	Indice *ptr = NULL;
	Record buf;
	int r;
	off_t pos;

	ptr = (Indice *) bsearch( (void *)nome, (void *) Ndx, NRecord,
			sizeof(Indice), MyCompare);
	if (ptr == NULL)
	{
//		err_sys("nome non trovato");
		return NULL;
	}
	lseek( fd, ptr->posizione, SEEK_SET);
	if ((r = read (fd, &buf, sizeof(Record))) < 0)
			err_sys("Reading record");

	return &buf;
}

Record *RicercaSequenziale( char * nome)
{
	static Record buf;
	int r;
	off_t pos;

	if((pos = lseek (fd, (off_t) 0, SEEK_SET)) < 0)
		err_sys("Seeking:");
	do
	{
		r = read (fd, &buf, sizeof(Record));
		if (r < 0 )
			err_sys("Reading");

		if (r == 0)
			return NULL;	// EOF raggiunta

		if (strcmp (nome, buf.nome) == 0)
			return &buf;
	} while( 1);
}

