From cd60b79511d54c5b19c28aeb24b08403bee847eb Mon Sep 17 00:00:00 2001 From: bjoernf Date: Sat, 2 Sep 2023 19:25:58 +0200 Subject: [PATCH] make user choose if all day event and get uuid --- src/Makefile | 4 +++- src/insert_event.c | 43 +++++++++++++++++++++++++++++++++++++++++-- src/insert_event.h | 1 + 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/Makefile b/src/Makefile index be7b47e..844c555 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,5 +1,6 @@ CC = gcc CFLAGS = -Wall +LDFLAGS = -luuid # List of all source files (assuming they're all in the same directory) SRC_FILES = $(wildcard *.c) @@ -7,8 +8,9 @@ SRC_FILES = $(wildcard *.c) # Generate a list of object files by replacing the .c extension with .o OBJ_FILES = $(SRC_FILES:.c=.o) +# linking ics_analyzer: $(OBJ_FILES) - gcc -Wall $(OBJ_FILES) -o ics_analyzer + gcc -Wall $(OBJ_FILES) -o ics_analyzer $(LDFLAGS) main.o: main.c $(CC) $(CFLAGS) -c $< diff --git a/src/insert_event.c b/src/insert_event.c index 03ce9ea..bf62390 100644 --- a/src/insert_event.c +++ b/src/insert_event.c @@ -4,15 +4,27 @@ #include #include #include +#include void insert_event(char *file_name) { int myfd = open(file_name, O_RDWR); - + int all_day_event = 0; char summary_buf[256] = "SUMMARY:"; - char *input_buffer = &summary_buf[8]; + uuid_t uuid; + char uuid_str[37] = ""; printf("Insert a new event\n"); + + printf("Is this an all day event? [y/n] "); + all_day_event = binary_user_choice(); + + if (all_day_event) { + printf("this will be an all day event\n"); + } else { + printf("this will not be an all day event\n"); + } + printf("SUMMARY: "); fgets (input_buffer, (sizeof(summary_buf)-strlen(summary_buf)), stdin); @@ -24,8 +36,15 @@ void insert_event(char *file_name) { summary_buf[strlen(summary_buf)-1] = '\r'; summary_buf[strlen(summary_buf)] = '\n'; + uuid_generate(uuid); + // parse uuid to a string + uuid_unparse(uuid, uuid_str); + seek_cal_end(myfd); write(myfd, "BEGIN:VEVENT\r\n", strlen("BEGIN:VEVENT\r\n")); + write(myfd, "UID:", strlen("UID:")); + write(myfd, uuid_str, strlen(uuid_str)); + write(myfd, "\r\n", strlen("\r\n")); write(myfd, summary_buf, strlen(summary_buf)); write(myfd, "END:VCALENDAR\r\n", strlen("END:VCALENDAR\r\n")); @@ -55,3 +74,23 @@ void seek_cal_end(int fd) { lseek(fd, -2, SEEK_CUR); } } + + +int binary_user_choice() { + char input_buffer[64] = ""; + if (fgets (input_buffer, sizeof(input_buffer), stdin) == NULL) { + printf ("an fgets error occured\n"); + } + if (! strchr(input_buffer, '\n')) { + printf ("Input buffer overflow!\n"); + exit(1); + } + if (input_buffer[0] == 'n' || input_buffer[0] == 'N') { + return 0; + } else if (input_buffer[0] == 'y' || input_buffer[0] == 'Y') { + return 1; + } else { + printf ("Please enter a N/n or Y/y character!\n"); + exit(1); + } +} diff --git a/src/insert_event.h b/src/insert_event.h index 4719351..d9ce902 100644 --- a/src/insert_event.h +++ b/src/insert_event.h @@ -2,3 +2,4 @@ void insert_event(char *file_name); void seek_cal_end(int fd); +int binary_user_choice();