Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
428 changes: 428 additions & 0 deletions .gitignore

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions class7/Phill/arguments.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
Basic example of how arguments work.
Compile and run. Instructions are in the program.

* NOTICE * the first arument [0] is always the command used to run the program.

There is a standard way to parse arguments in C:
https://man7.org/linux/man-pages/man3/getopt.3.html
https://www.geeksforgeeks.org/c/getopt-function-in-c-to-parse-command-line-arguments/
*/


#include <stdio.h>

int main(int argc, char **argv) {
if (argc == 1)
printf("This program will help you test command line arguments.\n"
"Pass arguments by entering the program name then any number of arguments separated by spaces.\n");
else
for (int x = 0; x < argc; x++)
printf("argument %d: %s\n", x, argv[x]);
return 0;
}
143 changes: 143 additions & 0 deletions class7/Phill/fileops.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
#include "fileops.h"
#include "ingredients.h"
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>

int passed_error_checks = 0;

//generic failure program end
void Failure(FILE *file_pointer) {
printf("program failed\n");
if (file_pointer != NULL) fclose(file_pointer);
exit(EXIT_FAILURE);
}

//means of checking for file pointer errors
void CheckErrorFile(FILE *file_pointer) {

//shut down the program if something bad happens
if (file_pointer == NULL || ferror(file_pointer) != 0) {
printf("file pointer address: %p\n", file_pointer);
printf("Error number: %d\n", errno);
perror("perror");
printf("strerror: %s\n", strerror(errno));
Failure(file_pointer);
}

//success counter
passed_error_checks++;
}

//error checking using errno
void CheckErrorClosed() {

//shut down the program if something bad happens
if (errno != 0) {
printf("Error number: %d\n", errno);
perror("perror");
printf("strerror: %s\n", strerror(errno));
Failure(NULL);
}

//success counter
passed_error_checks++;
}

void WriteInventory(int *ram_inventory, int size) {
if (size != INGREDIENT_LIST_SIZE) {
printf("ingrediant array is not the right size.\n"
"expected [%d] found [%d]\n",
INGREDIENT_LIST_SIZE, size);
Failure(NULL);
}

FILE *file_pointer = fopen(INVENTORY_FILE_NAME, "wb");
CheckErrorFile(file_pointer);

fwrite(ram_inventory, sizeof(int), size, file_pointer);
CheckErrorFile(file_pointer);

fclose(file_pointer);
CheckErrorClosed();
}

void ReadInventory(int *ram_inventory, int size) {
if (size != INGREDIENT_LIST_SIZE) {
printf("ingrediant array is not the right size.\n"
"expected [%d] found [%d]\n",
INGREDIENT_LIST_SIZE, size);
Failure(NULL);
}

FILE *file_pointer = fopen(INVENTORY_FILE_NAME, "rb");
//special catch to create new file
if (errno == 2 && file_pointer == NULL) return;
CheckErrorFile(file_pointer);

size = fread(ram_inventory, sizeof(int), INGREDIENT_LIST_SIZE, file_pointer);
CheckErrorFile(file_pointer);

if (size != INGREDIENT_LIST_SIZE) {
printf("Wrong number of ingredients in file.\n"
"expected [%d] found [%d]\n",
INGREDIENT_LIST_SIZE, size);
Failure(NULL);
}

fclose(file_pointer);
CheckErrorClosed();
}

void WriteOrder(customer_order *order) {

//check for null
if (order == NULL) {
printf("order was null\n");
Failure(NULL);
}

//open file
FILE *file_pointer = fopen(ORDERS_FILE_NAME, "a");
CheckErrorFile(file_pointer);

//write file
int added = fprintf(file_pointer, ORDER_PATERN,
order->order_time, order->name, order->toppings);

//make sure line was added
if (added < 1) {
printf("order was not added %d\n", added);
CheckErrorFile(file_pointer);
Failure(file_pointer);
}
CheckErrorFile(file_pointer);

fclose(file_pointer);
CheckErrorClosed();
}

void PrintOrders() {

FILE *file_ponter = fopen(ORDERS_FILE_NAME, "r");
CheckErrorFile(file_ponter);

customer_order *order = malloc(sizeof(customer_order));
while(fscanf(file_ponter, ORDER_PATERN,
&order->order_time, order->name, &order->toppings) == 3) {

//convert time to text
char time_buffer[100];
strcpy(time_buffer, ctime(&order->order_time));

//replace newline character with end of string null character
time_buffer[strlen(time_buffer)-1] = '\0';

//get list of toppings to exclude
char toppings_buffer[255];
ListExcludes(order->toppings, toppings_buffer);

printf("%s %s %s\n", time_buffer, order->name, toppings_buffer);
}
}
33 changes: 33 additions & 0 deletions class7/Phill/fileops.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
Standardized File opperations
*/

#ifndef FILEOPS_H_
#define FILEOPS_H_

#include <stdio.h>
#include <time.h>
#include <inttypes.h>

#define INVENTORY_FILE_NAME "inventory.bin"
#define ORDERS_FILE_NAME "orders.txt"
#define ORDER_PATERN "%08x %s %02x\n"

#define ORDER_MAX_NAME_LENGTH 15

typedef struct {
time_t order_time;
char name[ORDER_MAX_NAME_LENGTH];
uint8_t toppings;
}customer_order;

extern int passed_error_checks;
extern void Failure(FILE *file_pointer);
void CheckErrorFile(FILE *file_pointer);
void CheckErrorClosed();
void WriteInventory(int *ram_inventory, int size);
void ReadInventory(int *ram_inventory, int size);
void WriteOrder(customer_order *order);
void PrintOrders();

#endif
40 changes: 40 additions & 0 deletions class7/Phill/ingredients.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <string.h>
#include "ingredients.h"

//tries to match an ingredient
//returns index of ingredient or -1 if none match
//in real life, you would use a smarter algorithm
int MatchIngredient(char *arg) {
for (int x = 0; x < INGREDIENT_LIST_SIZE; x++)
if (strcmp(arg, INGREDIENT_LIST[x]) == 0)
return x;
return -1;
}

void ListExcludes(uint8_t toppings, char *buffer) {
int cursor = 0;
for (int x = 0; x < INGREDIENT_LIST_SIZE; x++) {

//look for ingredients NOT(!) included
if (!(toppings & INGREDIENT_FLAGS[x])) {

//get the length of the name of the ingredient to add
int add_size = strlen(INGREDIENT_LIST[x]);

//coppy it into the buffer
strncpy(&buffer[cursor], INGREDIENT_LIST[x], add_size);

//increment the cursor
cursor += add_size;

//add a space to the buffer and increment the cursor
buffer[cursor++] = ' ';
}
}
//terminate the string
buffer[cursor] = '\0';
}

const char *INGREDIENT_LIST[] = {BACON, LETTUCE, TOMATO, ONION, TURKEY, CHEESE, MAYO, RANCH};
const int INGREDIENT_FLAGS[] = {BACON_FLAG, LETTUCE_FLAG, TOMATO_FLAG,
ONION_FLAG, TURKEY_FLAG, CHEESE_FLAG, MAYO_FLAG, RANCH_FLAG};
50 changes: 50 additions & 0 deletions class7/Phill/ingredients.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Ingredients for the Sandwich Shop programs
*/

#ifndef INGREDIENTS_H_
#define INGREDIENTS_H_

#include <inttypes.h>

//1 or 0x01
#define BACON "bacon"
#define BACON_FLAG 0b00000001

//2 or 0x02
#define LETTUCE "lettuce"
#define LETTUCE_FLAG 0b00000010

//4 or 0x04
#define TOMATO "tomatoes"
#define TOMATO_FLAG 0b00000100

//8 or 0x08
#define ONION "onions"
#define ONION_FLAG 0b00001000

//16 or 0x10
#define TURKEY "turkey"
#define TURKEY_FLAG 0b00010000

//32 or 0x20
#define CHEESE "cheese"
#define CHEESE_FLAG 0b00100000

//64 or 0x40
#define MAYO "mayonnaise"
#define MAYO_FLAG 0b01000000

//128 or 0x80
#define RANCH "ranch"
#define RANCH_FLAG 0b10000000

int MatchIngredient(char *arg);
void ListExcludes(uint8_t toppings, char *buffer);
extern const char *INGREDIENT_LIST[];
extern const int INGREDIENT_FLAGS[];
#define INGREDIENT_LIST_SIZE 8
#define INGREDIENT_INVENTORY_MAX 10
#define ALL_INGREDIENTS 0xff

#endif
Loading