Code:
#ifndef FTT_H
#define FTT_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define IDLEN 8
#define NAMELEN 20
#define DESCLEN 256
#define EXTRACHARS 2
#define SEPARATOR "|"
/* the different denominations of money in existence there are 11 -
* 5c, 10c, 20c, 50c, $1, $2, $5, $10, $20, $50, $100
*/
#define NUMDENOMS 11
/*define a boolean datatype where FALSE is 0 and TRUE is non-zero
*/
typedef enum truefalse
{
FALSE=0, TRUE
} BOOLEAN;
/* money type is used in the system to represent dollars and cents without
* using a floatign point type such as float or double
*/
typedef struct money
{
unsigned dollars;
unsigned cents;
}MoneyType;
/* denomtype is used in the system to represent a particular denomination
* of money. the value is how much a single item in this denomination would
* be worth (5 for 5 cents, 10 for 10 cents, etc)
*/
typedef struct denom
{
unsigned value;
unsigned count;
}DenomType;
/* ItemType contains the base information about each item on the menu.
* It only contains the information for one item and has nothing about
* which menu category it will be on. This is because we want to allow
* a single item to be on more than one menu category
*/
typedef struct itemType
{
char id[IDLEN + EXTRACHARS];
char name[NAMELEN + EXTRACHARS];
char desc[DESCLEN + EXTRACHARS];
MoneyType price;
} ItemType;
typedef ItemType * ItemPtr;
/* ItemContainerType is the 'holder' for each Item in memory. We have used
* this so that one item can be listed under multiple categories as this
* was one of Fred's requirements for his menu system
*/
typedef struct itemContainer
{
ItemPtr item;
struct itemContainer * next;
} ItemContainerType;
typedef ItemContainerType * ItemContainerPtr;
/* CategoryType is the list type which is at the top of the list hierarchy.
*/
typedef struct catType
{
char id[IDLEN + EXTRACHARS];
char name[NAMELEN + EXTRACHARS];
char desc[DESCLEN + EXTRACHARS];
ItemContainerPtr firstItem;
struct catType * next;
} CategoryType;
typedef CategoryType* CategoryPtr;
/* fttType is the datatype that you will be passing around the system as
* the main datatype parameter. It is the link into the rest of the
* list hierarchy.
*/
typedef struct ftt
{
CategoryPtr categoryHead;
unsigned categoryCount;
DenomType cashTypes[NUMDENOMS];
}fttType;
typedef fttType * fttTypePtr;
#include "ftt_options.h"
/*LOAD DATA*/
BOOLEAN LoadData(fttTypePtr ftt, char* categoryFile, char* itemFile,
char* moneyFile)
{
/* FILE *readfile;
char input[80];
char ch;
char *categoryInput=NULL;
readfile = fopen(categoryFile, "r");
if (readfile == NULL) {
printf("Unable to open file.");
}
while(fgets(input, 80, readfile)!=NULL) {
categoryInput=strtok(input,"|");
while(categoryInput!=NULL){
printf( "%s\n", categoryInput );
categoryInput = strtok( NULL, "|" );
}
}*/
CategoryType *currentCat, *prevCat, *newCat;
ItemContainerType *currentItem, *prevItem, *newItem;
ItemType *current, *prev, * new;
/*MoneyType *newmoney;*/
FILE *fp1;
FILE *fp2;
char *token, *line, *token2, *line2;
char array[800];
char array2[800];
unsigned d;
double temp;
int i,j;
char nc_array[500];
/* Open menu file for reading. */
fp1 = fopen(categoryFile, "r");
/* check if fp1 menu file exists*/
if(fp1 == NULL)
{
printf("file %s does not exist \n", categoryFile);
exit(0);
}
/* initialize the previous node to NULL*/
prevCat = NULL;
while((line = fgets(array, 800, fp1)) != NULL)
{
/* allocate memory for CategoryType pointer*/
newCat = malloc(sizeof(CategoryType));
/* check if memory allocation succeeded if fails exit*/
if(newCat == NULL)
{
printf("Memory Allocation error for newCat\n");
exit(0);
}
/* if the prevCat is NULL point the new node to the
start of the list*/
if(prevCat == NULL)
{
ftt->categoryHead = newCat;
}
/* if it isnt at the start get the next -1node*/
else
{
prevCat->next = newCat;
}
/* tokenize the Pipe and copy the field
pointers into the struct*/
token = strtok(line, "|");
if(token == NULL)
{
printf("CatID missing\n");
}
else
strcpy(newCat->id, token);
/*token = strtok(NULL, "|");
newCat->drinkType = token[0];*/
token = strtok(NULL, "|");
if(token == NULL)
{
printf("Cat Name missing\n");
}
else
strcpy(newCat->name, token);
token = strtok(NULL, "|");
if(token == NULL)
{
printf("Cat desc missing\n");
}
else
strcpy(newCat->desc, token);
/* initialize everything to a safe state*/
newCat->next = NULL;
newCat->firstItem= NULL;
prevCat = newCat;
}
/* the current pointer points to headCategory*/
currentCat = ftt->categoryHead;
/* for testing purposes traverse through the list
and print out linked list*/
/*printf("Checking to see if updated\n");
while(currentCat != NULL)
{
printf("\n%s, %s, %s", currentCat->id,
currentCat->name,
currentCat->desc);*/
/* get the next row*/
/* currentCat = currentCat->next;
}*/
/* close the first pointer to the file*/
/* check if it can close it otherwise error*/
/* if(fclose(fp1)!=0)
{
fprintf(stderr, "cannot close file\n");
}*/
fclose(fp1);
/* Open submenu file for reading. */
fp2 = fopen(itemFile, "r");
/* check if sub menu file exists*/
if(fp2 == NULL)
{
printf("file %s does not exist \n", itemFile);
exit(0);
}
/*intialize pointer to the start*/
prevItem = NULL;
currentCat = ftt->categoryHead;
while((line = fgets(array, 800, fp2)) != NULL)
{
newItem = malloc(sizeof(ItemContainerType));
new=malloc(sizeof(ItemType));
/*newmoney=malloc(sizeof(MoneyType));*/
newItem->item=new;
/*newItem->item.price=newmoney;*/
token = strtok(line, "|");
strcpy(newItem->item->id, token);
token = strtok(NULL, "|");
strcpy(newItem->item->name, token);
token = strtok(NULL, "|");
strcpy(newItem->item->desc, token);
token = strtok(NULL, "|");
j = atoi(token);
d=(unsigned)j;
newItem->item->price.dollars=d/100;printf("%u\n",newItem->item->price.dollars);
newItem->item->price.cents=d%100;printf("%u\n",newItem->item->price.cents);
while(token!=NULL)
{ token = strtok(NULL, "|");
strcpy(nc_array, token);
/* put the current pointer to the head of the list*/
currentCat = ftt->categoryHead;
/* while current is pointing to a node*/
while(currentCat != NULL)
{ printf("Current Cat Id:%s\n",currentCat->id );
printf("Currrent nc_array:%s\n",nc_array);
if(strcmp(currentCat->id, nc_array) == 0)
{ printf("inside if\n");
currentCat->firstItem=newItem;
break;
}
currentCat = currentCat->next;printf("outside if\n");
}
printf("outside while\n");
}
/* from the start of the list*/
newItem->next = currentCat->firstItem;
/*THIS IS WHERE I GET SEG FAULT*/
printf("Lookin at seg fault2 \n");
currentCat->firstItem = newItem;
/* increment the counter*/
ftt->categoryCount++;
}
currentCat = ftt->categoryHead;
/* try to close the file*/
/* if it wont close provide an error*/
if(fclose(fp2)!=0)
{
fprintf(stderr, "cannot close file\n");
}
return FALSE;
}
