modified: src/iCal.c

modified:   src/iCal.h
	modified:   src/planner.c
	modified:   src/planner.h
	modified:   src/ui.c
carla
ketrptr 2024-12-17 10:37:31 +01:00
parent 0943d4f321
commit 809efb512a
5 changed files with 98 additions and 83 deletions

View File

@ -1,7 +1,13 @@
const char *iCalHeader = "BEGIN:VCALENDAR\r\n" //definition of commands for final iCal file
"VERSION:2.0\r\n"
"PRODID:-//hacksw/handcal//NONSGML v1.0//EN\r\n";
#include "llist.h"
#include "planner.h"
#include <stdio.h>
#include <string.h>
const char *iCalHeader =
"BEGIN:VCALENDAR\r\n" // definition of commands for final iCal file
"VERSION:2.0\r\n"
"PRODID:-//hacksw/handcal//NONSGML v1.0//EN\r\n";
const char *iCalEvent = "BEGIN:VEVENT\r\n"
"UID:%s%d\r\n"
"DTSTAMP:%s\r\n"
@ -57,16 +63,16 @@ conflicts.[13]
*/
//exportical function: generates ical file from llist
// exportical function: generates ical file from llist
int exportiCal(llist *events_ll) {
llist *ev_ll = events_ll; //input from llist
llist *ev_ll = events_ll; // input from llist
llistPrintE(ev_ll);
printf("%s", iCalHeader);
time_t now = time(NULL); //current time is captured an formatted
time_t now = time(NULL); // current time is captured an formatted
struct tm lc;
localtime_r(&now, &lc);
@ -76,7 +82,7 @@ int exportiCal(llist *events_ll) {
strcat(nameBuf, "dayplan.ics");
FILE *fp = fopen(nameBuf, "w");
if (fp == NULL) {
planLog("fopen failed!!", 1);
printf("fopen failed!!");
return 1;
}
// write iCal header to file
@ -94,7 +100,8 @@ int exportiCal(llist *events_ll) {
char timeStartBuf[17];
char timeEndBuf[17];
char timeStamp[17];
strftime(timeStamp, 17, "%Y%m%dT%k%M%SZ", &lc); //strftime to match ical format
strftime(timeStamp, 17, "%Y%m%dT%k%M%SZ",
&lc); // strftime to match ical format
printf("%s\n", timeStamp);
strftime(timeStartBuf, 17, "%Y%m%dT%k%M%SZ", &startlc);
printf("%s\n", timeStartBuf);
@ -115,16 +122,17 @@ int exportiCal(llist *events_ll) {
return 0;
}
//export data from llist to csv file
// export data from llist to csv file
const char *taskFileFormat = "%s,%lu,%lu,%d,%lu\n";
int taskLlToFile(llist *tll) {
// open file
FILE *fp = fopen("db.csv", "w");
if (fp == NULL)
return -1; //if file cannot be opend, return -1
if (fp == NULL)
return -1; // if file cannot be opend, return -1
llist *c = tll;
while (c != NULL) {
Task *ct = (Task *)c->data; //loop to go through tasks(name, created time, deadline, priority, etc.)
Task *ct = (Task *)c->data; // loop to go through tasks(name, created time,
// deadline, priority, etc.)
fprintf(fp, taskFileFormat, ct->name, ct->created, ct->deadline,
ct->priority, ct->spare);
c = c->next;

View File

@ -0,0 +1,7 @@
#ifndef iCAL
#define iCAL
#include "llist.h"
int exportiCal(llist *events_ll);
#endif

View File

@ -275,71 +275,72 @@ llist *genPlan(llist *head, time_t timeAvail) {
return events_ll;
}
const char *iCalHeader = "BEGIN:VCALENDAR\r\n"
"VERSION:2.0\r\n"
"PRODID:-//hacksw/handcal//NONSGML v1.0//EN\r\n";
const char *iCalEvent = "BEGIN:VEVENT\r\n"
"UID:%s%d\r\n"
"DTSTAMP:%s\r\n"
"DTSTART:%s\r\n"
"DTEND:%s\r\n"
"SUMMARY:%s\r\n"
"END:VEVENT\r\n";
int exportiCal(llist *events_ll) {
llist *ev_ll = events_ll;
llistPrintE(ev_ll);
printf("%s", iCalHeader);
time_t now = time(NULL);
struct tm lc;
localtime_r(&now, &lc);
// gen filename & open for write
char nameBuf[32];
strftime(nameBuf, 32 - 12, "%F", &lc);
strcat(nameBuf, "dayplan.ics");
FILE *fp = fopen(nameBuf, "w");
if (fp == NULL) {
planLog("fopen failed!!", 1);
return 1;
}
// write iCal header to file
fprintf(fp, "%s", iCalHeader);
// for every event in events_ll create VEVENT str and write to fp
int count = 0;
while (ev_ll != NULL) {
// gen iCal compatible time str
Event *current = ev_ll->data;
struct tm startlc;
struct tm endlc;
localtime_r(&current->plannedStartTime, &startlc);
localtime_r(&current->plannedEndTime, &endlc);
char timeStartBuf[17];
char timeEndBuf[17];
char timeStamp[17];
strftime(timeStamp, 17, "%Y%m%dT%k%M%SZ", &lc);
printf("%s\n", timeStamp);
strftime(timeStartBuf, 17, "%Y%m%dT%k%M%SZ", &startlc);
printf("%s\n", timeStartBuf);
strftime(timeEndBuf, 17, "%Y%m%dT%k%M%SZ", &endlc);
printf("%s\n", timeEndBuf);
fprintf(fp, iCalEvent, current->task->name, count, timeStamp, timeStartBuf,
timeEndBuf, current->task->name);
ev_ll = ev_ll->next;
count += 1;
}
// after all events are written end cal with
// END:VCALENDAR
fprintf(fp, "END:VCALENDAR\r\n");
fclose(fp);
return 0;
}
//
// const char *iCalHeader = "BEGIN:VCALENDAR\r\n"
// "VERSION:2.0\r\n"
// "PRODID:-//hacksw/handcal//NONSGML v1.0//EN\r\n";
// const char *iCalEvent = "BEGIN:VEVENT\r\n"
// "UID:%s%d\r\n"
// "DTSTAMP:%s\r\n"
// "DTSTART:%s\r\n"
// "DTEND:%s\r\n"
// "SUMMARY:%s\r\n"
// "END:VEVENT\r\n";
//
// int exportiCal(llist *events_ll) {
//
// llist *ev_ll = events_ll;
//
// llistPrintE(ev_ll);
//
// printf("%s", iCalHeader);
//
// time_t now = time(NULL);
// struct tm lc;
// localtime_r(&now, &lc);
//
// // gen filename & open for write
// char nameBuf[32];
// strftime(nameBuf, 32 - 12, "%F", &lc);
// strcat(nameBuf, "dayplan.ics");
// FILE *fp = fopen(nameBuf, "w");
// if (fp == NULL) {
// planLog("fopen failed!!", 1);
// return 1;
// }
// // write iCal header to file
// fprintf(fp, "%s", iCalHeader);
//
// // for every event in events_ll create VEVENT str and write to fp
// int count = 0;
// while (ev_ll != NULL) {
// // gen iCal compatible time str
// Event *current = ev_ll->data;
// struct tm startlc;
// struct tm endlc;
// localtime_r(&current->plannedStartTime, &startlc);
// localtime_r(&current->plannedEndTime, &endlc);
// char timeStartBuf[17];
// char timeEndBuf[17];
// char timeStamp[17];
// strftime(timeStamp, 17, "%Y%m%dT%k%M%SZ", &lc);
// printf("%s\n", timeStamp);
// strftime(timeStartBuf, 17, "%Y%m%dT%k%M%SZ", &startlc);
// printf("%s\n", timeStartBuf);
// strftime(timeEndBuf, 17, "%Y%m%dT%k%M%SZ", &endlc);
// printf("%s\n", timeEndBuf);
//
// fprintf(fp, iCalEvent, current->task->name, count, timeStamp,
// timeStartBuf,
// timeEndBuf, current->task->name);
// ev_ll = ev_ll->next;
// count += 1;
// }
//
// // after all events are written end cal with
// // END:VCALENDAR
// fprintf(fp, "END:VCALENDAR\r\n");
// fclose(fp);
//
// return 0;
// }

View File

@ -84,6 +84,6 @@ int cmpEvent(const void *a, const void *b);
*/
llist *genPlan(llist *head, time_t timeAvail);
int exportiCal(llist *head);
// int exportiCal(llist *head);
#endif // !PLANNER

View File

@ -7,6 +7,7 @@
#define _GNU_SOURCE
#include "ui.h"
#include "db.h"
#include "iCal.h"
#include "llist.h"
#include "planner.h" // for subject and event structs
#include <assert.h>
@ -19,8 +20,6 @@
#define localtime_r(T, Tm) (_localtime64_s(Tm, T) ? NULL : Tm)
#endif
const char *dbName = "db.csv";
int main(void) {