Compare commits

...

5 Commits

7 changed files with 64 additions and 35 deletions

1
.gitignore vendored
View File

@ -1,4 +1,5 @@
icscli
.gitconfig
*.o
*.out
tags

View File

@ -12,7 +12,7 @@ EXECUTABLE = "icscli"
# linking
$(EXECUTABLE): $(OBJ_FILES)
gcc $(CFLAGS) $(OBJ_FILES) -o $(EXECUTABLE) $(LDFLAGS)
$(CC) $(CFLAGS) $(OBJ_FILES) -o $(EXECUTABLE) $(LDFLAGS)
main.o: main.c
$(CC) $(CFLAGS) -c $<

View File

@ -2,16 +2,7 @@
#include "parse_ics.h"
#include "cli_arg_parsing.h"
#include "date_time_handling.h"
#include "string_handling.h"
#include "read_until_nl.h"
#include "read_until_string.h"
#include "seek_string_a.h"
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <limits.h>
#include <unistd.h>
int main(int argc, char **argv) {
char *ics_path = "";
@ -19,35 +10,16 @@ int main(int argc, char **argv) {
get_cli_args(argc, argv, &ics_path, &show_all_events);
char my_event[8192] = "";
char unfolded_event[8192] = "";
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: ");
pretty_print_date_time(current_date);
printf ("\n\n");
while(read_until_nl(myfd, my_event)) {
if (strncmp(my_event, "BEGIN:VEVENT", 12) == 0) {
// include the BEGIN:EVENT to not loose the new line of first field
lseek(myfd, -1, SEEK_CUR);
memset(my_event, '\0', sizeof(my_event));
read_until_string(myfd, my_event, "END:VEVENT");
unfolding_string(my_event, unfolded_event);
parse_event(unfolded_event, &head);
}
memset(my_event, '\0', sizeof(my_event));
}
// initialize linked list
struct event *head = NULL;
parse_ics_file(ics_path, &head);
print_upcoming(head, current_date, show_all_events);

View File

@ -1,11 +1,15 @@
#include "list_handling.h"
#include "parse_ics.h"
#include "read_until_nl.h"
#include "read_until_string.h"
#include "string_handling.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
void parse_event(char event_string[], struct event **head) {
char *start_date = strstr(event_string, "\nDTSTART");
@ -67,3 +71,32 @@ void unfolding_string(char *folded_string, char *unfolded_string) {
}
}
}
/* this function takes the head of an empty initialized event list
* and the path to the ics file
* it will "fill" the list
*/
void parse_ics_file(char *file_path, struct event **head) {
char my_event[8192] = "";
char unfolded_event[8192] = "";
int myfd = open(file_path, O_RDONLY);
if (myfd == -1) {
perror ("Error opening file");
exit(1);
}
while(read_until_nl(myfd, my_event)) {
if (strncmp(my_event, "BEGIN:VEVENT", 12) == 0) {
// include the BEGIN:EVENT to not loose the new line of first field
lseek(myfd, -1, SEEK_CUR);
memset(my_event, '\0', sizeof(my_event));
read_until_string(myfd, my_event, "END:VEVENT");
unfolding_string(my_event, unfolded_event);
parse_event(unfolded_event, head);
}
memset(my_event, '\0', sizeof(my_event));
}
close(myfd);
}

View File

@ -2,3 +2,4 @@
void parse_event(char event_string[], struct event **head);
void unfolding_string(char* folded_string, char* unfolded_string);
void parse_ics_file(char *file_path, struct event **head);

10
unit-tests/Makefile Normal file
View File

@ -0,0 +1,10 @@
CC = gcc
CFLAGS = -Wall -g -O0
test_parse_ics:
$(CC) $(CFLAGS) test_parse_ics.c ../src/parse_ics.c ../src/string_handling.c ../src/list_handling.c ../src/date_time_handling.c ../src/read_until_string.c ../src/read_until_nl.c -o test_parse_ics.out
./test_parse_ics.out
.PHONY:clean
clean:
-rm -vf *.out

View File

@ -0,0 +1,12 @@
#include "../src/list_handling.h"
#include "../src/parse_ics.h"
#include <stdio.h>
int main() {
// initialize empty list
struct event *head = NULL;
parse_ics_file("../tests/calendar.ics", &head);
print_list(head);
}