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