reorganizations and got time zone

This commit is contained in:
bjoernf 2023-09-07 14:29:47 +02:00
parent c19f1b8e6e
commit af4658f472
10 changed files with 136 additions and 103 deletions

View File

@ -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 <string.h>
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);
}
}

View File

@ -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);

View File

@ -1,8 +1,9 @@
#include "date_time_handling.h" #include "date_time_handling.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h>
#include <string.h> #include <string.h>
#include <time.h>
#include <unistd.h>
// buffer needs to contain a string with a strlen of 15 (format: "xxxxxxxxTxxxxxx") // buffer needs to contain a string with a strlen of 15 (format: "xxxxxxxxTxxxxxx")
// or a strlen of 16 (format: "YYYYmmddTHHMMSSZ") // or a strlen of 16 (format: "YYYYmmddTHHMMSSZ")
@ -50,3 +51,17 @@ void print_end_date(char end_date[]) {
printf ("%c%c", time[4], time[5]); 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;
}

View File

@ -2,5 +2,6 @@
#include <string.h> #include <string.h>
void get_date(char buffer[]); void get_date(char buffer[]);
char *get_tz();
void pretty_print_date_time(char date_time[]); void pretty_print_date_time(char date_time[]);
void print_end_date(char end_date[]); void print_end_date(char end_date[]);

View File

@ -1,4 +1,5 @@
#include "insert_event.h" #include "insert_event.h"
#include "string_handling.h"
#include "date_time_handling.h" #include "date_time_handling.h"
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h> #include <stdio.h>
@ -15,6 +16,10 @@ void insert_event(char *file_name) {
uuid_t uuid; uuid_t uuid;
char uuid_str[37] = ""; char uuid_str[37] = "";
char dtstamp[] = "YYYYmmddTHHMMSSZ"; 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"); 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")); write(myfd, "END:VCALENDAR\r\n", strlen("END:VCALENDAR\r\n"));
close(myfd); close(myfd);
free(time_zone);
exit(0); exit(0);
} }

View File

@ -1,12 +1,11 @@
#include "cli_arg_parsing.h" #include "cli_arg_parsing.h"
#include "date_time_handling.h" #include "date_time_handling.h"
#include "list_handling.h" #include "list_handling.h"
#include "cut_string.h" #include "string_handling.h"
#include "move_lines.h" #include "move_lines.h"
#include "read_until_nl.h" #include "read_until_nl.h"
#include "read_until_string.h" #include "read_until_string.h"
#include "seek_string_a.h" #include "seek_string_a.h"
#include "remove_whitespace.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <fcntl.h> #include <fcntl.h>

View File

@ -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 <string.h>
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<strlen(raw_string); i++) {
if (raw_string[i] == '\n' || raw_string[i] == '\r') {
continue;
} else {
processed_string[j] = raw_string[i];
j++;
}
}
processed_string[j] = '\0';
memset(raw_string, '\0', strlen(raw_string));
strcpy(raw_string, processed_string);
}
// remove new lines, carriage returns and spaces
void remove_whitespace(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<strlen(raw_string); i++) {
if (raw_string[i] == '\n' || raw_string[i] == '\r' \
|| raw_string[i] == ' ') {
continue;
} else {
processed_string[j] = raw_string[i];
j++;
}
}
processed_string[j] = '\0';
memset(raw_string, '\0', strlen(raw_string));
strcpy(raw_string, processed_string);
}

View File

@ -1,8 +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
#pragma once
void remove_nl_and_cr(char raw_string[]);
void remove_whitespace(char raw_string[]);

103
src/string_handling.c Normal file
View File

@ -0,0 +1,103 @@
// 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 <string.h>
#include <stdlib.h>
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<strlen(raw_string); i++) {
if (raw_string[i] == '\n' || raw_string[i] == '\r') {
continue;
} else {
processed_string[j] = raw_string[i];
j++;
}
}
processed_string[j] = '\0';
memset(raw_string, '\0', strlen(raw_string));
strcpy(raw_string, processed_string);
}
// remove new lines, carriage returns and spaces
void remove_whitespace(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<strlen(raw_string); i++) {
if (raw_string[i] == '\n' || raw_string[i] == '\r' \
|| raw_string[i] == ' ') {
continue;
} else {
processed_string[j] = raw_string[i];
j++;
}
}
processed_string[j] = '\0';
memset(raw_string, '\0', strlen(raw_string));
strcpy(raw_string, processed_string);
}
void remove_until_delim(char raw_string[], char delimiter, int occurence) {
int chunk = 0;
int j = 0;
char *tmp_string = malloc(strlen(raw_string));
memset(tmp_string, '\0', strlen(raw_string));
for (int i = 0; i < strlen(raw_string); i++) {
if (raw_string[i] == delimiter) {
chunk++;
}
if (chunk >= occurence) {
tmp_string[j] = raw_string[i];
j++;
}
}
strcpy(raw_string, tmp_string);
free(tmp_string);
}

8
src/string_handling.h Normal file
View File

@ -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);