Added manpage. The page is not complete but one can see how the program
[infodrom/dtaus] / dtaus.c
1 /*
2     dtaus.c - Datenträgeraustausch mit einer Bank
3     Copyright (c) 1996  Martin Schulze <joey@artis.uni-oldenburg.de>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19     $Id$
20  */
21
22 #include "dtaus.h"
23 #include "bigint.h"
24 #include <stdio.h>
25 #include <string.h>
26 #include <ctype.h>
27 #include <time.h>
28 #include <malloc.h>
29
30 /*
31  *  First: Some control structures
32  */
33
34 typedef struct
35 {
36   char *name;
37   unsigned short int pos;
38   unsigned short int len;
39   int type;
40 } dtaus_record;
41
42 #define REC_A   1
43 #define REC_C   2
44 #define REC_E   3
45
46 #define REQ     1
47 #define OPT     2
48 #define IGN     3
49
50 dtaus_record recA[] = {
51   {"Art", 5, 2, REQ},
52   {"Name", 23, 27, REQ},
53   {"Konto", 60, 10, REQ},
54   {"BLZ", 7, 8, REQ},
55   {"Referenz", 70, 10, OPT},
56   {"Datum", 50, 6, IGN},
57   {NULL, 0, 0}
58 };
59
60 #define A_TRANS 0
61 #define A_NAME  1
62 #define A_KTO   2
63 #define A_BLZ   3
64 #define A_REF   4
65 #define A_DATE  5
66 #define A_LEN   6
67
68 dtaus_record recC[] = {
69   {"Name", 93, 27, REQ},
70   {"Konto", 21, 10, REQ},
71   {"BLZ", 13, 8, REQ},
72   {"Transaktion", 44, 5, REQ},
73   {"Betrag", 50, 11, REQ},
74   {"Zweck", 155, 27, REQ},
75   {"myName", 128, 27, OPT},
76   {"myKonto", 69, 10, OPT},
77   {"myBLZ", 61, 8, OPT},
78   {"Text", 187, 29, OPT},
79   {"Extension", 216, 29, OPT},
80   {NULL, 0, 0}
81 };
82
83 #define C_NAME  0
84 #define C_KTO   1
85 #define C_BLZ   2
86 #define C_TRANS 3
87 #define C_VAL   4
88 #define C_ZWECK 5
89 #define C_MYNAM 6
90 #define C_MYKTO 7
91 #define C_MYBLZ 8
92 #define C_TEXT  9
93 #define C_EXT  10
94 #define C_LEN  11
95
96 dtaus_record recE[] = {
97   {"Anzahl", 10, 7, IGN},
98   {"Summe", 17, 13, IGN},
99   {"Kontos", 30, 17, IGN},
100   {"BLZs", 47, 17, IGN},
101   {NULL, 0, 0}
102 };
103
104 #define E_COUNT 0
105 #define E_VAL   1
106 #define E_KTO   2
107 #define E_BLZ   3
108 #define E_LEN   0
109
110 /*
111  *  Second: Some low level routines
112  */
113
114 size_t dtaus_nextrec (void **buf, FILE *f)
115 {
116   bzero (buf, 128);
117   return fread(buf, 128, 1, f);
118 }
119
120 char *upcase(char *s)
121 {
122   static char x[100];
123   static char *xp;
124   char *cp;
125
126   for (cp=s,xp=x; *cp; cp++,xp++) {
127     if (strchr(" 0123456789.,&-/+*$%ABCDEFGHIJKLMNOPQRSTUVWXYZ",*cp)) {
128         *xp = *cp;
129     } else if (strchr("abcdefghijklmnopqrstuvwxyz",*cp)) {
130       *xp = toupper(*cp);
131     } else if (strchr ("üÜäÄöÖß", *cp)) {
132       switch (*cp) {
133       case 'ü': *(xp++) = 'U'; *xp='E'; break;
134       case 'Ü': *(xp++) = 'U'; *xp='E'; break;
135       case 'ä': *(xp++) = 'A'; *xp='E'; break;
136       case 'Ä': *(xp++) = 'A'; *xp='E'; break;
137       case 'ö': *(xp++) = 'O'; *xp='E'; break;
138       case 'Ö': *(xp++) = 'O'; *xp='E'; break;
139       case 'ß': *(xp++) = 'S'; *xp='S'; break;
140       }
141     } else
142       *xp = ' ';
143   }
144   *xp = '\0';
145
146   return x;
147 }
148
149 char *downcase(char *s)
150 {
151   static char x[100];
152   char *cp;
153
154   strcpy (x, s);
155   
156   for (cp=x;*cp;cp++)
157     if (isupper(*cp))
158       *cp = tolower(*cp);
159   return x;
160 }
161
162 char *strip_spaces (char *s)
163 {
164   int i;
165   char *p;
166
167   for (i=strlen(s);(s[i-1] == ' '||s[i-1] == '\t')&&i>0; i--)
168     s[i-1] = '\0';
169   for (p=s; *p==' '; p++);
170   return p;
171 }
172
173 char *strip_zeros (char *s)
174 {
175   char *p;
176
177   for (p=s; *p=='0'; p++);
178   return p;
179 }
180
181 char dtaus_char(void *buf, unsigned int pos)
182 {
183   static char res;
184   char *bufp = buf;
185
186   bufp+=pos;
187   memcpy(&res, bufp, 1);
188   return res;
189 }
190
191 char *string2real(char *s)
192 {
193   static char res[20];
194   char *cp = s;
195
196   cp+=strlen(s)-2;
197   strncpy(res, s, strlen(s)-2);
198   res[strlen(s)-2] = '.';
199   res[strlen(s)-1] = s[strlen(s)-2];
200   res[strlen(s)] = s[strlen(s)-1];
201   return res;
202 }
203
204 char *real2string(char *s)
205 {
206   static char res[20];
207   char *cp;
208
209   strcpy(res, s);
210   for (cp=res; *cp&&!(*cp == ',')&&!(*cp == '.');cp++);
211   *(cp++) = *(cp+1);
212   *(cp++) = *(cp+1);
213   *cp = '\0';
214   return res;
215 }
216
217 char *string2trans (char *s)
218 {
219   static char res[30];
220
221   res[0] = '\0';
222   if (!strcmp(s, "04000"))
223     sprintf (res, "Abbuchung");
224   else if (!strcmp(s, "05000"))
225     sprintf (res, "Einzug");
226   else if (!strcmp(s, "05005"))
227     sprintf (res, "E-Cash");
228   else if (!strcmp(s, "05006"))
229     sprintf (res, "E-Cash-A");
230   else if (!strcmp(s, "51000"))
231     sprintf (res, "Gutschrift");
232   else if (!strcmp(s, "53000"))
233     sprintf (res, "Lohn");
234   else if (!strncmp(s, "5400", 4))
235     sprintf (res, "Vermögen");
236   /*  else if (!strcmp(s, "56000"))
237     sprintf (res, ""); / * Überweisung öffentlicher Kassen */
238   return res;
239 }
240
241 char *trans2string (char *s)
242 {
243   static char res[30];
244
245   res[0] = '\0';
246   if (!strcmp(s, "Abbuchung"))
247     sprintf (res, "04000");
248   else if (!strcmp(s, "Einzug"))
249     sprintf (res, "05000");
250   else if (!strcmp(s, "E-Cash"))
251     sprintf (res, "05005");
252   else if (!strcmp(s, "E-Cash-A"))
253     sprintf (res, "05006");
254   else if (!strcmp(s, "Gutschrift"))
255     sprintf (res, "51000");
256   else if (!strcmp(s, "Lohn"))
257     sprintf (res, "53000");
258   else if (!strncmp(s, "Vermögen", 4))
259     sprintf (res, "5400");
260   /*  else if (!strcmp(s, ""))
261     sprintf (res, "56000"); / * Überweisung öffentlicher Kassen */
262   return res;
263 }
264
265 char *string2ext (char *s)
266 {
267   static char res[30];
268
269   res[0] = '\0';
270   if (!strcmp(s, "01"))
271     sprintf (res, "Klient");
272   else if (!strcmp(s, "02"))
273     sprintf (res, "Text");
274   else if (!strcmp(s, "03"))
275     sprintf (res, "Auftraggeber");
276   return res;
277 }
278
279 char *ext2string (char *s)
280 {
281   static char res[3];
282
283   res[0] = '\0';
284   if (!strcmp(s, "Klient"))
285     sprintf (res, "01");
286   else if (!strcmp(s, "Text"))
287     sprintf (res, "02");
288   else if (!strcmp(s, "Auftraggeber"))
289     sprintf (res, "03");
290   return res;
291 }
292     
293 unsigned long int dtaus_int(void *buf, unsigned int pos, unsigned int len)
294 {
295   char tmp[30];
296   char *bufp = buf;
297   static unsigned long int res;
298
299   bufp+=pos;
300   memcpy(tmp, bufp, len);
301   tmp[len] = '\0';
302   sscanf(tmp, "%lu", &res);
303   return res;
304 }
305
306 /*
307  * returns the first word in this line, returns it and shortens the
308  * line.
309  */
310 char *extract_ident (char *line)
311 {
312   char *c, *x, *y;
313   static char word[30];
314
315   if (strlen(line) > 0) {
316     x = index (line, ' ');
317     y = index (line, '\t');
318
319     if (!x && !y) {
320         strncpy(word, line, 29);
321         line[0] = '\0';
322         return word;
323     }
324
325     /* Check which index returns the lower value, and check if the
326        value is non-NULL */
327     if ((c = (x && x<y)?x:(y?y:x))) { 
328       strncpy(word, line, c - line);
329       word[c-line] = '\0';
330       for (;*c == '\t' || *c == ' '; c++);
331       for (x=line; *c; c++,x++)
332         *x = *c;
333       *x = '\0';
334       strcpy(word, downcase(word));
335       return word;
336     }
337     return NULL;
338   }
339   return line;
340 }
341
342 int rec_index(char *ident, int type)
343 {
344   int i;
345   dtaus_record *rec = NULL;
346
347   switch (type) {
348   case REC_A:   rec = recA; break;
349   case REC_C:   rec = recC; break;
350   case REC_E:   rec = recE; break;
351   }
352
353   for (i=0; (rec[i].name); i++) {
354     if (!strcmp(ident, downcase(rec[i].name)))
355       return i;
356   }
357
358   return -1;
359 }
360
361 size_t control_nextline (void **buf, int len, FILE *f)
362 {
363   char line[100];
364   char tmp[100];
365   char *cp;
366   int i;
367
368   bzero (line, sizeof(line));
369   bzero (buf, len);
370   cp = line;
371
372   while (!strlen(line) && (cp = fgets(line, 100, f))) {
373     if (strlen(line)) {
374       if (line[0] != '#') {
375         if (line[strlen(line)-1] != '\n') { 
376           strcpy(tmp, line);
377           while (tmp[strlen(tmp)-1] != '\n' && (cp = fgets(tmp, 100, f)));
378         } else
379           line[strlen(line)-1] = '\0';
380         if (line[strlen(line)-1] == '\r')
381           line[strlen(line)-1] = '\0';
382         for (i=strlen(line);(line[i-1] == ' '||line[i-1] == '\t')&&i>0; i--)
383           line[i-1] = '\0';
384         if (line[0] == '#')
385           line[0] = '\0';
386       } else
387         line[0] = '\0';
388     }
389   }
390   for (cp=line; *cp==' '; cp++);
391
392   if (strlen(cp)) {
393     memcpy(buf, cp, strlen(cp));
394     return 1;
395   } else
396     return 0;
397 }
398
399 char *get_date()
400 {
401   static char res[10];
402   time_t timer;
403   struct tm *loctime;
404
405   timer = time ( NULL );
406   loctime = localtime(&timer);
407   sprintf(res, "%02d.%02d.%02d", loctime->tm_mday, loctime->tm_mon+1, loctime->tm_year);
408   return res;
409 }
410
411 void dtaus_prepareA(char *buf)
412 {
413   int i;
414   time_t timer;
415   struct tm *loctime;
416   char tmp[10];
417
418   bzero (buf, 129);
419   timer = time ( NULL );
420   loctime = localtime(&timer);
421
422   buf[0] = '0';
423   buf[1] = '1';
424   buf[2] = '2';
425   buf[3] = '8';
426   buf[4] = 'A';
427   for (i=15;i<15+8; i++) buf[i] = '0';
428   sprintf(tmp, "%02d%02d%2d", loctime->tm_mday, loctime->tm_mon+1, loctime->tm_year);
429   for (i=0; i<6; i++) buf[50+i] = tmp[i];
430   for (i=56;i<56+4; i++) buf[i] = ' ';
431   for (i=70;i<70+10; i++) buf[i] = '0';
432   for (i=80;i<80+48; i++) buf[i] = ' ';
433 }
434
435 void dtaus_prepareC(char *buf)
436 {
437   int i;
438
439   bzero (buf, 257);
440   buf[0] = '0';
441   buf[1] = '2';
442   buf[2] = '1';
443   buf[3] = '6';
444   buf[4] = 'C';
445   for (i=31;i<31+13; i++) buf[i] = '0';
446   buf[49] = ' ';
447   for (i=79;i<79+11; i++) buf[i] = '0';
448   for (i=90;i<90+3; i++) buf[i] = ' ';
449   for (i=120;i<120+8; i++) buf[i] = ' ';
450   for (i=182;i<182+3; i++) buf[i] = ' ';
451   for (i=185;i<185+2; i++) buf[i] = '0';
452   for (i=187;i<187+(29*2); i++) buf[i] = ' ';
453   for (i=245;i<245+11; i++) buf[i] = ' ';
454 }
455
456 void dtaus_prepareE(char *buf)
457 {
458   int i;
459
460   bzero (buf, 129);
461   buf[0] = '0';
462   buf[1] = '1';
463   buf[2] = '2';
464   buf[3] = '8';
465   buf[4] = 'E';
466   for (i=5;i<5+5; i++) buf[i] = ' ';
467   for (i=64;i<64+13; i++) buf[i] = '0';
468   for (i=77;i<77+51; i++) buf[i] = ' ';
469 }
470
471 int dtaus_writeA(FILE *f, char **values)
472 {
473   char buf[129];
474   char tmp[30];
475   int i;
476   
477   for (i=0; (recA[i].name); i++)
478     if ((recA[i].type == REQ) && !values[i]) {
479       fprintf (stderr, "Anfangsdatensatz ist nicht vollständig, kein %s.\n", recA[i].name);
480       return 0;
481     }
482   if (!(((values[A_TRANS][0] == 'L')||(values[A_TRANS][0] == 'G'))
483         &&((values[A_TRANS][1] == 'B')||(values[A_TRANS][1] == 'K')))) {
484     fprintf (stderr, "Ungueltiger Typ, nur LK, GK, LB oder GB erlaubt.\n");
485     return 0;
486   }
487
488   dtaus_prepareA(buf);
489   buf[5] = values[A_TRANS][0];
490   buf[6] = values[A_TRANS][1];
491   sprintf (tmp, "%08s", values[A_BLZ]);
492   for (i=0; i<8; i++) buf[recA[A_BLZ].pos+i] = tmp[i];
493   sprintf (tmp, "%-27s", upcase(values[A_NAME]));
494   for (i=0; i<27; i++) buf[recA[A_NAME].pos+i] = tmp[i];
495   sprintf (tmp, "%010s", values[A_KTO]);
496   for (i=0; i<10; i++) buf[recA[A_KTO].pos+i] = tmp[i];
497
498   fputs(buf, f);
499   return 1;
500 }
501
502 int dtaus_writeC(FILE *f, char **valuesA, char **values)
503 {
504   char buf[257];
505   char tmp[30];
506   int i;
507
508   /*
509   for (i=0; (recC[i].name); i++)
510     if (values[i])
511       printf ("%s: %s\n", recC[i].name, values[i]);
512       */
513
514   for (i=0; (recC[i].name); i++)
515     if ((recC[i].type == REQ) && !values[i]) {
516       fprintf (stderr, "Datensatz ist nicht vollständig, kein %s.\n", recC[i].name);
517       return 0;
518     }
519   sprintf (tmp, "%s", trans2string(values[C_TRANS]));
520   if (!strlen(tmp)) {
521     fprintf (stderr, "Ungültiger Typ, nur Abbuchung, Einzug, E-Cash, E-Cash-A, Gutschrift und Lohn erlaubt.\n");
522     return 0;
523   }
524
525   dtaus_prepareC(buf);
526   if (!values[C_TEXT]) {
527     buf[1] = '1';
528     buf[2] = '8';
529     buf[3] = '7';
530   }
531   for (i=0; i<5; i++) buf[recC[C_TRANS].pos+i] = tmp[i];
532   if (values[C_MYBLZ])
533     sprintf (tmp, "%08s", values[C_MYBLZ]);
534   else
535     sprintf (tmp, "%08s", valuesA[A_BLZ]);
536   for (i=0; i<recC[C_MYBLZ].len; i++) buf[5+i] = tmp[i];
537   for (i=0; i<recC[C_MYBLZ].len; i++) buf[recC[C_MYBLZ].pos+i] = tmp[i];
538   sprintf (tmp, "%08s", values[C_BLZ]);
539   for (i=0; i<recC[C_BLZ].len; i++) buf[recC[C_BLZ].pos+i] = tmp[i];
540   sprintf (tmp, "%010s", values[C_KTO]);
541   for (i=0; i<recC[C_KTO].len; i++) buf[recC[C_KTO].pos+i] = tmp[i];
542   sprintf (tmp, "%011s", real2string(values[C_VAL]));
543   for (i=0; i<recC[C_VAL].len; i++) buf[recC[C_VAL].pos+i] = tmp[i];
544   if (values[C_MYKTO])
545     sprintf (tmp, "%010s", values[C_MYKTO]);
546   else
547     sprintf (tmp, "%010s", valuesA[A_KTO]);
548   for (i=0; i<recC[C_MYKTO].len; i++) buf[recC[C_MYKTO].pos+i] = tmp[i];
549   sprintf (tmp, "%-27s", upcase(values[C_NAME]));
550   for (i=0; i<recC[C_NAME].len; i++) buf[recC[C_NAME].pos+i] = tmp[i];
551   if (values[C_MYNAM])
552     sprintf (tmp, "%-27s", upcase(values[C_MYNAM]));
553   else
554     sprintf (tmp, "%-27s", upcase(valuesA[A_NAME]));
555   for (i=0; i<recC[C_MYNAM].len; i++) buf[recC[C_MYNAM].pos+i] = tmp[i];
556   sprintf (tmp, "%-27s", upcase(values[C_ZWECK]));
557   for (i=0; i<recC[C_ZWECK].len; i++) buf[recC[C_ZWECK].pos+i] = tmp[i];
558
559   if (!values[C_TEXT]) {
560     buf[recC[C_TEXT].pos+0] = ' ';
561     buf[recC[C_TEXT].pos+1] = ' ';
562   } else {
563     buf[185+0] = '0';
564     buf[185+1] = '1';
565     buf[recC[C_TEXT].pos+0] = '0';
566     buf[recC[C_TEXT].pos+1] = '2';
567     sprintf (tmp, "%-27s", upcase(values[C_TEXT]));
568     for (i=0; i<recC[C_TEXT].len-2; i++) buf[recC[C_TEXT].pos+2+i] = tmp[i];
569   }
570   fputs(buf, f);
571   return 1;
572 }
573
574 int dtaus_writeE(FILE *f, int count, bigint sum, bigint blz, bigint kto)
575 {
576   char buf[129];
577   char tmp[30];
578   int i;
579   
580   dtaus_prepareE(buf);
581
582   sprintf (tmp, "%07d", count);
583   for (i=0; i<recE[E_COUNT].len; i++) buf[recE[E_COUNT].pos+i] = tmp[i];
584   bigint_sprintf (tmp, "%013s", sum);
585   for (i=0; i<recE[E_VAL].len; i++) buf[recE[E_VAL].pos+i] = tmp[i];
586   bigint_sprintf (tmp, "%013s", sum);
587   for (i=0; i<recE[E_VAL].len; i++) buf[recE[E_VAL].pos+i] = tmp[i];
588   bigint_sprintf (tmp, "%017s", kto);
589   for (i=0; i<recE[E_KTO].len; i++) buf[recE[E_KTO].pos+i] = tmp[i];
590   bigint_sprintf (tmp, "%017s", blz);
591   for (i=0; i<recE[E_BLZ].len; i++) buf[recE[E_BLZ].pos+i] = tmp[i];
592
593   fputs(buf, f);
594   return 1;
595 }
596
597 /*
598  *  Third: Some high level routines
599  */
600
601 void dtaus2control (char *cdtaus, char *ccontrol)
602 {
603   FILE *fdtaus, *fcontrol;
604   void *buf;
605   void *bufp;
606   char tmp[30];
607   char x[30];
608   int index, countC;
609
610   if (!cdtaus)
611     if (!(fdtaus = fopen("DTAUS0.TXT", "r")))
612       if (!(fdtaus = fopen("dtaus0.txt", "r")))
613         return;
614   if (cdtaus)
615     if (!(fdtaus = fopen(cdtaus, "r")))
616       return;
617   if (!ccontrol)
618     fcontrol = stdout;
619   else
620     if (!(fcontrol = fopen(ccontrol, "w")))
621       return;
622   if (!(buf = (char *)malloc (512)))
623     return;
624
625   /* 
626    * Record A lesen
627    */
628   if (dtaus_nextrec(buf, fdtaus) == 1) {
629     if (dtaus_char(buf,4) == 'A') {
630       fprintf(fcontrol, "BEGIN {\n");
631       bufp = buf;
632
633       for (index=A_TRANS; index < A_DATE; index++) {
634         bufp = buf + recA[index].pos;
635         memcpy(tmp, bufp, recA[index].len); tmp[recA[index].len] = '\0';
636         fprintf(fcontrol, "  %s\t%s\n", recA[index].name, strip_zeros(strip_spaces(tmp)));
637       }
638
639       index = A_DATE; bufp = buf + recA[index].pos;
640       memcpy(tmp, bufp, recA[index].len); tmp[recA[index].len] = '\0';
641       fprintf(fcontrol, "  %s\t%s\n", recA[index].name, strip_spaces(tmp));
642
643       fprintf(fcontrol, "}\n\n");
644     } else {
645       fprintf (stderr, "Datei fängt nicht mit dem Anfangsdatensatz an.\n");
646       return;
647     }
648   } else {
649     fprintf (stderr, "Der Anfangsdatensatz ist kaputt.\n");
650     return;
651   }
652
653   /*
654    * Record C lesen
655    */
656   if (dtaus_nextrec(buf, fdtaus) == 1) {
657     while (dtaus_char(buf,4) == 'C') {
658       bufp = buf + 128;
659       if (dtaus_nextrec(bufp, fdtaus) == 1) {
660         fprintf(fcontrol, "{\n");
661
662         for (index=C_NAME; index <= C_MYBLZ; index++) {
663           bufp = buf + recC[index].pos;
664           memcpy(tmp, bufp, recC[index].len); tmp[recC[index].len] = '\0';
665           if (index == C_VAL)
666             fprintf(fcontrol, "  %s\t%s\n", recC[index].name, strip_zeros(string2real(tmp)));
667           else if (index == C_TRANS)
668             fprintf(fcontrol, "  %s\t%s\n", recC[index].name, string2trans(tmp));
669           else
670             fprintf(fcontrol, "  %s\t%s\n", recC[index].name, strip_zeros(strip_spaces(tmp)));
671         }
672
673         for (index=C_TEXT; index <= C_EXT; index++) {
674           if (!(dtaus_char(buf,recC[index].pos) == ' ')) {
675             bufp = buf + recC[index].pos;
676             memcpy(x, bufp, 2); tmp[2] = '\0'; bufp+=2;
677             memcpy(tmp, bufp, recC[index].len-2); tmp[recC[index].len-2] = '\0';
678             fprintf(fcontrol, "  %s\t%s\n", string2ext(x), strip_spaces(tmp));
679           }
680         }
681
682         countC = dtaus_int(buf, 185, 2);
683         countC = countC<3?0:countC-2/4;
684         countC += countC%4>0?1:0;
685         /* FIXME: Erweiterungsfelder werden ignoriert */
686
687         fprintf(fcontrol, "}\n");
688       } else {
689         fprintf (stderr, "Der zweite Teil der Transaktion ist kaputt.\n");
690         return;
691       }
692       if (dtaus_nextrec(buf, fdtaus) != 1)
693         bzero (buf, sizeof(buf));
694     }
695   }
696
697   /*
698    * Record E lesen
699    *   (gelesen ist er eigentlich schon...)
700    */
701   if (dtaus_char(buf,4) == 'E') {
702     if (dtaus_char(buf,4) == 'E') {
703       fprintf(fcontrol, "END {\n");
704
705       for (index=E_COUNT; index <= E_BLZ; index++) {
706         bufp = buf + recE[index].pos;
707         memcpy(tmp, bufp, recE[index].len); tmp[recE[index].len] = '\0';
708         if (index == E_VAL)
709           fprintf(fcontrol, "  %s\t%s\n", recE[index].name, strip_zeros(string2real(tmp)));
710         else
711           fprintf(fcontrol, "  %s\t%s\n", recE[index].name, strip_zeros(tmp));
712       }
713
714       fprintf(fcontrol, "}\n");
715     } else {
716       fprintf (stderr, "Das ist kein Abschlußdatensatz.\n");
717       return;
718     }
719   } else {
720     fprintf (stderr, "Der Abschlußdatensatz ist leer oder kaputt.\n");
721     return;
722   }
723   fclose(fcontrol);
724   fclose(fdtaus);
725 }
726
727 int control2dtaus (char *ccontrol, char *cdtaus, char *cbeleg, char *ccheck)
728 {
729   FILE *fdtaus, *fcontrol, *fbeleg, *fcheck;
730   void *buf;
731   char *ident;
732   int  recindex;
733   char tmp[30];
734   char line[100];
735   char *valA[A_LEN], *valC[C_LEN];
736   int count;
737   bigint sum_val, sum_blz, sum_kto, bi;
738
739   if (!cdtaus)
740     if (!(fdtaus = fopen("dtaus0.txt", "w")))
741       return 0;
742   if (cdtaus)
743     if (!(fdtaus = fopen(cdtaus, "w")))
744       return 0;
745   if (!ccontrol)
746     if (!(fcontrol = fopen("dtaus0.ctl", "r")))
747       if (!(fcontrol = fopen("DTAUS0.CTL", "r")))
748         return 0;
749   if (ccontrol)
750     if (!(fcontrol = fopen(ccontrol, "r")))
751       return 0;
752   if (!cbeleg)
753     if (!(fbeleg = fopen("dtaus0.doc", "w")))
754       return 0;
755   if (cbeleg)
756     if (!(fbeleg = fopen(cbeleg, "w")))
757       return 0;
758   if (!ccheck)
759     fcheck = stdout;
760   else
761     if (!(fcheck = fopen(ccheck, "w")))
762       return 0;
763
764
765   if (!(buf = (char *)malloc (512)))
766     return 0;
767
768   /* 
769    * Record A lesen
770    */
771   bzero(valA, sizeof(valA));
772   control_nextline ((void *)line, 100, fcontrol);
773   ident = extract_ident(line);
774   if (!strcmp(ident, "begin") && (line[0] == '{')) {
775     fprintf(fbeleg, "\n\n");
776     fprintf(fbeleg, "\n    Begleitzettel\n\n");
777     fprintf(fbeleg, "\n    Belegloser Datentraegeraustausch\n\n");
778     fprintf(fbeleg, "\n    Sammeleinziehungsauftrag\n\n");
779     fprintf(fbeleg, "\n    VOL ........................:\n");
780     fprintf(fbeleg, "\n    Erstellungsdatum ...........: %s\n", get_date());
781
782     control_nextline ((void *)line, 100, fcontrol);
783     while (strlen(line) && line[0] != '}') {
784       ident = extract_ident(line);
785       if ((recindex = rec_index(ident, REC_A)) != -1)
786         if (recA[recindex].type != IGN)
787           if ((valA[recindex] = (char *)malloc (strlen(line)+1)))
788             strcpy(valA[recindex], line);
789       control_nextline ((void *)line, 100, fcontrol);
790     }
791     if (!dtaus_writeA(fdtaus, valA)) {
792       fprintf (stderr, "Konnte den Anfangsdatensatz nicht schreiben.\n");
793       return 0;
794     }
795
796     fprintf (fcheck, "\n\n\n");
797     fprintf (fcheck, "    Sammeleinzeiehungsauftrag\n\n");
798     fprintf (fcheck, "    Erstellungsdatum : %s\n\n\n", get_date());
799     fprintf (fcheck, "     %-10s  %-8s  %-30s   %12s\n", "Kontonr.", "BLZ", "Name", "Betrag");
800     fprintf (fcheck, "    --------------------------------------------------------------------\n");
801   } else {
802     fprintf (stderr, "Datei fängt nicht mit dem Anfangsdatensatz (BEGIN) an.\n");
803     return 0;
804   }
805
806   /* 
807    * Record C lesen
808    */
809   count = 0;
810   sum_val = bigint_int(0);
811   sum_blz = bigint_int(0);
812   sum_kto = bigint_int(0);
813   bzero(valC, sizeof(valC));
814   control_nextline ((void *)line, 100, fcontrol);
815   if (line[0] == '{') {
816     while (strlen(line) && line[0] == '{') {
817       control_nextline ((void *)line, 100, fcontrol);
818       while (strlen(line) && line[0] != '}') {
819         ident = extract_ident(line);
820         if ((recindex = rec_index(ident, REC_C)) != -1)
821           if (recC[recindex].type != IGN)
822             if ((valC[recindex] = (char *)malloc (strlen(line)+1)))
823               strcpy(valC[recindex], line);
824         control_nextline ((void *)line, 100, fcontrol);
825       }
826       if (!dtaus_writeC(fdtaus, valA, valC)) {
827         fprintf (stderr, "Konnte den regulären Datensatz nicht schreiben.\n");
828         return 0;
829       }
830       count++;
831       bi = bigint_string(real2string(valC[C_VAL])); sum_val = bigint_add(sum_val, bi);
832       bi = bigint_string(valC[C_BLZ]); sum_blz = bigint_add(sum_blz, bi);
833       bi = bigint_string(valC[C_KTO]); sum_kto = bigint_add(sum_kto, bi);
834
835       fprintf (fcheck, "     %10s  %8s  %-30s   %12s\n", valC[C_KTO], valC[C_BLZ], valC[C_NAME], valC[C_VAL]);
836       for (recindex=0; recindex<C_LEN; recindex++)
837         if (valC[recindex])
838           free(valC[recindex]);
839       bzero(valC, sizeof(valC));
840       control_nextline ((void *)line, 100, fcontrol);
841     }
842   } else {
843     fprintf (stderr, "Kein regulärer Datensatz?\n");
844     return 0;
845   }
846
847   /* 
848    * Record E lesen
849    */
850   dtaus_writeE(fdtaus, count, sum_val, sum_blz, sum_kto);
851   fprintf (fcheck, "    --------------------------------------------------------------------\n");
852   bigint_sprintf (tmp, "%s", sum_val);
853   fprintf (fbeleg, "\n    Anzahl .....................: %d\n", count);
854   recindex=strlen(tmp);
855   tmp[recindex+1] = '\0';
856   tmp[recindex] = tmp[recindex-1];
857   tmp[recindex-1] = tmp[recindex-2];
858   tmp[recindex-2] = '.';
859   fprintf (fcheck, "     %-52s %14s\n", "Summe", tmp);
860   fprintf (fbeleg, "\n    Summe ......................: %s\n", tmp);
861   bigint_sprintf (tmp, "%s", sum_kto);
862   fprintf (fbeleg, "\n    Kontrollsumme Kontonummern .: %s\n", tmp);
863   bigint_sprintf (tmp, "%s", sum_blz);
864   fprintf (fbeleg, "\n    Kontrollsumme Bankleitzahlen: %s\n", tmp);
865   fprintf (fbeleg, "\n    Unsere Kontonummer .........: %s\n", valA[A_KTO]);
866   fprintf (fbeleg, "\n    Unsere Bankleitzahl ........: %s\n", valA[A_BLZ]);
867   fprintf (fbeleg, "\n\n\n\n\n    __________________________________________________\n");
868   fprintf (fbeleg, "    Ort, Datum                     Unterschrift\n");
869   for (recindex=0; recindex<A_LEN; recindex++)
870     if (valA[recindex])
871       free(valA[recindex]);
872   fclose(fdtaus);
873   fclose(fcontrol);
874   fclose(fbeleg);
875   if (ccheck)
876     fclose(fcheck);
877   return count;
878 }