annotate bday.c @ 16:79d22407a6be

a lot of refactoring
author markus schnalke <meillo@marmaro.de>
date Mon, 24 Feb 2014 21:11:38 +0100
parents 032af48d590b
children d18a3b2b76bd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
22b6e71de68e initial commit; codebase from birthday; just the needed stuff; substituted getopt by own code
meillo@marmaro.de
parents:
diff changeset
1 /*
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
2 bday -- Birthday/Anniversary reminder
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
3
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
4 (c) 2007,2014 markus schnalke <meillo@marmaro.de>
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
5 (c) 1994-1999 AS Mortimer
8
19c1ad697022 beautifing :-)
meillo@marmaro.de
parents: 7
diff changeset
6
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
7 This program is free software; you can redistribute it and/or
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
8 modify it under the terms of the GNU General Public License as
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
9 published by the Free Software Foundation; either version 2 of the
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
10 License, or (at your option) any later version. You may also
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
11 distribute it under the Artistic License, as comes with Perl.
0
22b6e71de68e initial commit; codebase from birthday; just the needed stuff; substituted getopt by own code
meillo@marmaro.de
parents:
diff changeset
12
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
13 This program is distributed in the hope that it will be useful,
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
0
22b6e71de68e initial commit; codebase from birthday; just the needed stuff; substituted getopt by own code
meillo@marmaro.de
parents:
diff changeset
16
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
17 You should have received a copy of the GNU General Public License
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
18 along with this program; if not, write to the Free Software
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
8
19c1ad697022 beautifing :-)
meillo@marmaro.de
parents: 7
diff changeset
20
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
21 You should also have recieved a copy of the Artistic license with
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
22 this program.
8
19c1ad697022 beautifing :-)
meillo@marmaro.de
parents: 7
diff changeset
23
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
24 =====================================================================
9
4f48b4f86e3d added COPYRIGHT and COPYING; adjusted copyright hints in the code
meillo@marmaro.de
parents: 8
diff changeset
25
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
26 Input is read through standard input. For example: bday < ~/.birthdays
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
27 The input (file) has to have the following format:
0
22b6e71de68e initial commit; codebase from birthday; just the needed stuff; substituted getopt by own code
meillo@marmaro.de
parents:
diff changeset
28
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
29 date flags text
0
22b6e71de68e initial commit; codebase from birthday; just the needed stuff; substituted getopt by own code
meillo@marmaro.de
parents:
diff changeset
30
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
31 where:
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
32 date is YYYY-MM-DD
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
33 flags is ONE or ZERO of
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
34 #ann for an anniversary
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
35 #ev for an event
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
36 and zero or more of
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
37 #w<n> to set the warn-in-advance time to n days
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
38 (don't include the brackets! :)
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
39 #to<date>
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
40 #for<days>
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
41 to specify the length of time taken by an
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
42 event, for example a holiday
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
43 separated by spaces.
0
22b6e71de68e initial commit; codebase from birthday; just the needed stuff; substituted getopt by own code
meillo@marmaro.de
parents:
diff changeset
44
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
45 Lines preceeded by # are treated as comments.
0
22b6e71de68e initial commit; codebase from birthday; just the needed stuff; substituted getopt by own code
meillo@marmaro.de
parents:
diff changeset
46
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
47 Note: If you deviate from this format, I cannot guarantee anything about
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
48 it's behaviour. In most cases, it will just quietly ignore the
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
49 error, which probably isn't ideal behaviour. Oh, well.
8
19c1ad697022 beautifing :-)
meillo@marmaro.de
parents: 7
diff changeset
50
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
51 =====================================================================
8
19c1ad697022 beautifing :-)
meillo@marmaro.de
parents: 7
diff changeset
52 */
0
22b6e71de68e initial commit; codebase from birthday; just the needed stuff; substituted getopt by own code
meillo@marmaro.de
parents:
diff changeset
53
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
54
8
19c1ad697022 beautifing :-)
meillo@marmaro.de
parents: 7
diff changeset
55 /* standard time to warn in advance, when no explicit w flag is given. */
19c1ad697022 beautifing :-)
meillo@marmaro.de
parents: 7
diff changeset
56 #define DEF_WARN 14
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
57
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
58
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
59 #include <stdarg.h>
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
60 #include <stdio.h>
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
61 #include <stdlib.h>
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
62 #include <string.h>
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
63 #include <sys/types.h>
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
64 #include <time.h>
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
65 #include <unistd.h>
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
66
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
67
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
68
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
69 /* ========== Global constants and data types */
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
70
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
71
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
72 /* -------- modifier flags */
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
73 #define F_MTYPE 0x07
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
74 #define F_TANNIVERSARY 2
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
75 #define F_TEVENT 3
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
76
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
77 /* flags processed immediately on encountering */
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
78 #define F_MIMMEDIATE 0x24
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
79 #define F_WTIME_P 0x08
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
80 #define F_FORDAYS 0x16
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
81 #define F_TODATE 0x24
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
82
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
83 struct _ftable {
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
84 char* txt;
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
85 unsigned flag;
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
86 };
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
87 const struct _ftable FTABLE[];
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
88
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
89 struct date {
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
90 unsigned day;
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
91 unsigned month;
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
92 unsigned year;
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
93 };
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
94
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
95 struct event {
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
96 char* text;
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
97 struct date date;
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
98 struct date enddate;
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
99 int warn;
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
100 };
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
101
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
102 /* ========== Global Variables */
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
103
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
104 struct event *readlist(void);
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
105 void gettoday(void);
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
106 unsigned delta(struct date *);
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
107 unsigned ddiff(struct date *D1, struct date *D2);
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
108 void liststrings(struct event *evl);
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
109 char *tdelta(struct date *d);
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
110 char *ttime(int yr, int mn, int wk, int dy);
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
111 char *skptok(char *ptr);
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
112 int evcmp(const void *e1, const void *e2);
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
113
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
114
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
115 struct date today;
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
116 int def_warn = DEF_WARN;
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
117
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
118
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
119 const struct _ftable FTABLE[] = {
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
120 {"#ann",F_TANNIVERSARY},
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
121 {"#ev", F_TEVENT},
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
122 {"#w", F_WTIME_P},
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
123 {"#to", F_TODATE},
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
124 {"#for", F_FORDAYS},
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
125 {NULL, 0}
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
126 };
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
127
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
128
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
129
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
130
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
131
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
132
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
133 /*
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
134 xmalloc/xrealloc functions
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
135 Note: the x* functions are lifted straight from the GNU libc info docs
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
136 $Id: xmalloc.c,v 1.2 1999/01/16 17:08:59 andy Exp $
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
137 */
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
138
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
139 void *
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
140 xmalloc(size_t size)
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
141 {
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
142 register void *value = malloc (size);
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
143 if (value == 0) {
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
144 fprintf(stderr, "virtual memory exhausted\n");
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
145 exit(1);
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
146 }
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
147 return value;
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
148 }
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
149
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
150 void *
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
151 xrealloc(void *ptr, size_t size)
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
152 {
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
153 register void *value = realloc (ptr, size);
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
154 if (value == 0) {
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
155 fprintf(stderr, "virtual memory exhausted\n");
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
156 exit(1);
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
157 }
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
158 return value;
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
159 }
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
160
8
19c1ad697022 beautifing :-)
meillo@marmaro.de
parents: 7
diff changeset
161
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
162 /* ========== */
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
163
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
164
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
165 /*
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
166 like strcat(), but lets the buffer automagically grow :-)
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
167 */
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
168 int
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
169 append(char *where, int size, char *what)
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
170 {
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
171 if (strlen(what) > ((size) - strlen(where))) {
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
172 xrealloc(where, (size) + 128 + strlen(what));
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
173 size += 128 + strlen(what);
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
174 }
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
175 strcat(where, what);
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
176 return size;
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
177 }
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
178
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
179 /* ========== */
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
180
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
181
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
182 int
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
183 before(struct date a, struct date b)
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
184 {
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
185 if (a.month < b.month) {
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
186 return 1;
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
187 } else if (a.month == b.month && a.day < b.day) {
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
188 return 1;
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
189 } else {
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
190 return 0;
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
191 }
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
192 }
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
193
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
194 int
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
195 ydelta(struct date a, struct date b)
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
196 {
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
197 return b.year - a.year + before(a, b);
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
198 }
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
199
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
200 /*
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
201 returns the length of the given month
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
202 */
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
203 int
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
204 mlen(int month, int year)
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
205 {
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
206 unsigned mlendat[] = {31,0,31,30,31,30,31,31,30,31,30,31};
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
207
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
208 if (mlendat[month - 1]) {
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
209 return mlendat[month - 1];
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
210 } else {
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
211 if (year%4==0 && (year%100!=0 || year%400==0)) {
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
212 return 29;
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
213 } else {
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
214 return 28;
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
215 }
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
216 }
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
217 }
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
218
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
219
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
220
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
221 /*
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
222 returns delta(d) in days, weeks, months, etc
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
223 the returned buffer is malloc()ed, do not forget to free() it
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
224 */
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
225 char *
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
226 tdelta(struct date *d)
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
227 {
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
228 int dy, wk, mn, yr;
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
229 char *tmp;
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
230 char *buf = xmalloc(128);
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
231 int size = 128;
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
232
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
233 *buf = 0;
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
234 switch (delta(d)) {
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
235 case 0:
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
236 size = append(buf, size, "TODAY");
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
237 return buf;
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
238 case 1:
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
239 size = append(buf, size, "Tomorrow");
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
240 return buf;
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
241 default:
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
242 /* like delta(), we ignore the year */
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
243 yr = -before(*d, today);
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
244 mn = d->month - today.month;
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
245 dy = d->day - today.day;
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
246
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
247 if (dy < 0) {
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
248 dy += mlen(today.month, today.year);
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
249 mn--;
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
250 }
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
251 if (mn < 0) {
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
252 mn += 12;
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
253 yr++;
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
254 }
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
255
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
256 wk = (dy / 7);
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
257 dy %= 7;
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
258
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
259 size = append(buf, size, "In ");
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
260 tmp = ttime(yr, mn, wk, dy);
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
261 size = append(buf, size, tmp);
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
262 free(tmp);
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
263
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
264 return buf;
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
265 }
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
266 }
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
267
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
268
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
269
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
270
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
271
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
272 void
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
273 donum(char *buf, int size, int n, char *txt, int *terms)
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
274 {
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
275 char tmp[128];
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
276
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
277 if (n <= 0) {
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
278 return;
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
279 }
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
280 snprintf(tmp, sizeof(tmp), "%d", n);
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
281 size = append(buf, size, tmp);
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
282 size = append(buf, size, " ");
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
283 size = append(buf, size, txt);
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
284 if (n != 1) {
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
285 size = append(buf, size, "s");
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
286 }
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
287 if (--*terms == 1) {
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
288 size = append(buf, size, " and ");
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
289 } else if (*terms > 1) {
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
290 size = append(buf, size, ", ");
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
291 }
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
292 }
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
293
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
294
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
295 /* returns allocated buffer, don't forget to free() */
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
296 char *
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
297 ttime(int yr, int mn, int wk, int dy)
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
298 {
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
299 int size = 128;
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
300 char *buf = xmalloc(size);
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
301 int terms = (yr!=0) + (mn!=0) + (wk!=0) + (dy!=0);
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
302
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
303 *buf = '\0'; /* Initialize buffer */
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
304
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
305 donum(buf, size, yr, "year", &terms);
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
306 donum(buf, size, mn, "month", &terms);
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
307 donum(buf, size, wk, "week", &terms);
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
308 donum(buf, size, dy, "day", &terms);
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
309
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
310 return buf;
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
311 }
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
312
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
313
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
314
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
315
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
316
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
317
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
318 /*
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
319 lists the birthdays in their string format, one by one, and passes
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
320 the string to a function.
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
321 */
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
322 void
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
323 liststrings(struct event *evl)
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
324 {
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
325 int i,j;
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
326 char *buf, *tmp;
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
327 int size;
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
328
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
329 for (i=0; evl[i].text; i++) {
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
330 size = 128;
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
331 buf = xmalloc(size);
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
332 *buf = '\0';
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
333
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
334 if (evl[i].warn == -1 && delta(&(evl[i].date))==0) {
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
335 size = append(buf, size, evl[i].text);
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
336 } else if (evl[i].enddate.day == 0) {
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
337
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
338 if (delta(&(evl[i].date)) <= evl[i].warn) {
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
339 tmp = tdelta(&(evl[i].date));
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
340 size = append(buf, size, tmp);
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
341 size = append(buf, size, ": ");
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
342 size = append(buf, size, evl[i].text);
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
343 free(tmp);
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
344 }
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
345 } else {
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
346 if (delta(&(evl[i].date)) <= evl[i].warn) {
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
347 size = append(buf, size, evl[i].text);
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
348 size = append(buf, size, " for ");
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
349 /* +1 because, if the difference between
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
350 two dates is one day, then the length of
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
351 an event on those days is two days */
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
352 j = ddiff(&(evl[i].date),&(evl[i].enddate)) + 1;
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
353 tmp = ttime(0, 0, j/7, j%7);
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
354 size = append(buf, size, tmp);
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
355 free(tmp);
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
356 size = append(buf, size, " ");
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
357 tmp = tdelta(&(evl[i].date));
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
358 size = append(buf, size, tmp);
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
359 } else if (delta(&(evl[i].enddate)) <= evl[i].warn) {
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
360 size = append(buf, size, evl[i].text);
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
361 size = append(buf, size, " ");
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
362 j = delta(&(evl[i].enddate));
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
363 if (j) {
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
364 size = append(buf, size, "for ");
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
365 tmp = ttime(0, 0, j/7, j%7);
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
366 size = append(buf, size, tmp);
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
367 free(tmp);
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
368 size = append(buf, size, " longer");
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
369 } else {
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
370 size = append(buf, size, "finishes today");
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
371 }
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
372 }
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
373 }
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
374 if (*buf) {
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
375 size = append(buf, size, ".");
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
376 puts(buf);
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
377 }
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
378 free(buf);
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
379 }
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
380 }
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
381
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
382
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
383
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
384
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
385
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
386
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
387
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
388
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
389 /*
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
390 sort the events by the time before the next time they come up,
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
391 putting those where the start has passed but we are still in the
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
392 time-period first
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
393 */
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
394 int
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
395 evcmp(const void *p1, const void *p2)
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
396 {
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
397 struct event *e1=(struct event *) p1;
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
398 struct event *e2=(struct event *) p2;
8
19c1ad697022 beautifing :-)
meillo@marmaro.de
parents: 7
diff changeset
399 unsigned d1, d2;
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
400
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
401 /*
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
402 if the delta for the enddate is less than that for the start
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
403 date, then we have passed the start date but not yet the end
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
404 date, and so we should display the enddate; otherwise, we
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
405 should display the start date
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
406 */
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
407
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
408 d1=delta(&(e1->date));
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
409 if (e1->enddate.day && delta(&(e1->enddate)) < d1)
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
410 d1=delta(&(e1->enddate));
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
411
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
412 d2=delta(&(e2->date));
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
413 if (e2->enddate.day && delta(&(e2->enddate)) < d2)
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
414 d2=delta(&(e2->enddate));
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
415
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
416 if (d1 < d2) return -1;
8
19c1ad697022 beautifing :-)
meillo@marmaro.de
parents: 7
diff changeset
417 if (d1 > d2) return 1;
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
418
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
419 return strcmp(e1->text, e2->text);
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
420 }
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
421
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
422
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
423
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
424
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
425
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
426
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
427 /*
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
428 difference in days between two dates
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
429 it is assumed that D1 < D2, and so the result is always positive
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
430 */
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
431 unsigned
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
432 ddiff(struct date *D1, struct date *D2)
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
433 {
8
19c1ad697022 beautifing :-)
meillo@marmaro.de
parents: 7
diff changeset
434 struct date d1, d2;
19c1ad697022 beautifing :-)
meillo@marmaro.de
parents: 7
diff changeset
435 int dd, m;
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
436
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
437 /* make working copies */
8
19c1ad697022 beautifing :-)
meillo@marmaro.de
parents: 7
diff changeset
438 d1 = *D1;
19c1ad697022 beautifing :-)
meillo@marmaro.de
parents: 7
diff changeset
439 d2 = *D2;
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
440
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
441 /* sort out zero years */
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
442 if (d1.year == 0 || d2.year==0) {
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
443 if (d1.year != d2.year) {
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
444 if (d1.year == 0) {
8
19c1ad697022 beautifing :-)
meillo@marmaro.de
parents: 7
diff changeset
445 if (before(d1,d2))
19c1ad697022 beautifing :-)
meillo@marmaro.de
parents: 7
diff changeset
446 d1.year = d2.year;
19c1ad697022 beautifing :-)
meillo@marmaro.de
parents: 7
diff changeset
447 else
19c1ad697022 beautifing :-)
meillo@marmaro.de
parents: 7
diff changeset
448 d1.year = d2.year - 1;
19c1ad697022 beautifing :-)
meillo@marmaro.de
parents: 7
diff changeset
449 } else {
19c1ad697022 beautifing :-)
meillo@marmaro.de
parents: 7
diff changeset
450 if (before(d1, d2))
19c1ad697022 beautifing :-)
meillo@marmaro.de
parents: 7
diff changeset
451 d2.year = d1.year;
19c1ad697022 beautifing :-)
meillo@marmaro.de
parents: 7
diff changeset
452 else
19c1ad697022 beautifing :-)
meillo@marmaro.de
parents: 7
diff changeset
453 d2.year = d1.year + 1;
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
454 }
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
455 } else { /* both years zero */
8
19c1ad697022 beautifing :-)
meillo@marmaro.de
parents: 7
diff changeset
456 if (before(d1, d2))
19c1ad697022 beautifing :-)
meillo@marmaro.de
parents: 7
diff changeset
457 d1.year = d2.year = today.year;
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
458 else {
8
19c1ad697022 beautifing :-)
meillo@marmaro.de
parents: 7
diff changeset
459 d1.year = today.year;
19c1ad697022 beautifing :-)
meillo@marmaro.de
parents: 7
diff changeset
460 d2.year = d1.year + 1;
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
461 }
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
462 }
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
463 }
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
464
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
465 /* now we can actually do the comparison ... */
8
19c1ad697022 beautifing :-)
meillo@marmaro.de
parents: 7
diff changeset
466 dd = 0;
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
467
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
468 /* to start with, we work in months */
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
469 for (m=d1.month; m < d2.month + (d2.year-d1.year)*12; m++)
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
470 dd += mlen(((m-1)%12)+1, d1.year + m/12);
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
471
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
472 /*
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
473 and then we renormalise for the days within the months
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
474 the first month was included in our calculations
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
475 */
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
476 dd -= d1.day;
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
477 /* but the last one wasn't */
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
478 dd += d2.day;
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
479
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
480 return dd;
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
481 }
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
482
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
483
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
484
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
485
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
486
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
487
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
488
0
22b6e71de68e initial commit; codebase from birthday; just the needed stuff; substituted getopt by own code
meillo@marmaro.de
parents:
diff changeset
489
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
490 /*
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
491 actually until the next anniversary of ...
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
492 */
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
493 unsigned
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
494 delta(struct date *date)
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
495 {
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
496 struct date d;
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
497 unsigned dt, mn;
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
498
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
499 memcpy(&d, date, sizeof(struct date));
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
500
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
501 /* past the end of the year */
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
502 if (before(d, today)) {
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
503 d.year = 1;
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
504 } else {
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
505 d.year = 0;
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
506 }
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
507
8
19c1ad697022 beautifing :-)
meillo@marmaro.de
parents: 7
diff changeset
508 for (mn = today.month, dt=0; mn < d.month + 12*d.year; mn++) {
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
509 dt += mlen(((mn-1)%12) + 1,today.year + mn/12);
8
19c1ad697022 beautifing :-)
meillo@marmaro.de
parents: 7
diff changeset
510 }
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
511
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
512 dt -= today.day;
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
513 dt += d.day;
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
514
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
515 return dt;
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
516 }
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
517
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
518
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
519
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
520
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
521
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
522
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
523 void
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
524 gettoday(void)
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
525 {
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
526 struct tm *tm;
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
527 time_t t;
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
528
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
529 time(&t);
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
530 tm = localtime(&t);
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
531 today.day = tm->tm_mday;
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
532 today.month = tm->tm_mon + 1; /* 1-12 instead of 0-11 */
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
533 today.year = tm->tm_year + 1900;
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
534 }
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
535
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
536
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
537
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
538
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
539
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
540
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
541
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
542
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
543
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
544
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
545 struct event *
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
546 readlist()
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
547 {
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
548 int i, j, k, l, d;
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
549 struct event *evl;
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
550 char buf[1024], buf2[1024];
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
551 char *ptr, *cp;
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
552 unsigned flags;
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
553
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
554 /* initialise */
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
555 gettoday();
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
556
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
557 for (i = 0, evl = NULL; fgets(buf, sizeof(buf), stdin) != NULL; i++) {
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
558 evl = (struct event *) xrealloc(evl, sizeof(struct event) * (i + 1));
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
559
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
560 /* ignore comments and empty lines */
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
561 if (*buf == '#' || *buf == '\n') {
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
562 i--;
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
563 continue;
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
564 }
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
565
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
566 /* parse string in buf */
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
567
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
568 ptr = strchr(buf, ' '); /* start of text */
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
569
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
570 /* not a valid line, so ignore it! Cool, huh? */
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
571 /* Attention: only recognizes lines without '=' */
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
572 if (!ptr) {
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
573 fprintf(stderr, "WARNING: Invalid input line:\n\t%s", buf);
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
574 i--;
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
575 continue;
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
576 }
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
577
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
578 *(ptr++) = '\0';
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
579 ptr[strlen(ptr)-1] = '\0';
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
580
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
581 j = sscanf(buf, "%u-%u-%u", &(evl[i].date.year),
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
582 &(evl[i].date.month), &(evl[i].date.day));
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
583 if (j != 3) {
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
584 fprintf(stderr, "Error: Invalid date:\t%s\n", buf);
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
585 i--;
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
586 continue;
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
587 }
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
588
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
589 /* parse flags */
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
590
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
591 evl[i].warn = def_warn;
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
592 evl[i].enddate.day = 0;
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
593 evl[i].enddate.month = 0;
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
594 evl[i].enddate.year = 0;
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
595
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
596 flags = 0;
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
597 j = 0;
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
598 cp = skptok(ptr);
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
599 for (cp=ptr; *cp && *cp=='#'; cp=skptok(cp)) {
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
600 for (k = 0; FTABLE[k].txt && strncmp(FTABLE[k].txt, cp, strlen(FTABLE[k].txt)); k++) {
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
601 }
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
602
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
603 switch (FTABLE[k].flag) {
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
604 case F_WTIME_P: /* #w<n> -- sets warning time */
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
605 sscanf(cp, "#w%u", &(evl[i].warn));
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
606 break;
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
607 case F_FORDAYS: /* #for<days> -- sets the duration of the event */
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
608 sscanf(cp, "#for%u", &d);
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
609 evl[i].enddate=evl[i].date;
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
610 for (l = 1; l < d; l++) {
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
611 evl[i].enddate.day++;
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
612 if (evl[i].enddate.day > mlen(evl[i].enddate.month, evl[i].enddate.year)) {
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
613 evl[i].enddate.month++;
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
614 evl[i].enddate.day = 1;
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
615 }
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
616 if (evl[i].enddate.month > 12) {
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
617 evl[i].enddate.year++;
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
618 evl[i].enddate.month = 1;
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
619 }
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
620 }
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
621 break;
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
622 case F_TODATE: /* #to<date> -- sets the end date of the event */
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
623 l = sscanf(cp, "#to%u-%u-%u", &(evl[i].enddate.year), &(evl[i].enddate.month), &(evl[i].enddate.day));
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
624 if (l == 2) {
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
625 evl[i].enddate.year = 0;
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
626 }
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
627 break;
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
628 case 0:
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
629 break;
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
630 default:
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
631 flags |= FTABLE[k].flag;
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
632 break;
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
633 }
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
634 }
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
635
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
636
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
637 /* construct event text */
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
638
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
639 switch(flags & F_MTYPE) {
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
640 default: /* assume it's a birthday */
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
641 if (!evl[i].date.year) {
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
642 sprintf(buf2, "%s has a birthday", cp);
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
643 break;
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
644 }
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
645 int tmp_age = ydelta(evl[i].date, today);
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
646 sprintf(buf2, "%s is %d year%s old",
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
647 cp, tmp_age, (tmp_age>1)?"s":"");
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
648 break;
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
649 case F_TANNIVERSARY:
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
650 if (evl[i].date.year) {
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
651 sprintf(buf2, "%s %d years ago",
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
652 cp, ydelta(evl[i].date, today));
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
653 } else {
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
654 strcpy(buf2, cp);
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
655 }
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
656 break;
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
657 case F_TEVENT:
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
658 /* if a year was specified, and this
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
659 warning isn't for it, ignore! */
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
660 if ((evl[i].date.year && ydelta(evl[i].date, today))
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
661 && (!evl[i].enddate.year || ydelta(evl[i].enddate, today))) {
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
662 i--;
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
663 continue;
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
664 }
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
665 strcpy(buf2, cp);
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
666 break;
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
667 }
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
668 evl[i].text = strdup(buf2);
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
669 }
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
670
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
671 evl = (struct event *) xrealloc(evl, sizeof(struct event) * (i + 1));
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
672 evl[i].date.day = 0;
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
673 evl[i].date.month = 0;
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
674 evl[i].date.year = 0;
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
675 evl[i].text = (char *) NULL;
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
676
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
677 fclose(stdin);
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
678
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
679 /* NB uses i from above */
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
680 qsort(evl, i, sizeof(struct event), evcmp);
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
681 return evl;
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
682 }
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
683
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
684
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
685
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
686
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
687
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
688 char *
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
689 skptok(char *ptr)
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
690 {
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
691 while (*ptr && (*ptr!=' ' && *ptr!='\t')) {
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
692 ptr++;
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
693 }
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
694 while (*ptr && (*ptr==' ' || *ptr=='\t')) {
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
695 ptr++;
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
696 }
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
697 return ptr;
7
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
698 }
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
699
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
700
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
701
b6f4c7fba64a all sources now in one file
meillo@marmaro.de
parents: 6
diff changeset
702
0
22b6e71de68e initial commit; codebase from birthday; just the needed stuff; substituted getopt by own code
meillo@marmaro.de
parents:
diff changeset
703
22b6e71de68e initial commit; codebase from birthday; just the needed stuff; substituted getopt by own code
meillo@marmaro.de
parents:
diff changeset
704
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
705 int
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
706 main(int argc, char *argv[])
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
707 {
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
708 while (--argc > 0 && **++argv == '-') {
5
5af6bf2cb271 reads only stdin now, no files anymore; removed -f option aswell; code beatifing
meillo@marmaro.de
parents: 3
diff changeset
709 if (strcmp(argv[0], "-W") == 0) {
6
fc6e40f7bd5a minor stuff
meillo@marmaro.de
parents: 5
diff changeset
710 /* TODO: catch if no value given */
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
711 def_warn = atoi((++argv)[0]);
0
22b6e71de68e initial commit; codebase from birthday; just the needed stuff; substituted getopt by own code
meillo@marmaro.de
parents:
diff changeset
712 argc--;
22b6e71de68e initial commit; codebase from birthday; just the needed stuff; substituted getopt by own code
meillo@marmaro.de
parents:
diff changeset
713 } else {
3
dc2f94280b01 new Makefile; removed MinWarn and MaxWarn; adjusted manpage
meillo@marmaro.de
parents: 0
diff changeset
714 fprintf(stderr, "unknown option %s\n", argv[0]);
0
22b6e71de68e initial commit; codebase from birthday; just the needed stuff; substituted getopt by own code
meillo@marmaro.de
parents:
diff changeset
715 exit(1);
22b6e71de68e initial commit; codebase from birthday; just the needed stuff; substituted getopt by own code
meillo@marmaro.de
parents:
diff changeset
716 }
22b6e71de68e initial commit; codebase from birthday; just the needed stuff; substituted getopt by own code
meillo@marmaro.de
parents:
diff changeset
717 }
16
79d22407a6be a lot of refactoring
markus schnalke <meillo@marmaro.de>
parents: 15
diff changeset
718 liststrings(readlist());
15
032af48d590b reformating of the source code
markus schnalke <meillo@marmaro.de>
parents: 14
diff changeset
719 return 0;
0
22b6e71de68e initial commit; codebase from birthday; just the needed stuff; substituted getopt by own code
meillo@marmaro.de
parents:
diff changeset
720 }