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