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