first commit
This commit is contained in:
commit
85c68bc097
7
Makefile
Normal file
7
Makefile
Normal file
@ -0,0 +1,7 @@
|
||||
debug:
|
||||
gcc -Wall -g *.c
|
||||
gdb a.out
|
||||
|
||||
run:
|
||||
gcc -Wall ./src/*.c
|
||||
./a.out
|
314
calendar.ics
Normal file
314
calendar.ics
Normal file
@ -0,0 +1,314 @@
|
||||
BEGIN:VCALENDAR
|
||||
CALSCALE:GREGORIAN
|
||||
PRODID:-//Ximian//NONSGML Evolution Calendar//EN
|
||||
VERSION:2.0
|
||||
X-EVOLUTION-DATA-REVISION:2023-07-19T15:30:34.391871Z(3)
|
||||
BEGIN:VTIMEZONE
|
||||
TZID:/freeassociation.sourceforge.net/Europe/Berlin
|
||||
X-LIC-LOCATION:Europe/Berlin
|
||||
BEGIN:STANDARD
|
||||
TZNAME:CET
|
||||
TZOFFSETFROM:+005328
|
||||
TZOFFSETTO:+0100
|
||||
DTSTART:18930401T000000
|
||||
END:STANDARD
|
||||
BEGIN:DAYLIGHT
|
||||
TZNAME:CEST
|
||||
TZOFFSETFROM:+0100
|
||||
TZOFFSETTO:+0200
|
||||
DTSTART:19160430T230000
|
||||
END:DAYLIGHT
|
||||
BEGIN:STANDARD
|
||||
TZNAME:CET
|
||||
TZOFFSETFROM:+0200
|
||||
TZOFFSETTO:+0100
|
||||
DTSTART:19161001T010000
|
||||
END:STANDARD
|
||||
BEGIN:DAYLIGHT
|
||||
TZNAME:CEST
|
||||
TZOFFSETFROM:+0100
|
||||
TZOFFSETTO:+0200
|
||||
DTSTART:19170416T020000
|
||||
RRULE:FREQ=YEARLY;UNTIL=19180415T010000Z;BYDAY=3MO;BYMONTH=4
|
||||
END:DAYLIGHT
|
||||
BEGIN:STANDARD
|
||||
TZNAME:CET
|
||||
TZOFFSETFROM:+0200
|
||||
TZOFFSETTO:+0100
|
||||
DTSTART:19170917T030000
|
||||
RRULE:FREQ=YEARLY;UNTIL=19180916T010000Z;BYDAY=3MO;BYMONTH=9
|
||||
END:STANDARD
|
||||
BEGIN:DAYLIGHT
|
||||
TZNAME:CEST
|
||||
TZOFFSETFROM:+0100
|
||||
TZOFFSETTO:+0200
|
||||
DTSTART:19400401T020000
|
||||
RDATE:19430329T020000
|
||||
END:DAYLIGHT
|
||||
BEGIN:STANDARD
|
||||
TZNAME:CET
|
||||
TZOFFSETFROM:+0200
|
||||
TZOFFSETTO:+0100
|
||||
DTSTART:19421102T030000
|
||||
END:STANDARD
|
||||
BEGIN:STANDARD
|
||||
TZNAME:CET
|
||||
TZOFFSETFROM:+0200
|
||||
TZOFFSETTO:+0100
|
||||
DTSTART:19431004T030000
|
||||
RRULE:FREQ=YEARLY;UNTIL=19441002T010000Z;BYDAY=1MO;BYMONTH=10
|
||||
END:STANDARD
|
||||
BEGIN:DAYLIGHT
|
||||
TZNAME:CEST
|
||||
TZOFFSETFROM:+0100
|
||||
TZOFFSETTO:+0200
|
||||
DTSTART:19440403T020000
|
||||
RRULE:FREQ=YEARLY;UNTIL=19450402T010000Z;BYDAY=1MO;BYMONTH=4
|
||||
END:DAYLIGHT
|
||||
BEGIN:DAYLIGHT
|
||||
TZNAME:CEMT
|
||||
TZOFFSETFROM:+0200
|
||||
TZOFFSETTO:+0300
|
||||
DTSTART:19450524T020000
|
||||
END:DAYLIGHT
|
||||
BEGIN:DAYLIGHT
|
||||
TZNAME:CEST
|
||||
TZOFFSETFROM:+0300
|
||||
TZOFFSETTO:+0200
|
||||
DTSTART:19450924T030000
|
||||
END:DAYLIGHT
|
||||
BEGIN:STANDARD
|
||||
TZNAME:CET
|
||||
TZOFFSETFROM:+0200
|
||||
TZOFFSETTO:+0100
|
||||
DTSTART:19451118T030000
|
||||
RDATE:19461007T030000
|
||||
END:STANDARD
|
||||
BEGIN:DAYLIGHT
|
||||
TZNAME:CEST
|
||||
TZOFFSETFROM:+0100
|
||||
TZOFFSETTO:+0200
|
||||
DTSTART:19460414T020000
|
||||
END:DAYLIGHT
|
||||
BEGIN:DAYLIGHT
|
||||
TZNAME:CEST
|
||||
TZOFFSETFROM:+0100
|
||||
TZOFFSETTO:+0200
|
||||
DTSTART:19470406T030000
|
||||
END:DAYLIGHT
|
||||
BEGIN:DAYLIGHT
|
||||
TZNAME:CEMT
|
||||
TZOFFSETFROM:+0200
|
||||
TZOFFSETTO:+0300
|
||||
DTSTART:19470511T030000
|
||||
END:DAYLIGHT
|
||||
BEGIN:DAYLIGHT
|
||||
TZNAME:CEST
|
||||
TZOFFSETFROM:+0300
|
||||
TZOFFSETTO:+0200
|
||||
DTSTART:19470629T030000
|
||||
END:DAYLIGHT
|
||||
BEGIN:STANDARD
|
||||
TZNAME:CET
|
||||
TZOFFSETFROM:+0200
|
||||
TZOFFSETTO:+0100
|
||||
DTSTART:19471005T030000
|
||||
RRULE:FREQ=YEARLY;UNTIL=19491002T010000Z;BYDAY=1SU;BYMONTH=10
|
||||
END:STANDARD
|
||||
BEGIN:DAYLIGHT
|
||||
TZNAME:CEST
|
||||
TZOFFSETFROM:+0100
|
||||
TZOFFSETTO:+0200
|
||||
DTSTART:19480418T020000
|
||||
RDATE:19490410T020000
|
||||
RDATE:19800406T020000
|
||||
END:DAYLIGHT
|
||||
BEGIN:STANDARD
|
||||
TZNAME:CET
|
||||
TZOFFSETFROM:+0200
|
||||
TZOFFSETTO:+0100
|
||||
DTSTART:19800928T030000
|
||||
RRULE:FREQ=YEARLY;UNTIL=19950924T010000Z;BYDAY=-1SU;BYMONTH=9
|
||||
END:STANDARD
|
||||
BEGIN:DAYLIGHT
|
||||
TZNAME:CEST
|
||||
TZOFFSETFROM:+0100
|
||||
TZOFFSETTO:+0200
|
||||
DTSTART:19810329T020000
|
||||
RRULE:FREQ=YEARLY;UNTIL=20370329T010000Z;BYDAY=-1SU;BYMONTH=3
|
||||
END:DAYLIGHT
|
||||
BEGIN:STANDARD
|
||||
TZNAME:CET
|
||||
TZOFFSETFROM:+0200
|
||||
TZOFFSETTO:+0100
|
||||
DTSTART:19961027T030000
|
||||
RRULE:FREQ=YEARLY;UNTIL=20361026T010000Z;BYDAY=-1SU;BYMONTH=10
|
||||
END:STANDARD
|
||||
BEGIN:STANDARD
|
||||
TZNAME:CET
|
||||
TZOFFSETFROM:+0200
|
||||
TZOFFSETTO:+0100
|
||||
DTSTART:20371025T030000
|
||||
RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10
|
||||
END:STANDARD
|
||||
BEGIN:DAYLIGHT
|
||||
TZNAME:CEST
|
||||
TZOFFSETFROM:+0100
|
||||
TZOFFSETTO:+0200
|
||||
DTSTART:20380328T020000
|
||||
RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=3
|
||||
END:DAYLIGHT
|
||||
END:VTIMEZONE
|
||||
BEGIN:VEVENT
|
||||
UID:fdcb901ffea509c9993c7ccd1ede47148f499022
|
||||
DTSTAMP:20230510T153553Z
|
||||
DTSTART;TZID=/freeassociation.sourceforge.net/Europe/Berlin:
|
||||
20230612T090000
|
||||
DTEND;TZID=/freeassociation.sourceforge.net/Europe/Berlin:
|
||||
20230612T092500
|
||||
SEQUENCE:2
|
||||
SUMMARY:12:00 Werkstatt
|
||||
TRANSP:OPAQUE
|
||||
CLASS:PUBLIC
|
||||
CREATED:20230510T153646Z
|
||||
LAST-MODIFIED:20230510T153646Z
|
||||
END:VEVENT
|
||||
BEGIN:VEVENT
|
||||
UID:cde7c41ee121397509f0d88b7c6d188f0508b4ed
|
||||
DTSTAMP:20230510T153553Z
|
||||
DTSTART;VALUE=DATE:20230612
|
||||
DTEND;VALUE=DATE:20230613
|
||||
SEQUENCE:2
|
||||
SUMMARY:HOME OFFICE
|
||||
TRANSP:OPAQUE
|
||||
CLASS:PUBLIC
|
||||
CREATED:20230510T153655Z
|
||||
LAST-MODIFIED:20230510T153655Z
|
||||
END:VEVENT
|
||||
BEGIN:VEVENT
|
||||
UID:c8297cf0792b5910f7b270b1477daf9d6dfcd4b2
|
||||
DTSTAMP:20230518T085020Z
|
||||
DTSTART;TZID=/freeassociation.sourceforge.net/Europe/Berlin:
|
||||
20230518T130000
|
||||
DTEND;TZID=/freeassociation.sourceforge.net/Europe/Berlin:
|
||||
20230518T140000
|
||||
SEQUENCE:2
|
||||
SUMMARY:go for a walk
|
||||
TRANSP:OPAQUE
|
||||
CLASS:PUBLIC
|
||||
CREATED:20230518T091107Z
|
||||
LAST-MODIFIED:20230518T091107Z
|
||||
END:VEVENT
|
||||
BEGIN:VEVENT
|
||||
UID:fddba04ef1150f1d725fd62fa4da720362432196
|
||||
DTSTAMP:20230518T085020Z
|
||||
DTSTART;TZID=/freeassociation.sourceforge.net/Europe/Berlin:
|
||||
20230519T090000
|
||||
DTEND;TZID=/freeassociation.sourceforge.net/Europe/Berlin:
|
||||
20230519T170000
|
||||
SEQUENCE:2
|
||||
SUMMARY:working
|
||||
TRANSP:OPAQUE
|
||||
CLASS:PUBLIC
|
||||
CREATED:20230518T091320Z
|
||||
LAST-MODIFIED:20230518T091320Z
|
||||
END:VEVENT
|
||||
BEGIN:VEVENT
|
||||
UID:580febe9d714174e36fc604120dd2bf73a745bae
|
||||
DTSTAMP:20230518T085020Z
|
||||
DTSTART;TZID=/freeassociation.sourceforge.net/Europe/Berlin:
|
||||
20230519T180000
|
||||
DTEND;TZID=/freeassociation.sourceforge.net/Europe/Berlin:
|
||||
20230519T190000
|
||||
SEQUENCE:2
|
||||
SUMMARY:fly to New York
|
||||
TRANSP:OPAQUE
|
||||
CLASS:PUBLIC
|
||||
CREATED:20230518T091406Z
|
||||
LAST-MODIFIED:20230518T091406Z
|
||||
END:VEVENT
|
||||
BEGIN:VEVENT
|
||||
UID:7ed683872694a9553fec9b161b4b873059f21380
|
||||
DTSTAMP:20230518T085020Z
|
||||
DTSTART;TZID=/freeassociation.sourceforge.net/Europe/Berlin:
|
||||
20230519T170000
|
||||
DTEND;TZID=/freeassociation.sourceforge.net/Europe/Berlin:
|
||||
20230519T173000
|
||||
SEQUENCE:2
|
||||
SUMMARY:buy screws and dowels
|
||||
TRANSP:OPAQUE
|
||||
CLASS:PUBLIC
|
||||
CREATED:20230518T091446Z
|
||||
LAST-MODIFIED:20230518T091446Z
|
||||
END:VEVENT
|
||||
BEGIN:VEVENT
|
||||
UID:6c7d898399f4f598af4590993e190b2b0ffe6710
|
||||
DTSTAMP:20230518T085020Z
|
||||
DTSTART;TZID=/freeassociation.sourceforge.net/Europe/Berlin:
|
||||
20230518T170000
|
||||
DTEND;TZID=/freeassociation.sourceforge.net/Europe/Berlin:
|
||||
20230518T171500
|
||||
SEQUENCE:2
|
||||
SUMMARY:take a shower
|
||||
TRANSP:OPAQUE
|
||||
CLASS:PUBLIC
|
||||
CREATED:20230518T091708Z
|
||||
LAST-MODIFIED:20230518T091708Z
|
||||
END:VEVENT
|
||||
BEGIN:VEVENT
|
||||
UID:6c5e539ca1c266d2df74d02567a3678e8fd31dd1
|
||||
DTSTAMP:20230601T155814Z
|
||||
DTSTART;TZID=/freeassociation.sourceforge.net/Europe/Berlin:
|
||||
20230617T073000
|
||||
DTEND;TZID=/freeassociation.sourceforge.net/Europe/Berlin:
|
||||
20230617T093000
|
||||
SEQUENCE:3
|
||||
SUMMARY:take exam
|
||||
TRANSP:OPAQUE
|
||||
DESCRIPTION:um 8:00 startet das exam\num 7:30 kann man schon mit einem
|
||||
"check-in" starten\n\nAUSWEIS!\nWEBCAM!\nHEADSET!
|
||||
CLASS:PUBLIC
|
||||
CREATED:20230601T165026Z
|
||||
LAST-MODIFIED:20230601T165058Z
|
||||
END:VEVENT
|
||||
BEGIN:VEVENT
|
||||
UID:251b5064c6f62b6c99304045e6f3c1a7a4e6f840
|
||||
DTSTAMP:20230604T154734Z
|
||||
DTSTART;VALUE=DATE:20230608
|
||||
DTEND;VALUE=DATE:20230609
|
||||
SEQUENCE:2
|
||||
SUMMARY:FEIERTAG
|
||||
TRANSP:OPAQUE
|
||||
CLASS:PUBLIC
|
||||
CREATED:20230604T154847Z
|
||||
LAST-MODIFIED:20230604T154847Z
|
||||
END:VEVENT
|
||||
BEGIN:VEVENT
|
||||
UID:a7cf77db2adcfe7b82d6cc071adc7cb858586cd2
|
||||
DTSTAMP:20230717T153059Z
|
||||
DTSTART;TZID=/freeassociation.sourceforge.net/Europe/Berlin:
|
||||
20230729T090000
|
||||
DTEND;TZID=/freeassociation.sourceforge.net/Europe/Berlin:
|
||||
20230729T103000
|
||||
SEQUENCE:2
|
||||
SUMMARY:9:00 exam
|
||||
TRANSP:OPAQUE
|
||||
CLASS:PUBLIC
|
||||
CREATED:20230717T153132Z
|
||||
LAST-MODIFIED:20230717T153132Z
|
||||
END:VEVENT
|
||||
BEGIN:VEVENT
|
||||
UID:ce6cc0e3ca1bb6a22783ace61034b12c57325b4e
|
||||
DTSTAMP:20230719T152822Z
|
||||
DTSTART;TZID=/freeassociation.sourceforge.net/Europe/Berlin:
|
||||
20230831T180000
|
||||
DTEND;TZID=/freeassociation.sourceforge.net/Europe/Berlin:
|
||||
20230831T220000
|
||||
SEQUENCE:3
|
||||
SUMMARY:dentist
|
||||
TRANSP:OPAQUE
|
||||
CLASS:PUBLIC
|
||||
CREATED:20230719T153023Z
|
||||
LAST-MODIFIED:20230719T153034Z
|
||||
END:VEVENT
|
||||
END:VCALENDAR
|
41
src/cut_string.c
Normal file
41
src/cut_string.c
Normal 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
5
src/cut_string.h
Normal 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
12
src/date_time_handling.c
Normal 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
4
src/date_time_handling.h
Normal file
@ -0,0 +1,4 @@
|
||||
#include <time.h>
|
||||
#include <string.h>
|
||||
|
||||
void get_date(char buffer[]);
|
54
src/list_handling.c
Normal file
54
src/list_handling.c
Normal 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
12
src/list_handling.h
Normal 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
66
src/main.c
Normal 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
59
src/move_lines.c
Normal 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
20
src/move_lines.h
Normal 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
16
src/read_until_nl.c
Normal 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
5
src/read_until_nl.h
Normal 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
26
src/read_until_string.c
Normal 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
7
src/read_until_string.h
Normal 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
45
src/remove_whitespace.c
Normal 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
8
src/remove_whitespace.h
Normal 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
22
src/seek_string_a.c
Normal 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
6
src/seek_string_a.h
Normal 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[]);
|
Loading…
x
Reference in New Issue
Block a user