/* menu.c    Demonstration of an array of structs  */
/* P. Conrad for CISC105, Fall 2004 */


#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MENU_ITEM_NAME_LEN 20
#define NUM_MENU_ITEMS 50

enum MenuCat_E
  {
    MENUCAT_APPETIZER = 0,
    MENUCAT_SALAD = 1,
    MENUCAT_MAINCOURSE = 2,
    MENUCAT_SIDEDISH = 3,
    MENUCAT_DESSERT = 4,
    MENUCAT_BEVERAGE = 5,
    NUM_MENUCATS = 6    
  }; /* notice semicolon after } */

struct MenuItem_S
{
  char name[20]; /* @@@ replace with MENU_ITEM_NAME_LEN */
  enum MenuCat_E cat;
  int price; /* in cents */

}; /* notice semicolon after } */

#define INPUT_LINE_LEN 256

void searchForMenuItemsUntilQuit(struct MenuItem_S menuItems[], 
				 int count);

void printAllMenuItems(struct MenuItem_S menuItems[], 
		       int count);


int main(void)
{
  struct MenuItem_S menuItems[50]; /* what's wrong here? */
 
  char inputline[INPUT_LINE_LEN];

  /* pointers to various parts of the input line*/

  char *namePart;
  char *catPart;
  char *pricePart;
  
  int count; /* how many menu items are there */
  int i;

  FILE *infile;

  infile = fopen("menu.dat","r");

  if (infile==NULL)
    {
      fprintf(stderr,"Sorry, could not open menu.dat\n");
      exit(-1);
    }

  /* note: with fgets, you test against NULL, not EOF! */
  /* see man fgets for more information */

  count = 0;
  while ( fgets(inputline, INPUT_LINE_LEN, infile) != NULL)
    {
      printf("I just read %s from the input file\n", inputline);
      /* add code to break this line into pieces and put it in the struct */

      namePart = strtok(inputline,",");
      catPart = strtok(inputline,",");
      pricePart = strtok(inputline,",");
      
      strncpy(menuItems[count].name,namePart,20);
      menuItems[count].cat = (enum MenuCat_E ) atoi(catPart);
      menuItems[count].price = atoi(pricePart);
      
      count++;
    }


  /* print all the menu items */

  printAllMenuItems(menuItems, count);

  /* add code to allow user to search for menu items until they type in quit */

  searchForMenuItemsUntilQuit(menuItems,count);


  return 0;

}

void printAllMenuItems(struct MenuItem_S menuItems[], 
		       int count)
{
  
  int i;

  /* Now print out all the menu items that were read in */
  
  printf("%10s %10s %10s\n","Name","Category","Price");
  printf("%10s %10s %10s\n","----","--------","-----");
  
  /* for (i=0; i<  ???? ; i++) */
  
  printf("%10s %10d %10d\n",
	 menuItems[i].name, 
	 menuItems[i].cat, 
	 menuItems[i].price);
  
}


void searchForMenuItemsUntilQuit(struct MenuItem_S menuItems[], 
				 int count)
{


}

