summaryrefslogtreecommitdiff
path: root/C/final-project-z2h/src/parse.c
diff options
context:
space:
mode:
authorCarlos Maiolino <[email protected]>2025-07-10 22:55:07 +0200
committerCarlos Maiolino <[email protected]>2025-07-10 22:56:55 +0200
commitd98f46ce647846b0aa30b2e16a30fd4e152a1bf5 (patch)
tree267474fcc77cf20b428f6f4c7f768ca09f4cfe0e /C/final-project-z2h/src/parse.c
parent869e68986aa8f69af6e7842260a68d1e5c6f796f (diff)
Add new code
Signed-off-by: Carlos Maiolino <[email protected]>
Diffstat (limited to 'C/final-project-z2h/src/parse.c')
-rw-r--r--C/final-project-z2h/src/parse.c162
1 files changed, 162 insertions, 0 deletions
diff --git a/C/final-project-z2h/src/parse.c b/C/final-project-z2h/src/parse.c
new file mode 100644
index 0000000..0f9b8e7
--- /dev/null
+++ b/C/final-project-z2h/src/parse.c
@@ -0,0 +1,162 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <arpa/inet.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <string.h>
+
+#include "common.h"
+#include "parse.h"
+
+void list_employees(struct dbheader_t *dbhdr, struct employee_t *employees) {
+ int i = 0;
+ for (; i < dbhdr->count; i++) {
+ printf("Employee %d\n", i);
+ printf("\tName: %s\n", employees[i].name);
+ printf("\tAddress: %s\n", employees[i].address);
+ printf("\tHours: %d\n", employees[i].hours);
+ }
+}
+
+int add_employee(struct dbheader_t *dbhdr, struct employee_t *employees, char *addstring) {
+ printf("%s\n", addstring);
+
+ char *name = strtok(addstring, ",");
+
+ char *addr = strtok(NULL, ",");
+
+ char *hours = strtok(NULL, ",");
+
+ printf("%s %s %s\n", name, addr, hours);
+
+
+ strncpy(employees[dbhdr->count-1].name, name, sizeof(employees[dbhdr->count-1].name));
+ strncpy(employees[dbhdr->count-1].address, addr, sizeof(employees[dbhdr->count-1].address));
+
+ employees[dbhdr->count-1].hours = atoi(hours);
+
+
+ return STATUS_SUCCESS;
+}
+
+int read_employees(int fd, struct dbheader_t *dbhdr, struct employee_t **employeesOut) {
+ if (fd < 0) {
+ printf("Got a bad FD from the user\n");
+ return STATUS_ERROR;
+ }
+
+
+ int count = dbhdr->count;
+
+ struct employee_t *employees = calloc(count, sizeof(struct employee_t));
+ if (employees == (void*)-1) {
+ printf("Malloc failed\n");
+ return STATUS_ERROR;
+ }
+
+ read(fd, employees, count*sizeof(struct employee_t));
+
+ int i = 0;
+ for (; i < count; i++) {
+ employees[i].hours = ntohl(employees[i].hours);
+ }
+
+ *employeesOut = employees;
+ return STATUS_SUCCESS;
+
+}
+
+int output_file(int fd, struct dbheader_t *dbhdr, struct employee_t *employees) {
+ if (fd < 0) {
+ printf("Got a bad FD from the user\n");
+ return STATUS_ERROR;
+ }
+
+ int realcount = dbhdr->count;
+
+ dbhdr->magic = htonl(dbhdr->magic);
+ dbhdr->filesize = htonl(sizeof(struct dbheader_t) + (sizeof(struct employee_t) * realcount));
+ dbhdr->count = htons(dbhdr->count);
+ dbhdr->version = htons(dbhdr->version);
+
+ lseek(fd, 0, SEEK_SET);
+
+ write(fd, dbhdr, sizeof(struct dbheader_t));
+
+ int i = 0;
+ for (; i < realcount; i++) {
+ employees[i].hours = htonl(employees[i].hours);
+ write(fd, &employees[i], sizeof(struct employee_t));
+ }
+
+ return STATUS_SUCCESS;
+
+}
+
+int validate_db_header(int fd, struct dbheader_t **headerOut) {
+ if (fd < 0) {
+ printf("Got a bad FD from the user\n");
+ return STATUS_ERROR;
+ }
+
+ struct dbheader_t *header = calloc(1, sizeof(struct dbheader_t));
+ if (header == (void*)-1) {
+ printf("Malloc failed create a db header\n");
+ return STATUS_ERROR;
+ }
+
+ if (read(fd, header, sizeof(struct dbheader_t)) != sizeof(struct dbheader_t)) {
+ perror("read");
+ free(header);
+ return STATUS_ERROR;
+ }
+
+ header->version = ntohs(header->version);
+ header->count = ntohs(header->count);
+ header->magic = ntohl(header->magic);
+ header->filesize = ntohl(header->filesize);
+
+ if (header->magic != HEADER_MAGIC) {
+ printf("Impromper header magic\n");
+ free(header);
+ return -1;
+ }
+
+
+ if (header->version != 1) {
+ printf("Impromper header version\n");
+ free(header);
+ return -1;
+ }
+
+ struct stat dbstat = {0};
+ fstat(fd, &dbstat);
+ if (header->filesize != dbstat.st_size) {
+ printf("Corrupted database\n");
+ free(header);
+ return -1;
+ }
+
+ *headerOut = header;
+}
+
+int create_db_header(int fd, struct dbheader_t **headerOut) {
+ struct dbheader_t *header = calloc(1, sizeof(struct dbheader_t));
+ if (header == (void*)-1) {
+ printf("Malloc failed to create db header\n");
+ return STATUS_ERROR;
+ }
+
+ header->version = 0x1;
+ header->count = 0;
+ header->magic = HEADER_MAGIC;
+ header->filesize = sizeof(struct dbheader_t);
+
+ *headerOut = header;
+
+ return STATUS_SUCCESS;
+}
+
+