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,5 +1,11 @@
const char *iCalHeader = "BEGIN:VCALENDAR\r\n" //definition of commands for final iCal file #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" "VERSION:2.0\r\n"
"PRODID:-//hacksw/handcal//NONSGML v1.0//EN\r\n"; "PRODID:-//hacksw/handcal//NONSGML v1.0//EN\r\n";
const char *iCalEvent = "BEGIN:VEVENT\r\n" const char *iCalEvent = "BEGIN:VEVENT\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) { int exportiCal(llist *events_ll) {
llist *ev_ll = events_ll; //input from llist llist *ev_ll = events_ll; // input from llist
llistPrintE(ev_ll); llistPrintE(ev_ll);
printf("%s", iCalHeader); 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; struct tm lc;
localtime_r(&now, &lc); localtime_r(&now, &lc);
@ -76,7 +82,7 @@ int exportiCal(llist *events_ll) {
strcat(nameBuf, "dayplan.ics"); strcat(nameBuf, "dayplan.ics");
FILE *fp = fopen(nameBuf, "w"); FILE *fp = fopen(nameBuf, "w");
if (fp == NULL) { if (fp == NULL) {
planLog("fopen failed!!", 1); printf("fopen failed!!");
return 1; return 1;
} }
// write iCal header to file // write iCal header to file
@ -94,7 +100,8 @@ int exportiCal(llist *events_ll) {
char timeStartBuf[17]; char timeStartBuf[17];
char timeEndBuf[17]; char timeEndBuf[17];
char timeStamp[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); printf("%s\n", timeStamp);
strftime(timeStartBuf, 17, "%Y%m%dT%k%M%SZ", &startlc); strftime(timeStartBuf, 17, "%Y%m%dT%k%M%SZ", &startlc);
printf("%s\n", timeStartBuf); printf("%s\n", timeStartBuf);
@ -115,16 +122,17 @@ int exportiCal(llist *events_ll) {
return 0; 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"; const char *taskFileFormat = "%s,%lu,%lu,%d,%lu\n";
int taskLlToFile(llist *tll) { int taskLlToFile(llist *tll) {
// open file // open file
FILE *fp = fopen("db.csv", "w"); FILE *fp = fopen("db.csv", "w");
if (fp == NULL) if (fp == NULL)
return -1; //if file cannot be opend, return -1 return -1; // if file cannot be opend, return -1
llist *c = tll; llist *c = tll;
while (c != NULL) { 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, fprintf(fp, taskFileFormat, ct->name, ct->created, ct->deadline,
ct->priority, ct->spare); ct->priority, ct->spare);
c = c->next; 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; return events_ll;
} }
//
const char *iCalHeader = "BEGIN:VCALENDAR\r\n" // const char *iCalHeader = "BEGIN:VCALENDAR\r\n"
"VERSION:2.0\r\n" // "VERSION:2.0\r\n"
"PRODID:-//hacksw/handcal//NONSGML v1.0//EN\r\n"; // "PRODID:-//hacksw/handcal//NONSGML v1.0//EN\r\n";
const char *iCalEvent = "BEGIN:VEVENT\r\n" // const char *iCalEvent = "BEGIN:VEVENT\r\n"
"UID:%s%d\r\n" // "UID:%s%d\r\n"
"DTSTAMP:%s\r\n" // "DTSTAMP:%s\r\n"
"DTSTART:%s\r\n" // "DTSTART:%s\r\n"
"DTEND:%s\r\n" // "DTEND:%s\r\n"
"SUMMARY:%s\r\n" // "SUMMARY:%s\r\n"
"END:VEVENT\r\n"; // "END:VEVENT\r\n";
//
int exportiCal(llist *events_ll) { // int exportiCal(llist *events_ll) {
//
llist *ev_ll = events_ll; // llist *ev_ll = events_ll;
//
llistPrintE(ev_ll); // llistPrintE(ev_ll);
//
printf("%s", iCalHeader); // printf("%s", iCalHeader);
//
time_t now = time(NULL); // time_t now = time(NULL);
struct tm lc; // struct tm lc;
localtime_r(&now, &lc); // localtime_r(&now, &lc);
//
// gen filename & open for write // // gen filename & open for write
char nameBuf[32]; // char nameBuf[32];
strftime(nameBuf, 32 - 12, "%F", &lc); // strftime(nameBuf, 32 - 12, "%F", &lc);
strcat(nameBuf, "dayplan.ics"); // strcat(nameBuf, "dayplan.ics");
FILE *fp = fopen(nameBuf, "w"); // FILE *fp = fopen(nameBuf, "w");
if (fp == NULL) { // if (fp == NULL) {
planLog("fopen failed!!", 1); // planLog("fopen failed!!", 1);
return 1; // return 1;
} // }
// write iCal header to file // // write iCal header to file
fprintf(fp, "%s", iCalHeader); // fprintf(fp, "%s", iCalHeader);
//
// for every event in events_ll create VEVENT str and write to fp // // for every event in events_ll create VEVENT str and write to fp
int count = 0; // int count = 0;
while (ev_ll != NULL) { // while (ev_ll != NULL) {
// gen iCal compatible time str // // gen iCal compatible time str
Event *current = ev_ll->data; // Event *current = ev_ll->data;
struct tm startlc; // struct tm startlc;
struct tm endlc; // struct tm endlc;
localtime_r(&current->plannedStartTime, &startlc); // localtime_r(&current->plannedStartTime, &startlc);
localtime_r(&current->plannedEndTime, &endlc); // localtime_r(&current->plannedEndTime, &endlc);
char timeStartBuf[17]; // char timeStartBuf[17];
char timeEndBuf[17]; // char timeEndBuf[17];
char timeStamp[17]; // char timeStamp[17];
strftime(timeStamp, 17, "%Y%m%dT%k%M%SZ", &lc); // strftime(timeStamp, 17, "%Y%m%dT%k%M%SZ", &lc);
printf("%s\n", timeStamp); // printf("%s\n", timeStamp);
strftime(timeStartBuf, 17, "%Y%m%dT%k%M%SZ", &startlc); // strftime(timeStartBuf, 17, "%Y%m%dT%k%M%SZ", &startlc);
printf("%s\n", timeStartBuf); // printf("%s\n", timeStartBuf);
strftime(timeEndBuf, 17, "%Y%m%dT%k%M%SZ", &endlc); // strftime(timeEndBuf, 17, "%Y%m%dT%k%M%SZ", &endlc);
printf("%s\n", timeEndBuf); // printf("%s\n", timeEndBuf);
//
fprintf(fp, iCalEvent, current->task->name, count, timeStamp, timeStartBuf, // fprintf(fp, iCalEvent, current->task->name, count, timeStamp,
timeEndBuf, current->task->name); // timeStartBuf,
ev_ll = ev_ll->next; // timeEndBuf, current->task->name);
count += 1; // ev_ll = ev_ll->next;
} // count += 1;
// }
// after all events are written end cal with //
// END:VCALENDAR // // after all events are written end cal with
fprintf(fp, "END:VCALENDAR\r\n"); // // END:VCALENDAR
fclose(fp); // fprintf(fp, "END:VCALENDAR\r\n");
// fclose(fp);
return 0; //
} // return 0;
// }

View File

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

View File

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