]> Pileus Git - lackey/blob - src/cal.c
Add simple config file parser
[lackey] / src / cal.c
1 /*
2  * Copyright (C) 2012 Andy Spencer <andy753421@gmail.com>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <stdlib.h>
19
20 #include "util.h"
21 #include "date.h"
22 #include "cal.h"
23
24 /* Macros */
25 #define CAL(name) \
26         void     name##_config(const char *group, const char *name, const char *key, const char *value); \
27         event_t *name##_events(date_t start, date_t end); \
28         todo_t  *name##_todos(date_t start, date_t end)
29
30 /* Prototypes */
31 CAL(dummy);
32 CAL(ical);
33
34 /* Global data */
35 event_t *EVENTS;
36 todo_t  *TODOS;
37
38 /* Local data */
39 static date_t start;
40 static date_t end;
41
42 /* Merge events and todos */
43 static void add_event(event_t **first, event_t **last, event_t **next)
44 {
45         if (*last)
46                 (*last)->next = *next;
47         else
48                 (*first) = *next;
49         (*last) = (*next);
50         (*next) = (*next)->next;
51 }
52
53 static void add_todo(todo_t **first, todo_t **last, todo_t **next)
54 {
55         if (*last)
56                 (*last)->next = *next;
57         else
58                 (*first) = *next;
59         (*last) = (*next);
60         (*next) = (*next)->next;
61 }
62
63 static event_t *merge_events(event_t *a, event_t *b)
64 {
65         event_t *first = NULL, *last = NULL;
66         while (a && b)
67                 if (compare(&a->start, &b->start) <= 0)
68                         add_event(&first, &last, &a);
69                 else
70                         add_event(&first, &last, &b);
71         while (a) add_event(&first, &last, &a);
72         while (b) add_event(&first, &last, &b);
73         return first;
74 }
75
76 static todo_t *merge_todos(todo_t *a, todo_t *b)
77 {
78         todo_t *first = NULL, *last = NULL;
79         while (a && b)
80                 if (compare(&a->start, &b->start) <= 0)
81                         add_todo(&first, &last, &a);
82                 else
83                         add_todo(&first, &last, &b);
84         while (a) add_todo(&first, &last, &a);
85         while (b) add_todo(&first, &last, &b);
86         return first;
87 }
88
89 /* Initialize */
90 void cal_init(void)
91 {
92         /* Load a year's worth of data */
93         cal_load(YEAR-1, DEC, 31-7, 366+7+7);
94
95         /* Debug */
96         for (event_t *e = EVENTS; e; e = e->next)
97                 debug("event: %04d-%02d-%02d %02d:%02d: %s - %s",
98                                 e->start.year, e->start.month, e->start.day,
99                                 e->start.hour, e->start.min, e->name, e->desc);
100         for (todo_t *e = TODOS; e; e = e->next)
101                 debug("todo: %04d-%02d-%02d %02d:%02d: %s - %s",
102                                 e->start.year, e->start.month, e->start.day,
103                                 e->start.hour, e->start.min, e->name, e->desc);
104 }
105
106 /* Load events and todos */
107 void cal_load(year_t year, month_t month, day_t day, int days)
108 {
109         year_t  eyear  = year;
110         month_t emonth = month;
111         day_t   eday   = day;
112         add_days(&eyear, &emonth, &eday, days);
113
114         /* Skip if we already loaded enough info */
115         if (!before(&start, year,  month,  day,  0, 0) &&
116              before(&end,  eyear, emonth, eday, 24, 0))
117                 return;
118
119         /* Free uneeded data */
120         for (event_t *next, *cur = EVENTS; cur; cur = next) {
121                 next = cur->next;
122                 if (cur->name) free(cur->name);
123                 if (cur->desc) free(cur->desc);
124                 if (cur->loc)  free(cur->loc);
125                 if (cur->cat)  free(cur->cat);
126                 free(cur);
127         }
128         for (todo_t *next, *cur = TODOS; cur; cur = next) {
129                 next = cur->next;
130                 if (cur->name) free(cur->name);
131                 if (cur->desc) free(cur->desc);
132                 if (cur->cat)  free(cur->cat);
133                 free(cur);
134         }
135
136         /* Push dates out a bit to avoid reloading,
137          * enough to at least cover the current year */
138         add_days(&year,  &month,  &day, -366);
139         add_days(&eyear, &emonth, &eday, 366);
140         start = (date_t){year,  month,  day};
141         end   = (date_t){eyear, emonth, eday};
142
143         /* Load events */
144         EVENTS = merge_events(
145                 dummy_events(start, end),
146                  ical_events(start, end));
147
148         /* Load todos */
149         TODOS  = merge_todos(
150                 dummy_todos(start, end),
151                  ical_todos(start, end));
152
153 }
154
155 /* Config parser */
156 void cal_config(const char *group, const char *name, const char *key, const char *value)
157 {
158         if (match(group, "dummy"))
159                 dummy_config(group, name, key, value);
160         else if (match(group, "ical"))
161                 ical_config(group, name, key, value);
162 }