From af4658f47222333ae6afe898d44153df04cf3626 Mon Sep 17 00:00:00 2001 From: bjoernf Date: Thu, 7 Sep 2023 14:29:47 +0200 Subject: [PATCH] reorganizations and got time zone --- src/cut_string.c | 41 ---------------- src/cut_string.h | 5 -- src/date_time_handling.c | 19 +++++++- src/date_time_handling.h | 1 + src/insert_event.c | 6 +++ src/main.c | 3 +- src/remove_whitespace.c | 45 ----------------- src/remove_whitespace.h | 8 --- src/string_handling.c | 103 +++++++++++++++++++++++++++++++++++++++ src/string_handling.h | 8 +++ 10 files changed, 136 insertions(+), 103 deletions(-) delete mode 100644 src/cut_string.c delete mode 100644 src/cut_string.h delete mode 100644 src/remove_whitespace.c delete mode 100644 src/remove_whitespace.h create mode 100644 src/string_handling.c create mode 100644 src/string_handling.h diff --git a/src/cut_string.c b/src/cut_string.c deleted file mode 100644 index 7d546cc..0000000 --- a/src/cut_string.c +++ /dev/null @@ -1,41 +0,0 @@ -// cut a string into two parts by the first occurence of delimiter -// and choose the first part (side 0) or the second part (side 1) -// the chosen part will overwrite the original string - -// cut a string into two parts by delimiter -// and choose the first part (side 0) or the second part (side 1) -// the chosen part will overwrite the original string - -#include - -void cut_string(char my_string[], char delimiter, int side) { - char part1[256] = ""; - char part2[256] = ""; - - int split = 0; - - int j = 0; - for (int i = 0; i < strlen(my_string); i++) { - if (my_string[i] == delimiter) { - if (split == 0) { - split = 1; - continue; - } - } - - if (split == 0) { - part1[i] = my_string[i]; - } else { - part2[j] = my_string[i]; - j++; - } - } - - memset(my_string, '\0', strlen(my_string)); - if (side == 0) { - strcpy(my_string, part1); - } else { - strcpy(my_string, part2); - } -} - diff --git a/src/cut_string.h b/src/cut_string.h deleted file mode 100644 index 9b8803d..0000000 --- a/src/cut_string.h +++ /dev/null @@ -1,5 +0,0 @@ -#pragma once - -// cut string into two parts and choose one part -// side is 0 or 1 -void cut_string(char my_string[], char delimiter, int side); diff --git a/src/date_time_handling.c b/src/date_time_handling.c index 648c1cc..b4178f2 100644 --- a/src/date_time_handling.c +++ b/src/date_time_handling.c @@ -1,8 +1,9 @@ #include "date_time_handling.h" #include #include -#include #include +#include +#include // buffer needs to contain a string with a strlen of 15 (format: "xxxxxxxxTxxxxxx") // or a strlen of 16 (format: "YYYYmmddTHHMMSSZ") @@ -49,4 +50,18 @@ void print_end_date(char end_date[]) { printf ("%c%c:", time[2], time[3]); printf ("%c%c", time[4], time[5]); } - + +char *get_tz() { + char *timezone_path = malloc(256); + ssize_t bytes_read = readlink("/etc/localtime", timezone_path, 255); + + if (bytes_read != -1) { + // Null-terminate the string + timezone_path[bytes_read] = '\0'; + } else { + perror("readlink"); + exit(1); + } + + return timezone_path; +} diff --git a/src/date_time_handling.h b/src/date_time_handling.h index 9a9415b..cb92e19 100644 --- a/src/date_time_handling.h +++ b/src/date_time_handling.h @@ -2,5 +2,6 @@ #include void get_date(char buffer[]); +char *get_tz(); void pretty_print_date_time(char date_time[]); void print_end_date(char end_date[]); diff --git a/src/insert_event.c b/src/insert_event.c index 049d555..e1e24f9 100644 --- a/src/insert_event.c +++ b/src/insert_event.c @@ -1,4 +1,5 @@ #include "insert_event.h" +#include "string_handling.h" #include "date_time_handling.h" #include #include @@ -15,6 +16,10 @@ void insert_event(char *file_name) { uuid_t uuid; char uuid_str[37] = ""; char dtstamp[] = "YYYYmmddTHHMMSSZ"; + char *time_zone = get_tz(); + remove_until_delim(time_zone, '/', 4); + + printf ("tz is: %s\n", time_zone); printf("Insert a new event\n"); @@ -56,6 +61,7 @@ void insert_event(char *file_name) { write(myfd, "END:VCALENDAR\r\n", strlen("END:VCALENDAR\r\n")); close(myfd); + free(time_zone); exit(0); } diff --git a/src/main.c b/src/main.c index 07fe3a7..f3d5d8f 100644 --- a/src/main.c +++ b/src/main.c @@ -1,12 +1,11 @@ #include "cli_arg_parsing.h" #include "date_time_handling.h" #include "list_handling.h" -#include "cut_string.h" +#include "string_handling.h" #include "move_lines.h" #include "read_until_nl.h" #include "read_until_string.h" #include "seek_string_a.h" -#include "remove_whitespace.h" #include #include #include diff --git a/src/remove_whitespace.c b/src/remove_whitespace.c deleted file mode 100644 index 646e2aa..0000000 --- a/src/remove_whitespace.c +++ /dev/null @@ -1,45 +0,0 @@ -// this function removes all new lines and carriage returns from a string -// you might want to write a new function that replaces '\r' and '\n' -// with a delimiter of user's choice - -#include - -void remove_nl_and_cr(char raw_string[]) { - char processed_string[strlen(raw_string)]; - - // counter for num of elements of processed_string - int j = 0; - for (int i = 0; i +#include + +void cut_string(char my_string[], char delimiter, int side) { + char part1[256] = ""; + char part2[256] = ""; + + int split = 0; + + int j = 0; + for (int i = 0; i < strlen(my_string); i++) { + if (my_string[i] == delimiter) { + if (split == 0) { + split = 1; + continue; + } + } + + if (split == 0) { + part1[i] = my_string[i]; + } else { + part2[j] = my_string[i]; + j++; + } + } + + memset(my_string, '\0', strlen(my_string)); + if (side == 0) { + strcpy(my_string, part1); + } else { + strcpy(my_string, part2); + } +} + +// this function removes all new lines and carriage returns from a string +// you might want to write a new function that replaces '\r' and '\n' +// with a delimiter of user's choice +void remove_nl_and_cr(char raw_string[]) { + char processed_string[strlen(raw_string)]; + + // counter for num of elements of processed_string + int j = 0; + for (int i = 0; i= occurence) { + tmp_string[j] = raw_string[i]; + j++; + } + } + strcpy(raw_string, tmp_string); + free(tmp_string); +} diff --git a/src/string_handling.h b/src/string_handling.h new file mode 100644 index 0000000..114e836 --- /dev/null +++ b/src/string_handling.h @@ -0,0 +1,8 @@ +#pragma once + +// cut string into two parts and choose one part +// side is 0 or 1 +void cut_string(char my_string[], char delimiter, int side); +void remove_nl_and_cr(char raw_string[]); +void remove_whitespace(char raw_string[]); +void remove_until_delim(char raw_string[], char delimiter, int occurence);