/*TITLE supermarket pricing file initialization */ /****keyword-flag*** "%v %f %n" */ /* "9 20-Mar-98,20:14:10 SUPINIT.CPP" */ #include #include #include "superm.h" PriceFile* initialize(char *price_file_name); void process(PriceFile* price_file,char *source_file_name, char *list_file_name); void terminate(PriceFile* price_file); int main(int argc, char *argv[]) { PriceFile* price_file; if (argc < 4) { printf("Usage: supinit source_file price_file list_file\n"); exit(1); } price_file = initialize(argv[2]); process(price_file,argv[1],argv[3]); terminate(price_file); return 0; } #define MAX_DESCRIPTIONS 100 #define MAX_DESC_LENGTH 100 PriceFile* initialize(char *price_file_name) { PriceFile *price_file; FILE *dos_file_stream; ItemRecord **temp_cache_ptr; ItemRecord *temp_cache_data; unsigned i; ItemRecord dummy_record; unsigned *temp_record_number_ptr; price_file = (PriceFile *)malloc(sizeof(PriceFile)); memset(&dummy_record,0,sizeof(ItemRecord)); dummy_record.upc[0] = (unsigned char)INVALID_BCD_VALUE; dos_file_stream = fopen(price_file_name,"w+b"); if (dos_file_stream == NULL) { printf("Can't open price file %s.\n",price_file_name); exit(1); } price_file->dos_file_stream = dos_file_stream; temp_cache_ptr = (ItemRecord **)calloc(CACHE_SIZE, sizeof(ItemRecord *)); temp_cache_data = (ItemRecord *)calloc(CACHE_SIZE, sizeof(ItemRecord)); for (i = 0; i < CACHE_SIZE; i ++) { temp_cache_ptr[i] = temp_cache_data ++; *(temp_cache_ptr[i]) = dummy_record; } price_file->item_cache = temp_cache_ptr; temp_record_number_ptr = (unsigned *)calloc(CACHE_SIZE, sizeof(unsigned)); for (i = 0; i < CACHE_SIZE; i ++) temp_record_number_ptr[i] = (unsigned)-1; price_file->file_record_number = temp_record_number_ptr; return(price_file); } void process(PriceFile* price_file,char *source_file_name, char *list_file_name) { unsigned i; int j; int k; unsigned record_number; FILE *source_file_stream; FILE *list_file_stream; char *item_description[MAX_DESCRIPTIONS]; char description[MAX_DESC_LENGTH]; char *desc_status; int desc_count; ItemRecord dummy_record; FILE *dos_file_stream; char ascii_code_number[ASCII_KEY_SIZE+1]; char *lf_pos; int status; dos_file_stream = price_file->dos_file_stream; memset(&dummy_record,0,sizeof(ItemRecord)); dummy_record.upc[0] = (unsigned char)INVALID_BCD_VALUE; for (i = 0; i < FILE_SIZE; i ++) { position_record(price_file,i); fwrite(&dummy_record,1,sizeof(ItemRecord),dos_file_stream); } source_file_stream = fopen(source_file_name,"r"); if (source_file_stream == NULL) { printf("Can't open source file %s.\n",source_file_name); exit(1); } list_file_stream = fopen(list_file_name,"w"); if (list_file_stream == NULL) { printf("Can't open list file %s.\n",list_file_name); exit(1); } for (i = 0; i < MAX_DESCRIPTIONS; i ++) { desc_status = fgets(description,MAX_DESC_LENGTH,source_file_stream); if (desc_status == NULL) break; item_description[i] = (char *)malloc(strlen(description)+1); lf_pos = strchr(description,'\n'); *lf_pos = 0; strcpy(item_description[i],description); } desc_count = i; for (i = 0; i < FILE_CAPACITY; i ++) { j = i % desc_count; k = i / desc_count; sprintf(description,"%s #%d",item_description[j],k); ascii_to_radix40(&dummy_record.description[0],(unsigned char*)description, DESCRIPTION_CHARS); sprintf(ascii_code_number,"%010ld",(long)rand()+i); ascii_to_BCD(&dummy_record.upc[0],ascii_code_number,ASCII_KEY_SIZE); fprintf(list_file_stream,"%s %s\n",ascii_code_number,description); dummy_record.price = 100*j+k; status = lookup_record_number(price_file,ascii_code_number, &record_number); if (status == NOT_IN_FILE) { position_record(price_file,record_number); fwrite(&dummy_record,1,sizeof(ItemRecord),dos_file_stream); } else if (status == FILE_FULL) { printf("Cannot add entry for item #%s to file\n",ascii_code_number); break; } else if (status == FOUND) printf("Item #%s already in file\n",ascii_code_number); } fclose(dos_file_stream); } void terminate(PriceFile* price_file) { FILE *dos_file_stream; dos_file_stream = price_file->dos_file_stream; fclose(dos_file_stream); }