diary

Text-based journaling program
git clone https://git.in0rdr.ch/diary.git
Log | Files | Refs | README | LICENSE

commit 73653992eaa307d59685d00ce742f46c4fd737af
parent d754b9dde05989865a69aaeebf8ed84c631628df
Author: Andreas Gruhler <agruhl@gmx.ch>
Date:   Sun,  3 Oct 2021 00:08:03 +0200

new file for import functionality

Diffstat:
MMakefile | 2+-
Msrc/diary.h | 1+
Asrc/import.c | 27+++++++++++++++++++++++++++
Asrc/import.h | 13+++++++++++++
Msrc/utils.c | 47+++++++++++++++++++++--------------------------
Msrc/utils.h | 3+--
6 files changed, 64 insertions(+), 29 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,6 +1,6 @@ TARGET = diary SRCDIR = src/ -_SRC = utils.c caldav.c diary.c +_SRC = import.c utils.c caldav.c diary.c SRC = $(addprefix $(SRCDIR), $(_SRC)) PREFIX ?= /usr/local BINDIR ?= $(DESTDIR)$(PREFIX)/bin diff --git a/src/diary.h b/src/diary.h @@ -17,6 +17,7 @@ #include <locale.h> #include <langinfo.h> #include "utils.h" +#include "import.h" #include "caldav.h" #define XDG_CONFIG_HOME_FALLBACK "~/.config" diff --git a/src/import.c b/src/import.c @@ -0,0 +1,26 @@ +#include "import.h" + +/* Import journal entries from an ics file */ +void ics_import(const char* ics_input) { + FILE* pfile = fopen(ics_input, "r"); + + fseek(pfile, 0, SEEK_END); + long ics_bytes = ftell(pfile) + 1; + rewind(pfile); + + char* ics = malloc(ics_bytes); + fread(ics, 1, ics_bytes, pfile); + fclose(pfile); + + ics[ics_bytes] = 0; + // fprintf(stderr, "Import ICS file: %s\n", ics); + + long search_pos = 0; + char* desc = extract_ical_field(ics, "BEGIN", &search_pos, false); + if (desc != NULL) { + fprintf(stderr, "Import DESCRIPTION: %s\n", desc); + fprintf(stderr, "Search pos: %li\n", search_pos); + free(desc); + } + free(ics); +} +\ No newline at end of file diff --git a/src/import.h b/src/import.h @@ -0,0 +1,12 @@ +#ifndef DIARY_IMPORT_H +#define DIARY_IMPORT_H + +#include <stdio.h> +#include <stdlib.h> +#include <regex.h> +#include <stdbool.h> +#include "utils.h" + +void ics_import(const char* ics_input); + +#endif +\ No newline at end of file diff --git a/src/utils.c b/src/utils.c @@ -210,7 +210,10 @@ char* unfold(const char* str) { return buf; } -char* extract_ical_field(const char* ics, char* key, bool multiline) { +/* Find ical key in string. The value of 'start_pos' is set to the start position + of the value (match) after the colon (':'). +*/ +char* extract_ical_field(const char* ics, char* key, long* start_pos, bool multiline) { regex_t re; regmatch_t pm[1]; char key_regex[strlen(key) + 1]; @@ -241,21 +244,34 @@ char* extract_ical_field(const char* ics, char* key, bool multiline) { fprintf(stderr, "Extracted ical result value: %s\n", res); fprintf(stderr, "Extracted ical result size: %li\n", strlen(res)); + fprintf(stderr, "Sizeof ics: %li\n", strlen(ics)); + fprintf(stderr, "Start pos: %li\n", *start_pos); + fprintf(stderr, "Res: %s\n", res); + *start_pos = res - icscp; + fprintf(stderr, "Start pos: %li\n", *start_pos); + if (strlen(res) == 0) { - // empty remote description + // empty remote value res = NULL; } else if (multiline) { - res = unfold(ics + (res - icscp)); + res = unfold(icscp + *start_pos); } break; } // key not in this line, advance line res = strtok(NULL, "\n"); } - fprintf(stderr, "Sizeof ics: %li\n", strlen(ics)); + char* buf = malloc(strlen(res) + 1); + if (buf == NULL) { + perror("malloc failed"); + return NULL; + } + strcpy(buf, res); + + regfree(&re); free(icscp); - return res; + return buf; } // Return expanded file path @@ -319,27 +335,6 @@ void fpath(const char* dir, size_t dir_size, const struct tm* date, char** rpath strcat(*rpath, dstr); } -/* Import journal entries from an ics file */ -void ics_import(const char* ics_input) { - FILE* pfile = fopen(ics_input, "r"); - - fseek(pfile, 0, SEEK_END); - long ics_bytes = ftell(pfile) + 1; - rewind(pfile); - - char* ics = malloc(ics_bytes); - fread(ics, 1, ics_bytes, pfile); - fclose(pfile); - - ics[ics_bytes] = 0; - // fprintf(stderr, "Import ICS file: %s\n", ics); - - - char* desc = extract_ical_field(ics, "DESCRIPTION", true); - fprintf(stderr, "Import DESCRIPTION: %s\n", desc); - free (desc); -} - config CONFIG = { .range = 1, .weekday = 1, diff --git a/src/utils.h b/src/utils.h @@ -26,11 +26,10 @@ void update_date(WINDOW* header, struct tm* curs_date); char* extract_json_value(const char* json, char* key, bool quoted); char* fold(const char* str); char* unfold(const char* str); -char* extract_ical_field(const char* ical, char* key, bool multline); +char* extract_ical_field(const char* ical, char* key, long* start_pos, bool multline); char* expand_path(const char* str); char* strrstr(char *haystack, char *needle); void fpath(const char* dir, size_t dir_size, const struct tm* date, char** rpath, size_t rpath_size); -void ics_import(const char* ics_input); typedef struct {