From 81d5f195721f4d1f56c0e153bbd11652734b5233 Mon Sep 17 00:00:00 2001 From: PhillBenoit <19508075+PhillBenoit@users.noreply.github.com> Date: Tue, 9 Sep 2025 02:14:22 -0700 Subject: [PATCH 1/2] move files and add first chunks of code --- .gitignore | 428 +++++++++++++++++++++++ class7/Phill/arguments.c | 23 ++ class7/Phill/ingredients.c | 14 + class7/Phill/ingredients.h | 45 +++ class7/Phill/inventory.c | 186 ++++++++++ class7/{ => Ted}/cars.c | 0 class7/{ => Ted}/contacts.dat | Bin class7/{ => Ted}/dir_ops.c | 0 class7/{ => Ted}/employee_app/employee.c | 0 class7/{ => Ted}/employee_app/employee.h | 0 class7/{ => Ted}/employee_app/main.c | 0 class7/{ => Ted}/file_info.c | 0 class7/{ => Ted}/file_ops.c | 0 class7/{ => Ted}/filestructs.c | 0 class7/{ => Ted}/filestructs2.c | 0 class7/{ => Ted}/nestedstruct.c | 0 class7/{ => Ted}/openread.c | 0 class7/{ => Ted}/openwrite.c | 0 class7/{ => Ted}/person/address.c | 0 class7/{ => Ted}/person/address.h | 0 class7/{ => Ted}/person/main.c | 0 class7/{ => Ted}/person/person.c | 0 class7/{ => Ted}/person/person.h | 0 class7/{ => Ted}/read_write_file.c | 0 class7/{ => Ted}/structs.c | 0 class7/{ => Ted}/structs2.c | 0 class7/{ => Ted}/structstring.c | 0 27 files changed, 696 insertions(+) create mode 100644 .gitignore create mode 100644 class7/Phill/arguments.c create mode 100644 class7/Phill/ingredients.c create mode 100644 class7/Phill/ingredients.h create mode 100644 class7/Phill/inventory.c rename class7/{ => Ted}/cars.c (100%) rename class7/{ => Ted}/contacts.dat (100%) rename class7/{ => Ted}/dir_ops.c (100%) rename class7/{ => Ted}/employee_app/employee.c (100%) rename class7/{ => Ted}/employee_app/employee.h (100%) rename class7/{ => Ted}/employee_app/main.c (100%) rename class7/{ => Ted}/file_info.c (100%) rename class7/{ => Ted}/file_ops.c (100%) rename class7/{ => Ted}/filestructs.c (100%) rename class7/{ => Ted}/filestructs2.c (100%) rename class7/{ => Ted}/nestedstruct.c (100%) rename class7/{ => Ted}/openread.c (100%) rename class7/{ => Ted}/openwrite.c (100%) rename class7/{ => Ted}/person/address.c (100%) rename class7/{ => Ted}/person/address.h (100%) rename class7/{ => Ted}/person/main.c (100%) rename class7/{ => Ted}/person/person.c (100%) rename class7/{ => Ted}/person/person.h (100%) rename class7/{ => Ted}/read_write_file.c (100%) rename class7/{ => Ted}/structs.c (100%) rename class7/{ => Ted}/structs2.c (100%) rename class7/{ => Ted}/structstring.c (100%) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..47a94ef --- /dev/null +++ b/.gitignore @@ -0,0 +1,428 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/main/VisualStudio.gitignore + +# User-specific files +*.rsuser +*.suo +*.user +*.userosscache +*.sln.docstates +*.env + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Mono auto generated files +mono_crash.* + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ + +[Dd]ebug/x64/ +[Dd]ebugPublic/x64/ +[Rr]elease/x64/ +[Rr]eleases/x64/ +bin/x64/ +obj/x64/ + +[Dd]ebug/x86/ +[Dd]ebugPublic/x86/ +[Rr]elease/x86/ +[Rr]eleases/x86/ +bin/x86/ +obj/x86/ + +[Ww][Ii][Nn]32/ +[Aa][Rr][Mm]/ +[Aa][Rr][Mm]64/ +[Aa][Rr][Mm]64[Ee][Cc]/ +bld/ +[Oo]bj/ +[Oo]ut/ +[Ll]og/ +[Ll]ogs/ + +# Build results on 'Bin' directories +**/[Bb]in/* +# Uncomment if you have tasks that rely on *.refresh files to move binaries +# (https://github.com/github/gitignore/pull/3736) +#!**/[Bb]in/*.refresh + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* +*.trx + +# NUnit +*.VisualState.xml +TestResult.xml +nunit-*.xml + +# Approval Tests result files +*.received.* + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ + +# ASP.NET Scaffolding +ScaffoldingReadMe.txt + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_h.h +*.ilk +*.meta +*.obj +*.idb +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +# but not Directory.Build.rsp, as it configures directory-level build defaults +!Directory.Build.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*_wpftmp.csproj +*.log +*.tlog +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Coverlet is a free, cross platform Code Coverage Tool +coverage*.json +coverage*.xml +coverage*.info + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# NuGet Symbol Packages +*.snupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx +*.appxbundle +*.appxupload + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!?*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser +*- [Bb]ackup.rdl +*- [Bb]ackup ([0-9]).rdl +*- [Bb]ackup ([0-9][0-9]).rdl + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio 6 workspace and project file (working project files containing files to include in project) +*.dsw +*.dsp + +# Visual Studio 6 technical files +*.ncb +*.aps + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +**/.paket/paket.exe +paket-files/ + +# FAKE - F# Make +**/.fake/ + +# CodeRush personal settings +**/.cr/personal + +# Python Tools for Visual Studio (PTVS) +**/__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +#tools/** +#!tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog +MSBuild_Logs/ + +# AWS SAM Build and Temporary Artifacts folder +.aws-sam + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +**/.mfractor/ + +# Local History for Visual Studio +**/.localhistory/ + +# Visual Studio History (VSHistory) files +.vshistory/ + +# BeatPulse healthcheck temp database +healthchecksdb + +# Backup folder for Package Reference Convert tool in Visual Studio 2017 +MigrationBackup/ + +# Ionide (cross platform F# VS Code tools) working folder +**/.ionide/ + +# Fody - auto-generated XML schema +FodyWeavers.xsd + +# VS Code files for those working on multiple tools +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +!.vscode/*.code-snippets + +# Local History for Visual Studio Code +.history/ + +# Built Visual Studio Code Extensions +*.vsix + +# Windows Installer files from build outputs +*.cab +*.msi +*.msix +*.msm +*.msp diff --git a/class7/Phill/arguments.c b/class7/Phill/arguments.c new file mode 100644 index 0000000..410db30 --- /dev/null +++ b/class7/Phill/arguments.c @@ -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 + +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; +} diff --git a/class7/Phill/ingredients.c b/class7/Phill/ingredients.c new file mode 100644 index 0000000..0aaca71 --- /dev/null +++ b/class7/Phill/ingredients.c @@ -0,0 +1,14 @@ +#include +#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; +} + +const char *INGREDIENT_LIST[] = {BACON, LETTUCE, TOMATO, ONION, TURKEY, CHEESE, MAYO, RANCH}; \ No newline at end of file diff --git a/class7/Phill/ingredients.h b/class7/Phill/ingredients.h new file mode 100644 index 0000000..fd64fab --- /dev/null +++ b/class7/Phill/ingredients.h @@ -0,0 +1,45 @@ +/* +Ingredients for the Sandwich Shop programs +*/ + +#ifndef INGREDIENTS_H_ +#define INGREDIENTS_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 + +extern int MatchIngredient(char *arg); +extern const char *INGREDIENT_LIST[]; +#define INGREDIENT_LIST_SIZE 8 +#define INGREDIENT_MAX 10 + +#endif \ No newline at end of file diff --git a/class7/Phill/inventory.c b/class7/Phill/inventory.c new file mode 100644 index 0000000..74f6618 --- /dev/null +++ b/class7/Phill/inventory.c @@ -0,0 +1,186 @@ +#include +#include +#include +#include +#include "ingredients.h" + +#define ADD "add" +#define FILE_NAME "inventory.bin" + +int passed_error_checks = 0; +FILE *file_pointer; + +//generic failure program end +void Failure() { + printf("program failed\n"); + fclose(file_pointer); + exit(EXIT_FAILURE); +} + +//means of checking for file pointer errors +void CheckError() { + + //shut down the program if something bad happens + if (ferror(file_pointer) != 0) { + printf("Error number: %d\n", errno); + perror("perror"); + printf("strerror: %s\n", strerror(errno)); + Failure(); + } + + //success counter + passed_error_checks++; +} + +//creates a starter file +void *WriteStarter() { + + //manually reset the no file error + errno = 0; + + //create file + file_pointer = fopen(FILE_NAME, "wb"); + CheckError(); + + //declare data to write + int stock_amount = INGREDIENT_MAX / 2; + + //loop through ingredients and write them all + for (int x = 0; x < INGREDIENT_LIST_SIZE; x++) { + fwrite(&stock_amount, sizeof(int), 1, file_pointer); + CheckError(); + } + + //reopen for reading + freopen(FILE_NAME, "rb", file_pointer); + CheckError(); + + printf("new inventory created\n"); +} + +//Matches an argument to an ingredient to add to inventory +int MatchArgument(char *arg) { + int value = MatchIngredient(arg); + + //makes sure a positive value was returned + if (value > -1) return value; + printf("ingredient not recognized: %s\n", arg); + Failure(); +} + +int GetValue(char *arg) { + + //make sure the argument isn't too long + if (strlen(arg) > 20) { + printf("too long to be converted into a number: %s\n", arg); + Failure(); + } + + //parse the integer + int value = atoi(arg); + + //turn it back into a string + char check[20]; + sprintf(check, "%d", value); + + //compare the strings + if (strcmp(arg, check) != 0) { + //if they are unequal, something bad happened + printf("%s and %s (%d) did not match.\n", check, arg, value); + Failure(); + } + + //make sure the value is positive + if (value > 0) return value; + printf("%d is not a positive value.\n", value); + Failure(); +} + +int main(int argc, char **argv) { + + //try to open the file + file_pointer = fopen(FILE_NAME, "rb"); + + //create file if it does not exist + if (file_pointer == NULL && errno == 2) WriteStarter(); + //error check if something bad happened + else CheckError(); + + //check to see if the amount of arguments can be used to add inventory + if (argc == 3) { + int ingredient = MatchArgument(argv[1]); + int amount = GetValue(argv[2]); + int inventory[INGREDIENT_LIST_SIZE]; + int total; + + //read the current inventory + fread(&inventory, sizeof(int), INGREDIENT_LIST_SIZE, file_pointer); + CheckError(); + + //calculate the new total to test before writing + total = amount + inventory[ingredient]; + + //make sure the user can't overfill inventory + if (total > INGREDIENT_MAX) { + printf("Total (%d) was greater than the inventory limit of %d.\n", + total, INGREDIENT_MAX); + printf("Adding too many may cause spoilage!\n"); + Failure(); + } + + //check to see if something REALLY bad happened + if (total < inventory[ingredient]) { + printf("Adding (%d) somehow reduced inventory (%d) to (%d).\n", + amount, inventory[ingredient], total); + Failure(); + } + + //reopen file for writing + freopen(FILE_NAME, "wb", file_pointer); + CheckError(); + + //write + inventory[ingredient] = total; + fwrite(&inventory, sizeof(int), INGREDIENT_LIST_SIZE, file_pointer); + CheckError(); + + printf("%d %s added to inventory for a total of %d.\n", + amount, INGREDIENT_LIST[ingredient], total); + } + + //display inventory + else { + printf("Current Inventory\n"); + printf("-----------------\n"); + for (int x = 0; x < INGREDIENT_LIST_SIZE; x++) { + + //declare a space to read + int amount; + + //read + fread(&amount, sizeof(int), 1, file_pointer); + CheckError(file_pointer); + + //display + printf("%s %d\n", INGREDIENT_LIST[x], amount); + } + } + + //close the file + fclose(file_pointer); + + //The pointer is freed and can no longer be accessed!! + //Our shortcut method will no longer work!! + //Checking error number directly instead. + if (errno != 0 ) { + printf("Close File Error\n"); + printf("Error number: %d\n", errno); + perror("perror"); + printf("strerror: %s\n", strerror(errno)); + exit(EXIT_FAILURE); + } + + printf("program exited normally with %d successfull error checks.\n", + passed_error_checks); + return EXIT_SUCCESS; +} \ No newline at end of file diff --git a/class7/cars.c b/class7/Ted/cars.c similarity index 100% rename from class7/cars.c rename to class7/Ted/cars.c diff --git a/class7/contacts.dat b/class7/Ted/contacts.dat similarity index 100% rename from class7/contacts.dat rename to class7/Ted/contacts.dat diff --git a/class7/dir_ops.c b/class7/Ted/dir_ops.c similarity index 100% rename from class7/dir_ops.c rename to class7/Ted/dir_ops.c diff --git a/class7/employee_app/employee.c b/class7/Ted/employee_app/employee.c similarity index 100% rename from class7/employee_app/employee.c rename to class7/Ted/employee_app/employee.c diff --git a/class7/employee_app/employee.h b/class7/Ted/employee_app/employee.h similarity index 100% rename from class7/employee_app/employee.h rename to class7/Ted/employee_app/employee.h diff --git a/class7/employee_app/main.c b/class7/Ted/employee_app/main.c similarity index 100% rename from class7/employee_app/main.c rename to class7/Ted/employee_app/main.c diff --git a/class7/file_info.c b/class7/Ted/file_info.c similarity index 100% rename from class7/file_info.c rename to class7/Ted/file_info.c diff --git a/class7/file_ops.c b/class7/Ted/file_ops.c similarity index 100% rename from class7/file_ops.c rename to class7/Ted/file_ops.c diff --git a/class7/filestructs.c b/class7/Ted/filestructs.c similarity index 100% rename from class7/filestructs.c rename to class7/Ted/filestructs.c diff --git a/class7/filestructs2.c b/class7/Ted/filestructs2.c similarity index 100% rename from class7/filestructs2.c rename to class7/Ted/filestructs2.c diff --git a/class7/nestedstruct.c b/class7/Ted/nestedstruct.c similarity index 100% rename from class7/nestedstruct.c rename to class7/Ted/nestedstruct.c diff --git a/class7/openread.c b/class7/Ted/openread.c similarity index 100% rename from class7/openread.c rename to class7/Ted/openread.c diff --git a/class7/openwrite.c b/class7/Ted/openwrite.c similarity index 100% rename from class7/openwrite.c rename to class7/Ted/openwrite.c diff --git a/class7/person/address.c b/class7/Ted/person/address.c similarity index 100% rename from class7/person/address.c rename to class7/Ted/person/address.c diff --git a/class7/person/address.h b/class7/Ted/person/address.h similarity index 100% rename from class7/person/address.h rename to class7/Ted/person/address.h diff --git a/class7/person/main.c b/class7/Ted/person/main.c similarity index 100% rename from class7/person/main.c rename to class7/Ted/person/main.c diff --git a/class7/person/person.c b/class7/Ted/person/person.c similarity index 100% rename from class7/person/person.c rename to class7/Ted/person/person.c diff --git a/class7/person/person.h b/class7/Ted/person/person.h similarity index 100% rename from class7/person/person.h rename to class7/Ted/person/person.h diff --git a/class7/read_write_file.c b/class7/Ted/read_write_file.c similarity index 100% rename from class7/read_write_file.c rename to class7/Ted/read_write_file.c diff --git a/class7/structs.c b/class7/Ted/structs.c similarity index 100% rename from class7/structs.c rename to class7/Ted/structs.c diff --git a/class7/structs2.c b/class7/Ted/structs2.c similarity index 100% rename from class7/structs2.c rename to class7/Ted/structs2.c diff --git a/class7/structstring.c b/class7/Ted/structstring.c similarity index 100% rename from class7/structstring.c rename to class7/Ted/structstring.c From 7d93e0d40e8fbdd37ba20aa5725285087ced8ad7 Mon Sep 17 00:00:00 2001 From: PhillBenoit <19508075+PhillBenoit@users.noreply.github.com> Date: Tue, 9 Sep 2025 16:59:45 -0700 Subject: [PATCH 2/2] pree class push --- class7/Phill/fileops.c | 143 +++++++++++++++++++++++++++++++++++++ class7/Phill/fileops.h | 33 +++++++++ class7/Phill/ingredients.c | 28 +++++++- class7/Phill/ingredients.h | 9 ++- class7/Phill/inventory.c | 116 +++++++----------------------- class7/Phill/orders.c | 79 ++++++++++++++++++++ 6 files changed, 314 insertions(+), 94 deletions(-) create mode 100644 class7/Phill/fileops.c create mode 100644 class7/Phill/fileops.h create mode 100644 class7/Phill/orders.c diff --git a/class7/Phill/fileops.c b/class7/Phill/fileops.c new file mode 100644 index 0000000..7a3d53e --- /dev/null +++ b/class7/Phill/fileops.c @@ -0,0 +1,143 @@ +#include "fileops.h" +#include "ingredients.h" +#include +#include +#include +#include + +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); + } +} diff --git a/class7/Phill/fileops.h b/class7/Phill/fileops.h new file mode 100644 index 0000000..f0fa548 --- /dev/null +++ b/class7/Phill/fileops.h @@ -0,0 +1,33 @@ +/* +Standardized File opperations +*/ + +#ifndef FILEOPS_H_ +#define FILEOPS_H_ + +#include +#include +#include + +#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 \ No newline at end of file diff --git a/class7/Phill/ingredients.c b/class7/Phill/ingredients.c index 0aaca71..a35f2c2 100644 --- a/class7/Phill/ingredients.c +++ b/class7/Phill/ingredients.c @@ -11,4 +11,30 @@ int MatchIngredient(char *arg) { return -1; } -const char *INGREDIENT_LIST[] = {BACON, LETTUCE, TOMATO, ONION, TURKEY, CHEESE, MAYO, RANCH}; \ No newline at end of file +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}; \ No newline at end of file diff --git a/class7/Phill/ingredients.h b/class7/Phill/ingredients.h index fd64fab..7a9c608 100644 --- a/class7/Phill/ingredients.h +++ b/class7/Phill/ingredients.h @@ -5,6 +5,8 @@ Ingredients for the Sandwich Shop programs #ifndef INGREDIENTS_H_ #define INGREDIENTS_H_ +#include + //1 or 0x01 #define BACON "bacon" #define BACON_FLAG 0b00000001 @@ -37,9 +39,12 @@ Ingredients for the Sandwich Shop programs #define RANCH "ranch" #define RANCH_FLAG 0b10000000 -extern int MatchIngredient(char *arg); +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_MAX 10 +#define INGREDIENT_INVENTORY_MAX 10 +#define ALL_INGREDIENTS 0xff #endif \ No newline at end of file diff --git a/class7/Phill/inventory.c b/class7/Phill/inventory.c index 74f6618..947b307 100644 --- a/class7/Phill/inventory.c +++ b/class7/Phill/inventory.c @@ -3,34 +3,7 @@ #include #include #include "ingredients.h" - -#define ADD "add" -#define FILE_NAME "inventory.bin" - -int passed_error_checks = 0; -FILE *file_pointer; - -//generic failure program end -void Failure() { - printf("program failed\n"); - fclose(file_pointer); - exit(EXIT_FAILURE); -} - -//means of checking for file pointer errors -void CheckError() { - - //shut down the program if something bad happens - if (ferror(file_pointer) != 0) { - printf("Error number: %d\n", errno); - perror("perror"); - printf("strerror: %s\n", strerror(errno)); - Failure(); - } - - //success counter - passed_error_checks++; -} +#include "fileops.h" //creates a starter file void *WriteStarter() { @@ -38,22 +11,16 @@ void *WriteStarter() { //manually reset the no file error errno = 0; - //create file - file_pointer = fopen(FILE_NAME, "wb"); - CheckError(); - //declare data to write - int stock_amount = INGREDIENT_MAX / 2; + int stock_amount = INGREDIENT_INVENTORY_MAX / 2; + int inventory[INGREDIENT_LIST_SIZE]; - //loop through ingredients and write them all - for (int x = 0; x < INGREDIENT_LIST_SIZE; x++) { - fwrite(&stock_amount, sizeof(int), 1, file_pointer); - CheckError(); - } + //loop through ingredients and set them all + for (int x = 0; x < INGREDIENT_LIST_SIZE; x++) + inventory[x] = stock_amount; - //reopen for reading - freopen(FILE_NAME, "rb", file_pointer); - CheckError(); + //send array for writing + WriteInventory(inventory, INGREDIENT_LIST_SIZE); printf("new inventory created\n"); } @@ -65,7 +32,7 @@ int MatchArgument(char *arg) { //makes sure a positive value was returned if (value > -1) return value; printf("ingredient not recognized: %s\n", arg); - Failure(); + Failure(NULL); } int GetValue(char *arg) { @@ -73,7 +40,7 @@ int GetValue(char *arg) { //make sure the argument isn't too long if (strlen(arg) > 20) { printf("too long to be converted into a number: %s\n", arg); - Failure(); + Failure(NULL); } //parse the integer @@ -87,62 +54,53 @@ int GetValue(char *arg) { if (strcmp(arg, check) != 0) { //if they are unequal, something bad happened printf("%s and %s (%d) did not match.\n", check, arg, value); - Failure(); + Failure(NULL); } //make sure the value is positive if (value > 0) return value; printf("%d is not a positive value.\n", value); - Failure(); + Failure(NULL); } int main(int argc, char **argv) { - //try to open the file - file_pointer = fopen(FILE_NAME, "rb"); + int inventory[INGREDIENT_LIST_SIZE]; + ReadInventory(inventory, INGREDIENT_LIST_SIZE); //create file if it does not exist - if (file_pointer == NULL && errno == 2) WriteStarter(); - //error check if something bad happened - else CheckError(); + if (errno == 2) { + WriteStarter(); + ReadInventory(inventory, INGREDIENT_LIST_SIZE); + } //check to see if the amount of arguments can be used to add inventory if (argc == 3) { int ingredient = MatchArgument(argv[1]); int amount = GetValue(argv[2]); - int inventory[INGREDIENT_LIST_SIZE]; int total; - //read the current inventory - fread(&inventory, sizeof(int), INGREDIENT_LIST_SIZE, file_pointer); - CheckError(); - //calculate the new total to test before writing total = amount + inventory[ingredient]; //make sure the user can't overfill inventory - if (total > INGREDIENT_MAX) { + if (total > INGREDIENT_INVENTORY_MAX) { printf("Total (%d) was greater than the inventory limit of %d.\n", - total, INGREDIENT_MAX); + total, INGREDIENT_INVENTORY_MAX); printf("Adding too many may cause spoilage!\n"); - Failure(); + Failure(NULL); } //check to see if something REALLY bad happened if (total < inventory[ingredient]) { printf("Adding (%d) somehow reduced inventory (%d) to (%d).\n", amount, inventory[ingredient], total); - Failure(); + Failure(NULL); } - //reopen file for writing - freopen(FILE_NAME, "wb", file_pointer); - CheckError(); - //write inventory[ingredient] = total; - fwrite(&inventory, sizeof(int), INGREDIENT_LIST_SIZE, file_pointer); - CheckError(); + WriteInventory(inventory, INGREDIENT_LIST_SIZE); printf("%d %s added to inventory for a total of %d.\n", amount, INGREDIENT_LIST[ingredient], total); @@ -152,32 +110,8 @@ int main(int argc, char **argv) { else { printf("Current Inventory\n"); printf("-----------------\n"); - for (int x = 0; x < INGREDIENT_LIST_SIZE; x++) { - - //declare a space to read - int amount; - - //read - fread(&amount, sizeof(int), 1, file_pointer); - CheckError(file_pointer); - - //display - printf("%s %d\n", INGREDIENT_LIST[x], amount); - } - } - - //close the file - fclose(file_pointer); - - //The pointer is freed and can no longer be accessed!! - //Our shortcut method will no longer work!! - //Checking error number directly instead. - if (errno != 0 ) { - printf("Close File Error\n"); - printf("Error number: %d\n", errno); - perror("perror"); - printf("strerror: %s\n", strerror(errno)); - exit(EXIT_FAILURE); + for (int x = 0; x < INGREDIENT_LIST_SIZE; x++) + printf("%s %d\n", INGREDIENT_LIST[x], inventory[x]); } printf("program exited normally with %d successfull error checks.\n", diff --git a/class7/Phill/orders.c b/class7/Phill/orders.c new file mode 100644 index 0000000..823b56d --- /dev/null +++ b/class7/Phill/orders.c @@ -0,0 +1,79 @@ +#include +#include +#include +#include +#include "fileops.h" +#include "ingredients.h" + +int main(int argc, char **argv) { + + if (argc > 1) { + + //load inventory + int inventory[INGREDIENT_LIST_SIZE]; + ReadInventory(inventory, INGREDIENT_LIST_SIZE); + + //make sure name isn't an ingredient + int match_name = MatchIngredient(argv[1]); + if (match_name > -1) { + printf("name cannot be an ingredient\n"); + Failure(NULL); + } + + //start with all toppings + int toppings = ALL_INGREDIENTS; + + for (int arg = 2; arg < argc; arg++) { + int match = MatchIngredient(argv[arg]); + if (match == -1) { + printf("ingrdient not recognized (%s)\n", argv[arg]); + Failure(NULL); + } + + //make sure ingredient is already on the sandwich + if (toppings & INGREDIENT_FLAGS[match]) + //remove ingredient + toppings = toppings ^ INGREDIENT_FLAGS[match]; + else { + printf("%s was already removed\n", argv[arg]); + Failure(NULL); + } + } + + //verify all requested toppings are in the inventory + for(int ingredient = 0; ingredient < INGREDIENT_LIST_SIZE; ingredient++) { + + //check to see if customer wants this ingtrdirnt + if (toppings & INGREDIENT_FLAGS[ingredient]) { + + //check inventory + if (inventory[ingredient] == 0) { + printf("not enough %s in inventory for the order\n", INGREDIENT_LIST[ingredient]); + Failure(NULL); + } + //set inventory + else inventory[ingredient]--; + } + } + + //setup formatted order + customer_order order; + strcpy(order.name, argv[1]); + order.toppings = toppings; + order.order_time = time(NULL); + + //write files + WriteOrder(&order); + WriteInventory(inventory, INGREDIENT_LIST_SIZE); + printf("order for %s was written\n", order.name); + } + + //display orders + else { + PrintOrders(); + } + + printf("program exited normally with %d successfull error checks.\n", + passed_error_checks); + return EXIT_SUCCESS; +} \ No newline at end of file