~woffs/geb

ref: e9b559ed269802b364538a6002d74dc3fb52ff27 geb/geb.c -rw-r--r-- 5.4 KiB
e9b559ed — Frank Doepper all UTF-8 9 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
 * geb.c - Geburtstagsvorwarner (german language)
 * Copyright (C) 1998 Frank Doepper
 */

const char cvsid[]="$Id: geb.c,v 1.12 2007-02-12 09:49:37 woffs Exp $";

/*
 * 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.
 *
 * 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.  See the
 * GNU General Public License for more details.
 *
 * 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
 *
 */

#include <stdio.h>
#include <string.h>
#include <time.h>
#include <malloc.h>
#include <unistd.h>

#define NOYEAR 16000
#define VORAUS 7
#define MAXERR 20

#ifdef WITHANSI
  #define BOLDMODE "\e[1m"
  #define NOMODE "\e[m"
#endif

struct zeile {
  int tag, monat, jahr;
  char text[80];
};

char d[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
const char *wochentag[]={"Sonntag","Montag","Dienstag","Mittwoch",
                          "Donnerstag","Freitag","Sonnabend"};

static const struct datum {
  int tag,monat,dow,jahr;
} jetzt;

struct liste {
  char text[80];
  struct liste *next;
} *hook[VORAUS+1];

long lnum=0;

void ausgeben(struct datum *d) {
  printf("Heute ist %s, der %d.%d.%d.\n",wochentag[d->dow],d->tag,d->monat,d->jahr);
}

int holezeile(FILE *fd, struct zeile *z) {
  char puffer[80],dist;

  do {
    lnum++;
    if (fgets(puffer,80,fd)==NULL) return (-1);
  }
  while ((puffer[0]==' ') || (puffer[0]==';') ||
         (puffer[0]=='#') || (puffer[0]=='\r') || (puffer[0]=='\n'));
  if (sscanf(puffer,"%d.%d.%c",&(z->tag),&(z->monat),&dist)!=3) return (lnum);
  if ((z->tag<1) || (z->tag>31)) return (lnum);
  if ((z->monat<1) || (z->monat>12)) return (lnum);
  if (dist==' ')
    z->jahr=NOYEAR;
  else
    if (sscanf(puffer,"%*d.%*d.%d",&(z->jahr))!=1) return (lnum);
  strcpy(z->text,strchr(puffer,' ')+1);
  while ((z->text[strlen(z->text)-1]=='\n') ||
         (z->text[strlen(z->text)-1]=='\r'))
    z->text[strlen(z->text)-1]=0;
  return 0;
}

void increment(struct datum *da) /* macht morgen aus heute */
  {
  if (((da->jahr % 4)==0) && !(((da->jahr % 100)==0) && ((da->jahr % 400)!=0)))
    d[2]=29; else d[2]=28;
  if (++da->tag>d[da->monat]) {
    da->tag=1;
    if (++da->monat>12) {
      da->monat=1;
      da->jahr++;
    }
  }
}

int gleich(struct datum *h, struct zeile *d) {
  return ((h->tag==d->tag) && (h->monat==d->monat));
}

void ausgabe(struct zeile *z, int i, struct datum *h) {
  struct liste *elem,*ptr;
#ifdef WITHANSI
  char name[88];
#endif

  if ((elem=(struct liste *)malloc(sizeof(struct liste)))==NULL)
    _exit(5);
  elem->next=NULL;
#ifdef WITHANSI
  sprintf(name,"%s%s%s",BOLDMODE,z->text,NOMODE);
#endif
  if (z->jahr==NOYEAR)
    sprintf(elem->text,"ist %s",
#ifdef WITHANSI
    name
#else
    z->text
#endif
    );
  else
    sprintf(elem->text,"wird %s %d Jahr%s alt",
#ifdef WITHANSI
    name
#else
    z->text
#endif
    ,h->jahr-z->jahr,(h->jahr-z->jahr==1)?"":"e");
  if (hook[i]==NULL)
    hook[i]=elem;
  else {
    ptr=hook[i];
    while (ptr->next!=NULL) ptr=ptr->next;
    ptr->next=elem;
  }
}

void raus(struct datum *h) {
  int i;
  struct liste *ptr;
  char bald[20];

  for (i=0; i<=VORAUS; i++) {
    switch (i) {
      case 0: strcpy(bald,"Heute");break;
      case 1: strcpy(bald,"Morgen");break;
      case 2: 
#ifndef __DJGPP__
#ifdef UTF8
        strcpy(bald,"Übermorgen"); /* UTF-8 */
#else
        strcpy(bald,"\334bermorgen"); /* ISO-8859-1 */
#endif
#else
        strcpy(bald,"\232bermorgen"); /* DOS-Codepage */
#endif
        break;
      case 7: strcpy(bald,"In einer Woche");break;
      default: sprintf(bald,"Am %s",wochentag[(h->dow+i)%7]);
    }
    for (ptr=hook[i]; ptr!=NULL; ptr=ptr->next) {
      if (ptr==hook[i]) printf("\n%s %s",bald,ptr->text);
      else printf("\n und %s",ptr->text);
      if (ptr->next==NULL) printf(".\n");
    }
  }
}

int main (int argc, char *argv[]) {
  FILE *fd;
  struct datum dat;
  long i;
  struct zeile z;
  int err=0;
  struct datum jetzt;

  for(i=0;i<=VORAUS;i++) hook[i]=NULL;
  if (argc==2)
    fd=fopen(argv[1],"r");
  else {
    fprintf(stderr,"Geburtstagsvorwarner, Version %s\n",VER);
    fprintf(stderr,"(C) 1999-2002 Frank Doepper. Dieses Programm unterliegt der GPL.\n");
    fprintf(stderr,"Syntax: %s <Listendatei>\n",argv[0]);
    return(1);
    }
  if (fd==NULL) {
    perror(argv[1]);
    return 2;
  }

  {
    char ttt[15];
    time_t tp;

    time(&tp);
    strftime(ttt,15,"%d.%m.%Y.%w",localtime(&tp));
    sscanf(ttt,"%d.%d.%d.%d",&jetzt.tag,&jetzt.monat,&jetzt.jahr,&jetzt.dow);
  }

  while ((i=holezeile(fd,&z))!=(-1)) {
    if (i>0) {
      fprintf(stderr,"Fehler in Listendatei in Zeile %ld.\n",i);
      if (++err>=MAXERR) {
        fprintf(stderr,"Zu viele Fehler. Abbruch.\n");
        fclose(fd);
        return 1;
      }
    }
    else {
      dat=jetzt;
      for(i=0;i<=VORAUS;i++,increment(&dat)) {
        if (gleich(&dat,&z)) ausgabe(&z,i,&dat);
      }
    }
  }
  if (!feof(fd)) {
    perror(argv[1]);
    fclose(fd);
    return 2;
  }
  printf("\n");
  dat=jetzt;
  ausgeben(&dat);
  raus(&dat);
  printf("\n");
  fclose(fd);
  return 0;
}