comparison bdengine.c @ 4:5326c222cd4e

removed MESSAGE; code beautifing workaround for MESSAGE: use an event with warn 0 (message=yyyy-mm-dd w 0)
author meillo@marmaro.de
date Mon, 17 Dec 2007 12:14:41 +0100
parents dc2f94280b01
children 5af6bf2cb271
comparison
equal deleted inserted replaced
3:dc2f94280b01 4:5326c222cd4e
31 name/event/whatever=date flags 31 name/event/whatever=date flags
32 where: 32 where:
33 date is dd/mm, dd/mm/yy (assumes 20th century!) or dd/mm/yyyy 33 date is dd/mm, dd/mm/yy (assumes 20th century!) or dd/mm/yyyy
34 flags is ONE or ZERO of 34 flags is ONE or ZERO of
35 o bd for a birthday (default) 35 o bd for a birthday (default)
36 o bir for a birthday (exactly equivalent to `bd')
37 o ann for an anniversary 36 o ann for an anniversary
38 o ev for an event 37 o ev for an event
39 and zero or more of 38 and zero or more of
40 o w<n> to set the warn-in-advance time to n days (don't include the 39 o w <n> to set the warn-in-advance time to n days (don't include the
41 brackets! :) 40 brackets! :)
42 o to<date> 41 o to <date>
43 o for<days> 42 o for <days>
44 to specify the length of time taken by an event, for example a 43 to specify the length of time taken by an event, for example a
45 holiday. 44 holiday.
46 45
47 Comment lines are preceeded by #. 46 Comment lines are preceeded by #.
48 47
123 const struct _ftable FTABLE[] = { 122 const struct _ftable FTABLE[] = {
124 {"bir",F_TBIRTHDAY}, 123 {"bir",F_TBIRTHDAY},
125 {"bd", F_TBIRTHDAY}, 124 {"bd", F_TBIRTHDAY},
126 {"ann",F_TANNIVERSARY}, 125 {"ann",F_TANNIVERSARY},
127 {"ev", F_TEVENT}, 126 {"ev", F_TEVENT},
128 {"mes", F_TMESSAGE},
129 {"w", F_WTIME_P}, 127 {"w", F_WTIME_P},
130 {"to", F_TODATE}, 128 {"to", F_TODATE},
131 {"for", F_FORDAYS}, 129 {"for", F_FORDAYS},
132 {NULL, 0} 130 {NULL, 0}
133 }; 131 };
496 494
497 gettoday(); 495 gettoday();
498 496
499 if (fname[0] == '-' && fname[1] == 0) { 497 if (fname[0] == '-' && fname[1] == 0) {
500 /* read from stdin */ 498 /* read from stdin */
501 file=stdin; 499 file = stdin;
502 } else { 500 } else {
503 /* now read it */ 501 /* now read it */
504 if((file=fopen(fname, "rt"))==NULL) { 502 if ((file = fopen(fname, "rt")) == NULL) {
505 fprintf(stderr, "Unable to open file \"%s\"\n", fname); 503 fprintf(stderr, "Unable to open file \"%s\"\n", fname);
506 exit(1); 504 exit(1);
507 } 505 }
508 } 506 }
509 507
510 508
511 for (i = 0, evl=NULL; fgets(buf, sizeof(buf), file) != NULL; i++) { 509 for (i = 0, evl = NULL; fgets(buf, sizeof(buf), file) != NULL; i++) {
512 evl = (struct event *) xrealloc(evl, sizeof(struct event) * (i + 1)); 510 evl = (struct event *) xrealloc(evl, sizeof(struct event) * (i + 1));
513 511
514 /* ignore comments and empty lines */ 512 /* ignore comments and empty lines */
515 if (*buf == '#' || *buf == '\n') { 513 if (*buf == '#' || *buf == '\n') {
516 i--; 514 i--;
519 517
520 /* parse string in buf */ 518 /* parse string in buf */
521 ptr = strrchr(buf, '='); /* allow '=' in text */ 519 ptr = strrchr(buf, '='); /* allow '=' in text */
522 520
523 /* not a valid line, so ignore it! Cool, huh? */ 521 /* not a valid line, so ignore it! Cool, huh? */
522 /* Attention: only recognizes lines without '=' */
524 if (ptr == NULL) { 523 if (ptr == NULL) {
525 fprintf(stderr, "WARNING: Invalid line in input file:\n%s", buf); 524 fprintf(stderr, "WARNING: Invalid line in input file:\n%s", buf);
526 i--; 525 i--;
527 continue; 526 continue;
528 } 527 }
529 528
530 *(ptr++) = 0; 529 *(ptr++) = 0;
531 530
532 j = sscanf(ptr, "%u-%u-%u", &(evl[i].date.year), &(evl[i].date.month), &(evl[i].date.day)); 531 j = sscanf(ptr, "%u-%u-%u", &(evl[i].date.year), &(evl[i].date.month), &(evl[i].date.day));
533 /* if our year is only two digits, add 1900 to it ... */
534 if(evl[i].date.year < 100) evl[i].date.year+=1900;
535 /* ... unless it wasn't read, in which case set it to zero */ 532 /* ... unless it wasn't read, in which case set it to zero */
536 if(j==2) evl[i].date.year=0; 533 if (j==2) {
534 evl[i].date.year = 0;
535 }
536
537 537
538 /* parse flags */ 538 /* parse flags */
539 539
540 evl[i].warn=iDWarn; 540 evl[i].warn = iDWarn;
541 evl[i].enddate.day=evl[i].enddate.month=evl[i].enddate.year=0; 541 evl[i].enddate.day = 0;
542 542 evl[i].enddate.month = 0;
543 flags=j=0; 543 evl[i].enddate.year = 0;
544 while(j = skptok(j, ptr),ptr[j]!=0) { 544
545 for (k = 0; FTABLE[k].txt != NULL && strncmp(FTABLE[k].txt, ptr + j, strlen(FTABLE[k].txt)); k++); 545 flags = 0;
546 j = 0;
547
548 while(j = skptok(j, ptr), ptr[j] != 0) {
549 for (k = 0; FTABLE[k].txt != NULL && strncmp(FTABLE[k].txt, ptr + j, strlen(FTABLE[k].txt)); k++) {
550 }
551
546 switch (FTABLE[k].flag) { 552 switch (FTABLE[k].flag) {
547 case F_WTIME_P: /* w<n> -- sets warning time */ 553 case F_WTIME_P: /* w <n> -- sets warning time */
548 sscanf(ptr + j, "w %u", &(evl[i].warn)); 554 sscanf(ptr + j, "w %u", &(evl[i].warn));
549 break; 555 break;
550 case F_FORDAYS: /* for<days> -- sets the duration of the event */ 556 case F_FORDAYS: /* for <days> -- sets the duration of the event */
551 sscanf(ptr + j, "for %d", &d); 557 sscanf(ptr + j, "for %u", &d);
552 evl[i].enddate=evl[i].date; 558 evl[i].enddate=evl[i].date;
553 for (l=1; l < d; l++) { 559 for (l = 1; l < d; l++) {
554 evl[i].enddate.day++; 560 evl[i].enddate.day++;
555 if (evl[i].enddate.day > mlen(evl[i].enddate.month, 561 if (evl[i].enddate.day > mlen(evl[i].enddate.month, evl[i].enddate.year)) {
556 evl[i].enddate.year)) {
557 evl[i].enddate.month++; 562 evl[i].enddate.month++;
558 evl[i].enddate.day=1; 563 evl[i].enddate.day = 1;
559 } 564 }
560 if (evl[i].enddate.month > 12) { 565 if (evl[i].enddate.month > 12) {
561 evl[i].enddate.year++; 566 evl[i].enddate.year++;
562 evl[i].enddate.month=1; 567 evl[i].enddate.month = 1;
563 } 568 }
564 } 569 }
565 break; 570 break;
566 case F_TODATE: 571 case F_TODATE: /* to <date> -- sets the end date of the event */
567 l = sscanf(ptr + j, "to %u-%u-%u", &(evl[i].enddate.year), &(evl[i].enddate.month), &(evl[i].enddate.day)); 572 l = sscanf(ptr + j, "to %u-%u-%u", &(evl[i].enddate.year), &(evl[i].enddate.month), &(evl[i].enddate.day));
568 if (evl[i].enddate.year < 100) {
569 evl[i].enddate.year+=1900;
570 }
571 if (l == 2) { 573 if (l == 2) {
572 evl[i].enddate.year=0; 574 evl[i].enddate.year = 0;
573 } 575 }
574 break; 576 break;
575 case 0: 577 case 0:
576 break; 578 break;
577 default: 579 default:
578 flags|=FTABLE[k].flag; 580 flags |= FTABLE[k].flag;
579 break; 581 break;
580 } 582 }
581 } 583 }
582 584
583 585
585 587
586 switch(flags & F_MTYPE) { 588 switch(flags & F_MTYPE) {
587 case F_TBIRTHDAY: 589 case F_TBIRTHDAY:
588 default: /* assume it's a birthday */ 590 default: /* assume it's a birthday */
589 if (evl[i].date.year != 0) { 591 if (evl[i].date.year != 0) {
590 int tmp_age=ydelta(evl[i].date, today); 592 int tmp_age = ydelta(evl[i].date, today);
591 if (tmp_age!=1) { 593 if (tmp_age != 1) {
592 sprintf(buf2, "%s is %d years old", buf, tmp_age); 594 sprintf(buf2, "%s is %d years old", buf, tmp_age);
593 } else { 595 } else {
594 sprintf(buf2, "%s is %d year old", buf, tmp_age); 596 sprintf(buf2, "%s is %d year old", buf, tmp_age);
595 } 597 }
596 } else { 598 } else {
604 strcpy(buf2, buf); 606 strcpy(buf2, buf);
605 } 607 }
606 break; 608 break;
607 case F_TEVENT: 609 case F_TEVENT:
608 /* if a year was specified, and this warning isn't for it, ignore! */ 610 /* if a year was specified, and this warning isn't for it, ignore! */
609 if ((evl[i].date.year != 0 && ydelta(evl[i].date, today) != 0) && 611 if ((evl[i].date.year != 0 && ydelta(evl[i].date, today) != 0) && (evl[i].enddate.year == 0 || ydelta(evl[i].enddate, today) != 0)) {
610 (evl[i].enddate.year == 0 || ydelta(evl[i].enddate, today) != 0)) {
611 i--; 612 i--;
612 continue; 613 continue;
613 } 614 }
614 strcpy(buf2, buf); 615 strcpy(buf2, buf);
615 break; 616 break;
616 case F_TMESSAGE:
617 /* Like an event, except that it only comes up on the given date, and no text at all is appended */
618 if ((evl[i].date.year != 0 && ydelta(evl[i].date, today) != 0) &&
619 (evl[i].enddate.year == 0 || ydelta(evl[i].enddate, today) != 0)) {
620 i--;
621 continue;
622 }
623 strcpy(buf2, buf);
624 evl[i].warn=-1; /* special code! */
625 break;
626 } 617 }
627 evl[i].text = strdup(buf2); 618 evl[i].text = strdup(buf2);
628 } 619 }
629 620
630 evl = (struct event *) xrealloc(evl, sizeof(struct event) * (i + 1)); 621 evl = (struct event *) xrealloc(evl, sizeof(struct event) * (i + 1));
631 evl[i].date.day=evl[i].date.month=evl[i].date.year=0; 622 evl[i].date.day = 0;
623 evl[i].date.month = 0;
624 evl[i].date.year = 0;
632 evl[i].text = (char *) NULL; 625 evl[i].text = (char *) NULL;
633 626
634 fclose(file); 627 fclose(file);
635 free(fname); 628 free(fname);
636 629