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