]> Pileus Git - lackey/blob - src/cal.c
Make prototypes internal to cal.c and view.c
[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         event_t *name##_events(cal_t *cal, year_t year, month_t month, day_t day, int days); \
27         todo_t  *name##_todos(cal_t *cal, year_t year, month_t month, day_t day, int days)
28
29 /* Prototypes */
30 CAL(dummy);
31 CAL(ical);
32
33 event_t *cal_events(year_t year, month_t month, day_t day, int days);
34 todo_t  *cal_todos(year_t year, month_t month, day_t day, int days);
35
36 /* Global data */
37 event_t *EVENTS;
38 todo_t  *TODOS;
39
40 /* Merge events and todos */
41 static void add_event(event_t **first, event_t **last, event_t **next)
42 {
43         if (*last)
44                 (*last)->next = *next;
45         else
46                 (*first) = *next;
47         (*last) = (*next);
48         (*next) = (*next)->next;
49 }
50
51 static void add_todo(todo_t **first, todo_t **last, todo_t **next)
52 {
53         if (*last)
54                 (*last)->next = *next;
55         else
56                 (*first) = *next;
57         (*last) = (*next);
58         (*next) = (*next)->next;
59 }
60
61 static event_t *merge_events(event_t *a, event_t *b)
62 {
63         event_t *first = NULL, *last = NULL;
64         while (a && b)
65                 if (compare(&a->start, &b->start) <= 0)
66                         add_event(&first, &last, &a);
67                 else
68                         add_event(&first, &last, &b);
69         while (a) add_event(&first, &last, &a);
70         while (b) add_event(&first, &last, &b);
71         return first;
72 }
73
74 static todo_t *merge_todos(todo_t *a, todo_t *b)
75 {
76         todo_t *first = NULL, *last = NULL;
77         while (a && b)
78                 if (compare(&a->start, &b->start) <= 0)
79                         add_todo(&first, &last, &a);
80                 else
81                         add_todo(&first, &last, &b);
82         while (a) add_todo(&first, &last, &a);
83         while (b) add_todo(&first, &last, &b);
84         return first;
85 }
86
87 /* Initialize */
88 void cal_init(void)
89 {
90         EVENTS = cal_events(2012, JAN, 0, 366);
91         TODOS  = cal_todos(2012, JAN, 0, 366);
92
93         /* Debug */
94         for (event_t *e = EVENTS; e; e = e->next)
95                 debug("event: %04d-%02d-%02d %02d:%02d: %s - %s",
96                                 e->start.year, e->start.month, e->start.day,
97                                 e->start.hour, e->start.min, e->name, e->desc);
98         for (todo_t *e = TODOS; e; e = e->next)
99                 debug("todo: %04d-%02d-%02d %02d:%02d: %s - %s",
100                                 e->start.year, e->start.month, e->start.day,
101                                 e->start.hour, e->start.min, e->name, e->desc);
102 }
103
104 /* Get events */
105 event_t *cal_events(year_t year, month_t month, day_t day, int days)
106 {
107         event_t *dummy = dummy_events(0, year, month, day, days);
108         event_t *ical  =  ical_events(0, year, month, day, days);
109         return merge_events(dummy, ical);
110 }
111
112 /* Get todos */
113 todo_t *cal_todos(year_t year, month_t month, day_t day, int days)
114 {
115         todo_t *dummy = dummy_todos(0, year, month, day, days);
116         todo_t *ical  =  ical_todos(0, year, month, day, days);
117         return merge_todos(dummy, ical);
118 }