2 dtaus.c - Datenträgeraustausch mit einer Bank
3 Copyright (c) 1996 Martin Schulze <joey@artis.uni-oldenburg.de>
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.
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.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
31 * First: Some control structures
37 unsigned short int pos;
38 unsigned short int len;
50 dtaus_record recA[] = {
52 {"Name", 23, 27, REQ},
53 {"Konto", 60, 10, REQ},
55 {"Referenz", 70, 10, OPT},
56 {"Datum", 50, 6, IGN},
68 dtaus_record recC[] = {
69 {"Name", 93, 27, REQ},
70 {"Konto", 21, 10, REQ},
72 {"Transaktion", 44, 5, REQ},
73 {"Betrag", 50, 11, REQ},
74 {"Zweck", 155, 27, REQ},
75 {"myName", 128, 27, OPT},
76 {"myKonto", 69, 10, OPT},
77 {"myBLZ", 61, 8, OPT},
78 {"Text", 187, 29, OPT},
79 {"Extension", 216, 29, OPT},
96 dtaus_record recE[] = {
97 {"Anzahl", 10, 7, IGN},
98 {"Summe", 17, 13, IGN},
99 {"Kontos", 30, 17, IGN},
100 {"BLZs", 47, 17, IGN},
111 * Second: Some low level routines
114 size_t dtaus_nextrec (void **buf, FILE *f)
117 return fread(buf, 128, 1, f);
120 char *upcase(char *s)
126 for (cp=s,xp=x; *cp; cp++,xp++) {
127 if (strchr(" 0123456789.,&-/+*$%ABCDEFGHIJKLMNOPQRSTUVWXYZ",*cp)) {
129 } else if (strchr("abcdefghijklmnopqrstuvwxyz",*cp)) {
131 } else if (strchr ("üÜäÄöÖß", *cp)) {
133 case 'ü': *(xp++) = 'U'; *xp='E'; break;
134 case 'Ü': *(xp++) = 'U'; *xp='E'; break;
135 case 'ä': *(xp++) = 'A'; *xp='E'; break;
136 case 'Ä': *(xp++) = 'A'; *xp='E'; break;
137 case 'ö': *(xp++) = 'O'; *xp='E'; break;
138 case 'Ö': *(xp++) = 'O'; *xp='E'; break;
139 case 'ß': *(xp++) = 'S'; *xp='S'; break;
149 char *downcase(char *s)
162 char *strip_spaces (char *s)
167 for (i=strlen(s);(s[i-1] == ' '||s[i-1] == '\t')&&i>0; i--)
169 for (p=s; *p==' '; p++);
173 char *strip_zeros (char *s)
177 for (p=s; *p=='0'; p++);
181 char dtaus_char(void *buf, unsigned int pos)
187 memcpy(&res, bufp, 1);
191 char *string2real(char *s)
197 strncpy(res, s, strlen(s)-2);
198 res[strlen(s)-2] = '.';
199 res[strlen(s)-1] = s[strlen(s)-2];
200 res[strlen(s)] = s[strlen(s)-1];
204 char *real2string(char *s)
210 for (cp=res; *cp&&!(*cp == ',')&&!(*cp == '.');cp++);
217 char *string2trans (char *s)
222 if (!strcmp(s, "04000"))
223 sprintf (res, "Abbuchung");
224 else if (!strcmp(s, "05000"))
225 sprintf (res, "Einzug");
226 else if (!strcmp(s, "05005"))
227 sprintf (res, "E-Cash");
228 else if (!strcmp(s, "05006"))
229 sprintf (res, "E-Cash-A");
230 else if (!strcmp(s, "51000"))
231 sprintf (res, "Gutschrift");
232 else if (!strcmp(s, "53000"))
233 sprintf (res, "Lohn");
234 else if (!strncmp(s, "5400", 4))
235 sprintf (res, "Vermögen");
236 /* else if (!strcmp(s, "56000"))
237 sprintf (res, ""); / * Überweisung öffentlicher Kassen */
241 char *trans2string (char *s)
246 if (!strcmp(s, "Abbuchung"))
247 sprintf (res, "04000");
248 else if (!strcmp(s, "Einzug"))
249 sprintf (res, "05000");
250 else if (!strcmp(s, "E-Cash"))
251 sprintf (res, "05005");
252 else if (!strcmp(s, "E-Cash-A"))
253 sprintf (res, "05006");
254 else if (!strcmp(s, "Gutschrift"))
255 sprintf (res, "51000");
256 else if (!strcmp(s, "Lohn"))
257 sprintf (res, "53000");
258 else if (!strncmp(s, "Vermögen", 4))
259 sprintf (res, "5400");
260 /* else if (!strcmp(s, ""))
261 sprintf (res, "56000"); / * Überweisung öffentlicher Kassen */
265 char *string2ext (char *s)
270 if (!strcmp(s, "01"))
271 sprintf (res, "Klient");
272 else if (!strcmp(s, "02"))
273 sprintf (res, "Text");
274 else if (!strcmp(s, "03"))
275 sprintf (res, "Auftraggeber");
279 char *ext2string (char *s)
284 if (!strcmp(s, "Klient"))
286 else if (!strcmp(s, "Text"))
288 else if (!strcmp(s, "Auftraggeber"))
293 unsigned long int dtaus_int(void *buf, unsigned int pos, unsigned int len)
297 static unsigned long int res;
300 memcpy(tmp, bufp, len);
302 sscanf(tmp, "%lu", &res);
307 * returns the first word in this line, returns it and shortens the
310 char *extract_ident (char *line)
313 static char word[30];
315 if (strlen(line) > 0) {
316 x = index (line, ' ');
317 y = index (line, '\t');
320 strncpy(word, line, 29);
325 /* Check which index returns the lower value, and check if the
327 if ((c = (x && x<y)?x:(y?y:x))) {
328 strncpy(word, line, c - line);
330 for (;*c == '\t' || *c == ' '; c++);
331 for (x=line; *c; c++,x++)
334 strcpy(word, downcase(word));
342 int rec_index(char *ident, int type)
345 dtaus_record *rec = NULL;
348 case REC_A: rec = recA; break;
349 case REC_C: rec = recC; break;
350 case REC_E: rec = recE; break;
353 for (i=0; (rec[i].name); i++) {
354 if (!strcmp(ident, downcase(rec[i].name)))
361 size_t control_nextline (void **buf, int len, FILE *f)
368 bzero (line, sizeof(line));
372 while (!strlen(line) && (cp = fgets(line, 100, f))) {
374 if (line[0] != '#') {
375 if (line[strlen(line)-1] != '\n') {
377 while (tmp[strlen(tmp)-1] != '\n' && (cp = fgets(tmp, 100, f)));
379 line[strlen(line)-1] = '\0';
380 if (line[strlen(line)-1] == '
\r')
381 line[strlen(line)-1] = '\0';
382 for (i=strlen(line);(line[i-1] == ' '||line[i-1] == '\t')&&i>0; i--)
390 for (cp=line; *cp==' '; cp++);
393 memcpy(buf, cp, strlen(cp));
405 timer = time ( NULL );
406 loctime = localtime(&timer);
407 sprintf(res, "%02d.%02d.%02d", loctime->tm_mday, loctime->tm_mon+1, loctime->tm_year);
411 void dtaus_prepareA(char *buf)
419 timer = time ( NULL );
420 loctime = localtime(&timer);
427 for (i=15;i<15+8; i++) buf[i] = '0';
428 sprintf(tmp, "%02d%02d%2d", loctime->tm_mday, loctime->tm_mon+1, loctime->tm_year);
429 for (i=0; i<6; i++) buf[50+i] = tmp[i];
430 for (i=56;i<56+4; i++) buf[i] = ' ';
431 for (i=70;i<70+10; i++) buf[i] = '0';
432 for (i=80;i<80+48; i++) buf[i] = ' ';
435 void dtaus_prepareC(char *buf)
445 for (i=31;i<31+13; i++) buf[i] = '0';
447 for (i=79;i<79+11; i++) buf[i] = '0';
448 for (i=90;i<90+3; i++) buf[i] = ' ';
449 for (i=120;i<120+8; i++) buf[i] = ' ';
450 for (i=182;i<182+3; i++) buf[i] = ' ';
451 for (i=185;i<185+2; i++) buf[i] = '0';
452 for (i=187;i<187+(29*2); i++) buf[i] = ' ';
453 for (i=245;i<245+11; i++) buf[i] = ' ';
456 void dtaus_prepareE(char *buf)
466 for (i=5;i<5+5; i++) buf[i] = ' ';
467 for (i=64;i<64+13; i++) buf[i] = '0';
468 for (i=77;i<77+51; i++) buf[i] = ' ';
471 int dtaus_writeA(FILE *f, char **values)
477 for (i=0; (recA[i].name); i++)
478 if ((recA[i].type == REQ) && !values[i]) {
479 fprintf (stderr, "Anfangsdatensatz ist nicht vollständig, kein %s.\n", recA[i].name);
482 if (!(((values[A_TRANS][0] == 'L')||(values[A_TRANS][0] == 'G'))
483 &&((values[A_TRANS][1] == 'B')||(values[A_TRANS][1] == 'K')))) {
484 fprintf (stderr, "Ungueltiger Typ, nur LK, GK, LB oder GB erlaubt.\n");
489 buf[5] = values[A_TRANS][0];
490 buf[6] = values[A_TRANS][1];
491 sprintf (tmp, "%08s", values[A_BLZ]);
492 for (i=0; i<8; i++) buf[recA[A_BLZ].pos+i] = tmp[i];
493 sprintf (tmp, "%-27s", upcase(values[A_NAME]));
494 for (i=0; i<27; i++) buf[recA[A_NAME].pos+i] = tmp[i];
495 sprintf (tmp, "%010s", values[A_KTO]);
496 for (i=0; i<10; i++) buf[recA[A_KTO].pos+i] = tmp[i];
502 int dtaus_writeC(FILE *f, char **valuesA, char **values)
509 for (i=0; (recC[i].name); i++)
511 printf ("%s: %s\n", recC[i].name, values[i]);
514 for (i=0; (recC[i].name); i++)
515 if ((recC[i].type == REQ) && !values[i]) {
516 fprintf (stderr, "Datensatz ist nicht vollständig, kein %s.\n", recC[i].name);
519 sprintf (tmp, "%s", trans2string(values[C_TRANS]));
521 fprintf (stderr, "Ungültiger Typ, nur Abbuchung, Einzug, E-Cash, E-Cash-A, Gutschrift und Lohn erlaubt.\n");
526 if (!values[C_TEXT]) {
531 for (i=0; i<5; i++) buf[recC[C_TRANS].pos+i] = tmp[i];
533 sprintf (tmp, "%08s", values[C_MYBLZ]);
535 sprintf (tmp, "%08s", valuesA[A_BLZ]);
536 for (i=0; i<recC[C_MYBLZ].len; i++) buf[5+i] = tmp[i];
537 for (i=0; i<recC[C_MYBLZ].len; i++) buf[recC[C_MYBLZ].pos+i] = tmp[i];
538 sprintf (tmp, "%08s", values[C_BLZ]);
539 for (i=0; i<recC[C_BLZ].len; i++) buf[recC[C_BLZ].pos+i] = tmp[i];
540 sprintf (tmp, "%010s", values[C_KTO]);
541 for (i=0; i<recC[C_KTO].len; i++) buf[recC[C_KTO].pos+i] = tmp[i];
542 sprintf (tmp, "%011s", real2string(values[C_VAL]));
543 for (i=0; i<recC[C_VAL].len; i++) buf[recC[C_VAL].pos+i] = tmp[i];
545 sprintf (tmp, "%010s", values[C_MYKTO]);
547 sprintf (tmp, "%010s", valuesA[A_KTO]);
548 for (i=0; i<recC[C_MYKTO].len; i++) buf[recC[C_MYKTO].pos+i] = tmp[i];
549 sprintf (tmp, "%-27s", upcase(values[C_NAME]));
550 for (i=0; i<recC[C_NAME].len; i++) buf[recC[C_NAME].pos+i] = tmp[i];
552 sprintf (tmp, "%-27s", upcase(values[C_MYNAM]));
554 sprintf (tmp, "%-27s", upcase(valuesA[A_NAME]));
555 for (i=0; i<recC[C_MYNAM].len; i++) buf[recC[C_MYNAM].pos+i] = tmp[i];
556 sprintf (tmp, "%-27s", upcase(values[C_ZWECK]));
557 for (i=0; i<recC[C_ZWECK].len; i++) buf[recC[C_ZWECK].pos+i] = tmp[i];
559 if (!values[C_TEXT]) {
560 buf[recC[C_TEXT].pos+0] = ' ';
561 buf[recC[C_TEXT].pos+1] = ' ';
565 buf[recC[C_TEXT].pos+0] = '0';
566 buf[recC[C_TEXT].pos+1] = '2';
567 sprintf (tmp, "%-27s", upcase(values[C_TEXT]));
568 for (i=0; i<recC[C_TEXT].len-2; i++) buf[recC[C_TEXT].pos+2+i] = tmp[i];
574 int dtaus_writeE(FILE *f, int count, bigint sum, bigint blz, bigint kto)
582 sprintf (tmp, "%07d", count);
583 for (i=0; i<recE[E_COUNT].len; i++) buf[recE[E_COUNT].pos+i] = tmp[i];
584 bigint_sprintf (tmp, "%013s", sum);
585 for (i=0; i<recE[E_VAL].len; i++) buf[recE[E_VAL].pos+i] = tmp[i];
586 bigint_sprintf (tmp, "%013s", sum);
587 for (i=0; i<recE[E_VAL].len; i++) buf[recE[E_VAL].pos+i] = tmp[i];
588 bigint_sprintf (tmp, "%017s", kto);
589 for (i=0; i<recE[E_KTO].len; i++) buf[recE[E_KTO].pos+i] = tmp[i];
590 bigint_sprintf (tmp, "%017s", blz);
591 for (i=0; i<recE[E_BLZ].len; i++) buf[recE[E_BLZ].pos+i] = tmp[i];
598 * Third: Some high level routines
601 void dtaus2control (char *cdtaus, char *ccontrol)
603 FILE *fdtaus, *fcontrol;
611 if (!(fdtaus = fopen("DTAUS0.TXT", "r")))
612 if (!(fdtaus = fopen("dtaus0.txt", "r")))
615 if (!(fdtaus = fopen(cdtaus, "r")))
620 if (!(fcontrol = fopen(ccontrol, "w")))
622 if (!(buf = (char *)malloc (512)))
628 if (dtaus_nextrec(buf, fdtaus) == 1) {
629 if (dtaus_char(buf,4) == 'A') {
630 fprintf(fcontrol, "BEGIN {\n");
633 for (index=A_TRANS; index < A_DATE; index++) {
634 bufp = buf + recA[index].pos;
635 memcpy(tmp, bufp, recA[index].len); tmp[recA[index].len] = '\0';
636 fprintf(fcontrol, " %s\t%s\n", recA[index].name, strip_zeros(strip_spaces(tmp)));
639 index = A_DATE; bufp = buf + recA[index].pos;
640 memcpy(tmp, bufp, recA[index].len); tmp[recA[index].len] = '\0';
641 fprintf(fcontrol, " %s\t%s\n", recA[index].name, strip_spaces(tmp));
643 fprintf(fcontrol, "}\n\n");
645 fprintf (stderr, "Datei fängt nicht mit dem Anfangsdatensatz an.\n");
649 fprintf (stderr, "Der Anfangsdatensatz ist kaputt.\n");
656 if (dtaus_nextrec(buf, fdtaus) == 1) {
657 while (dtaus_char(buf,4) == 'C') {
659 if (dtaus_nextrec(bufp, fdtaus) == 1) {
660 fprintf(fcontrol, "{\n");
662 for (index=C_NAME; index <= C_MYBLZ; index++) {
663 bufp = buf + recC[index].pos;
664 memcpy(tmp, bufp, recC[index].len); tmp[recC[index].len] = '\0';
666 fprintf(fcontrol, " %s\t%s\n", recC[index].name, strip_zeros(string2real(tmp)));
667 else if (index == C_TRANS)
668 fprintf(fcontrol, " %s\t%s\n", recC[index].name, string2trans(tmp));
670 fprintf(fcontrol, " %s\t%s\n", recC[index].name, strip_zeros(strip_spaces(tmp)));
673 for (index=C_TEXT; index <= C_EXT; index++) {
674 if (!(dtaus_char(buf,recC[index].pos) == ' ')) {
675 bufp = buf + recC[index].pos;
676 memcpy(x, bufp, 2); tmp[2] = '\0'; bufp+=2;
677 memcpy(tmp, bufp, recC[index].len-2); tmp[recC[index].len-2] = '\0';
678 fprintf(fcontrol, " %s\t%s\n", string2ext(x), strip_spaces(tmp));
682 countC = dtaus_int(buf, 185, 2);
683 countC = countC<3?0:countC-2/4;
684 countC += countC%4>0?1:0;
685 /* FIXME: Erweiterungsfelder werden ignoriert */
687 fprintf(fcontrol, "}\n");
689 fprintf (stderr, "Der zweite Teil der Transaktion ist kaputt.\n");
692 if (dtaus_nextrec(buf, fdtaus) != 1)
693 bzero (buf, sizeof(buf));
699 * (gelesen ist er eigentlich schon...)
701 if (dtaus_char(buf,4) == 'E') {
702 if (dtaus_char(buf,4) == 'E') {
703 fprintf(fcontrol, "END {\n");
705 for (index=E_COUNT; index <= E_BLZ; index++) {
706 bufp = buf + recE[index].pos;
707 memcpy(tmp, bufp, recE[index].len); tmp[recE[index].len] = '\0';
709 fprintf(fcontrol, " %s\t%s\n", recE[index].name, strip_zeros(string2real(tmp)));
711 fprintf(fcontrol, " %s\t%s\n", recE[index].name, strip_zeros(tmp));
714 fprintf(fcontrol, "}\n");
716 fprintf (stderr, "Das ist kein Abschlußdatensatz.\n");
720 fprintf (stderr, "Der Abschlußdatensatz ist leer oder kaputt.\n");
727 int control2dtaus (char *ccontrol, char *cdtaus, char *cbeleg, char *ccheck)
729 FILE *fdtaus, *fcontrol, *fbeleg, *fcheck;
735 char *valA[A_LEN], *valC[C_LEN];
737 bigint sum_val, sum_blz, sum_kto, bi;
740 if (!(fdtaus = fopen("dtaus0.txt", "w")))
743 if (!(fdtaus = fopen(cdtaus, "w")))
746 if (!(fcontrol = fopen("dtaus0.ctl", "r")))
747 if (!(fcontrol = fopen("DTAUS0.CTL", "r")))
750 if (!(fcontrol = fopen(ccontrol, "r")))
753 if (!(fbeleg = fopen("dtaus0.doc", "w")))
756 if (!(fbeleg = fopen(cbeleg, "w")))
761 if (!(fcheck = fopen(ccheck, "w")))
765 if (!(buf = (char *)malloc (512)))
771 bzero(valA, sizeof(valA));
772 control_nextline ((void *)line, 100, fcontrol);
773 ident = extract_ident(line);
774 if (!strcmp(ident, "begin") && (line[0] == '{')) {
775 fprintf(fbeleg, "\n\n");
776 fprintf(fbeleg, "\n Begleitzettel\n\n");
777 fprintf(fbeleg, "\n Belegloser Datentraegeraustausch\n\n");
778 fprintf(fbeleg, "\n Sammeleinziehungsauftrag\n\n");
779 fprintf(fbeleg, "\n VOL ........................:\n");
780 fprintf(fbeleg, "\n Erstellungsdatum ...........: %s\n", get_date());
782 control_nextline ((void *)line, 100, fcontrol);
783 while (strlen(line) && line[0] != '}') {
784 ident = extract_ident(line);
785 if ((recindex = rec_index(ident, REC_A)) != -1)
786 if (recA[recindex].type != IGN)
787 if ((valA[recindex] = (char *)malloc (strlen(line)+1)))
788 strcpy(valA[recindex], line);
789 control_nextline ((void *)line, 100, fcontrol);
791 if (!dtaus_writeA(fdtaus, valA)) {
792 fprintf (stderr, "Konnte den Anfangsdatensatz nicht schreiben.\n");
796 fprintf (fcheck, "\n\n\n");
797 fprintf (fcheck, " Sammeleinzeiehungsauftrag\n\n");
798 fprintf (fcheck, " Erstellungsdatum : %s\n\n\n", get_date());
799 fprintf (fcheck, " %-10s %-8s %-30s %12s\n", "Kontonr.", "BLZ", "Name", "Betrag");
800 fprintf (fcheck, " --------------------------------------------------------------------\n");
802 fprintf (stderr, "Datei fängt nicht mit dem Anfangsdatensatz (BEGIN) an.\n");
810 sum_val = bigint_int(0);
811 sum_blz = bigint_int(0);
812 sum_kto = bigint_int(0);
813 bzero(valC, sizeof(valC));
814 control_nextline ((void *)line, 100, fcontrol);
815 if (line[0] == '{') {
816 while (strlen(line) && line[0] == '{') {
817 control_nextline ((void *)line, 100, fcontrol);
818 while (strlen(line) && line[0] != '}') {
819 ident = extract_ident(line);
820 if ((recindex = rec_index(ident, REC_C)) != -1)
821 if (recC[recindex].type != IGN)
822 if ((valC[recindex] = (char *)malloc (strlen(line)+1)))
823 strcpy(valC[recindex], line);
824 control_nextline ((void *)line, 100, fcontrol);
826 if (!dtaus_writeC(fdtaus, valA, valC)) {
827 fprintf (stderr, "Konnte den regulären Datensatz nicht schreiben.\n");
831 bi = bigint_string(real2string(valC[C_VAL])); sum_val = bigint_add(sum_val, bi);
832 bi = bigint_string(valC[C_BLZ]); sum_blz = bigint_add(sum_blz, bi);
833 bi = bigint_string(valC[C_KTO]); sum_kto = bigint_add(sum_kto, bi);
835 fprintf (fcheck, " %10s %8s %-30s %12s\n", valC[C_KTO], valC[C_BLZ], valC[C_NAME], valC[C_VAL]);
836 for (recindex=0; recindex<C_LEN; recindex++)
838 free(valC[recindex]);
839 bzero(valC, sizeof(valC));
840 control_nextline ((void *)line, 100, fcontrol);
843 fprintf (stderr, "Kein regulärer Datensatz?\n");
850 dtaus_writeE(fdtaus, count, sum_val, sum_blz, sum_kto);
851 fprintf (fcheck, " --------------------------------------------------------------------\n");
852 bigint_sprintf (tmp, "%s", sum_val);
853 fprintf (fbeleg, "\n Anzahl .....................: %d\n", count);
854 recindex=strlen(tmp);
855 tmp[recindex+1] = '\0';
856 tmp[recindex] = tmp[recindex-1];
857 tmp[recindex-1] = tmp[recindex-2];
858 tmp[recindex-2] = '.';
859 fprintf (fcheck, " %-52s %14s\n", "Summe", tmp);
860 fprintf (fbeleg, "\n Summe ......................: %s\n", tmp);
861 bigint_sprintf (tmp, "%s", sum_kto);
862 fprintf (fbeleg, "\n Kontrollsumme Kontonummern .: %s\n", tmp);
863 bigint_sprintf (tmp, "%s", sum_blz);
864 fprintf (fbeleg, "\n Kontrollsumme Bankleitzahlen: %s\n", tmp);
865 fprintf (fbeleg, "\n Unsere Kontonummer .........: %s\n", valA[A_KTO]);
866 fprintf (fbeleg, "\n Unsere Bankleitzahl ........: %s\n", valA[A_BLZ]);
867 fprintf (fbeleg, "\n\n\n\n\n __________________________________________________\n");
868 fprintf (fbeleg, " Ort, Datum Unterschrift\n");
869 for (recindex=0; recindex<A_LEN; recindex++)
871 free(valA[recindex]);