first commit

This commit is contained in:
2023-08-15 16:31:07 +02:00
commit 85c68bc097
20 changed files with 729 additions and 0 deletions

41
src/cut_string.c Normal file
View File

@ -0,0 +1,41 @@
// 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);
}
}

5
src/cut_string.h Normal file
View File

@ -0,0 +1,5 @@
#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);

12
src/date_time_handling.c Normal file
View File

@ -0,0 +1,12 @@
#include "date_time_handling.h"
#include <time.h>
#include <string.h>
void get_date(char buffer[]) {
// add 1 because strlen does not include the null character
size_t buffer_size = strlen(buffer) + 1;
time_t my_unix_ts = time(NULL);
struct tm* my_tm_local = localtime(&my_unix_ts);
strftime(buffer, buffer_size, "%Y%m%dT%H%M%S", my_tm_local);
}

4
src/date_time_handling.h Normal file
View File

@ -0,0 +1,4 @@
#include <time.h>
#include <string.h>
void get_date(char buffer[]);

54
src/list_handling.c Normal file
View File

@ -0,0 +1,54 @@
#include "list_handling.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void print_list(struct event *head) {
while (head != NULL) {
printf("%s\n", head->date);
printf("%s\n", head->summary);
head = head->next;
}
}
void sorted_insert(struct event** head, char date[], char summary[]) {
struct event *new_node = malloc(sizeof(struct event));
strcpy((*new_node).date, date);
strcpy((*new_node).summary, summary);
if (*head == NULL || strcmp((*head)->date, new_node->date) >= 0) {
new_node->next = *head;
*head = new_node;
}
else {
// Locate the node before the point of insertion
struct event* current = *head;
while (current->next!=NULL && strcmp(current->next->date, new_node->date) < 0) {
current = current->next;
}
new_node->next = current->next;
current->next = new_node;
}
}
void free_list(struct event *head)
{
struct event* tmp;
while (head != NULL)
{
tmp = head;
head = head->next;
free(tmp);
}
}
void print_upcoming(struct event *head, char current_date[]) {
while (head != NULL) {
if (strcmp(head->date, current_date) >= 0) {
printf("%s\n", head->date);
printf("%s\n", head->summary);
}
head = head->next;
}
}

12
src/list_handling.h Normal file
View File

@ -0,0 +1,12 @@
#pragma once
struct event {
char summary[256];
char date[256];
struct event *next;
};
void print_list(struct event *head);
void sorted_insert(struct event **head, char date[], char summary[]);
void free_list(struct event *head);
void print_upcoming(struct event *head, char current_date[]);

66
src/main.c Normal file
View File

@ -0,0 +1,66 @@
#include "date_time_handling.h"
#include "list_handling.h"
#include "cut_string.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 <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <limits.h>
int main() {
const char ICS_PATH[] = "calendar.ics";
char my_line[4096] = "";
int myfd = open(ICS_PATH, O_RDONLY);
if (myfd == -1) {
perror ("Error opening file");
return 1;
}
// initialize linked list
struct event *head = NULL;
static char current_date[] = "xxxxxxxxTxxxxxx";
get_date(current_date);
printf ("Current date and time: %s\n\n", current_date);
char date[256] = "";
char summary[256] = "";
while(read_until_nl(myfd, my_line)) {
if (strncmp(my_line, "BEGIN:VEVENT", 12) == 0) {
memset(my_line, '\0', sizeof(my_line));
// go to DTSTART, but dont write to a variable
seek_string_a(myfd, "DTSTART");
read_until_string(myfd, my_line, "DTEND");
remove_whitespace(my_line);
cut_string(my_line, ':', 1);
strcpy(date, my_line);
memset(my_line, '\0', sizeof(my_line));
seek_string_a(myfd, "SUMMARY:");
read_until_string(myfd, my_line, "TRANSP:");
remove_nl_and_cr(my_line);
strcpy(summary, my_line);
memset(my_line, '\0', sizeof(my_line));
sorted_insert(&head, date, summary);
memset(date, '\0', sizeof(date));
memset(summary, '\0', sizeof(summary));
}
memset(my_line, '\0', sizeof(my_line));
}
//print_list(head);
print_upcoming(head, current_date);
free_list(head);
return 0;
}

59
src/move_lines.c Normal file
View File

@ -0,0 +1,59 @@
// functions that can move the file position conveniently
// remember that the last line of a file is usually empty and goes from '\n' to EOF
#include "move_lines.h"
#include <unistd.h>
// function to move back/up
void go_back_x_lines(int fd, int lines) {
for (int i = 0; i < lines; i++) {
seek_previous_line(fd);
}
}
void seek_previous_line(int fd) {
seek_line_start(fd);
lseek(fd, -1, SEEK_CUR);
seek_line_start(fd);
}
// set file position to the beginning of the line
void seek_line_start(int fd) {
char cur_char = '\0';
while (cur_char != '\n') {
int ret_lseek = lseek(fd, -1, SEEK_CUR);
if (ret_lseek == -1)
break;
read(fd, &cur_char, 1);
if (cur_char != '\n')
lseek(fd, -1, SEEK_CUR);
}
}
// function to move forward/down
void go_forward_x_lines(int fd, int lines) {
for (int i = 0; i < lines; i++) {
seek_next_line(fd);
}
}
void seek_next_line(int fd) {
seek_line_end(fd);
lseek(fd, 1, SEEK_CUR);
}
// set file position to the end of the line
void seek_line_end(int fd) {
char cur_char = '\0';
while (cur_char != '\n') {
// return code 0 of read indicates EOF
if (read(fd, &cur_char, 1) == 0)
break;
}
lseek(fd, -1, SEEK_CUR);
}

20
src/move_lines.h Normal file
View File

@ -0,0 +1,20 @@
// functions that can move the file position conveniently
// remember that the last line of a file is usually empty and goes from '\n' to EOF
#pragma once
// function to move back/up
void go_back_x_lines(int fd, int lines);
void seek_previous_line(int fd);
// set file position to the beginning of the line
void seek_line_start(int fd);
// function to move forward/down
void go_forward_x_lines(int fd, int lines);
void seek_next_line(int fd);
// set file position to the end of the line
void seek_line_end(int fd);

16
src/read_until_nl.c Normal file
View File

@ -0,0 +1,16 @@
#include <unistd.h>
// this function will read until the next newline character
// and save the characters in a string you provide in arg2
int read_until_nl(int fd, char line[]) {
int i = 0;
int ret_read = 0;
while ((ret_read = read(fd, &line[i], 1))) {
if (line[i] == '\n') {
break;
}
i++;
}
return ret_read;
}

5
src/read_until_nl.h Normal file
View File

@ -0,0 +1,5 @@
#pragma once
// this function will read until the next newline character
// and save the characters in a string you provide in arg2
int read_until_nl(int fd, char line[]);

26
src/read_until_string.c Normal file
View File

@ -0,0 +1,26 @@
// the same as read_until_string, but this function not only truncates but fills remaining chars with null characters
// this function is cleaner and nicer to work with, but not as fast as "read_until_string"
// ct means "clean truncate"
// return 1 on success, -1 when buffer is NULL
#include <unistd.h>
#include <string.h>
void read_until_string(int fd, char buffer[], char search_string[]) {
int i = 0;
int j = 0;
while(read(fd, &buffer[i], 1)) {
if (buffer[i] == search_string[j]) {
j++;
} else {
j = 0;
}
if (j == (strlen(search_string))) {
break;
}
i++;
}
size_t truncate_position = strlen(buffer) - strlen(search_string);
memset(&buffer[truncate_position], '\0', truncate_position);
}

7
src/read_until_string.h Normal file
View File

@ -0,0 +1,7 @@
// the same as read_until_string, but this function not only truncates but fills remaining chars with null characters
// this function is cleaner and nicer to work with, but not as fast as "read_until_string"
// ct means "clean truncate"
#pragma once
void read_until_string(int fd, char buffer[], char search_string[]);

45
src/remove_whitespace.c Normal file
View File

@ -0,0 +1,45 @@
// 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);
}

8
src/remove_whitespace.h Normal file
View File

@ -0,0 +1,8 @@
// 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[]);

22
src/seek_string_a.c Normal file
View File

@ -0,0 +1,22 @@
// this function will seek for a string in an open file
// it will place the file position AFTER that string
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
void seek_string_a(int fd, char search_string[]) {
int j = 0;
char char_reader = '\0';
while(read(fd, &char_reader, 1)) {
if (char_reader == search_string[j]) {
j++;
} else {
j = 0;
}
if (j == (strlen(search_string))) {
break;
}
}
}

6
src/seek_string_a.h Normal file
View File

@ -0,0 +1,6 @@
// this function will seek for a string in an open file
// it will place the file position AFTER that string
#pragma once
void seek_string_a(int fd, char search_string[]);