

/* Add, change, or delete for a "sales stock" file.... */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <process.h>


#include "i_f_eng.c"

#define FLOAT_LENGTH 10

struct stock_record_type  {
	char number[12];   /* This is the only key. */
	char quantity[FLOAT_LENGTH];
	char description[30];
	char package_type[2];
	char list_price[FLOAT_LENGTH];
	char cost[FLOAT_LENGTH];
	char flag[8];   /* An array of 8 different 1-char. flags. */
	}  stock_record;

char stock_file_name[]="STOCK.IFE";    /* File related variables. */
int  errcode,stock_file_handle;

char temp_string[132];


/*---------------------------- Subroutines --------------------------------*/

int get_numeric_input()
{
char the_input[8];
int temp;
gets(the_input);
temp = atoi(the_input);
return(temp);
}

void get_floating_input(char the_destination[])
{
float temp_float;
char temp_string[FLOAT_LENGTH+1];
while (1)         /* Keep going until we get a good number. */
	{
	scanf("%f",temp_float);
	if ( (!(temp_float < 0)) && (temp_float < 1000000) )  {
		sprintf(temp_string,"%f",temp_float);
		strncpy(the_destination,temp_string,FLOAT_LENGTH);
		break;  }
	printf("?Invalid input; please reenter: ");
	}
}


void get_number()
{
printf("                        ************\n");
printf("Enter the stock number: ");
gets(temp_string);
memset(stock_record.number,0,sizeof(stock_record.number));
strncpy(stock_record.number,temp_string,sizeof(stock_record.number));
}

void get_name_person()
{
printf("Enter the person's name as LAST, FIRST MIDDLE (you have the whole line):\n");
gets(temp_string);
memset(stock_record.number,0,sizeof(stock_record.number));
strncpy(stock_record.number,temp_string,sizeof(stock_record.number));
}

void get_quantity()
{
printf("Enter the quantity: ");
get_floating_input(stock_record.quantity);
}

void get_description()
{
printf("                       ******************************\n");
printf("Enter the description: ");
gets(temp_string);
memset(stock_record.description,0,sizeof(stock_record.description));
strncpy(stock_record.description,temp_string,sizeof(stock_record.description));
}

void get_package_type()
{
printf("                        **\n");
printf("Enter the package type: ");
gets(temp_string);
memset(stock_record.package_type,0,sizeof(stock_record.package_type));
strncpy(stock_record.package_type,temp_string,sizeof(stock_record.package_type));
}

void get_list_price()
{
printf("Enter the list price: ");
get_floating_input(stock_record.list_price);
}

void get_cost()
{
printf("Enter the cost: ");
get_floating_input(stock_record.cost);
}

void get_flag()
{
printf("                   12345678\n");
printf("Enter all 8 flags: ");
gets(temp_string);
memset(stock_record.flag,0,sizeof(stock_record.flag));
strncpy(stock_record.flag,temp_string,sizeof(stock_record.flag));
}


void show_stock_record()
{
printf("\n\n\n");
memset(temp_string,0,sizeof(temp_string));
strncpy(temp_string,stock_record.number,sizeof(stock_record.number));
printf("0.  Stock number:   %s\n",temp_string);
memset(temp_string,0,sizeof(temp_string));
strncpy(temp_string,stock_record.quantity,sizeof(stock_record.quantity));
printf("1.  Quantity:  %s\n",temp_string);
memset(temp_string,0,sizeof(temp_string));
strncpy(temp_string,stock_record.description,sizeof(stock_record.description));
printf("2.  Description: %s\n",temp_string);
memset(temp_string,0,sizeof(temp_string));
strncpy(temp_string,stock_record.package_type,sizeof(stock_record.package_type));
printf("3.  Package type: %s\n",temp_string);
memset(temp_string,0,sizeof(temp_string));
strncpy(temp_string,stock_record.list_price,sizeof(stock_record.list_price));
printf("4.  List price: %s\n",temp_string);
memset(temp_string,0,sizeof(temp_string));
strncpy(temp_string,stock_record.cost,sizeof(stock_record.cost));
printf("5.  Cost:  %s\n",temp_string);
memset(temp_string,0,sizeof(temp_string));
strncpy(temp_string,stock_record.flag,sizeof(stock_record.flag));
printf("6.  Flags:       %s\n",temp_string);
}


/*---------------------------- Procedures ---------------------------------*/

void add_stock()
{
printf("\n\n--- Adding to stock file ---\n\n");
memset(&stock_record,0,sizeof(stock_record));  /* Initialize to nulls. */
get_number();
get_quantity();
get_description();
get_package_type();
get_list_price();
get_cost();
get_flag();
errcode = indexed_file(&stock_file_handle,I_F_WRITE,&stock_record,0);
if (errcode)  {
	printf("?Error writing record to stock file; error code: %d\n",errcode);
	exit(1);  }
printf("Stock record added!\n\n");
}


void inquire_stock()
{
printf("\n\n--- Stock file inquiry ---\n\n");
get_number();
errcode = indexed_file(&stock_file_handle,I_F_START,&stock_record,0);
if (errcode)  {
	printf("*No number >= that one*\n\n");
	return;  }
while (!indexed_file(&stock_file_handle,I_F_READNEXT,&stock_record,0))
	{
	show_stock_record();
	printf("'C'hange, 'D'elete, 'Q'uit or <Return> for next item: ");
	gets(temp_string);
	if (toupper(temp_string[0]) == 'Q')  /* Quit? */
		break;
	if (toupper(temp_string[0]) == 'D')  /* Delete this record? */
		{
		printf("Really delete this record? ");
		gets(temp_string);
		if (toupper(temp_string[0]) == 'Y')
			{
			errcode = indexed_file(&stock_file_handle,I_F_DELETE,&stock_record,0);
			if (errcode)  {
				printf("?Error deleting stock record; error code: %d\n",errcode);
				exit(1);  }
			printf("Stock record deleted!\n\n");
			}
		}
	if (toupper(temp_string[0]) == 'C')  /* Change this record? */
		{
		while (1)  /* Change loop. */
			{
			show_stock_record();
			printf("\n'E'nd (save changes), 'Q'uit, or field to change: ");
			gets(temp_string);
			switch(toupper(temp_string[0]))
				{
				case 'E': errcode = indexed_file(&stock_file_handle,I_F_REWRITE,&stock_record,0);
						  if (errcode)  {
							  printf("?Invalid rewrite in change menu; error code: %d\n",errcode);
							  exit(1);  }
						  return;
				case 'Q': return;
				case '0': get_number();       break;
				case '1': get_quantity();     break;
				case '2': get_description();  break;
				case '3': get_package_type(); break;
				case '4': get_list_price();   break;
				case '5': get_cost();         break;
				case '6': get_flag();         break;
				}
			}
		}
	/* Go read the next record in the file. */
	}
printf("\n\n");
}


/*--------------------------- The Code -------------------------------------*/
int main()
{
char menu_command[64];

/* Open file and display opening banner. */
errcode = indexed_file(&stock_file_handle,I_F_OPEN_IO,stock_file_name,0);
if (errcode)
	{
	printf("?Error opening stock file %s; aborting....\n",stock_file_name);
	printf("<Return> to continue: ");
	gets(temp_string);
	if (!strcmp(temp_string,"init_files"))  {
		strcpy(I_F_FILE_ATTRIBUTES,"1716 3 0 80 1 80 80 1 412 12 1 ");
		errcode = indexed_file(&stock_file_handle,I_F_OPEN_WRITE,stock_file_name,0);
		printf("Stock file creation error code: %d\n",errcode);
		exit(1);                            }
	indexed_file(&stock_file_handle,I_F_CLOSE,"",0);
	exit(1);
	}

while (1)      /* Accept main menu choices until done. */
	{
	printf("\n\n\n");
	printf("Stock File Operations\n");
	printf("---------------------\n");
	printf("A. Add item\n");
	printf("I. Inquire/change/delete\n");
	printf("E. End program\n");
	printf("\nYour choice? ");
	gets(menu_command);
	printf("\n\n");
	switch (toupper(menu_command[0]))
		{
		case 'A': add_stock();     break;
		case 'I': inquire_stock(); break;
		}
	if (toupper(menu_command[0]) == 'E')
		break;
	}

errcode = indexed_file(&stock_file_handle,I_F_CLOSE,"",0);  /* Close & end. */
if (errcode)
	{
	printf("?Error closing stock file; call EUGENE!\n");
	exit(2);
	}
printf("\nBye!\n");
return(0);
}

