Remove superflous code
[infodrom/dtaus] / dtaus.c
1 /*
2     dtaus.c - Belegloser Datenträgeraustausch mit einer Bank
3     Copyright (c) 1996,8,2001-4  Martin Schulze <joey@infodrom.org>
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
18
19     $Id$
20  */
21
22 #include "dtaus.h"
23 #include "bigint.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <ctype.h>
28 #include <time.h>
29 #include <malloc.h>
30 #include "latex.h"
31
32 #define DEFAULT_EURO
33 #ifndef DEFAULT_EURO
34 int use_euro = 0;
35 #else
36 int use_euro = 1;
37 #endif
38
39 /*
40  *  First: Some control structures
41  */
42
43 typedef struct
44 {
45   char *name;
46   unsigned short int pos;
47   unsigned short int len;
48   int type;
49 } dtaus_record;
50
51 #define REC_A   1
52 #define REC_C   2
53 #define REC_E   3
54
55 #define REQ     1
56 #define OPT     2
57 #define IGN     3
58
59 dtaus_record recA[] = {
60   {"Art", 5, 2, REQ},
61   {"Name", 23, 27, REQ},
62   {"Konto", 60, 10, REQ},
63   {"BLZ", 7, 8, REQ},
64   {"Referenz", 70, 10, OPT},
65   {"Datum", 50, 6, IGN},
66   {"Ausfuehrung", 95, 8, OPT},
67   {"Currency", 127, 1, OPT},
68   {"Euro", 127, 1, OPT},
69   {"DM", 127, 1, OPT},
70   {NULL, 0, 0, 0}
71 };
72
73 #define A_TRANS 0
74 #define A_NAME  1
75 #define A_KTO   2
76 #define A_BLZ   3
77 #define A_REF   4
78 #define A_DATE  5
79 #define A_TODO  6
80 #define A_CURR  7
81 #define A_EURO  8
82 #define A_DM    9
83 #define A_LEN   10
84 #define A_LOOP  7
85
86 dtaus_record recC[] = {
87   {"Name", 93, 27, REQ},
88   {"Konto", 21, 10, REQ},
89   {"BLZ", 13, 8, REQ},
90   {"Transaktion", 44, 5, REQ},
91 #ifndef DEFAULT_EURO
92   {"Betrag", 50, 11, REQ},
93 #else
94   {"Betrag", 79, 11, REQ},
95 #endif
96   {"Zweck", 155, 27, REQ},
97   {"myName", 128, 27, OPT},
98   {"myKonto", 69, 10, OPT},
99   {"myBLZ", 61, 8, OPT},
100   {"Text", 187, 29, OPT},
101   {"Extension", 216, 29, OPT},
102   {"Currency", 182, 1, IGN},
103 #ifndef DEFAULT_EURO
104   {"Betrag-Euro", 79, 11, IGN},
105 #else
106   {"Betrag-DM", 50, 11, IGN},
107 #endif
108   {NULL, 0, 0, 0}
109 };
110
111 #define C_NAME  0
112 #define C_KTO   1
113 #define C_BLZ   2
114 #define C_TRANS 3
115 #define C_VAL   4
116 #define C_ZWECK 5
117 #define C_MYNAM 6
118 #define C_MYKTO 7
119 #define C_MYBLZ 8
120 #define C_TEXT  9
121 #define C_EXT  10
122 #define C_EURO 11
123 #ifndef DEFAULT_EURO
124 #define C_EUR  12
125 #else
126 #define C_DM   12
127 #endif
128 #define C_LEN  13
129 #define C_LOOP 11
130
131 #define MAX_TEXT 14
132
133 dtaus_record recE[] = {
134   {"Anzahl", 10, 7, IGN},
135 #ifndef DEFAULT_EURO
136   {"Summe", 17, 13, IGN},
137 #else
138   {"Summe", 64, 13, IGN},
139 #endif
140   {"Kontos", 30, 17, IGN},
141   {"BLZs", 47, 17, IGN},
142 #ifndef DEFAULT_EURO
143   {"Summe-Euro", 64, 13, IGN},
144 #else
145   {"Summe-DM", 17, 13, IGN},
146 #endif
147   {NULL, 0, 0, 0}
148 };
149
150 #define E_COUNT 0
151 #define E_VAL   1
152 #define E_KTO   2
153 #define E_BLZ   3
154 #ifndef DEFAULT_EURO
155 #define E_EUR   4
156 #else
157 #define E_DM    4
158 #endif
159 #define E_LEN   5
160
161 /*
162  *  Second: Some low level routines
163  */
164
165 size_t dtaus_nextrec (void **buf, FILE *f)
166 {
167   memset (buf, 0, 128);
168   return fread(buf, 128, 1, f);
169 }
170
171 char *upcase(char *s)
172 {
173   static char x[100];
174   static char *xp;
175   char *cp;
176
177   for (cp=s,xp=x; *cp; cp++,xp++) {
178     if (strchr(" 0123456789.,&-/+*$%ABCDEFGHIJKLMNOPQRSTUVWXYZ",*cp)) {
179         *xp = *cp;
180     } else if (strchr("abcdefghijklmnopqrstuvwxyz",*cp)) {
181       *xp = toupper(*cp);
182     } else if (strchr ("üÜäÄöÖß", *cp)) {
183       switch (*cp) {
184       case 'ü': *(xp++) = 'U'; *xp='E'; break;
185       case 'Ü': *(xp++) = 'U'; *xp='E'; break;
186       case 'ä': *(xp++) = 'A'; *xp='E'; break;
187       case 'Ä': *(xp++) = 'A'; *xp='E'; break;
188       case 'ö': *(xp++) = 'O'; *xp='E'; break;
189       case 'Ö': *(xp++) = 'O'; *xp='E'; break;
190       case 'ß': *(xp++) = 'S'; *xp='S'; break;
191       }
192     } else
193       /*
194        * Filter out all other characters since credit institutes won't
195        * accept the file otherwise.
196        */
197       *xp = ' ';
198   }
199   *xp = '\0';
200
201   return x;
202 }
203
204 char *downcase(char *s)
205 {
206   static char x[100];
207   char *cp;
208
209   memset (x, 0, sizeof (x));
210   strncpy (x, s, 99);
211   
212   for (cp=x;*cp;cp++)
213     if (isupper(*cp))
214       *cp = tolower(*cp);
215   return x;
216 }
217
218 char *strip_spaces (char *s)
219 {
220   int i;
221   char *p;
222
223   for (i=strlen(s);(s[i-1] == ' '||s[i-1] == '\t')&&i>0; i--)
224     s[i-1] = '\0';
225   for (p=s; *p==' '; p++);
226   return p;
227 }
228
229 char *strip_zeros (char *s)
230 {
231   char *p;
232
233   for (p=s; *p=='0'; p++);
234   return p;
235 }
236
237 char *strip_nondigits (char *s)
238 {
239   char *p;
240   char *x;
241
242   for (x=s,p=s;*x;x++)
243     if (isdigit (*x))
244       *(p++) = *x;
245   *(p++) = '\0';
246
247   return s;
248 }
249
250 char dtaus_char (void *buf, unsigned int pos)
251 {
252   static char res;
253   char *bufp = (char *)buf;
254
255   bufp+=pos;
256   memcpy(&res, bufp, 1);
257   return res;
258 }
259
260 char *string2real(char *s)
261 {
262   static char res[20];
263   char *cp = s;
264
265   cp+=strlen(s)-2;
266   strncpy(res, s, strlen(s)-2);
267   res[strlen(s)-2] = '.';
268   res[strlen(s)-1] = s[strlen(s)-2];
269   res[strlen(s)] = s[strlen(s)-1];
270   return res;
271 }
272
273 char *real2string(char *s)
274 {
275   static char res[20];
276   char *cp;
277   char *xp;     /* only to avoid a GCC warning */
278
279   strncpy(res, s, sizeof(res)-1);
280   res[sizeof(res)-1] = 0;
281   for (cp=res; *cp&&!(*cp == ',')&&!(*cp == '.');cp++);
282
283   if ((cp-res) >= (sizeof(res)-3)) { 
284     /* Bail out, since the number is too large, shouldn't be possible though. */
285     fprintf (stderr, "Value %s too large.\n", res);
286     exit (1);
287   }
288
289   if (*cp == '.' || *cp == ',') {
290     if (*(cp+1)) {
291       /* 1st decimal place */
292       xp = cp+1;
293       if (*xp && isdigit(*xp))
294         *(cp++) = *xp;
295       else
296         *(cp++) = '0';
297       /* 2nd decimal place */
298       xp = cp+1;
299       if (*xp && isdigit(*xp))
300         *(cp++) = *xp;
301       else
302         *(cp++) = '0';
303     } else {
304       *(cp++) = '0';
305       *(cp++) = '0';
306     }
307   } else {
308     *(cp++) = '0';
309     *(cp++) = '0';
310   }
311   *cp = '\0';
312   return res;
313 }
314
315 /*
316  * Return the last digit of the year as character
317  */
318 char get_date_lastdigit()
319 {
320   time_t timer;
321   struct tm *loctime;
322
323   timer = time ( NULL );
324   loctime = localtime(&timer);
325   return loctime->tm_year % 10 + '0';
326 }
327
328 char *string2trans (char *s)
329 {
330   static char res[30];
331
332   res[0] = '\0';
333   if (!strcmp(s, "04000"))
334     sprintf (res, "Abbuchung");
335   else if (!strcmp(s, "05000"))
336     sprintf (res, "Einzug");
337   else if (!strcmp(s, "05005"))
338     sprintf (res, "E-Cash");
339   else if (!strcmp(s, "05006"))
340     sprintf (res, "E-Cash-A");
341   else if (!strcmp(s, "51000"))
342     sprintf (res, "Gutschrift");
343   else if (!strcmp(s, "53000"))
344     sprintf (res, "Lohn");
345   else if (!strncmp(s, "5400", 4))
346     sprintf (res, "Vermögen %c",s[4]);
347   /*  else if (!strcmp(s, "56000"))
348     sprintf (res, ""); / * Überweisung öffentlicher Kassen */
349   return res;
350 }
351
352 char *trans2string (char *s)
353 {
354   static char res[30];
355   char *cp;
356
357   res[0] = '\0';
358   if (!strcmp(s, "Abbuchung"))
359     sprintf (res, "04000");
360   else if (!strcmp(s, "Einzug"))
361     sprintf (res, "05000");
362   else if (!strcmp(s, "E-Cash"))
363     sprintf (res, "05005");
364   else if (!strcmp(s, "E-Cash-A"))
365     sprintf (res, "05006");
366   else if (!strcmp(s, "Gutschrift"))
367     sprintf (res, "51000");
368   else if (!strcmp(s, "Lohn"))
369     sprintf (res, "53000");
370   else {
371     cp = NULL;
372     if (!strncmp(s, "Vermögen", 8))
373       cp = s+8;
374     if (!strncmp(s, "Vermoegen", 9))
375       cp = s+9;
376
377     if (!cp) {
378       fprintf (stderr, "Unbekannte Transaktion `%s'\n", res);
379       exit (1);
380     }
381
382     /*
383       Vermögen --> 5400<heutiges Jahr>
384       Vermögen 8 -> 5400<8>
385       Vermögen 2003 -> 5400<3>
386      */
387
388     if (*cp) while (!isspace(*cp)) cp++;
389     while (isspace(*cp)) cp++;
390
391     if (!*cp || !isdigit(*cp))
392       sprintf (res, "5400%c",get_date_lastdigit());
393     else {
394       while (isdigit(*cp)) cp++;
395       sprintf (res, "5400%c",*(cp-1));
396     }
397   }
398   /*  else if (!strcmp(s, ""))
399     sprintf (res, "56000"); / * Überweisung öffentlicher Kassen */
400   return res;
401 }
402
403 char *string2ext (char *s)
404 {
405   static char res[30];
406
407   res[0] = '\0';
408   if (!strcmp(s, "01"))
409     sprintf (res, "Kunde");
410   else if (!strcmp(s, "02"))
411     sprintf (res, "Text");
412   else if (!strcmp(s, "03"))
413     sprintf (res, "Auftraggeber");
414   return res;
415 }
416
417 char *ext2string (char *s)
418 {
419   static char res[3];
420
421   res[0] = '\0';
422   if (!strcmp(s, "Kunde"))
423     sprintf (res, "01");
424   else if (!strcmp(s, "Text"))
425     sprintf (res, "02");
426   else if (!strcmp(s, "Auftraggeber"))
427     sprintf (res, "03");
428   return res;
429 }
430     
431 unsigned long int dtaus_int(void *buf, unsigned int pos, unsigned int len)
432 {
433   char tmp[30];
434   char *bufp = (char *)buf;
435   static unsigned long int res;
436
437   bufp+=pos;
438   memcpy(tmp, bufp, len);
439   tmp[len] = '\0';
440   sscanf(tmp, "%lu", &res);
441   return res;
442 }
443
444 /*
445  * returns the first word in this line, returns it and shortens the
446  * line.
447  */
448 char *extract_ident (char *line)
449 {
450   char *c, *x, *y;
451   static char word[30];
452
453   if (strlen(line) > 0) {
454     x = strchr (line, ' ');
455     y = strchr (line, '\t');
456
457     if (!x && !y) {
458         strncpy(word, line, 29);
459         line[0] = '\0';
460         return word;
461     }
462
463     /* Check which index returns the lower value, and check if the
464        value is non-NULL */
465     if ((c = (x && x<y)?x:(y?y:x))) { 
466       strncpy(word, line, c - line);
467       word[c-line] = '\0';
468       for (;*c == '\t' || *c == ' '; c++);
469       for (x=line; *c; c++,x++)
470         *x = *c;
471       *x = '\0';
472       strcpy(word, downcase(word));
473       return word;
474     }
475     return NULL;
476   }
477   return line;
478 }
479
480 /*
481  * Pads a string with zero's on the left side.
482  */
483 char *padzeroclip (char *s, int len)
484 {
485   char *p, *q;
486
487   if (strlen(s) == len) return s;
488   if (strlen(s) > len) {
489       q=s+len;
490       *(q--) = '\0';
491       return s;
492   }
493
494   q=s+len;
495   *(q--) = '\0';
496   for (p=s+strlen(s)-1;p>=s;p--)
497     *(q--)=*p;
498   for (;q>=s;) *(q--)='0';
499   return s;
500 }
501
502 int rec_index(char *ident, int type)
503 {
504   int i;
505   dtaus_record *rec = NULL;
506
507   switch (type) {
508   case REC_A:   rec = recA; break;
509   case REC_C:   rec = recC; break;
510   case REC_E:   rec = recE; break;
511   }
512
513   for (i=0; (rec[i].name); i++) {
514     if (!strcmp(ident, downcase(rec[i].name)))
515       return i;
516   }
517
518   return -1;
519 }
520
521 size_t control_nextline (void **buf, int len, FILE *f)
522 {
523   char line[100];
524   char tmp[100];
525   char *cp;
526   int i;
527
528   memset (line, 0, sizeof(line));
529   memset (buf, 0, len);
530   cp = line;
531
532   while (!strlen(line) && (cp = fgets(line, 100, f))) {
533     if (strlen(line)) {
534       if (line[0] != '#') {
535         if (line[strlen(line)-1] != '\n') { 
536           strcpy(tmp, line);
537           while (tmp[strlen(tmp)-1] != '\n' && (cp = fgets(tmp, 100, f)));
538         } else
539           line[strlen(line)-1] = '\0';
540         if (line[strlen(line)-1] == '\r')
541           line[strlen(line)-1] = '\0';
542         for (i=strlen(line);(line[i-1] == ' '||line[i-1] == '\t')&&i>0; i--)
543           line[i-1] = '\0';
544       } else
545         line[0] = '\0';
546     }
547   }
548   for (cp=line; *cp==' '; cp++);
549
550   if (strlen(cp)) {
551     memcpy(buf, cp, strlen(cp));
552     return 1;
553   } else
554     return 0;
555 }
556
557 /*
558  * Return the current date nicely formatted
559  */
560 char *get_date()
561 {
562   static char res[10];
563   time_t timer;
564   struct tm *loctime;
565
566   timer = time ( NULL );
567   loctime = localtime(&timer);
568   sprintf(res, "%02d.%02d.%02d", loctime->tm_mday, loctime->tm_mon+1, loctime->tm_year % 100);
569   return res;
570 }
571
572 /*
573  *  Prepare a record A according to the specs.
574  *  See dtaus.txt for explanation
575  */
576 void dtaus_prepareA (char *buf)
577 {
578   int i;
579   time_t timer;
580   struct tm *loctime;
581   char tmp[10];
582
583   memset (buf, 0, 129);
584   timer = time ( NULL );
585   loctime = localtime(&timer);
586
587   buf[0] = '0';
588   buf[1] = '1';
589   buf[2] = '2';
590   buf[3] = '8';
591   buf[4] = 'A';
592   for (i=15;i<15+8; i++) buf[i] = '0';          /* A5 */
593   sprintf(tmp, "%02d%02d%02d", loctime->tm_mday, loctime->tm_mon+1, loctime->tm_year % 100);
594   for (i=0; i<6; i++) buf[50+i] = tmp[i];       /* A7 (Date) */
595   for (i=56;i<56+4; i++) buf[i] = ' ';          /* A8 */
596   for (i=70;i<70+10; i++) buf[i] = '0';         /* A10 */
597   for (i=80;i<80+48; i++) buf[i] = ' ';         /* A11 */
598   if (use_euro)
599     buf[recA[A_CURR].pos] = '1';                                /* A12 (Currency) */
600   else
601     buf[recA[A_CURR].pos] = ' ';                                /* A12 (Currency) */
602 }
603
604 /*
605  *  Prepare a record C according to the specs.
606  *  See dtaus.txt for explanation
607  */
608 void dtaus_prepareC (char *buf, int normaltext, int maxtext)
609 {
610   int i;
611   int appendix = 0;
612
613   memset (buf, 0, 257);
614
615   if (normaltext)
616     appendix = 1;
617   appendix += maxtext;
618   i = 187 + (appendix * 29);
619
620   /* Bail out if the number is too large, shouldn't be possible though */
621   if (i >= 1000)
622     exit (1);
623
624   buf[0] = (i/1000)+48;i-=(i/1000)*100;
625   buf[1] = (i/100)+48;i-=(i/100)*100;
626   buf[2] = (i/10)+48;i-=(i/10)*10;
627   buf[3] = i+48;
628   buf[4] = 'C';
629
630   for (i=31;i<31+13; i++) buf[i] = '0';         /* C6 */
631   buf[49] = ' ';                                /* C8 */
632   for (i=50;i<50+11; i++) buf[i] = '0';         /* C9 (Betrag) */
633   for (i=79;i<79+11; i++) buf[i] = '0';         /* C12 (Betrag Euro) */
634   for (i=90;i<90+3; i++) buf[i] = ' ';          /* C13 */
635   for (i=93;i<90+27; i++) buf[i] = ' ';         /* C14a (Kunde) */
636   for (i=120;i<120+8; i++) buf[i] = ' ';        /* C14b */
637   if (use_euro)
638     buf[recC[C_EURO].pos] = '1';                                /* C17a (Currency) */
639   else
640     buf[recC[C_EURO].pos] = ' ';                                /* C17a (Currency) */
641   for (i=183;i<183+2; i++) buf[i] = ' ';        /* C17b */
642   for (i=187;i<187+(29*2); i++) buf[i] = ' ';   /* C19-C22 (misc text) */
643   for (i=245;i<245+11; i++) buf[i] = ' ';       /* C23 */
644
645   buf[185+0] = (appendix/10)+48;appendix-=(appendix/10)*10;
646   buf[185+1] = appendix+48;
647 }
648
649 /*
650  *  Prepare a record E according to the specs.
651  *  See dtaus.txt for explanation
652  */
653 void dtaus_prepareE (char *buf)
654 {
655   int i;
656
657   memset (buf, 0, 129);
658   buf[0] = '0';
659   buf[1] = '1';
660   buf[2] = '2';
661   buf[3] = '8';
662   buf[4] = 'E';
663   for (i=5;i<5+5; i++) buf[i] = ' ';    /* E3 */
664   for (i=17;i<17+13; i++) buf[i] = '0'; /* E8 (Check Betrag) */
665   for (i=64;i<64+13; i++) buf[i] = '0'; /* E8 (Check Euro) */
666   for (i=77;i<77+51; i++) buf[i] = ' '; /* E9 */
667 }
668
669 int dtaus_writeA(FILE *f, char **values)
670 {
671   char buf[129];
672   char tmp[30];
673   int i;
674   
675   for (i=0; (recA[i].name); i++)
676     if ((recA[i].type == REQ) && !values[i]) {
677       fprintf (stderr, "Anfangsdatensatz ist nicht vollständig, kein %s.\n", recA[i].name);
678       return 0;
679     }
680   if (!(((values[A_TRANS][0] == 'L')||(values[A_TRANS][0] == 'G'))
681         &&((values[A_TRANS][1] == 'B')||(values[A_TRANS][1] == 'K')))) {
682     fprintf (stderr, "Ungültiger Typ, nur LK, GK, LB oder GB erlaubt.\n");
683     return 0;
684   }
685
686   i=A_NAME;if (values[i] && strlen(values[i]) > recA[i].len)
687     values[i][recA[i].len] = '\0';
688
689   dtaus_prepareA(buf);
690   buf[5] = values[A_TRANS][0];
691   buf[6] = values[A_TRANS][1];
692   sprintf (tmp, "%s", padzeroclip (strip_nondigits (values[A_BLZ]),recA[A_BLZ].len));
693   for (i=0; i<recA[A_BLZ].len; i++) buf[recA[A_BLZ].pos+i] = tmp[i];
694   sprintf (tmp, "%-27.27s", upcase(values[A_NAME]));
695   for (i=0; i<27; i++) buf[recA[A_NAME].pos+i] = tmp[i];
696   if (values[A_TODO]) {
697     sprintf (tmp, "%s", padzeroclip (strip_nondigits (values[A_TODO]),recA[A_TODO].len));
698     for (i=0; i<recA[A_TODO].len; i++) buf[recA[A_TODO].pos+i] = tmp[i];
699   }
700   sprintf (tmp, "%s", padzeroclip (strip_nondigits (values[A_KTO]),recA[A_KTO].len));
701   for (i=0; i<recA[A_KTO].len; i++) buf[recA[A_KTO].pos+i] = tmp[i];
702
703   fputs(buf, f);
704   return 1;
705 }
706
707 int dtaus_writeC(FILE *f, char **valuesA, char **values, char **text)
708 {
709   char buf[257];
710   char appendix[129];
711   char tmp[30];
712   int i, k;
713   int maxtext = 0;
714   int fieldnr;
715
716   /* Just count */
717   if (text) for (maxtext=0;text[maxtext];maxtext++);
718
719 #if DEBUG
720   for (i=0; (recC[i].name); i++)
721     if (values[i])
722       printf ("%s: %s\n", recC[i].name, values[i]);
723 #endif
724
725   for (i=0; (recC[i].name); i++)
726     if ((recC[i].type == REQ) && !values[i]) {
727       fprintf (stderr, "Datensatz ist nicht vollständig, kein %s.\n", recC[i].name);
728       return 0;
729     }
730   sprintf (tmp, "%s", trans2string(values[C_TRANS]));
731   if (!strlen(tmp)) {
732     fprintf (stderr, "Ungültige Transaktion, nur Abbuchung, Einzug, E-Cash, E-Cash-A, Gutschrift und Lohn erlaubt.\n");
733     return 0;
734   }
735
736   i=C_TEXT;if (values[i] && strlen(values[i]) > recC[i].len)
737     values[i][recC[i].len-2] = '\0';
738   i=C_ZWECK;if (values[i] && strlen(values[i]) > recC[i].len)
739     values[i][recC[i].len] = '\0';
740   i=C_MYNAM;if (values[i] && strlen(values[i]) > recC[i].len)
741     values[i][recC[i].len] = '\0';
742   i=C_TEXT;if (values[i] && strlen(values[i]) > recC[i].len)
743     values[i][recC[i].len] = '\0';
744
745   dtaus_prepareC (buf, values[C_TEXT] != NULL, maxtext);
746   for (i=0; i<5; i++) buf[recC[C_TRANS].pos+i] = tmp[i];
747   if (values[C_MYBLZ])
748     sprintf (tmp, "%s", padzeroclip (strip_nondigits (values[C_MYBLZ]),8));
749   else
750     sprintf (tmp, "%s", padzeroclip (strip_nondigits (valuesA[A_BLZ]),8));
751   for (i=0; i<recC[C_MYBLZ].len; i++) buf[5+i] = tmp[i];
752   for (i=0; i<recC[C_MYBLZ].len; i++) buf[recC[C_MYBLZ].pos+i] = tmp[i];
753   sprintf (tmp, "%s", padzeroclip (strip_nondigits (values[C_BLZ]),8));
754   for (i=0; i<recC[C_BLZ].len; i++) buf[recC[C_BLZ].pos+i] = tmp[i];
755   sprintf (tmp, "%s", padzeroclip (strip_nondigits (values[C_KTO]),10));
756   for (i=0; i<recC[C_KTO].len; i++) buf[recC[C_KTO].pos+i] = tmp[i];
757   sprintf (tmp, "%s", padzeroclip (real2string(values[C_VAL]),11));
758 #ifndef DEFAULT_EURO
759   if (!use_euro)
760     for (i=0; i<recC[C_VAL].len; i++) buf[recC[C_VAL].pos+i] = tmp[i];
761   else
762     for (i=0; i<recC[C_EUR].len; i++) buf[recC[C_EUR].pos+i] = tmp[i];
763 #else
764   if (use_euro)
765     for (i=0; i<recC[C_VAL].len; i++) buf[recC[C_VAL].pos+i] = tmp[i];
766   else
767     for (i=0; i<recC[C_DM].len; i++) buf[recC[C_DM].pos+i] = tmp[i];
768 #endif
769   if (values[C_MYKTO])
770     sprintf (tmp, "%s", padzeroclip (strip_nondigits (values[C_MYKTO]),10));
771   else
772     sprintf (tmp, "%s", padzeroclip (strip_nondigits (valuesA[A_KTO]),10));
773   for (i=0; i<recC[C_MYKTO].len; i++) buf[recC[C_MYKTO].pos+i] = tmp[i];
774   sprintf (tmp, "%-27.27s", upcase(values[C_NAME]));
775   for (i=0; i<recC[C_NAME].len; i++) buf[recC[C_NAME].pos+i] = tmp[i];
776   if (values[C_MYNAM])
777     sprintf (tmp, "%-27.27s", upcase(values[C_MYNAM]));
778   else
779     sprintf (tmp, "%-27.27s", upcase(valuesA[A_NAME]));
780   for (i=0; i<recC[C_MYNAM].len; i++) buf[recC[C_MYNAM].pos+i] = tmp[i];
781   sprintf (tmp, "%-27.27s", upcase(values[C_ZWECK]));
782   for (i=0; i<recC[C_ZWECK].len; i++) buf[recC[C_ZWECK].pos+i] = tmp[i];
783
784   if (values[C_TEXT]) {
785     buf[recC[C_TEXT].pos+0] = '0';
786     buf[recC[C_TEXT].pos+1] = '2';
787     sprintf (tmp, "%-27.27s", upcase(values[C_TEXT]));
788     for (i=0; i<recC[C_TEXT].len-2; i++) buf[recC[C_TEXT].pos+2+i] = tmp[i];
789   }
790
791   if (text) {
792     buf[recC[C_EXT].pos+0] = '0';
793     buf[recC[C_EXT].pos+1] = '2';
794     sprintf (tmp, "%-27.27s", upcase(text[0]));
795     for (i=0; i<recC[C_EXT].len-2; i++) buf[recC[C_EXT].pos+2+i] = tmp[i];
796   }
797
798   fputs(buf, f);
799
800   if (text && maxtext > 1) {
801     fieldnr=1;
802     while (fieldnr<maxtext) {
803       memset (appendix, ' ', 128);
804       appendix[128] = '\0';
805       for (k=0; k<4 && (fieldnr+k)<maxtext; k++) {
806         sprintf (tmp, "%-27.27s", upcase(text[fieldnr+k]));
807         appendix[k*29] = '0';
808         appendix[(k*29)+1] = '2';
809         for (i=0; i<recC[C_TEXT].len-2; i++) appendix[(k*29)+2+i] = tmp[i];
810       }
811       fputs(appendix, f);
812       fieldnr += k;
813     }
814   }
815
816   return 1;
817 }
818
819 int dtaus_writeE(FILE *f, int count, bigint sum, bigint blz, bigint kto)
820 {
821   char buf[129];
822   char tmp[30];
823   int i;
824   
825   dtaus_prepareE(buf);
826
827   sprintf (tmp, "%07d", count);
828   for (i=0; i<recE[E_COUNT].len; i++) buf[recE[E_COUNT].pos+i] = tmp[i];
829   bigint_sprintf (tmp, "%s", sum);
830   padzeroclip (tmp,13);
831 #ifndef DEFAULT_EURO
832   if (!use_euro)
833     for (i=0; i<recE[E_VAL].len; i++) buf[recE[E_VAL].pos+i] = tmp[i];
834   else
835     for (i=0; i<recE[E_EUR].len; i++) buf[recE[E_EUR].pos+i] = tmp[i];
836 #else
837   if (use_euro)
838     for (i=0; i<recE[E_VAL].len; i++) buf[recE[E_VAL].pos+i] = tmp[i];
839   else
840     for (i=0; i<recE[E_DM].len; i++) buf[recE[E_DM].pos+i] = tmp[i];
841 #endif
842   bigint_sprintf (tmp, "%s", kto);
843   padzeroclip (tmp,17);
844   for (i=0; i<recE[E_KTO].len; i++) buf[recE[E_KTO].pos+i] = tmp[i];
845   bigint_sprintf (tmp, "%s", blz);
846   padzeroclip (tmp,17);
847   for (i=0; i<recE[E_BLZ].len; i++) buf[recE[E_BLZ].pos+i] = tmp[i];
848
849   fputs(buf, f);
850   return 1;
851 }
852
853 void printctln(FILE *f, char *field, char *value)
854 {
855   if (strlen(field) && strlen(value))
856     fprintf(f, "  %s\t%s\n", field, value);
857 }
858
859 /*
860  * one date line, format it properly
861  */
862 void printctlndate(FILE *f, char *field, char *value)
863 {
864   char mydate[11];
865   int i;
866
867   if (!strlen(field) || !strlen(value))
868     return;
869
870   for (i=0;isspace (value[i]) && i<= strlen (value); i++);
871   if (i == strlen (value))
872     return;
873
874   memset (mydate, 0, sizeof (mydate));
875   if (strlen (value) == 6) {
876     mydate[0] = value[0];
877     mydate[1] = value[1];
878     mydate[2] = '.';
879     mydate[3] = value[2];
880     mydate[4] = value[3];
881     mydate[5] = '.';
882     mydate[6] = value[4];
883     mydate[7] = value[5];
884     fprintf(f, "  %s\t%s\n", field, mydate);
885   } else if (strlen (value) == 8) {
886     mydate[0] = value[0];
887     mydate[1] = value[1];
888     mydate[2] = '.';
889     mydate[3] = value[2];
890     mydate[4] = value[3];
891     mydate[5] = '.';
892     mydate[6] = value[4];
893     mydate[7] = value[5];
894     mydate[8] = value[6];
895     mydate[9] = value[7];
896     fprintf(f, "  %s\t%s\n", field, mydate);
897   } else {
898     fprintf (stderr, "Broken date field: %s\n", value);
899     fprintf(f, "  %s\t%s\n", field, value);
900   }
901 }
902
903
904 /*
905  *  Third: Some high level routines
906  */
907
908 void dtaus2control (char *cdtaus, char *ccontrol)
909 {
910   FILE *fdtaus, *fcontrol;
911   char *buf;
912   char *bufp;
913   char tmp[30];
914   char x[30];
915   int index;
916   int extC;
917   div_t res;
918
919   if (!cdtaus) {
920     if (!(fdtaus = fopen("DTAUS0.TXT", "r")))
921       if (!(fdtaus = fopen("dtaus0.txt", "r")))
922         return;
923   } else {
924     if (!strcmp (cdtaus, "-"))
925       fdtaus = stdin;
926     else
927       if (!(fdtaus = fopen(cdtaus, "r")))
928         return;
929   }
930   if (!ccontrol) 
931     fcontrol = stdout;
932   else
933     if (!strcmp (ccontrol, "-"))
934       fcontrol = stdout;
935     else
936       if (!(fcontrol = fopen(ccontrol, "w")))
937         return;
938   if ((buf = (char *)malloc (512)) == NULL)
939     return;
940
941   /* 
942    * Record A lesen
943    */
944   if (dtaus_nextrec((void *)buf, fdtaus) == 1) {
945     if (dtaus_char(buf,4) == 'A') {
946       fprintf(fcontrol, "BEGIN {\n");
947       bufp = buf;
948
949       for (index=A_TRANS; index < A_LOOP; index++) {
950         bufp = buf + recA[index].pos;
951         memcpy(tmp, bufp, recA[index].len); tmp[recA[index].len] = '\0';
952         if (index == A_DATE || index == A_TODO)
953           printctlndate(fcontrol, recA[index].name, tmp);
954         else
955           printctln(fcontrol, recA[index].name, strip_zeros(strip_spaces(tmp)));
956       }
957
958       bufp = buf + recA[A_CURR].pos;
959       if (*bufp == '1') {
960         use_euro = 1;
961         fprintf(fcontrol, "  Euro\n");
962       } else {
963         use_euro = 0;
964         fprintf(fcontrol, "  DM\n");
965       }
966
967       fprintf(fcontrol, "}\n\n");
968     } else {
969       fprintf (stderr, "Datei fängt nicht mit dem Anfangsdatensatz an.\n");
970       return;
971     }
972   } else {
973     fprintf (stderr, "Der Anfangsdatensatz ist kaputt.\n");
974     return;
975   }
976
977   /*
978    * Record C lesen
979    */
980   if (dtaus_nextrec((void *)buf, fdtaus) == 1) {
981     while (dtaus_char(buf,4) == 'C') {
982       bufp = buf + 128;
983       if (dtaus_nextrec((void *)bufp, fdtaus) != 1) {
984         fprintf (stderr, "Der zweite Teil der Transaktion ist kaputt.\n");
985         return;
986       } else {
987         fprintf(fcontrol, "{\n");
988
989         for (index=C_NAME; index < C_LOOP; index++) {
990           if (index == C_TEXT || index == C_EXT)
991             continue;
992 #ifndef DEFAULT_EURO
993           if (use_euro && index == C_VAL)
994             index = C_EUR;
995 #else
996           if (!use_euro && index == C_VAL)
997             index = C_DM;
998 #endif
999           bufp = buf + recC[index].pos;
1000           memcpy(tmp, bufp, recC[index].len); tmp[recC[index].len] = '\0';
1001
1002           /*
1003            * C_EUR and C_DM are outside of the loop, can only be
1004            * selected for the non-default currency, but the value
1005            * should be stored in the normal record field.
1006            */
1007 #ifndef DEFAULT_EURO
1008           if (index == C_EUR)
1009 #else
1010           if (index == C_DM)
1011 #endif
1012             printctln(fcontrol, recC[C_VAL].name, strip_zeros(string2real(tmp)));
1013           else if (index == C_VAL)
1014             printctln(fcontrol, recC[index].name, strip_zeros(string2real(tmp)));
1015           else if (index == C_TRANS)
1016             printctln(fcontrol, recC[index].name, strip_zeros(string2trans(tmp)));
1017           else
1018             printctln(fcontrol, recC[index].name, strip_zeros(strip_spaces(tmp)));
1019 #ifndef DEFAULT_EURO
1020           if (use_euro && index == C_EUR)
1021             index = C_VAL;
1022 #else
1023           if (!use_euro && index == C_DM)
1024             index = C_VAL;
1025 #endif
1026         }
1027
1028         for (index=C_TEXT; index <= C_EXT; index++) {
1029           if (!(dtaus_char(buf,recC[index].pos) == ' ')) {
1030             bufp = buf + recC[index].pos;
1031             memcpy(x, bufp, 2); tmp[2] = '\0'; bufp+=2;
1032             memcpy(tmp, bufp, recC[index].len-2); tmp[recC[index].len-2] = '\0';
1033             printctln(fcontrol, string2ext(x), strip_spaces(tmp));
1034           }
1035         }
1036
1037         /* Number of extension records for this C record */
1038         extC = dtaus_int(buf, 185, 2);
1039         extC -= 2;
1040         if (extC > 0) {
1041           res = div (extC, 4);
1042           extC = res.quot;
1043           if (res.rem) extC++;
1044         }
1045       }
1046       if (dtaus_nextrec((void *)buf, fdtaus) != 1)
1047         memset (buf, 0, sizeof(buf));
1048
1049       /*
1050        * Are there extension records that we have to check?
1051        *
1052        * 2nd half of the AND is wrong, but since dtaus < 0.5 wrote 01
1053        *     instead of 00 we should let it in so it can read its own
1054        *     old files...  *sigh*
1055        */
1056       while (extC > 0 && dtaus_char(buf,4) != 'C' && dtaus_char(buf,4) != 'E') {
1057         for (index=0; index < 4; index++) {
1058           if ((dtaus_char(buf,index*29) != ' ')) {
1059             bufp = buf + index*29;
1060             memcpy(x, bufp, 2); tmp[2] = '\0'; bufp+=2;
1061             memcpy(tmp, bufp, recC[C_TEXT].len-2); tmp[recC[C_TEXT].len-2] = '\0';
1062             printctln(fcontrol, string2ext(x), strip_spaces(tmp));
1063           }
1064         }
1065         if (dtaus_nextrec((void *)buf, fdtaus) != 1)
1066           memset (buf, 0, sizeof(buf));
1067         extC--;
1068       }
1069       fprintf(fcontrol, "}\n");
1070     }
1071   }
1072
1073   /*
1074    * Record E lesen
1075    *   (gelesen ist er eigentlich schon...)
1076    */
1077   if (dtaus_char(buf,4) == 'E') {
1078     if (dtaus_char(buf,4) == 'E') {
1079       fprintf(fcontrol, "\nEND {\n");
1080
1081       for (index=E_COUNT; index <= E_BLZ; index++) {
1082 #ifndef DEFAULT_EURO
1083         if (use_euro && index == E_VAL)
1084           index = E_EUR;
1085 #else
1086         if (!use_euro && index == E_VAL)
1087           index = E_DM;
1088 #endif
1089
1090         bufp = buf + recE[index].pos;
1091         memcpy(tmp, bufp, recE[index].len); tmp[recE[index].len] = '\0';
1092
1093 #ifndef DEFAULT_EURO
1094         if (index == E_VAL || index == E_EUR)
1095 #else
1096         if (index == E_VAL || index == E_DM)
1097 #endif
1098           printctln(fcontrol, recE[E_VAL].name, strip_zeros(string2real(tmp)));
1099         else
1100           printctln(fcontrol, recE[index].name, strip_zeros(tmp));
1101 #ifndef DEFAULT_EURO
1102           if (use_euro && index == E_EUR)
1103             index = E_VAL;
1104 #else
1105           if (!use_euro && index == E_DM)
1106             index = E_VAL;
1107 #endif
1108       }
1109
1110       fprintf(fcontrol, "}\n");
1111     } else {
1112       fprintf (stderr, "Das ist kein Abschlußdatensatz.\n");
1113       return;
1114     }
1115   } else {
1116     fprintf (stderr, "Der Abschlußdatensatz ist leer oder kaputt.\n");
1117     return;
1118   }
1119   fclose(fcontrol);
1120   fclose(fdtaus);
1121 }
1122
1123 int control2dtaus (char *ccontrol, char *cdtaus, char *cbeleg, char *ccheck, char *latex)
1124 {
1125   FILE *fdtaus, *fcontrol, *fbeleg, *fcheck;
1126   void *buf;
1127   char *ident;
1128   int  recindex;
1129   char line[100];
1130   char *valA[A_LEN], *valC[C_LEN];
1131   int count;
1132   bigint sum_val, sum_blz, sum_kto, bi;
1133   char **text = NULL;
1134   char *cp;
1135   int textindex = 0;
1136   int len, i;
1137   char *type = NULL;
1138   char *currency = NULL;
1139   char date_todo[11];
1140   char ssum_val[30], ssum_kto[30], ssum_blz[30];
1141
1142   if (!cdtaus) {
1143     if (!(fdtaus = fopen("dtaus0.txt", "w")))
1144       return 0;
1145   } else {
1146     if (!strcmp (cdtaus, "-"))
1147       fdtaus = stdout;
1148     else
1149       if (!(fdtaus = fopen(cdtaus, "w")))
1150         return 0;
1151   }
1152   if (!ccontrol) {
1153     if (!(fcontrol = fopen("dtaus0.ctl", "r")))
1154       if (!(fcontrol = fopen("DTAUS0.CTL", "r")))
1155         return 0;
1156   } else {
1157     if (!strcmp (ccontrol, "-"))
1158       fcontrol = stdin;
1159     else
1160       if (!(fcontrol = fopen(ccontrol, "r")))
1161         return 0;
1162   }
1163   if (!cbeleg) {
1164     if (!(fbeleg = fopen("dtaus0.doc", "w")))
1165       return 0;
1166   } else {
1167     if (!(fbeleg = fopen(cbeleg, "w")))
1168       return 0;
1169   }
1170   if (!ccheck)
1171     fcheck = stdout;
1172   else
1173     if (!(fcheck = fopen(ccheck, "w")))
1174       return 0;
1175
1176
1177   if ((buf = (char *)malloc (512)) == NULL)
1178     return 0;
1179
1180   /* 
1181    * Record A lesen
1182    */
1183   memset (valA, 0, sizeof(valA));
1184   control_nextline ((void *)line, 100, fcontrol);
1185   ident = extract_ident(line);
1186   if (!strcmp(ident, "begin") && (line[0] == '{')) {
1187     control_nextline ((void *)line, 100, fcontrol);
1188     while (strlen(line) && line[0] != '}') {
1189       ident = extract_ident(line);
1190       if ((recindex = rec_index(ident, REC_A)) != -1)
1191         {
1192         if (recA[recindex].type != IGN)
1193           if ((valA[recindex] = (char *)malloc (strlen(line)+1)) != NULL)
1194             strcpy(valA[recindex], line);
1195         } else {
1196           if (! strcasecmp (ident, "euro"))
1197             use_euro = 1;
1198           if (! strcasecmp (ident, "dm"))
1199             use_euro = 0;
1200         }
1201       control_nextline ((void *)line, 100, fcontrol);
1202     }
1203     if (((recindex = rec_index("art", REC_A)) != -1) && valA[recindex] && strlen(valA[recindex])) {
1204       if (valA[recindex][0] == 'L')
1205         type = strdup ("Sammeleinziehungsauftrag");
1206       else if (valA[recindex][0] == 'G')
1207         type = strdup ("Sammelueberweisungsauftrag");
1208       else
1209         type = strdup ("Sammelauftrag");
1210
1211       if (use_euro)
1212         currency = strdup ("Euro");
1213       else
1214         currency = strdup ("DM");
1215
1216       if (valA[A_TODO])
1217         sprintf (date_todo, valA[A_TODO]);
1218       else
1219         memset (date_todo, 0, sizeof (date_todo));
1220
1221       fprintf(fbeleg, "\n\n");
1222       fprintf(fbeleg, "\n    Begleitzettel\n\n");
1223       fprintf(fbeleg, "\n    Belegloser Datentraegeraustausch\n\n");
1224       fprintf(fbeleg, "\n    %s\n\n", type);
1225       fprintf(fbeleg, "\n    VOL ........................:\n");
1226       fprintf(fbeleg, "\n    Erstellungsdatum ...........: %s\n", get_date());
1227       if (date_todo) {
1228         fprintf(fbeleg, "\n    Ausfuehrungsdatum ..........: %s\n", date_todo);
1229       }
1230       fprintf(fbeleg, "\n    Waehrung ...................: %s\n", currency);
1231     }
1232     if (!dtaus_writeA(fdtaus, valA)) {
1233       fprintf (stderr, "Konnte den Anfangsdatensatz nicht schreiben.\n");
1234       return 0;
1235     }
1236
1237     fprintf (fcheck, "\n\n\n");
1238     if (valA[recindex][0] == 'L')
1239       fprintf (fcheck, "    Sammeleinziehungsauftrag\n\n");
1240     else if (valA[recindex][0] == 'G')
1241       fprintf (fcheck, "    Sammelueberweisungsauftrag\n\n");
1242     else
1243       fprintf (fcheck, "    Sammelauftrag\n\n");
1244     fprintf (fcheck, "    Erstellungsdatum : %s\n\n", get_date());
1245     if (use_euro)
1246       fprintf (fcheck, "    Waehrung         : Euro\n\n\n");
1247     else
1248       fprintf (fcheck, "    Waehrung         : DM\n\n\n");
1249     fprintf (fcheck, "     %-10s  %-8s  %-30s   %12s\n", "Kontonr.", "BLZ", "Name", "Betrag");
1250     fprintf (fcheck, "    --------------------------------------------------------------------\n");
1251   } else {
1252     fprintf (stderr, "Datei fängt nicht mit dem Anfangsdatensatz (BEGIN) an.\n");
1253     return 0;
1254   }
1255
1256   /* 
1257    * Record C lesen
1258    */
1259   count = 0;
1260   sum_val = bigint_int(0);
1261   sum_blz = bigint_int(0);
1262   sum_kto = bigint_int(0);
1263   memset (valC, 0, sizeof(valC));
1264   control_nextline ((void *)line, 100, fcontrol);
1265   if (line[0] == '{') {
1266     while (strlen(line) && line[0] == '{') {
1267       control_nextline ((void *)line, 100, fcontrol);
1268       if (text) {
1269         for (textindex=0; textindex < MAX_TEXT && text[textindex]; textindex++)
1270           free (text[textindex]);
1271         free (text);
1272         text = NULL;
1273       }
1274       while (strlen(line) && line[0] != '}') {
1275         ident = extract_ident(line);
1276         if ((recindex = rec_index(ident, REC_C)) != -1)
1277           if (recC[recindex].type != IGN) {
1278             /*
1279              * Special exception to support multiple Text fields
1280              */
1281             if (recindex == C_TEXT && valC[recindex]) {
1282               if (!text) {
1283                 if ((text = (char **)malloc ((MAX_TEXT+1) * sizeof (char *))) == NULL)
1284                   return 0;
1285                 else {
1286                   textindex = 0;
1287                   memset (text, 0, (MAX_TEXT+1) * sizeof (char *));
1288                 }
1289               }
1290               if (textindex < MAX_TEXT) {
1291                 if ((cp = (char *)malloc (strlen (line) + 1)) == NULL)
1292                   return 0;
1293                 strcpy (cp, line);
1294                 cp[strlen (line)] = '\0';
1295                 text[textindex++] = cp;
1296               }
1297             } else {
1298               len = strlen(line);
1299               if (recindex == C_VAL) {
1300                 /* Convert commas to dots for later processing */
1301                 for (i=0; line[i]; i++) if (line[i] == ',') line[i] = '.';
1302
1303                 if ((cp = strchr (line, '.')) == NULL) {
1304                   if ((valC[recindex] = (char *)malloc (strlen(line)+4)) == NULL)
1305                     return 0;
1306                   sprintf (valC[recindex], "%s.00", line);
1307                 } else if ( ((len = cp - line + 3)) < strlen (line)) {
1308                   if ((valC[recindex] = (char *)malloc (len+1)) == NULL)
1309                     return 0;
1310                   strncpy (valC[recindex], line, len);
1311                   valC[recindex][len] = '\0';
1312                 } else {
1313                   if ((valC[recindex] = (char *)malloc (strlen(line)+1)) == NULL)
1314                     return 0;
1315                   strcpy(valC[recindex], line);
1316                 }
1317               } else {
1318                 if ((valC[recindex] = (char *)malloc (strlen(line)+1)) == NULL)
1319                   return 0;
1320                 strcpy(valC[recindex], line);
1321               }
1322             }
1323           }
1324         control_nextline ((void *)line, 100, fcontrol);
1325       }
1326       if (!dtaus_writeC(fdtaus, valA, valC, text)) {
1327         fprintf (stderr, "Konnte den regulären Datensatz nicht schreiben.\n");
1328         return 0;
1329       }
1330       count++;
1331       bi = bigint_string(real2string(valC[C_VAL])); sum_val = bigint_add(sum_val, bi);
1332       bi = bigint_string(valC[C_BLZ]); sum_blz = bigint_add(sum_blz, bi);
1333       bi = bigint_string(valC[C_KTO]); sum_kto = bigint_add(sum_kto, bi);
1334
1335       fprintf (fcheck, "     %10s  %8s  %-30s   %12s\n", valC[C_KTO], valC[C_BLZ], valC[C_NAME], valC[C_VAL]);
1336       for (recindex=0; recindex<C_LEN; recindex++)
1337         if (valC[recindex])
1338           free(valC[recindex]);
1339       memset (valC, 0, sizeof(valC));
1340       control_nextline ((void *)line, 100, fcontrol);
1341     }
1342   } else {
1343     fprintf (stderr, "Kein regulärer Datensatz?\n");
1344     return 0;
1345   }
1346
1347   /* 
1348    * Record E lesen
1349    */
1350   dtaus_writeE(fdtaus, count, sum_val, sum_blz, sum_kto);
1351   fprintf (fcheck, "    --------------------------------------------------------------------\n");
1352   fprintf (fbeleg, "\n    Anzahl .....................: %d\n", count);
1353   bigint_sprintf (ssum_val, "%s", sum_val);
1354   recindex=strlen(ssum_val);
1355   ssum_val[recindex+1] = '\0';
1356   ssum_val[recindex] = ssum_val[recindex-1];
1357   ssum_val[recindex-1] = ssum_val[recindex-2];
1358   ssum_val[recindex-2] = '.';
1359   fprintf (fcheck, "     %-52s %14s\n", "Summe", ssum_val);
1360   fprintf (fbeleg, "\n    Summe ......................: %s\n", ssum_val);
1361   bigint_sprintf (ssum_kto, "%s", sum_kto);
1362   fprintf (fbeleg, "\n    Kontrollsumme Kontonummern .: %s\n", ssum_kto);
1363   bigint_sprintf (ssum_blz, "%s", sum_blz);
1364   fprintf (fbeleg, "\n    Kontrollsumme Bankleitzahlen: %s\n", ssum_blz);
1365   fprintf (fbeleg, "\n    Unsere Kontonummer .........: %s\n", valA[A_KTO]);
1366   fprintf (fbeleg, "\n    Unsere Bankleitzahl ........: %s\n", valA[A_BLZ]);
1367   fprintf (fbeleg, "\n\n\n\n\n    __________________________________________________\n");
1368   fprintf (fbeleg, "    Ort, Datum                     Unterschrift\n");
1369
1370   if (latex)
1371     generate_latex_receipt (latex, type, get_date(), date_todo,
1372                             currency, count,
1373                             ssum_val, ssum_kto, ssum_blz,
1374                             valA[A_KTO], valA[A_BLZ]);
1375
1376   for (recindex=0; recindex<A_LEN; recindex++)
1377     if (valA[recindex])
1378       free(valA[recindex]);
1379   fclose(fdtaus);
1380   fclose(fcontrol);
1381   fclose(fbeleg);
1382   if (ccheck)
1383     fclose(fcheck);
1384   return count;
1385 }