Mercurial > bday
view 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 |
line wrap: on
line source
/* bday -- Birthday/Anniversary reminder (c) 2007,2014 markus schnalke <meillo@marmaro.de> (c) 1994-1999 AS Mortimer This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. You may also distribute it under the Artistic License, as comes with Perl. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA You should also have recieved a copy of the Artistic license with this program. ===================================================================== Input is read through standard input. For example: bday < ~/.birthdays The input (file) has to have the following format: date flags text where: date is YYYY-MM-DD flags is ONE or ZERO of #ann for an anniversary #ev for an event and zero or more of #w<n> to set the warn-in-advance time to n days (don't include the brackets! :) #to<date> #for<days> to specify the length of time taken by an event, for example a holiday separated by spaces. Lines preceeded by # are treated as comments. Note: If you deviate from this format, I cannot guarantee anything about it's behaviour. In most cases, it will just quietly ignore the error, which probably isn't ideal behaviour. Oh, well. ===================================================================== */ /* standard time to warn in advance, when no explicit w flag is given. */ #define DEF_WARN 14 #include <stdarg.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <time.h> #include <unistd.h> /* ========== Global constants and data types */ /* -------- modifier flags */ #define F_MTYPE 0x07 #define F_TANNIVERSARY 2 #define F_TEVENT 3 /* flags processed immediately on encountering */ #define F_MIMMEDIATE 0x24 #define F_WTIME_P 0x08 #define F_FORDAYS 0x16 #define F_TODATE 0x24 struct _ftable { char* txt; unsigned flag; }; const struct _ftable FTABLE[]; struct date { unsigned day; unsigned month; unsigned year; }; struct event { char* text; struct date date; struct date enddate; int warn; }; /* ========== Global Variables */ struct event *readlist(void); void gettoday(void); unsigned delta(struct date *); unsigned ddiff(struct date *D1, struct date *D2); void liststrings(struct event *evl); char *tdelta(struct date *d); char *ttime(int yr, int mn, int wk, int dy); char *skptok(char *ptr); int evcmp(const void *e1, const void *e2); struct date today; int def_warn = DEF_WARN; const struct _ftable FTABLE[] = { {"#ann",F_TANNIVERSARY}, {"#ev", F_TEVENT}, {"#w", F_WTIME_P}, {"#to", F_TODATE}, {"#for", F_FORDAYS}, {NULL, 0} }; /* xmalloc/xrealloc functions Note: the x* functions are lifted straight from the GNU libc info docs $Id: xmalloc.c,v 1.2 1999/01/16 17:08:59 andy Exp $ */ void * xmalloc(size_t size) { register void *value = malloc (size); if (value == 0) { fprintf(stderr, "virtual memory exhausted\n"); exit(1); } return value; } void * xrealloc(void *ptr, size_t size) { register void *value = realloc (ptr, size); if (value == 0) { fprintf(stderr, "virtual memory exhausted\n"); exit(1); } return value; } /* ========== */ /* like strcat(), but lets the buffer automagically grow :-) */ int append(char *where, int size, char *what) { if (strlen(what) > ((size) - strlen(where))) { xrealloc(where, (size) + 128 + strlen(what)); size += 128 + strlen(what); } strcat(where, what); return size; } /* ========== */ int before(struct date a, struct date b) { if (a.month < b.month) { return 1; } else if (a.month == b.month && a.day < b.day) { return 1; } else { return 0; } } int ydelta(struct date a, struct date b) { return b.year - a.year + before(a, b); } /* returns the length of the given month */ int mlen(int month, int year) { unsigned mlendat[] = {31,0,31,30,31,30,31,31,30,31,30,31}; if (mlendat[month - 1]) { return mlendat[month - 1]; } else { if (year%4==0 && (year%100!=0 || year%400==0)) { return 29; } else { return 28; } } } /* returns delta(d) in days, weeks, months, etc the returned buffer is malloc()ed, do not forget to free() it */ char * tdelta(struct date *d) { int dy, wk, mn, yr; char *tmp; char *buf = xmalloc(128); int size = 128; *buf = 0; switch (delta(d)) { case 0: size = append(buf, size, "TODAY"); return buf; case 1: size = append(buf, size, "Tomorrow"); return buf; default: /* like delta(), we ignore the year */ yr = -before(*d, today); mn = d->month - today.month; dy = d->day - today.day; if (dy < 0) { dy += mlen(today.month, today.year); mn--; } if (mn < 0) { mn += 12; yr++; } wk = (dy / 7); dy %= 7; size = append(buf, size, "In "); tmp = ttime(yr, mn, wk, dy); size = append(buf, size, tmp); free(tmp); return buf; } } void donum(char *buf, int size, int n, char *txt, int *terms) { char tmp[128]; if (n <= 0) { return; } snprintf(tmp, sizeof(tmp), "%d", n); size = append(buf, size, tmp); size = append(buf, size, " "); size = append(buf, size, txt); if (n != 1) { size = append(buf, size, "s"); } if (--*terms == 1) { size = append(buf, size, " and "); } else if (*terms > 1) { size = append(buf, size, ", "); } } /* returns allocated buffer, don't forget to free() */ char * ttime(int yr, int mn, int wk, int dy) { int size = 128; char *buf = xmalloc(size); int terms = (yr!=0) + (mn!=0) + (wk!=0) + (dy!=0); *buf = '\0'; /* Initialize buffer */ donum(buf, size, yr, "year", &terms); donum(buf, size, mn, "month", &terms); donum(buf, size, wk, "week", &terms); donum(buf, size, dy, "day", &terms); return buf; } /* lists the birthdays in their string format, one by one, and passes the string to a function. */ void liststrings(struct event *evl) { int i,j; char *buf, *tmp; int size; for (i=0; evl[i].text; i++) { size = 128; buf = xmalloc(size); *buf = '\0'; if (evl[i].warn == -1 && delta(&(evl[i].date))==0) { size = append(buf, size, evl[i].text); } else if (evl[i].enddate.day == 0) { if (delta(&(evl[i].date)) <= evl[i].warn) { tmp = tdelta(&(evl[i].date)); size = append(buf, size, tmp); size = append(buf, size, ": "); size = append(buf, size, evl[i].text); free(tmp); } } else { if (delta(&(evl[i].date)) <= evl[i].warn) { size = append(buf, size, evl[i].text); size = append(buf, size, " for "); /* +1 because, if the difference between two dates is one day, then the length of an event on those days is two days */ j = ddiff(&(evl[i].date),&(evl[i].enddate)) + 1; tmp = ttime(0, 0, j/7, j%7); size = append(buf, size, tmp); free(tmp); size = append(buf, size, " "); tmp = tdelta(&(evl[i].date)); size = append(buf, size, tmp); } else if (delta(&(evl[i].enddate)) <= evl[i].warn) { size = append(buf, size, evl[i].text); size = append(buf, size, " "); j = delta(&(evl[i].enddate)); if (j) { size = append(buf, size, "for "); tmp = ttime(0, 0, j/7, j%7); size = append(buf, size, tmp); free(tmp); size = append(buf, size, " longer"); } else { size = append(buf, size, "finishes today"); } } } if (*buf) { size = append(buf, size, "."); puts(buf); } free(buf); } } /* sort the events by the time before the next time they come up, putting those where the start has passed but we are still in the time-period first */ int evcmp(const void *p1, const void *p2) { struct event *e1=(struct event *) p1; struct event *e2=(struct event *) p2; unsigned d1, d2; /* if the delta for the enddate is less than that for the start date, then we have passed the start date but not yet the end date, and so we should display the enddate; otherwise, we should display the start date */ d1=delta(&(e1->date)); if (e1->enddate.day && delta(&(e1->enddate)) < d1) d1=delta(&(e1->enddate)); d2=delta(&(e2->date)); if (e2->enddate.day && delta(&(e2->enddate)) < d2) d2=delta(&(e2->enddate)); if (d1 < d2) return -1; if (d1 > d2) return 1; return strcmp(e1->text, e2->text); } /* difference in days between two dates it is assumed that D1 < D2, and so the result is always positive */ unsigned ddiff(struct date *D1, struct date *D2) { struct date d1, d2; int dd, m; /* make working copies */ d1 = *D1; d2 = *D2; /* sort out zero years */ if (d1.year == 0 || d2.year==0) { if (d1.year != d2.year) { if (d1.year == 0) { if (before(d1,d2)) d1.year = d2.year; else d1.year = d2.year - 1; } else { if (before(d1, d2)) d2.year = d1.year; else d2.year = d1.year + 1; } } else { /* both years zero */ if (before(d1, d2)) d1.year = d2.year = today.year; else { d1.year = today.year; d2.year = d1.year + 1; } } } /* now we can actually do the comparison ... */ dd = 0; /* to start with, we work in months */ for (m=d1.month; m < d2.month + (d2.year-d1.year)*12; m++) dd += mlen(((m-1)%12)+1, d1.year + m/12); /* and then we renormalise for the days within the months the first month was included in our calculations */ dd -= d1.day; /* but the last one wasn't */ dd += d2.day; return dd; } /* actually until the next anniversary of ... */ unsigned delta(struct date *date) { struct date d; unsigned dt, mn; memcpy(&d, date, sizeof(struct date)); /* past the end of the year */ if (before(d, today)) { d.year = 1; } else { d.year = 0; } for (mn = today.month, dt=0; mn < d.month + 12*d.year; mn++) { dt += mlen(((mn-1)%12) + 1,today.year + mn/12); } dt -= today.day; dt += d.day; return dt; } void gettoday(void) { struct tm *tm; time_t t; time(&t); tm = localtime(&t); today.day = tm->tm_mday; today.month = tm->tm_mon + 1; /* 1-12 instead of 0-11 */ today.year = tm->tm_year + 1900; } struct event * readlist() { int i, j, k, l, d; struct event *evl; char buf[1024], buf2[1024]; char *ptr, *cp; unsigned flags; /* initialise */ gettoday(); for (i = 0, evl = NULL; fgets(buf, sizeof(buf), stdin) != NULL; i++) { evl = (struct event *) xrealloc(evl, sizeof(struct event) * (i + 1)); /* ignore comments and empty lines */ if (*buf == '#' || *buf == '\n') { i--; continue; } /* parse string in buf */ ptr = strchr(buf, ' '); /* start of text */ /* not a valid line, so ignore it! Cool, huh? */ /* Attention: only recognizes lines without '=' */ if (!ptr) { fprintf(stderr, "WARNING: Invalid input line:\n\t%s", buf); i--; continue; } *(ptr++) = '\0'; ptr[strlen(ptr)-1] = '\0'; j = sscanf(buf, "%u-%u-%u", &(evl[i].date.year), &(evl[i].date.month), &(evl[i].date.day)); if (j != 3) { fprintf(stderr, "Error: Invalid date:\t%s\n", buf); i--; continue; } /* parse flags */ evl[i].warn = def_warn; evl[i].enddate.day = 0; evl[i].enddate.month = 0; evl[i].enddate.year = 0; flags = 0; j = 0; cp = skptok(ptr); for (cp=ptr; *cp && *cp=='#'; cp=skptok(cp)) { for (k = 0; FTABLE[k].txt && strncmp(FTABLE[k].txt, cp, strlen(FTABLE[k].txt)); k++) { } switch (FTABLE[k].flag) { case F_WTIME_P: /* #w<n> -- sets warning time */ sscanf(cp, "#w%u", &(evl[i].warn)); break; case F_FORDAYS: /* #for<days> -- sets the duration of the event */ sscanf(cp, "#for%u", &d); evl[i].enddate=evl[i].date; for (l = 1; l < d; l++) { evl[i].enddate.day++; if (evl[i].enddate.day > mlen(evl[i].enddate.month, evl[i].enddate.year)) { evl[i].enddate.month++; evl[i].enddate.day = 1; } if (evl[i].enddate.month > 12) { evl[i].enddate.year++; evl[i].enddate.month = 1; } } break; case F_TODATE: /* #to<date> -- sets the end date of the event */ l = sscanf(cp, "#to%u-%u-%u", &(evl[i].enddate.year), &(evl[i].enddate.month), &(evl[i].enddate.day)); if (l == 2) { evl[i].enddate.year = 0; } break; case 0: break; default: flags |= FTABLE[k].flag; break; } } /* construct event text */ switch(flags & F_MTYPE) { default: /* assume it's a birthday */ if (!evl[i].date.year) { sprintf(buf2, "%s has a birthday", cp); break; } int tmp_age = ydelta(evl[i].date, today); sprintf(buf2, "%s is %d year%s old", cp, tmp_age, (tmp_age>1)?"s":""); break; case F_TANNIVERSARY: if (evl[i].date.year) { sprintf(buf2, "%s %d years ago", cp, ydelta(evl[i].date, today)); } else { strcpy(buf2, cp); } break; case F_TEVENT: /* if a year was specified, and this warning isn't for it, ignore! */ if ((evl[i].date.year && ydelta(evl[i].date, today)) && (!evl[i].enddate.year || ydelta(evl[i].enddate, today))) { i--; continue; } strcpy(buf2, cp); break; } evl[i].text = strdup(buf2); } evl = (struct event *) xrealloc(evl, sizeof(struct event) * (i + 1)); evl[i].date.day = 0; evl[i].date.month = 0; evl[i].date.year = 0; evl[i].text = (char *) NULL; fclose(stdin); /* NB uses i from above */ qsort(evl, i, sizeof(struct event), evcmp); return evl; } char * skptok(char *ptr) { while (*ptr && (*ptr!=' ' && *ptr!='\t')) { ptr++; } while (*ptr && (*ptr==' ' || *ptr=='\t')) { ptr++; } return ptr; } int main(int argc, char *argv[]) { while (--argc > 0 && **++argv == '-') { if (strcmp(argv[0], "-W") == 0) { /* TODO: catch if no value given */ def_warn = atoi((++argv)[0]); argc--; } else { fprintf(stderr, "unknown option %s\n", argv[0]); exit(1); } } liststrings(readlist()); return 0; }