From d98f46ce647846b0aa30b2e16a30fd4e152a1bf5 Mon Sep 17 00:00:00 2001 From: Carlos Maiolino Date: Thu, 10 Jul 2025 22:55:07 +0200 Subject: Add new code Signed-off-by: Carlos Maiolino --- C/final-project-z2h/src/main.c | 108 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 C/final-project-z2h/src/main.c (limited to 'C/final-project-z2h/src/main.c') diff --git a/C/final-project-z2h/src/main.c b/C/final-project-z2h/src/main.c new file mode 100644 index 0000000..09cbde1 --- /dev/null +++ b/C/final-project-z2h/src/main.c @@ -0,0 +1,108 @@ +#include +#include +#include +#include + +#include "common.h" +#include "file.h" +#include "parse.h" + +void print_usage(char *argv[]) { + printf("Usage: %s -n -f \n", argv[0]); + printf("\t -n - create new database file\n"); + printf("\t -f - (required) path to database file\n"); + return; +} + +int main(int argc, char *argv[]) { + char *filepath = NULL; + char *portarg = NULL; + unsigned short port = 0; + bool newfile = false; + bool list = false; + char *addstring = NULL; + int c; + + int dbfd = -1; + struct dbheader_t *dbhdr = NULL; + struct employee_t *employees = NULL; + + while ((c = getopt(argc, argv, "nf:a:l")) != -1) { + switch (c) { + case 'n': + newfile = true; + break; + case 'f': + filepath = optarg; + break; + case 'p': + portarg = optarg; + break; + case 'a': + addstring = optarg; + break; + case 'l': + list = true; + break; + case '?': + printf("Unknown option -%c\n", c); + break; + default: + return -1; + + } + } + + if (filepath == NULL) { + printf("Filepath is a required argument\n"); + print_usage(argv); + + return 0; + } + + + if (newfile) { + dbfd = create_db_file(filepath); + if (dbfd == STATUS_ERROR) { + printf("Unable to create database file\n"); + return -1; + } + + if (create_db_header(dbfd, &dbhdr) == STATUS_ERROR) { + printf("Failed to create database header\n"); + return -1; + } + } else { + dbfd = open_db_file(filepath); + if (dbfd == STATUS_ERROR) { + printf("Unable to open database file\n"); + return -1; + } + + if (validate_db_header(dbfd, &dbhdr) == STATUS_ERROR) { + printf("Failed to validate database header\n"); + return -1; + } + } + + if (read_employees(dbfd, dbhdr, &employees) != STATUS_SUCCESS) { + printf("Failed to read employees"); + return 0; + } + + if (addstring) { + dbhdr->count++; + employees = realloc(employees, dbhdr->count*(sizeof(struct employee_t))); + add_employee(dbhdr, employees, addstring); + } + + if (list) { + list_employees(dbhdr, employees); + } + + + output_file(dbfd, dbhdr, employees); + + + return 0; +} -- cgit v1.2.3