comparison bdengine.c @ 5:5af6bf2cb271

reads only stdin now, no files anymore; removed -f option aswell; code beatifing
author meillo@marmaro.de
date Mon, 17 Dec 2007 15:09:03 +0100
parents 5326c222cd4e
children fc6e40f7bd5a
comparison
equal deleted inserted replaced
4:5326c222cd4e 5:5af6bf2cb271
65 #include <string.h> 65 #include <string.h>
66 #include <time.h> 66 #include <time.h>
67 67
68 #include <sys/types.h> 68 #include <sys/types.h>
69 #include <unistd.h> 69 #include <unistd.h>
70 #include <pwd.h>
71 70
72 #include "birthday.h" 71 #include "birthday.h"
73 72
74 /* ========== */ 73 /* ========== */
75 74
76 75
77 76
78 /* 77 /*
79 xmalloc/xrealloc functions, and fatal exit function 78 xmalloc/xrealloc functions
80 Note: the x* functions are lifted straight from the GNU libc info docs 79 Note: the x* functions are lifted straight from the GNU libc info docs
81 $Id: xmalloc.c,v 1.2 1999/01/16 17:08:59 andy Exp $ 80 $Id: xmalloc.c,v 1.2 1999/01/16 17:08:59 andy Exp $
82 */ 81 */
83 82
84 void* xmalloc (size_t size) { 83 void* xmalloc (size_t size) {
107 106
108 107
109 108
110 int skptok(int j, char *ptr); 109 int skptok(int j, char *ptr);
111 int evcmp(const void *e1, const void *e2); 110 int evcmp(const void *e1, const void *e2);
112 char *deffname(void);
113 111
114 112
115 /* ========== Global variables */ 113 /* ========== Global variables */
116 114
117 struct date today; 115 struct date today;
134 132
135 /* ========== */ 133 /* ========== */
136 134
137 135
138 136
139
140 /* compare the first strlen(a) characters of a and b */
141 #define strbegcmp(a,b) strncmp(a,b,strlen(a))
142
143 /* like strcat(), but lets the buffer automagically grow :-) 137 /* like strcat(), but lets the buffer automagically grow :-)
144 * (needs local variable "size" with the buffer size) */ 138 * (needs local variable "size" with the buffer size) */
145 #define append(where, what) do { \ 139 #define append(where, what) do { \
146 if (strlen(what) > (size - strlen(where))) { \ 140 if (strlen(what) > (size - strlen(where))) { \
147 xrealloc(where, size + 128 + strlen(what)); \ 141 xrealloc(where, size + 128 + strlen(what)); \
160 char *buf = xmalloc(128); 154 char *buf = xmalloc(128);
161 int size = 128; 155 int size = 128;
162 *buf = 0; 156 *buf = 0;
163 157
164 switch (delta(d)) { 158 switch (delta(d)) {
165 case 0: 159 case 0:
166 append(buf, "today"); 160 append(buf, "today");
167 return buf; 161 return buf;
168 case 1: 162 case 1:
169 append(buf, "tomorrow"); 163 append(buf, "tomorrow");
170 return buf; 164 return buf;
171 default: 165 default:
172 /* like delta(), we ignore the year */ 166 /* like delta(), we ignore the year */
173 yr=-before(*d,today); 167 yr=-before(*d,today);
174 mn=d->month - today.month; 168 mn=d->month - today.month;
175 dy=d->day - today.day; 169 dy=d->day - today.day;
176 170
177 if (dy < 0) { 171 if (dy < 0) {
178 dy += mlen(today.month, today.year); 172 dy += mlen(today.month, today.year);
179 mn--; 173 mn--;
180 } 174 }
181 if (mn < 0) { 175 if (mn < 0) {
182 mn += 12; 176 mn += 12;
183 yr++; 177 yr++;
184 } 178 }
185 179
186 wk = (dy/7); 180 wk = (dy / 7);
187 dy%=7; 181 dy %= 7;
188 182
189 append(buf, "in "); 183 append(buf, "in ");
190 tmp = ttime(yr, mn, wk, dy); 184 tmp = ttime(yr, mn, wk, dy);
191 append(buf, tmp); 185 append(buf, tmp);
192 free(tmp); 186 free(tmp);
193 187
194 if (*(buf + strlen(buf) - 1) == 's') 188 return buf;
195 append(buf, "'");
196 else
197 append(buf, "'s");
198
199 append(buf, " time");
200
201 return buf;
202 } 189 }
203 } 190 }
204 191
205 192
206 193
268 255
269 256
270 257
271 258
272 259
273 /* lists the birthdays in their string format, one by one, and passes the string 260 /* lists the birthdays in their string format, one by one, and passes the string to a function. */
274 to a function. */
275 void liststrings(struct event *evl, prnfunc outf) { 261 void liststrings(struct event *evl, prnfunc outf) {
276 int i,j; 262 int i,j;
277 char *buf, *tmp; 263 char *buf, *tmp;
278 int size; 264 int size;
279 265
294 } 280 }
295 } else { 281 } else {
296 if (delta(&(evl[i].date)) <= evl[i].warn) { 282 if (delta(&(evl[i].date)) <= evl[i].warn) {
297 append(buf, evl[i].text); 283 append(buf, evl[i].text);
298 append(buf, " for "); 284 append(buf, " for ");
299 /* +1 because, if the difference between two dates is one day, 285 /* +1 because, if the difference between two dates is one day, then the length of an event on those days is two days */
300 then the length of an event on those days is two days */
301 j = ddiff(&(evl[i].date),&(evl[i].enddate)) + 1; 286 j = ddiff(&(evl[i].date),&(evl[i].enddate)) + 1;
302 tmp = ttime(0, 0, j/7, j%7); 287 tmp = ttime(0, 0, j/7, j%7);
303 append(buf, tmp); 288 append(buf, tmp);
304 free(tmp); 289 free(tmp);
305 append(buf, " "); 290 append(buf, " ");
332 317
333 318
334 319
335 320
336 321
337 char* deffname(void) {
338 char buf[256];
339
340 strcpy(buf, getpwuid(getuid())->pw_dir);
341 strcat(buf, "/" DEFAULT_FILE);
342
343 return strdup(buf);
344 }
345
346
347
348
349
350 322
351 /* sort the events by the time before the next time they come up, putting those 323 /* sort the events by the time before the next time they come up, putting those
352 where the start has passed but we are still in the time-period first */ 324 where the start has passed but we are still in the time-period first */
353 int evcmp(const void *p1, const void *p2) { 325 int evcmp(const void *p1, const void *p2) {
354 struct event *e1=(struct event *)p1; 326 struct event *e1=(struct event *)p1;
469 441
470 time(&t); 442 time(&t);
471 tm = localtime(&t); 443 tm = localtime(&t);
472 today.day = tm->tm_mday; 444 today.day = tm->tm_mday;
473 today.month = tm->tm_mon + 1; /* 1-12 instead of 0-11 */ 445 today.month = tm->tm_mon + 1; /* 1-12 instead of 0-11 */
474 today.year = tm->tm_year; 446 today.year = tm->tm_year + 1900;
475 today.year += 1900; 447 }
476 } 448
477 449
478 450
479 451
480 452
481 453 struct event* readlist() {
482 struct event *readlist(char *fname) { 454 int i, j, k, l, d;
483 FILE *file;
484 int i,j,k,l,d;
485 struct event *evl; 455 struct event *evl;
486 char buf[1024], buf2[1024]; 456 char buf[1024], buf2[1024];
487 char *ptr; 457 char *ptr;
488 unsigned flags; 458 unsigned flags;
489 459
490 /* initialise */ 460 /* initialise */
491 if (fname==NULL) {
492 fname=deffname();
493 }
494
495 gettoday(); 461 gettoday();
496 462
497 if (fname[0] == '-' && fname[1] == 0) { 463 for (i = 0, evl = NULL; fgets(buf, sizeof(buf), stdin) != NULL; i++) {
498 /* read from stdin */
499 file = stdin;
500 } else {
501 /* now read it */
502 if ((file = fopen(fname, "rt")) == NULL) {
503 fprintf(stderr, "Unable to open file \"%s\"\n", fname);
504 exit(1);
505 }
506 }
507
508
509 for (i = 0, evl = NULL; fgets(buf, sizeof(buf), file) != NULL; i++) {
510 evl = (struct event *) xrealloc(evl, sizeof(struct event) * (i + 1)); 464 evl = (struct event *) xrealloc(evl, sizeof(struct event) * (i + 1));
511 465
512 /* ignore comments and empty lines */ 466 /* ignore comments and empty lines */
513 if (*buf == '#' || *buf == '\n') { 467 if (*buf == '#' || *buf == '\n') {
514 i--; 468 i--;
519 ptr = strrchr(buf, '='); /* allow '=' in text */ 473 ptr = strrchr(buf, '='); /* allow '=' in text */
520 474
521 /* not a valid line, so ignore it! Cool, huh? */ 475 /* not a valid line, so ignore it! Cool, huh? */
522 /* Attention: only recognizes lines without '=' */ 476 /* Attention: only recognizes lines without '=' */
523 if (ptr == NULL) { 477 if (ptr == NULL) {
524 fprintf(stderr, "WARNING: Invalid line in input file:\n%s", buf); 478 fprintf(stderr, "WARNING: Invalid line in input:\n%s", buf);
525 i--; 479 i--;
526 continue; 480 continue;
527 } 481 }
528 482
529 *(ptr++) = 0; 483 *(ptr++) = 0;
622 evl[i].date.day = 0; 576 evl[i].date.day = 0;
623 evl[i].date.month = 0; 577 evl[i].date.month = 0;
624 evl[i].date.year = 0; 578 evl[i].date.year = 0;
625 evl[i].text = (char *) NULL; 579 evl[i].text = (char *) NULL;
626 580
627 fclose(file); 581 fclose(stdin);
628 free(fname);
629 582
630 /* NB uses i from above */ 583 /* NB uses i from above */
631 qsort(evl, i, sizeof(struct event), evcmp); 584 qsort(evl, i, sizeof(struct event), evcmp);
632 return evl; 585 return evl;
633 } 586 }