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