make user choose if all day event and get uuid

This commit is contained in:
bjoernf 2023-09-02 19:25:58 +02:00
parent 72dbf969f5
commit cd60b79511
3 changed files with 45 additions and 3 deletions

View File

@ -1,5 +1,6 @@
CC = gcc CC = gcc
CFLAGS = -Wall CFLAGS = -Wall
LDFLAGS = -luuid
# List of all source files (assuming they're all in the same directory) # List of all source files (assuming they're all in the same directory)
SRC_FILES = $(wildcard *.c) 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 # Generate a list of object files by replacing the .c extension with .o
OBJ_FILES = $(SRC_FILES:.c=.o) OBJ_FILES = $(SRC_FILES:.c=.o)
# linking
ics_analyzer: $(OBJ_FILES) ics_analyzer: $(OBJ_FILES)
gcc -Wall $(OBJ_FILES) -o ics_analyzer gcc -Wall $(OBJ_FILES) -o ics_analyzer $(LDFLAGS)
main.o: main.c main.o: main.c
$(CC) $(CFLAGS) -c $< $(CC) $(CFLAGS) -c $<

View File

@ -4,15 +4,27 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <uuid/uuid.h>
void insert_event(char *file_name) { void insert_event(char *file_name) {
int myfd = open(file_name, O_RDWR); int myfd = open(file_name, O_RDWR);
int all_day_event = 0;
char summary_buf[256] = "SUMMARY:"; char summary_buf[256] = "SUMMARY:";
char *input_buffer = &summary_buf[8]; char *input_buffer = &summary_buf[8];
uuid_t uuid;
char uuid_str[37] = "";
printf("Insert a new event\n"); 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: "); printf("SUMMARY: ");
fgets (input_buffer, (sizeof(summary_buf)-strlen(summary_buf)), stdin); 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)-1] = '\r';
summary_buf[strlen(summary_buf)] = '\n'; summary_buf[strlen(summary_buf)] = '\n';
uuid_generate(uuid);
// parse uuid to a string
uuid_unparse(uuid, uuid_str);
seek_cal_end(myfd); seek_cal_end(myfd);
write(myfd, "BEGIN:VEVENT\r\n", strlen("BEGIN:VEVENT\r\n")); 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, summary_buf, strlen(summary_buf));
write(myfd, "END:VCALENDAR\r\n", strlen("END:VCALENDAR\r\n")); 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); 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);
}
}

View File

@ -2,3 +2,4 @@
void insert_event(char *file_name); void insert_event(char *file_name);
void seek_cal_end(int fd); void seek_cal_end(int fd);
int binary_user_choice();