f76cbdaa0a3e0ae9ae7200c6da5a4df28ff83628
[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   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 /*
347  * Pads a string with zero's on the left side.
348  */
349 char *padzeroclip (char *s, int len)
350 {
351   char *p, *q;
352
353   if (strlen(s) == len) return s;
354   if (strlen(s) > len) {
355       q=s+len;
356       *(q--) = '\0';
357       return s;
358   }
359
360   q=s+len;
361   *(q--) = '\0';
362   for (p=s+strlen(s)-1;p>=s;p--)
363     *(q--)=*p;
364   for (;q>=s;) *(q--)='0';
365   return s;
366 }
367
368 int rec_index(char *ident, int type)
369 {
370   int i;
371   dtaus_record *rec = NULL;
372
373   switch (type) {
374   case REC_A:   rec = recA; break;
375   case REC_C:   rec = recC; break;
376   case REC_E:   rec = recE; break;
377   }
378
379   for (i=0; (rec[i].name); i++) {
380     if (!strcmp(ident, downcase(rec[i].name)))
381       return i;
382   }
383
384   return -1;
385 }
386
387 size_t control_nextline (void **buf, int len, FILE *f)
388 {
389   char line[100];
390   char tmp[100];
391   char *cp;
392   int i;
393
394   bzero (line, sizeof(line));
395   bzero (buf, len);
396   cp = line;
397
398   while (!strlen(line) && (cp = fgets(line, 100, f))) {
399     if (strlen(line)) {
400       if (line[0] != '#') {
401         if (line[strlen(line)-1] != '\n') { 
402           strcpy(tmp, line);
403           while (tmp[strlen(tmp)-1] != '\n' && (cp = fgets(tmp, 100, f)));
404         } else
405           line[strlen(line)-1] = '\0';
406         if (line[strlen(line)-1] == '\r')
407           line[strlen(line)-1] = '\0';
408         for (i=strlen(line);(line[i-1] == ' '||line[i-1] == '\t')&&i>0; i--)
409           line[i-1] = '\0';
410         if (line[0] == '#')
411           line[0] = '\0';
412       } else
413         line[0] = '\0';
414     }
415   }
416   for (cp=line; *cp==' '; cp++);
417
418   if (strlen(cp)) {
419     memcpy(buf, cp, strlen(cp));
420     return 1;
421   } else
422     return 0;
423 }
424
425 char *get_date()
426 {
427   static char res[10];
428   time_t timer;
429   struct tm *loctime;
430
431   timer = time ( NULL );
432   loctime = localtime(&timer);
433   sprintf(res, "%02d.%02d.%02d", loctime->tm_mday, loctime->tm_mon+1, loctime->tm_year);
434   return res;
435 }
436
437 void dtaus_prepareA(char *buf)
438 {
439   int i;
440   time_t timer;
441   struct tm *loctime;
442   char tmp[10];
443
444   bzero (buf, 129);
445   timer = time ( NULL );
446   loctime = localtime(&timer);
447
448   buf[0] = '0';
449   buf[1] = '1';
450   buf[2] = '2';
451   buf[3] = '8';
452   buf[4] = 'A';
453   for (i=15;i<15+8; i++) buf[i] = '0';
454   sprintf(tmp, "%02d%02d%2d", loctime->tm_mday, loctime->tm_mon+1, loctime->tm_year);
455   for (i=0; i<6; i++) buf[50+i] = tmp[i];
456   for (i=56;i<56+4; i++) buf[i] = ' ';
457   for (i=70;i<70+10; i++) buf[i] = '0';
458   for (i=80;i<80+48; i++) buf[i] = ' ';
459 }
460
461 void dtaus_prepareC(char *buf)
462 {
463   int i;
464
465   bzero (buf, 257);
466   buf[0] = '0';
467   buf[1] = '2';
468   buf[2] = '1';
469   buf[3] = '6';
470   buf[4] = 'C';
471   for (i=31;i<31+13; i++) buf[i] = '0';
472   buf[49] = ' ';
473   for (i=79;i<79+11; i++) buf[i] = '0';
474   for (i=90;i<90+3; i++) buf[i] = ' ';
475   for (i=120;i<120+8; i++) buf[i] = ' ';
476   for (i=182;i<182+3; i++) buf[i] = ' ';
477   for (i=185;i<185+2; i++) buf[i] = '0';
478   for (i=187;i<187+(29*2); i++) buf[i] = ' ';
479   for (i=245;i<245+11; i++) buf[i] = ' ';
480 }
481
482 void dtaus_prepareE(char *buf)
483 {
484   int i;
485
486   bzero (buf, 129);
487   buf[0] = '0';
488   buf[1] = '1';
489   buf[2] = '2';
490   buf[3] = '8';
491   buf[4] = 'E';
492   for (i=5;i<5+5; i++) buf[i] = ' ';
493   for (i=64;i<64+13; i++) buf[i] = '0';
494   for (i=77;i<77+51; i++) buf[i] = ' ';
495 }
496
497 int dtaus_writeA(FILE *f, char **values)
498 {
499   char buf[129];
500   char tmp[30];
501   int i;
502   
503   for (i=0; (recA[i].name); i++)
504     if ((recA[i].type == REQ) && !values[i]) {
505       fprintf (stderr, "Anfangsdatensatz ist nicht vollständig, kein %s.\n", recA[i].name);
506       return 0;
507     }
508   if (!(((values[A_TRANS][0] == 'L')||(values[A_TRANS][0] == 'G'))
509         &&((values[A_TRANS][1] == 'B')||(values[A_TRANS][1] == 'K')))) {
510     fprintf (stderr, "Ungueltiger Typ, nur LK, GK, LB oder GB erlaubt.\n");
511     return 0;
512   }
513
514   i=A_NAME;if (values[i] && strlen(values[i]) > recA[i].len)
515     values[i][recA[i].len] = '\0';
516
517   dtaus_prepareA(buf);
518   buf[5] = values[A_TRANS][0];
519   buf[6] = values[A_TRANS][1];
520   sprintf (tmp, "%s", padzeroclip(values[A_BLZ],8));
521   for (i=0; i<8; i++) buf[recA[A_BLZ].pos+i] = tmp[i];
522   sprintf (tmp, "%-27s", upcase(values[A_NAME]));
523   for (i=0; i<27; i++) buf[recA[A_NAME].pos+i] = tmp[i];
524   sprintf (tmp, "%s", padzeroclip(values[A_KTO],10));
525   for (i=0; i<10; i++) buf[recA[A_KTO].pos+i] = tmp[i];
526
527   fputs(buf, f);
528   return 1;
529 }
530
531 int dtaus_writeC(FILE *f, char **valuesA, char **values)
532 {
533   char buf[257];
534   char tmp[30];
535   int i;
536
537   /*
538   for (i=0; (recC[i].name); i++)
539     if (values[i])
540       printf ("%s: %s\n", recC[i].name, values[i]);
541       */
542
543   for (i=0; (recC[i].name); i++)
544     if ((recC[i].type == REQ) && !values[i]) {
545       fprintf (stderr, "Datensatz ist nicht vollständig, kein %s.\n", recC[i].name);
546       return 0;
547     }
548   sprintf (tmp, "%s", trans2string(values[C_TRANS]));
549   if (!strlen(tmp)) {
550     fprintf (stderr, "Ungültiger Typ, nur Abbuchung, Einzug, E-Cash, E-Cash-A, Gutschrift und Lohn erlaubt.\n");
551     return 0;
552   }
553
554   i=C_TEXT;if (values[i] && strlen(values[i]) > recC[i].len)
555     values[i][recC[i].len] = '\0';
556   i=C_ZWECK;if (values[i] && strlen(values[i]) > recC[i].len)
557     values[i][recC[i].len] = '\0';
558   i=C_MYNAM;if (values[i] && strlen(values[i]) > recC[i].len)
559     values[i][recC[i].len] = '\0';
560   i=C_TEXT;if (values[i] && strlen(values[i]) > recC[i].len)
561     values[i][recC[i].len] = '\0';
562   i=C_EXT;if (values[i] && strlen(values[i]) > recC[i].len)
563     values[i][recC[i].len] = '\0';
564
565   dtaus_prepareC(buf);
566   if (!values[C_TEXT]) {
567     buf[1] = '1';
568     buf[2] = '8';
569     buf[3] = '7';
570   }
571   for (i=0; i<5; i++) buf[recC[C_TRANS].pos+i] = tmp[i];
572   if (values[C_MYBLZ])
573     sprintf (tmp, "%s", padzeroclip(values[C_MYBLZ],8));
574   else
575     sprintf (tmp, "%s", padzeroclip(valuesA[A_BLZ],8));
576   for (i=0; i<recC[C_MYBLZ].len; i++) buf[5+i] = tmp[i];
577   for (i=0; i<recC[C_MYBLZ].len; i++) buf[recC[C_MYBLZ].pos+i] = tmp[i];
578   sprintf (tmp, "%s", padzeroclip(values[C_BLZ],8));
579   for (i=0; i<recC[C_BLZ].len; i++) buf[recC[C_BLZ].pos+i] = tmp[i];
580   sprintf (tmp, "%s", padzeroclip(values[C_KTO],10));
581   for (i=0; i<recC[C_KTO].len; i++) buf[recC[C_KTO].pos+i] = tmp[i];
582   sprintf (tmp, "%s", padzeroclip(real2string(values[C_VAL]),11));
583   for (i=0; i<recC[C_VAL].len; i++) buf[recC[C_VAL].pos+i] = tmp[i];
584   if (values[C_MYKTO])
585     sprintf (tmp, "%s", padzeroclip(values[C_MYKTO],10));
586   else
587     sprintf (tmp, "%s", padzeroclip(valuesA[A_KTO],10));
588   for (i=0; i<recC[C_MYKTO].len; i++) buf[recC[C_MYKTO].pos+i] = tmp[i];
589   sprintf (tmp, "%-27s", upcase(values[C_NAME]));
590   for (i=0; i<recC[C_NAME].len; i++) buf[recC[C_NAME].pos+i] = tmp[i];
591   if (values[C_MYNAM])
592     sprintf (tmp, "%-27s", upcase(values[C_MYNAM]));
593   else
594     sprintf (tmp, "%-27s", upcase(valuesA[A_NAME]));
595   for (i=0; i<recC[C_MYNAM].len; i++) buf[recC[C_MYNAM].pos+i] = tmp[i];
596   sprintf (tmp, "%-27s", upcase(values[C_ZWECK]));
597   for (i=0; i<recC[C_ZWECK].len; i++) buf[recC[C_ZWECK].pos+i] = tmp[i];
598
599   if (!values[C_TEXT]) {
600     buf[recC[C_TEXT].pos+0] = ' ';
601     buf[recC[C_TEXT].pos+1] = ' ';
602   } else {
603     buf[185+0] = '0';
604     buf[185+1] = '1';
605     buf[recC[C_TEXT].pos+0] = '0';
606     buf[recC[C_TEXT].pos+1] = '2';
607     sprintf (tmp, "%-27s", upcase(values[C_TEXT]));
608     for (i=0; i<recC[C_TEXT].len-2; i++) buf[recC[C_TEXT].pos+2+i] = tmp[i];
609   }
610   fputs(buf, f);
611   return 1;
612 }
613
614 int dtaus_writeE(FILE *f, int count, bigint sum, bigint blz, bigint kto)
615 {
616   char buf[129];
617   char tmp[30];
618   int i;
619   
620   dtaus_prepareE(buf);
621
622   sprintf (tmp, "%07d", count);
623   for (i=0; i<recE[E_COUNT].len; i++) buf[recE[E_COUNT].pos+i] = tmp[i];
624   bigint_sprintf (tmp, "%s", sum);
625   padzeroclip(tmp,13);
626   for (i=0; i<recE[E_VAL].len; i++) buf[recE[E_VAL].pos+i] = tmp[i];
627   bigint_sprintf (tmp, "%s", sum);
628   padzeroclip(tmp,13);
629   for (i=0; i<recE[E_VAL].len; i++) buf[recE[E_VAL].pos+i] = tmp[i];
630   bigint_sprintf (tmp, "%s", kto);
631   padzeroclip(tmp,17);
632   for (i=0; i<recE[E_KTO].len; i++) buf[recE[E_KTO].pos+i] = tmp[i];
633   bigint_sprintf (tmp, "%s", blz);
634   padzeroclip(tmp,17);
635   for (i=0; i<recE[E_BLZ].len; i++) buf[recE[E_BLZ].pos+i] = tmp[i];
636
637   fputs(buf, f);
638   return 1;
639 }
640
641 void printctln(FILE *f, char *field, char *value)
642 {
643   if (strlen(field) && strlen(value))
644     fprintf(f, "  %s\t%s\n", field, value);
645 }
646
647
648 /*
649  *  Third: Some high level routines
650  */
651
652 void dtaus2control (char *cdtaus, char *ccontrol)
653 {
654   FILE *fdtaus, *fcontrol;
655   void *buf;
656   void *bufp;
657   char tmp[30];
658   char x[30];
659   int index, countC;
660
661   if (!cdtaus)
662     if (!(fdtaus = fopen("DTAUS0.TXT", "r")))
663       if (!(fdtaus = fopen("dtaus0.txt", "r")))
664         return;
665   if (cdtaus)
666     if (!(fdtaus = fopen(cdtaus, "r")))
667       return;
668   if (!ccontrol)
669     fcontrol = stdout;
670   else
671     if (!(fcontrol = fopen(ccontrol, "w")))
672       return;
673   if (!(buf = (char *)malloc (512)))
674     return;
675
676   /* 
677    * Record A lesen
678    */
679   if (dtaus_nextrec(buf, fdtaus) == 1) {
680     if (dtaus_char(buf,4) == 'A') {
681       fprintf(fcontrol, "BEGIN {\n");
682       bufp = buf;
683
684       for (index=A_TRANS; index < A_DATE; index++) {
685         bufp = buf + recA[index].pos;
686         memcpy(tmp, bufp, recA[index].len); tmp[recA[index].len] = '\0';
687         printctln(fcontrol, recA[index].name, strip_zeros(strip_spaces(tmp)));
688       }
689
690       index = A_DATE; bufp = buf + recA[index].pos;
691       memcpy(tmp, bufp, recA[index].len); tmp[recA[index].len] = '\0';
692       printctln(fcontrol, recA[index].name, strip_spaces(tmp));
693
694       index = A_EURO; bufp = buf + recA[index].pos;
695       memcpy(tmp, bufp, recA[index].len); tmp[recA[index].len] = '\0';
696       printctln(fcontrol, recA[index].name, strip_zeros(strip_spaces(tmp)));
697
698       fprintf(fcontrol, "}\n\n");
699     } else {
700       fprintf (stderr, "Datei fängt nicht mit dem Anfangsdatensatz an.\n");
701       return;
702     }
703   } else {
704     fprintf (stderr, "Der Anfangsdatensatz ist kaputt.\n");
705     return;
706   }
707
708   /*
709    * Record C lesen
710    */
711   if (dtaus_nextrec(buf, fdtaus) == 1) {
712     while (dtaus_char(buf,4) == 'C') {
713       bufp = buf + 128;
714       if (dtaus_nextrec(bufp, fdtaus) == 1) {
715         fprintf(fcontrol, "{\n");
716
717         for (index=C_NAME; index <= C_EURO; index++) {
718           bufp = buf + recC[index].pos;
719           memcpy(tmp, bufp, recC[index].len); tmp[recC[index].len] = '\0';
720           if (index == C_VAL)
721             printctln(fcontrol, recC[index].name, strip_zeros(string2real(tmp)));
722           else if (index == C_TRANS)
723             printctln(fcontrol, recC[index].name, strip_zeros(string2trans(tmp)));
724           else
725             printctln(fcontrol, recC[index].name, strip_zeros(strip_spaces(tmp)));
726         }
727
728         for (index=C_TEXT; index <= C_EXT; index++) {
729           if (!(dtaus_char(buf,recC[index].pos) == ' ')) {
730             bufp = buf + recC[index].pos;
731             memcpy(x, bufp, 2); tmp[2] = '\0'; bufp+=2;
732             memcpy(tmp, bufp, recC[index].len-2); tmp[recC[index].len-2] = '\0';
733             printctln(fcontrol, string2ext(x), strip_spaces(tmp));
734           }
735         }
736
737         countC = dtaus_int(buf, 185, 2);
738         countC = countC<3?0:countC-2/4;
739         countC += countC%4>0?1:0;
740         /* FIXME: Erweiterungsfelder werden ignoriert */
741
742         fprintf(fcontrol, "}\n");
743       } else {
744         fprintf (stderr, "Der zweite Teil der Transaktion ist kaputt.\n");
745         return;
746       }
747       if (dtaus_nextrec(buf, fdtaus) != 1)
748         bzero (buf, sizeof(buf));
749     }
750   }
751
752   /*
753    * Record E lesen
754    *   (gelesen ist er eigentlich schon...)
755    */
756   if (dtaus_char(buf,4) == 'E') {
757     if (dtaus_char(buf,4) == 'E') {
758       fprintf(fcontrol, "END {\n");
759
760       for (index=E_COUNT; index <= E_BLZ; index++) {
761         bufp = buf + recE[index].pos;
762         memcpy(tmp, bufp, recE[index].len); tmp[recE[index].len] = '\0';
763         if (index == E_VAL)
764           printctln(fcontrol, recE[index].name, strip_zeros(string2real(tmp)));
765         else
766           printctln(fcontrol, recE[index].name, strip_zeros(tmp));
767       }
768
769       fprintf(fcontrol, "}\n");
770     } else {
771       fprintf (stderr, "Das ist kein Abschlußdatensatz.\n");
772       return;
773     }
774   } else {
775     fprintf (stderr, "Der Abschlußdatensatz ist leer oder kaputt.\n");
776     return;
777   }
778   fclose(fcontrol);
779   fclose(fdtaus);
780 }
781
782 int control2dtaus (char *ccontrol, char *cdtaus, char *cbeleg, char *ccheck)
783 {
784   FILE *fdtaus, *fcontrol, *fbeleg, *fcheck;
785   void *buf;
786   char *ident;
787   int  recindex;
788   char tmp[30];
789   char line[100];
790   char *valA[A_LEN], *valC[C_LEN];
791   int count;
792   bigint sum_val, sum_blz, sum_kto, bi;
793
794   if (!cdtaus)
795     if (!(fdtaus = fopen("dtaus0.txt", "w")))
796       return 0;
797   if (cdtaus)
798     if (!(fdtaus = fopen(cdtaus, "w")))
799       return 0;
800   if (!ccontrol)
801     if (!(fcontrol = fopen("dtaus0.ctl", "r")))
802       if (!(fcontrol = fopen("DTAUS0.CTL", "r")))
803         return 0;
804   if (ccontrol)
805     if (!(fcontrol = fopen(ccontrol, "r")))
806       return 0;
807   if (!cbeleg)
808     if (!(fbeleg = fopen("dtaus0.doc", "w")))
809       return 0;
810   if (cbeleg)
811     if (!(fbeleg = fopen(cbeleg, "w")))
812       return 0;
813   if (!ccheck)
814     fcheck = stdout;
815   else
816     if (!(fcheck = fopen(ccheck, "w")))
817       return 0;
818
819
820   if (!(buf = (char *)malloc (512)))
821     return 0;
822
823   /* 
824    * Record A lesen
825    */
826   bzero(valA, sizeof(valA));
827   control_nextline ((void *)line, 100, fcontrol);
828   ident = extract_ident(line);
829   if (!strcmp(ident, "begin") && (line[0] == '{')) {
830     control_nextline ((void *)line, 100, fcontrol);
831     while (strlen(line) && line[0] != '}') {
832       ident = extract_ident(line);
833       if ((recindex = rec_index(ident, REC_A)) != -1)
834         if (recA[recindex].type != IGN)
835           if ((valA[recindex] = (char *)malloc (strlen(line)+1)))
836             strcpy(valA[recindex], line);
837       control_nextline ((void *)line, 100, fcontrol);
838     }
839     if (((recindex = rec_index("art", REC_A)) != -1) && valA[recindex] && strlen(valA[recindex])) {
840       fprintf(fbeleg, "\n\n");
841       fprintf(fbeleg, "\n    Begleitzettel\n\n");
842       fprintf(fbeleg, "\n    Belegloser Datentraegeraustausch\n\n");
843       if (valA[recindex][0] == 'L')
844         fprintf(fbeleg, "\n    Sammeleinziehungsauftrag\n\n");
845       else if (valA[recindex][0] == 'G')
846         fprintf(fbeleg, "\n    Sammelueberweisungsauftrag\n\n");
847       else
848         fprintf(fbeleg, "\n    Sammelauftrag\n\n");
849       fprintf(fbeleg, "\n    VOL ........................:\n");
850       fprintf(fbeleg, "\n    Erstellungsdatum ...........: %s\n", get_date());
851     }
852     if (!dtaus_writeA(fdtaus, valA)) {
853       fprintf (stderr, "Konnte den Anfangsdatensatz nicht schreiben.\n");
854       return 0;
855     }
856
857     fprintf (fcheck, "\n\n\n");
858     fprintf (fcheck, "    Sammeleinzeiehungsauftrag\n\n");
859     fprintf (fcheck, "    Erstellungsdatum : %s\n\n\n", get_date());
860     fprintf (fcheck, "     %-10s  %-8s  %-30s   %12s\n", "Kontonr.", "BLZ", "Name", "Betrag");
861     fprintf (fcheck, "    --------------------------------------------------------------------\n");
862   } else {
863     fprintf (stderr, "Datei fängt nicht mit dem Anfangsdatensatz (BEGIN) an.\n");
864     return 0;
865   }
866
867   /* 
868    * Record C lesen
869    */
870   count = 0;
871   sum_val = bigint_int(0);
872   sum_blz = bigint_int(0);
873   sum_kto = bigint_int(0);
874   bzero(valC, sizeof(valC));
875   control_nextline ((void *)line, 100, fcontrol);
876   if (line[0] == '{') {
877     while (strlen(line) && line[0] == '{') {
878       control_nextline ((void *)line, 100, fcontrol);
879       while (strlen(line) && line[0] != '}') {
880         ident = extract_ident(line);
881         if ((recindex = rec_index(ident, REC_C)) != -1)
882           if (recC[recindex].type != IGN)
883             if ((valC[recindex] = (char *)malloc (strlen(line)+1)))
884               strcpy(valC[recindex], line);
885             else
886               return 0;
887         control_nextline ((void *)line, 100, fcontrol);
888       }
889       if (!dtaus_writeC(fdtaus, valA, valC)) {
890         fprintf (stderr, "Konnte den regulären Datensatz nicht schreiben.\n");
891         return 0;
892       }
893       count++;
894       bi = bigint_string(real2string(valC[C_VAL])); sum_val = bigint_add(sum_val, bi);
895       bi = bigint_string(valC[C_BLZ]); sum_blz = bigint_add(sum_blz, bi);
896       bi = bigint_string(valC[C_KTO]); sum_kto = bigint_add(sum_kto, bi);
897
898       fprintf (fcheck, "     %10s  %8s  %-30s   %12s\n", valC[C_KTO], valC[C_BLZ], valC[C_NAME], valC[C_VAL]);
899       for (recindex=0; recindex<C_LEN; recindex++)
900         if (valC[recindex])
901           free(valC[recindex]);
902       bzero(valC, sizeof(valC));
903       control_nextline ((void *)line, 100, fcontrol);
904     }
905   } else {
906     fprintf (stderr, "Kein regulärer Datensatz?\n");
907     return 0;
908   }
909
910   /* 
911    * Record E lesen
912    */
913   dtaus_writeE(fdtaus, count, sum_val, sum_blz, sum_kto);
914   fprintf (fcheck, "    --------------------------------------------------------------------\n");
915   bigint_sprintf (tmp, "%s", sum_val);
916   fprintf (fbeleg, "\n    Anzahl .....................: %d\n", count);
917   recindex=strlen(tmp);
918   tmp[recindex+1] = '\0';
919   tmp[recindex] = tmp[recindex-1];
920   tmp[recindex-1] = tmp[recindex-2];
921   tmp[recindex-2] = '.';
922   fprintf (fcheck, "     %-52s %14s\n", "Summe", tmp);
923   fprintf (fbeleg, "\n    Summe ......................: %s\n", tmp);
924   bigint_sprintf (tmp, "%s", sum_kto);
925   fprintf (fbeleg, "\n    Kontrollsumme Kontonummern .: %s\n", tmp);
926   bigint_sprintf (tmp, "%s", sum_blz);
927   fprintf (fbeleg, "\n    Kontrollsumme Bankleitzahlen: %s\n", tmp);
928   fprintf (fbeleg, "\n    Unsere Kontonummer .........: %s\n", valA[A_KTO]);
929   fprintf (fbeleg, "\n    Unsere Bankleitzahl ........: %s\n", valA[A_BLZ]);
930   fprintf (fbeleg, "\n\n\n\n\n    __________________________________________________\n");
931   fprintf (fbeleg, "    Ort, Datum                     Unterschrift\n");
932   for (recindex=0; recindex<A_LEN; recindex++)
933     if (valA[recindex])
934       free(valA[recindex]);
935   fclose(fdtaus);
936   fclose(fcontrol);
937   fclose(fbeleg);
938   if (ccheck)
939     fclose(fcheck);
940   return count;
941 }