int lookup_record_and_number(PriceFile* price_file, char *ascii_key_value, ItemRecord **item_record, unsigned *record_number) { unsigned starting_cache_index; ItemRecord *temp_cache_data; ItemRecord temp_file_data; int status; unsigned starting_file_index; FILE *dos_file_stream; unsigned current_file_index; unsigned start_looking_in_cache; unsigned stop_looking_in_cache; unsigned i; unsigned cache_replace_index; BCD *bcd_key_value; static unsigned longest_search = 0; unsigned current_search; bcd_key_value = (BCD *)malloc(strlen(ascii_key_value)); lookup_count ++; dos_file_stream = price_file->dos_file_stream; starting_cache_index = compute_cache_hash(ascii_key_value); ascii_to_BCD(bcd_key_value,ascii_key_value,ASCII_KEY_SIZE); status = QUESTIONABLE; start_looking_in_cache = compute_starting_cache_hash(starting_cache_index); stop_looking_in_cache = compute_ending_cache_hash(starting_cache_index); for (i = start_looking_in_cache; i < stop_looking_in_cache; i ++) { temp_cache_data = price_file->item_cache[i]; if (memcmp(&temp_cache_data->upc,bcd_key_value,BCD_KEY_SIZE) == 0) { status = FOUND; hit_count ++; break; } } if (status == FOUND) { *item_record = temp_cache_data; *record_number = price_file->file_record_number[i]; free(bcd_key_value); return(status); } cache_replace_index = start_looking_in_cache + (lookup_count % MAPPING_FACTOR); for (i = start_looking_in_cache; i < stop_looking_in_cache; i ++) { temp_cache_data = price_file->item_cache[i]; if (temp_cache_data->upc[0] == INVALID_BCD_VALUE) { cache_replace_index = i; /* use an invalid entry, if there is one */ break; } } starting_file_index = compute_file_hash(ascii_key_value); for (current_file_index = starting_file_index; current_file_index < FILE_SIZE; current_file_index ++) { position_record(price_file,current_file_index); fread(&temp_file_data,1,sizeof(ItemRecord),dos_file_stream); if (temp_file_data.upc[0] == INVALID_BCD_VALUE) { status = NOT_IN_FILE; break; } if (memcmp(&temp_file_data.upc,bcd_key_value,BCD_KEY_SIZE) == 0) { status = FOUND; break; } } current_search = current_file_index - starting_file_index; if (current_search > longest_search) longest_search = current_search; temp_cache_data = price_file->item_cache[cache_replace_index]; if (status == FOUND) { memcpy(temp_cache_data,&temp_file_data,sizeof(ItemRecord)); price_file->file_record_number[cache_replace_index] = current_file_index; *item_record = temp_cache_data; *record_number = current_file_index; free(bcd_key_value); return(status); } else if (status == NOT_IN_FILE) { price_file->file_record_number[cache_replace_index] = current_file_index; *item_record = temp_cache_data; *record_number = current_file_index; free(bcd_key_value); return(NOT_IN_FILE); } for (current_file_index = 0; current_file_index < starting_file_index; current_file_index ++) { position_record(price_file,current_file_index); fread(&temp_file_data,1,sizeof(ItemRecord),dos_file_stream); if (temp_file_data.upc[0] == INVALID_BCD_VALUE) { status = NOT_IN_FILE; break; } if (memcmp(&temp_file_data.upc,bcd_key_value,BCD_KEY_SIZE) == 0) { status = FOUND; break; } } if (status == FOUND) { memcpy(temp_cache_data,&temp_file_data,sizeof(ItemRecord)); price_file->file_record_number[cache_replace_index] = current_file_index; *item_record = temp_cache_data; *record_number = current_file_index; free(bcd_key_value); return(status); } else if (status == NOT_IN_FILE) { price_file->file_record_number[cache_replace_index] = current_file_index; *item_record = temp_cache_data; *record_number = current_file_index; free(bcd_key_value); return(NOT_IN_FILE); } else { free(bcd_key_value); return(FILE_FULL); } }