2 dtaus.c - Belegloser Datenträgeraustausch mit einer Bank
3 Copyright (c) 1996,8,2001-4,5,9 Martin Schulze <joey@infodrom.org>
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
40 * First: Some control structures
46 unsigned short int pos;
47 unsigned short int len;
59 dtaus_record recA[] = {
61 {"Name", 23, 27, REQ},
62 {"Konto", 60, 10, REQ},
64 {"Referenz", 70, 10, OPT},
65 {"Datum", 50, 6, IGN},
66 {"Ausfuehrung", 95, 8, OPT},
67 {"Currency", 127, 1, OPT},
68 {"Euro", 127, 1, OPT},
86 dtaus_record recC[] = {
87 {"Name", 93, 27, REQ},
88 {"Konto", 21, 10, REQ},
90 {"Transaktion", 44, 5, REQ},
92 {"Betrag", 50, 11, REQ},
94 {"Betrag", 79, 11, REQ},
96 {"Zweck", 155, 27, REQ},
97 {"myName", 128, 27, OPT},
98 {"myKonto", 69, 10, OPT},
99 {"myBLZ", 61, 8, OPT},
100 {"Text", 187, 29, OPT},
101 {"Extension", 216, 29, OPT},
102 {"Currency", 182, 1, IGN},
104 {"Betrag-Euro", 79, 11, IGN},
106 {"Betrag-DM", 50, 11, IGN},
133 dtaus_record recE[] = {
134 {"Anzahl", 10, 7, IGN},
136 {"Summe", 17, 13, IGN},
138 {"Summe", 64, 13, IGN},
140 {"Kontos", 30, 17, IGN},
141 {"BLZs", 47, 17, IGN},
143 {"Summe-Euro", 64, 13, IGN},
145 {"Summe-DM", 17, 13, IGN},
162 * Second: Some low level routines
165 size_t dtaus_nextrec (void **buf, FILE *f)
167 memset (buf, 0, 128);
168 return fread(buf, 128, 1, f);
171 char *upcase(char *s)
177 for (cp=s,xp=x; *cp; cp++,xp++) {
178 if (strchr(" 0123456789.,&-/+*$%ABCDEFGHIJKLMNOPQRSTUVWXYZ",*cp)) {
180 } else if (strchr("abcdefghijklmnopqrstuvwxyz",*cp)) {
182 } else if (strchr ("üÜäÄöÖß", *cp)) {
184 case 'ü': *(xp++) = 'U'; *xp='E'; break;
185 case 'Ü': *(xp++) = 'U'; *xp='E'; break;
186 case 'ä': *(xp++) = 'A'; *xp='E'; break;
187 case 'Ä': *(xp++) = 'A'; *xp='E'; break;
188 case 'ö': *(xp++) = 'O'; *xp='E'; break;
189 case 'Ö': *(xp++) = 'O'; *xp='E'; break;
190 case 'ß': *(xp++) = 'S'; *xp='S'; break;
194 * Filter out all other characters since credit institutes won't
195 * accept the file otherwise.
204 char *downcase(char *s)
209 memset (x, 0, sizeof (x));
218 char *strip_spaces (char *s)
223 for (i=strlen(s);(s[i-1] == ' '||s[i-1] == '\t')&&i>0; i--)
225 for (p=s; *p==' '; p++);
229 char *strip_zeros (char *s)
233 for (p=s; *p=='0'; p++);
237 char *strip_nondigits (char *s)
250 char dtaus_char (void *buf, unsigned int pos)
253 char *bufp = (char *)buf;
256 memcpy(&res, bufp, 1);
260 char *string2real(char *s)
266 strncpy(res, s, strlen(s)-2);
267 res[strlen(s)-2] = '.';
268 res[strlen(s)-1] = s[strlen(s)-2];
269 res[strlen(s)] = s[strlen(s)-1];
273 char *real2string(char *s)
277 char *xp; /* only to avoid a GCC warning */
279 strncpy(res, s, sizeof(res)-1);
280 res[sizeof(res)-1] = 0;
281 for (cp=res; *cp&&!(*cp == ',')&&!(*cp == '.');cp++);
283 if ((cp-res) >= (sizeof(res)-3)) {
284 /* Bail out, since the number is too large, shouldn't be possible though. */
285 fprintf (stderr, "Value %s too large.\n", res);
289 if (*cp == '.' || *cp == ',') {
291 /* 1st decimal place */
293 if (*xp && isdigit(*xp))
297 /* 2nd decimal place */
299 if (*xp && isdigit(*xp))
316 * Return the last digit of the year as character
318 char get_date_lastdigit()
323 timer = time ( NULL );
324 loctime = localtime(&timer);
325 return loctime->tm_year % 10 + '0';
328 char *string2trans (char *s)
333 if (!strcmp(s, "04000"))
334 sprintf (res, "Abbuchung");
335 else if (!strcmp(s, "05000"))
336 sprintf (res, "Einzug");
337 else if (!strcmp(s, "05005"))
338 sprintf (res, "E-Cash");
339 else if (!strcmp(s, "05006"))
340 sprintf (res, "E-Cash-A");
341 else if (!strcmp(s, "51000"))
342 sprintf (res, "Gutschrift");
343 else if (!strcmp(s, "53000"))
344 sprintf (res, "Lohn");
345 else if (!strncmp(s, "5400", 4))
346 sprintf (res, "Vermögen %c",s[4]);
347 /* else if (!strcmp(s, "56000"))
348 sprintf (res, ""); / * Überweisung öffentlicher Kassen */
352 char *trans2string (char *s)
358 if (!strcmp(s, "Abbuchung"))
359 sprintf (res, "04000");
360 else if (!strcmp(s, "Einzug"))
361 sprintf (res, "05000");
362 else if (!strcmp(s, "E-Cash"))
363 sprintf (res, "05005");
364 else if (!strcmp(s, "E-Cash-A"))
365 sprintf (res, "05006");
366 else if (!strcmp(s, "Gutschrift"))
367 sprintf (res, "51000");
368 else if (!strcmp(s, "Lohn"))
369 sprintf (res, "53000");
372 if (!strncmp(s, "Vermögen", 8))
374 if (!strncmp(s, "Vermoegen", 9))
378 fprintf (stderr, "Unbekannte Transaktion `%s'\n", res);
383 Vermögen --> 5400<heutiges Jahr>
384 Vermögen 8 -> 5400<8>
385 Vermögen 2003 -> 5400<3>
388 if (*cp) while (!isspace(*cp)) cp++;
389 while (isspace(*cp)) cp++;
391 if (!*cp || !isdigit(*cp))
392 sprintf (res, "5400%c",get_date_lastdigit());
394 while (isdigit(*cp)) cp++;
395 sprintf (res, "5400%c",*(cp-1));
398 /* else if (!strcmp(s, ""))
399 sprintf (res, "56000"); / * Überweisung öffentlicher Kassen */
403 char *string2ext (char *s)
408 if (!strcmp(s, "01"))
409 sprintf (res, "Kunde");
410 else if (!strcmp(s, "02"))
411 sprintf (res, "Text");
412 else if (!strcmp(s, "03"))
413 sprintf (res, "Auftraggeber");
417 char *ext2string (char *s)
422 if (!strcmp(s, "Kunde"))
424 else if (!strcmp(s, "Text"))
426 else if (!strcmp(s, "Auftraggeber"))
431 unsigned long int dtaus_int(void *buf, unsigned int pos, unsigned int len)
434 char *bufp = (char *)buf;
435 static unsigned long int res;
438 memcpy(tmp, bufp, len);
440 sscanf(tmp, "%lu", &res);
445 * returns the first word in this line, returns it and shortens the
448 char *extract_ident (char *line)
451 static char word[30];
453 if (strlen(line) > 0) {
454 x = strchr (line, ' ');
455 y = strchr (line, '\t');
458 strncpy(word, downcase(line), 29);
463 /* Check which index returns the lower value, and check if the
465 if ((c = (x && x<y)?x:(y?y:x))) {
466 strncpy(word, line, c - line);
468 for (;*c == '\t' || *c == ' '; c++);
469 for (x=line; *c; c++,x++)
472 strcpy(word, downcase(word));
481 * Pads a string with zero's on the left side.
483 char *padzeroclip (char *s, int len)
487 if (strlen(s) == len) return s;
488 if (strlen(s) > len) {
496 for (p=s+strlen(s)-1;p>=s;p--)
498 for (;q>=s;) *(q--)='0';
502 int rec_index(char *ident, int type)
505 dtaus_record *rec = NULL;
508 case REC_A: rec = recA; break;
509 case REC_C: rec = recC; break;
510 case REC_E: rec = recE; break;
513 for (i=0; (rec[i].name); i++) {
514 if (!strcmp(ident, downcase(rec[i].name)))
521 size_t control_nextline (void **buf, int len, FILE *f)
528 memset (line, 0, sizeof(line));
529 memset (buf, 0, len);
532 while (!strlen(line) && (cp = fgets(line, 100, f))) {
534 if (line[0] != '#') {
535 if (line[strlen(line)-1] != '\n') {
537 while (tmp[strlen(tmp)-1] != '\n' && (cp = fgets(tmp, 100, f)));
539 line[strlen(line)-1] = '\0';
540 if (line[strlen(line)-1] == '\r')
541 line[strlen(line)-1] = '\0';
542 for (i=strlen(line);(line[i-1] == ' '||line[i-1] == '\t')&&i>0; i--)
548 for (cp=line; *cp==' '; cp++);
551 memcpy(buf, cp, strlen(cp));
558 * Return the current date nicely formatted
566 timer = time ( NULL );
567 loctime = localtime(&timer);
568 sprintf(res, "%02d.%02d.%02d", loctime->tm_mday, loctime->tm_mon+1, loctime->tm_year % 100);
573 * Prepare a record A according to the specs.
574 * See dtaus.txt for explanation
576 void dtaus_prepareA (char *buf)
583 memset (buf, 0, 129);
584 timer = time ( NULL );
585 loctime = localtime(&timer);
592 for (i=15;i<15+8; i++) buf[i] = '0'; /* A5 */
593 sprintf(tmp, "%02d%02d%02d", loctime->tm_mday, loctime->tm_mon+1, loctime->tm_year % 100);
594 for (i=0; i<6; i++) buf[50+i] = tmp[i]; /* A7 (Date) */
595 for (i=56;i<56+4; i++) buf[i] = ' '; /* A8 */
596 for (i=70;i<70+10; i++) buf[i] = '0'; /* A10 */
597 for (i=80;i<80+48; i++) buf[i] = ' '; /* A11 */
599 buf[recA[A_CURR].pos] = '1'; /* A12 (Currency) */
601 buf[recA[A_CURR].pos] = ' '; /* A12 (Currency) */
605 * Prepare a record C according to the specs.
606 * See dtaus.txt for explanation
608 void dtaus_prepareC (char *buf, int normaltext, int maxtext)
613 memset (buf, 0, 257);
618 i = 187 + (appendix * 29);
620 /* Bail out if the number is too large, shouldn't be possible though */
624 buf[0] = (i/1000)+48;i-=(i/1000)*100;
625 buf[1] = (i/100)+48;i-=(i/100)*100;
626 buf[2] = (i/10)+48;i-=(i/10)*10;
630 for (i=31;i<31+13; i++) buf[i] = '0'; /* C6 */
631 buf[49] = ' '; /* C8 */
632 for (i=50;i<50+11; i++) buf[i] = '0'; /* C9 (Betrag) */
633 for (i=79;i<79+11; i++) buf[i] = '0'; /* C12 (Betrag Euro) */
634 for (i=90;i<90+3; i++) buf[i] = ' '; /* C13 */
635 for (i=93;i<90+27; i++) buf[i] = ' '; /* C14a (Kunde) */
636 for (i=120;i<120+8; i++) buf[i] = ' '; /* C14b */
638 buf[recC[C_EURO].pos] = '1'; /* C17a (Currency) */
640 buf[recC[C_EURO].pos] = ' '; /* C17a (Currency) */
641 for (i=183;i<183+2; i++) buf[i] = ' '; /* C17b */
642 for (i=187;i<187+(29*2); i++) buf[i] = ' '; /* C19-C22 (misc text) */
643 for (i=245;i<245+11; i++) buf[i] = ' '; /* C23 */
645 buf[185+0] = (appendix/10)+48;appendix-=(appendix/10)*10;
646 buf[185+1] = appendix+48;
650 * Prepare a record E according to the specs.
651 * See dtaus.txt for explanation
653 void dtaus_prepareE (char *buf)
657 memset (buf, 0, 129);
663 for (i=5;i<5+5; i++) buf[i] = ' '; /* E3 */
664 for (i=17;i<17+13; i++) buf[i] = '0'; /* E8 (Check Betrag) */
665 for (i=64;i<64+13; i++) buf[i] = '0'; /* E8 (Check Euro) */
666 for (i=77;i<77+51; i++) buf[i] = ' '; /* E9 */
669 int dtaus_writeA(FILE *f, char **values)
675 for (i=0; (recA[i].name); i++)
676 if ((recA[i].type == REQ) && !values[i]) {
677 fprintf (stderr, "Anfangsdatensatz ist nicht vollständig, kein %s.\n", recA[i].name);
680 if (!(((values[A_TRANS][0] == 'L')||(values[A_TRANS][0] == 'G'))
681 &&((values[A_TRANS][1] == 'B')||(values[A_TRANS][1] == 'K')))) {
682 fprintf (stderr, "Ungültiger Typ, nur LK, GK, LB oder GB erlaubt.\n");
686 i=A_NAME;if (values[i] && strlen(values[i]) > recA[i].len)
687 values[i][recA[i].len] = '\0';
690 buf[5] = values[A_TRANS][0];
691 buf[6] = values[A_TRANS][1];
692 sprintf (tmp, "%s", padzeroclip (strip_nondigits (values[A_BLZ]),recA[A_BLZ].len));
693 for (i=0; i<recA[A_BLZ].len; i++) buf[recA[A_BLZ].pos+i] = tmp[i];
694 sprintf (tmp, "%-27.27s", upcase(values[A_NAME]));
695 for (i=0; i<27; i++) buf[recA[A_NAME].pos+i] = tmp[i];
696 if (values[A_TODO]) {
697 sprintf (tmp, "%s", padzeroclip (strip_nondigits (values[A_TODO]),recA[A_TODO].len));
698 for (i=0; i<recA[A_TODO].len; i++) buf[recA[A_TODO].pos+i] = tmp[i];
700 sprintf (tmp, "%s", padzeroclip (strip_nondigits (values[A_KTO]),recA[A_KTO].len));
701 for (i=0; i<recA[A_KTO].len; i++) buf[recA[A_KTO].pos+i] = tmp[i];
704 sprintf (tmp, "%s", padzeroclip(strip_nondigits (values[A_REF]), recA[A_REF].len));
705 for (i=0; i < recA[A_REF].len; i++) buf[recA[A_REF].pos+i] = tmp[i];
712 int dtaus_writeC(FILE *f, char **valuesA, char **values, char **text)
722 if (text) for (maxtext=0;text[maxtext];maxtext++);
725 for (i=0; (recC[i].name); i++)
727 printf ("%s: %s\n", recC[i].name, values[i]);
730 for (i=0; (recC[i].name); i++)
731 if ((recC[i].type == REQ) && !values[i]) {
732 fprintf (stderr, "Datensatz ist nicht vollständig, kein %s.\n", recC[i].name);
735 sprintf (tmp, "%s", trans2string(values[C_TRANS]));
737 fprintf (stderr, "Ungültige Transaktion, nur Abbuchung, Einzug, E-Cash, E-Cash-A, Gutschrift und Lohn erlaubt.\n");
741 i=C_TEXT;if (values[i] && strlen(values[i]) > recC[i].len)
742 values[i][recC[i].len-2] = '\0';
743 i=C_ZWECK;if (values[i] && strlen(values[i]) > recC[i].len)
744 values[i][recC[i].len] = '\0';
745 i=C_MYNAM;if (values[i] && strlen(values[i]) > recC[i].len)
746 values[i][recC[i].len] = '\0';
747 i=C_TEXT;if (values[i] && strlen(values[i]) > recC[i].len)
748 values[i][recC[i].len] = '\0';
750 dtaus_prepareC (buf, values[C_TEXT] != NULL, maxtext);
751 for (i=0; i<5; i++) buf[recC[C_TRANS].pos+i] = tmp[i];
753 sprintf (tmp, "%s", padzeroclip (strip_nondigits (values[C_MYBLZ]),8));
755 sprintf (tmp, "%s", padzeroclip (strip_nondigits (valuesA[A_BLZ]),8));
756 for (i=0; i<recC[C_MYBLZ].len; i++) buf[5+i] = tmp[i];
757 for (i=0; i<recC[C_MYBLZ].len; i++) buf[recC[C_MYBLZ].pos+i] = tmp[i];
758 sprintf (tmp, "%s", padzeroclip (strip_nondigits (values[C_BLZ]),8));
759 for (i=0; i<recC[C_BLZ].len; i++) buf[recC[C_BLZ].pos+i] = tmp[i];
760 sprintf (tmp, "%s", padzeroclip (strip_nondigits (values[C_KTO]),10));
761 for (i=0; i<recC[C_KTO].len; i++) buf[recC[C_KTO].pos+i] = tmp[i];
762 sprintf (tmp, "%s", padzeroclip (real2string(values[C_VAL]),11));
765 for (i=0; i<recC[C_VAL].len; i++) buf[recC[C_VAL].pos+i] = tmp[i];
767 for (i=0; i<recC[C_EUR].len; i++) buf[recC[C_EUR].pos+i] = tmp[i];
770 for (i=0; i<recC[C_VAL].len; i++) buf[recC[C_VAL].pos+i] = tmp[i];
772 for (i=0; i<recC[C_DM].len; i++) buf[recC[C_DM].pos+i] = tmp[i];
775 sprintf (tmp, "%s", padzeroclip (strip_nondigits (values[C_MYKTO]),10));
777 sprintf (tmp, "%s", padzeroclip (strip_nondigits (valuesA[A_KTO]),10));
778 for (i=0; i<recC[C_MYKTO].len; i++) buf[recC[C_MYKTO].pos+i] = tmp[i];
779 sprintf (tmp, "%-27.27s", upcase(values[C_NAME]));
780 for (i=0; i<recC[C_NAME].len; i++) buf[recC[C_NAME].pos+i] = tmp[i];
782 sprintf (tmp, "%-27.27s", upcase(values[C_MYNAM]));
784 sprintf (tmp, "%-27.27s", upcase(valuesA[A_NAME]));
785 for (i=0; i<recC[C_MYNAM].len; i++) buf[recC[C_MYNAM].pos+i] = tmp[i];
786 sprintf (tmp, "%-27.27s", upcase(values[C_ZWECK]));
787 for (i=0; i<recC[C_ZWECK].len; i++) buf[recC[C_ZWECK].pos+i] = tmp[i];
789 if (values[C_TEXT]) {
790 buf[recC[C_TEXT].pos+0] = '0';
791 buf[recC[C_TEXT].pos+1] = '2';
792 sprintf (tmp, "%-27.27s", upcase(values[C_TEXT]));
793 for (i=0; i<recC[C_TEXT].len-2; i++) buf[recC[C_TEXT].pos+2+i] = tmp[i];
797 buf[recC[C_EXT].pos+0] = '0';
798 buf[recC[C_EXT].pos+1] = '2';
799 sprintf (tmp, "%-27.27s", upcase(text[0]));
800 for (i=0; i<recC[C_EXT].len-2; i++) buf[recC[C_EXT].pos+2+i] = tmp[i];
805 if (text && maxtext > 1) {
807 while (fieldnr<maxtext) {
808 memset (appendix, ' ', 128);
809 appendix[128] = '\0';
810 for (k=0; k<4 && (fieldnr+k)<maxtext; k++) {
811 sprintf (tmp, "%-27.27s", upcase(text[fieldnr+k]));
812 appendix[k*29] = '0';
813 appendix[(k*29)+1] = '2';
814 for (i=0; i<recC[C_TEXT].len-2; i++) appendix[(k*29)+2+i] = tmp[i];
824 int dtaus_writeE(FILE *f, int count, bigint sum, bigint blz, bigint kto)
832 sprintf (tmp, "%07d", count);
833 for (i=0; i<recE[E_COUNT].len; i++) buf[recE[E_COUNT].pos+i] = tmp[i];
834 bigint_sprintf (tmp, "%s", sum);
835 padzeroclip (tmp,13);
838 for (i=0; i<recE[E_VAL].len; i++) buf[recE[E_VAL].pos+i] = tmp[i];
840 for (i=0; i<recE[E_EUR].len; i++) buf[recE[E_EUR].pos+i] = tmp[i];
843 for (i=0; i<recE[E_VAL].len; i++) buf[recE[E_VAL].pos+i] = tmp[i];
845 for (i=0; i<recE[E_DM].len; i++) buf[recE[E_DM].pos+i] = tmp[i];
847 bigint_sprintf (tmp, "%s", kto);
848 padzeroclip (tmp,17);
849 for (i=0; i<recE[E_KTO].len; i++) buf[recE[E_KTO].pos+i] = tmp[i];
850 bigint_sprintf (tmp, "%s", blz);
851 padzeroclip (tmp,17);
852 for (i=0; i<recE[E_BLZ].len; i++) buf[recE[E_BLZ].pos+i] = tmp[i];
858 void printctln(FILE *f, char *field, char *value)
860 if (strlen(field) && strlen(value))
861 fprintf(f, " %s\t%s\n", field, value);
865 * one date line, format it properly
867 void printctlndate(FILE *f, char *field, char *value)
872 if (!strlen(field) || !strlen(value))
875 for (i=0;isspace (value[i]) && i<= strlen (value); i++);
876 if (i == strlen (value))
879 memset (mydate, 0, sizeof (mydate));
880 if (strlen (value) == 6) {
881 mydate[0] = value[0];
882 mydate[1] = value[1];
884 mydate[3] = value[2];
885 mydate[4] = value[3];
887 mydate[6] = value[4];
888 mydate[7] = value[5];
889 fprintf(f, " %s\t%s\n", field, mydate);
890 } else if (strlen (value) == 8) {
891 mydate[0] = value[0];
892 mydate[1] = value[1];
894 mydate[3] = value[2];
895 mydate[4] = value[3];
897 mydate[6] = value[4];
898 mydate[7] = value[5];
899 mydate[8] = value[6];
900 mydate[9] = value[7];
901 fprintf(f, " %s\t%s\n", field, mydate);
903 fprintf (stderr, "Broken date field: %s\n", value);
904 fprintf(f, " %s\t%s\n", field, value);
910 * Third: Some high level routines
913 void dtaus2control (char *cdtaus, char *ccontrol)
915 FILE *fdtaus, *fcontrol;
925 if (!(fdtaus = fopen("DTAUS0.TXT", "r")))
926 if (!(fdtaus = fopen("dtaus0.txt", "r")))
929 if (!strcmp (cdtaus, "-"))
932 if (!(fdtaus = fopen(cdtaus, "r")))
938 if (!strcmp (ccontrol, "-"))
941 if (!(fcontrol = fopen(ccontrol, "w")))
943 if ((buf = (char *)malloc (512)) == NULL)
949 if (dtaus_nextrec((void *)buf, fdtaus) == 1) {
950 if (dtaus_char(buf,4) == 'A') {
951 fprintf(fcontrol, "BEGIN {\n");
954 for (index=A_TRANS; index < A_LOOP; index++) {
955 bufp = buf + recA[index].pos;
956 memcpy(tmp, bufp, recA[index].len); tmp[recA[index].len] = '\0';
957 if (index == A_DATE || index == A_TODO)
958 printctlndate(fcontrol, recA[index].name, tmp);
960 printctln(fcontrol, recA[index].name, strip_zeros(strip_spaces(tmp)));
963 bufp = buf + recA[A_CURR].pos;
966 fprintf(fcontrol, " Euro\n");
969 fprintf(fcontrol, " DM\n");
972 fprintf(fcontrol, "}\n\n");
974 fprintf (stderr, "Datei fängt nicht mit dem Anfangsdatensatz an.\n");
978 fprintf (stderr, "Der Anfangsdatensatz ist kaputt.\n");
985 if (dtaus_nextrec((void *)buf, fdtaus) == 1) {
986 while (dtaus_char(buf,4) == 'C') {
988 if (dtaus_nextrec((void *)bufp, fdtaus) != 1) {
989 fprintf (stderr, "Der zweite Teil der Transaktion ist kaputt.\n");
992 fprintf(fcontrol, "{\n");
994 for (index=C_NAME; index < C_LOOP; index++) {
995 if (index == C_TEXT || index == C_EXT)
998 if (use_euro && index == C_VAL)
1001 if (!use_euro && index == C_VAL)
1004 bufp = buf + recC[index].pos;
1005 memcpy(tmp, bufp, recC[index].len); tmp[recC[index].len] = '\0';
1008 * C_EUR and C_DM are outside of the loop, can only be
1009 * selected for the non-default currency, but the value
1010 * should be stored in the normal record field.
1012 #ifndef DEFAULT_EURO
1017 printctln(fcontrol, recC[C_VAL].name, strip_zeros(string2real(tmp)));
1018 else if (index == C_VAL)
1019 printctln(fcontrol, recC[index].name, strip_zeros(string2real(tmp)));
1020 else if (index == C_TRANS)
1021 printctln(fcontrol, recC[index].name, strip_zeros(string2trans(tmp)));
1023 printctln(fcontrol, recC[index].name, strip_zeros(strip_spaces(tmp)));
1024 #ifndef DEFAULT_EURO
1025 if (use_euro && index == C_EUR)
1028 if (!use_euro && index == C_DM)
1033 for (index=C_TEXT; index <= C_EXT; index++) {
1034 if (!(dtaus_char(buf,recC[index].pos) == ' ')) {
1035 bufp = buf + recC[index].pos;
1036 memcpy(x, bufp, 2); tmp[2] = '\0'; bufp+=2;
1037 memcpy(tmp, bufp, recC[index].len-2); tmp[recC[index].len-2] = '\0';
1038 printctln(fcontrol, string2ext(x), strip_spaces(tmp));
1042 /* Number of extension records for this C record */
1043 extC = dtaus_int(buf, 185, 2);
1046 res = div (extC, 4);
1048 if (res.rem) extC++;
1051 if (dtaus_nextrec((void *)buf, fdtaus) != 1)
1052 memset (buf, 0, sizeof(buf));
1055 * Are there extension records that we have to check?
1057 * 2nd half of the AND is wrong, but since dtaus < 0.5 wrote 01
1058 * instead of 00 we should let it in so it can read its own
1059 * old files... *sigh*
1061 while (extC > 0 && dtaus_char(buf,4) != 'C' && dtaus_char(buf,4) != 'E') {
1062 for (index=0; index < 4; index++) {
1063 if ((dtaus_char(buf,index*29) != ' ')) {
1064 bufp = buf + index*29;
1065 memcpy(x, bufp, 2); tmp[2] = '\0'; bufp+=2;
1066 memcpy(tmp, bufp, recC[C_TEXT].len-2); tmp[recC[C_TEXT].len-2] = '\0';
1067 printctln(fcontrol, string2ext(x), strip_spaces(tmp));
1070 if (dtaus_nextrec((void *)buf, fdtaus) != 1)
1071 memset (buf, 0, sizeof(buf));
1074 fprintf(fcontrol, "}\n");
1080 * (gelesen ist er eigentlich schon...)
1082 if (dtaus_char(buf,4) == 'E') {
1083 if (dtaus_char(buf,4) == 'E') {
1084 fprintf(fcontrol, "\nEND {\n");
1086 for (index=E_COUNT; index <= E_BLZ; index++) {
1087 #ifndef DEFAULT_EURO
1088 if (use_euro && index == E_VAL)
1091 if (!use_euro && index == E_VAL)
1095 bufp = buf + recE[index].pos;
1096 memcpy(tmp, bufp, recE[index].len); tmp[recE[index].len] = '\0';
1098 #ifndef DEFAULT_EURO
1099 if (index == E_VAL || index == E_EUR)
1101 if (index == E_VAL || index == E_DM)
1103 printctln(fcontrol, recE[E_VAL].name, strip_zeros(string2real(tmp)));
1105 printctln(fcontrol, recE[index].name, strip_zeros(tmp));
1106 #ifndef DEFAULT_EURO
1107 if (use_euro && index == E_EUR)
1110 if (!use_euro && index == E_DM)
1115 fprintf(fcontrol, "}\n");
1117 fprintf (stderr, "Das ist kein Abschlußdatensatz.\n");
1121 fprintf (stderr, "Der Abschlußdatensatz ist leer oder kaputt.\n");
1128 int control2dtaus (char *ccontrol, char *cdtaus, char *cbeleg, char *ccheck, char *latex)
1130 FILE *fdtaus, *fcontrol, *fbeleg, *fcheck;
1135 char *valA[A_LEN], *valC[C_LEN];
1137 bigint sum_val, sum_blz, sum_kto, bi;
1143 char *currency = NULL;
1145 char ssum_val[30], ssum_kto[30], ssum_blz[30];
1148 if (!(fdtaus = fopen("dtaus0.txt", "w")))
1151 if (!strcmp (cdtaus, "-"))
1154 if (!(fdtaus = fopen(cdtaus, "w")))
1158 if (!(fcontrol = fopen("dtaus0.ctl", "r")))
1159 if (!(fcontrol = fopen("DTAUS0.CTL", "r")))
1162 if (!strcmp (ccontrol, "-"))
1165 if (!(fcontrol = fopen(ccontrol, "r")))
1169 if (!(fbeleg = fopen("dtaus0.doc", "w")))
1172 if (!(fbeleg = fopen(cbeleg, "w")))
1178 if (!(fcheck = fopen(ccheck, "w")))
1182 if ((buf = (char *)malloc (512)) == NULL)
1188 memset (valA, 0, sizeof(valA));
1189 control_nextline ((void *)line, 100, fcontrol);
1190 ident = extract_ident(line);
1191 if (!strcmp(ident, "begin") && (line[0] == '{')) {
1192 control_nextline ((void *)line, 100, fcontrol);
1193 while (strlen(line) && line[0] != '}') {
1194 ident = extract_ident(line);
1195 if ((recindex = rec_index(ident, REC_A)) != -1)
1197 if (recA[recindex].type != IGN)
1198 if ((valA[recindex] = (char *)malloc (strlen(line)+1)) != NULL)
1199 strcpy(valA[recindex], line);
1201 if (! strcasecmp (ident, "euro"))
1203 if (! strcasecmp (ident, "dm"))
1206 control_nextline ((void *)line, 100, fcontrol);
1208 if (((recindex = rec_index("art", REC_A)) != -1) && valA[recindex] && strlen(valA[recindex])) {
1209 if (valA[recindex][0] == 'L')
1210 type = strdup ("Sammeleinziehungsauftrag");
1211 else if (valA[recindex][0] == 'G')
1212 type = strdup ("Sammelueberweisungsauftrag");
1214 type = strdup ("Sammelauftrag");
1217 currency = strdup ("Euro");
1219 currency = strdup ("DM");
1222 sprintf (date_todo, valA[A_TODO]);
1224 memset (date_todo, 0, sizeof (date_todo));
1226 fprintf(fbeleg, "\n\n");
1227 fprintf(fbeleg, "\n Begleitzettel\n\n");
1228 fprintf(fbeleg, "\n Belegloser Datentraegeraustausch\n\n");
1229 fprintf(fbeleg, "\n %s\n\n", type);
1230 fprintf(fbeleg, "\n VOL ........................:\n");
1231 fprintf(fbeleg, "\n Erstellungsdatum ...........: %s\n", get_date());
1232 if (strlen(date_todo)) {
1233 fprintf(fbeleg, "\n Ausfuehrungsdatum ..........: %s\n", date_todo);
1235 fprintf(fbeleg, "\n Waehrung ...................: %s\n", currency);
1237 if (!dtaus_writeA(fdtaus, valA)) {
1238 fprintf (stderr, "Konnte den Anfangsdatensatz nicht schreiben.\n");
1242 fprintf (fcheck, "\n\n\n");
1243 if (valA[recindex][0] == 'L')
1244 fprintf (fcheck, " Sammeleinziehungsauftrag\n\n");
1245 else if (valA[recindex][0] == 'G')
1246 fprintf (fcheck, " Sammelueberweisungsauftrag\n\n");
1248 fprintf (fcheck, " Sammelauftrag\n\n");
1249 fprintf (fcheck, " Erstellungsdatum : %s\n\n", get_date());
1251 fprintf (fcheck, " Waehrung : Euro\n\n\n");
1253 fprintf (fcheck, " Waehrung : DM\n\n\n");
1254 fprintf (fcheck, " %-10s %-8s %-30s %12s\n", "Kontonr.", "BLZ", "Name", "Betrag");
1255 fprintf (fcheck, " --------------------------------------------------------------------\n");
1257 fprintf (stderr, "Datei fängt nicht mit dem Anfangsdatensatz (BEGIN) an.\n");
1265 sum_val = bigint_int(0);
1266 sum_blz = bigint_int(0);
1267 sum_kto = bigint_int(0);
1268 memset (valC, 0, sizeof(valC));
1269 control_nextline ((void *)line, 100, fcontrol);
1270 if (line[0] == '{') {
1271 while (strlen(line) && line[0] == '{') {
1272 control_nextline ((void *)line, 100, fcontrol);
1274 for (textindex=0; textindex < MAX_TEXT && text[textindex]; textindex++)
1275 free (text[textindex]);
1279 while (strlen(line) && line[0] != '}') {
1280 ident = extract_ident(line);
1281 if ((recindex = rec_index(ident, REC_C)) != -1)
1282 if (recC[recindex].type != IGN) {
1284 * Special exception to support multiple Text fields
1286 if (recindex == C_TEXT && valC[recindex]) {
1288 if ((text = (char **)malloc ((MAX_TEXT+1) * sizeof (char *))) == NULL)
1292 memset (text, 0, (MAX_TEXT+1) * sizeof (char *));
1295 if (textindex < MAX_TEXT) {
1296 if ((cp = (char *)malloc (strlen (line) + 1)) == NULL)
1299 cp[strlen (line)] = '\0';
1300 text[textindex++] = cp;
1304 if (recindex == C_VAL) {
1305 /* Convert commas to dots for later processing */
1306 for (i=0; line[i]; i++) if (line[i] == ',') line[i] = '.';
1308 if ((cp = strchr (line, '.')) == NULL) {
1309 if (strlen(line) > 9) {
1310 fprintf (stderr, "Betrag %s zu gross (max. 9 Stellen)\n", line);
1313 if ((valC[recindex] = (char *)malloc (strlen(line)+4)) == NULL)
1315 sprintf (valC[recindex], "%s.00", line);
1316 } else if ( ((len = cp - line + 3)) < strlen (line)) {
1317 if (cp - line > 9) {
1318 fprintf (stderr, "Betrag %s zu gross (max. 9.2 Stellen)\n", line);
1321 if ((valC[recindex] = (char *)malloc (len+1)) == NULL)
1323 strncpy (valC[recindex], line, len);
1324 valC[recindex][len] = '\0';
1326 if (cp - line > 9) {
1327 fprintf (stderr, "Betrag %s zu gross (max. 9.2 Stellen)\n", line);
1330 if ((valC[recindex] = (char *)malloc (strlen(line)+1)) == NULL)
1332 strcpy(valC[recindex], line);
1335 if ((valC[recindex] = (char *)malloc (strlen(line)+1)) == NULL)
1337 strcpy(valC[recindex], line);
1341 control_nextline ((void *)line, 100, fcontrol);
1343 if (!dtaus_writeC(fdtaus, valA, valC, text)) {
1344 fprintf (stderr, "Konnte den regulären Datensatz nicht schreiben.\n");
1348 bi = bigint_string(real2string(valC[C_VAL])); sum_val = bigint_add(sum_val, bi);
1349 bi = bigint_string(valC[C_BLZ]); sum_blz = bigint_add(sum_blz, bi);
1350 bi = bigint_string(valC[C_KTO]); sum_kto = bigint_add(sum_kto, bi);
1352 fprintf (fcheck, " %10s %8s %-30s %12s\n", valC[C_KTO], valC[C_BLZ], valC[C_NAME], valC[C_VAL]);
1353 for (recindex=0; recindex<C_LEN; recindex++)
1355 free(valC[recindex]);
1356 memset (valC, 0, sizeof(valC));
1357 control_nextline ((void *)line, 100, fcontrol);
1360 fprintf (stderr, "Kein regulärer Datensatz?\n");
1367 dtaus_writeE(fdtaus, count, sum_val, sum_blz, sum_kto);
1368 fprintf (fcheck, " --------------------------------------------------------------------\n");
1369 fprintf (fbeleg, "\n Anzahl .....................: %d\n", count);
1370 bigint_sprintf (ssum_val, "%s", sum_val);
1371 recindex=strlen(ssum_val);
1372 ssum_val[recindex+1] = '\0';
1373 ssum_val[recindex] = ssum_val[recindex-1];
1374 ssum_val[recindex-1] = ssum_val[recindex-2];
1375 ssum_val[recindex-2] = '.';
1376 fprintf (fcheck, " %-52s %14s\n", "Summe", ssum_val);
1377 fprintf (fbeleg, "\n Summe ......................: %s\n", ssum_val);
1378 bigint_sprintf (ssum_kto, "%s", sum_kto);
1379 fprintf (fbeleg, "\n Kontrollsumme Kontonummern .: %s\n", ssum_kto);
1380 bigint_sprintf (ssum_blz, "%s", sum_blz);
1381 fprintf (fbeleg, "\n Kontrollsumme Bankleitzahlen: %s\n", ssum_blz);
1382 fprintf (fbeleg, "\n Unsere Kontonummer .........: %s\n", valA[A_KTO]);
1383 fprintf (fbeleg, "\n Unsere Bankleitzahl ........: %s\n", valA[A_BLZ]);
1384 fprintf (fbeleg, "\n\n\n\n\n __________________________________________________\n");
1385 fprintf (fbeleg, " Ort, Datum Unterschrift\n");
1388 generate_latex_receipt (latex, type, get_date(), date_todo,
1390 ssum_val, ssum_kto, ssum_blz,
1391 valA[A_KTO], valA[A_BLZ]);
1393 for (recindex=0; recindex<A_LEN; recindex++)
1395 free(valA[recindex]);