Added a define to make some routines default to EUR, may be useful for
[infodrom/dtaus] / dtaus.c
diff --git a/dtaus.c b/dtaus.c
index 0a5602c..ed82a84 100644 (file)
--- a/dtaus.c
+++ b/dtaus.c
@@ -28,6 +28,8 @@
 #include <time.h>
 #include <malloc.h>
 
 #include <time.h>
 #include <malloc.h>
 
+/* #define DEFAULT_EURO */
+
 /*
  *  First: Some control structures
  */
 /*
  *  First: Some control structures
  */
@@ -98,7 +100,7 @@ dtaus_record recC[] = {
 #define C_EXT  11
 #define C_LEN  12
 
 #define C_EXT  11
 #define C_LEN  12
 
-#define MAX_TEXT 60
+#define MAX_TEXT 61
 
 dtaus_record recE[] = {
   {"Anzahl", 10, 7, IGN},
 
 dtaus_record recE[] = {
   {"Anzahl", 10, 7, IGN},
@@ -455,6 +457,10 @@ char *get_date()
   return res;
 }
 
   return res;
 }
 
+/*
+ *  Prepare a record A according to the specs.
+ *  See dtaus.txt for explanation
+ */
 void dtaus_prepareA (char *buf)
 {
   int i;
 void dtaus_prepareA (char *buf)
 {
   int i;
@@ -471,14 +477,23 @@ void dtaus_prepareA (char *buf)
   buf[2] = '2';
   buf[3] = '8';
   buf[4] = 'A';
   buf[2] = '2';
   buf[3] = '8';
   buf[4] = 'A';
-  for (i=15;i<15+8; i++) buf[i] = '0';
+  for (i=15;i<15+8; i++) buf[i] = '0';         /* A5 */
   sprintf(tmp, "%02d%02d%02d", loctime->tm_mday, loctime->tm_mon+1, loctime->tm_year % 100);
   sprintf(tmp, "%02d%02d%02d", loctime->tm_mday, loctime->tm_mon+1, loctime->tm_year % 100);
-  for (i=0; i<6; i++) buf[50+i] = tmp[i];
-  for (i=56;i<56+4; i++) buf[i] = ' ';
-  for (i=70;i<70+10; i++) buf[i] = '0';
-  for (i=80;i<80+48; i++) buf[i] = ' ';
+  for (i=0; i<6; i++) buf[50+i] = tmp[i];      /* A7 (Date) */
+  for (i=56;i<56+4; i++) buf[i] = ' ';         /* A8 */
+  for (i=70;i<70+10; i++) buf[i] = '0';                /* A10 */
+  for (i=80;i<80+48; i++) buf[i] = ' ';                /* A11 */
+#ifdef DEFAULT_EURO
+  buf[127] = '1';                              /* A12 (Currency) */
+#else
+  buf[127] = ' ';                              /* A12 (Currency) */
+#endif
 }
 
 }
 
+/*
+ *  Prepare a record C according to the specs.
+ *  See dtaus.txt for explanation
+ */
 void dtaus_prepareC (char *buf, int normaltext, int maxtext)
 {
   int i;
 void dtaus_prepareC (char *buf, int normaltext, int maxtext)
 {
   int i;
@@ -491,14 +506,14 @@ void dtaus_prepareC (char *buf, int normaltext, int maxtext)
   if (normaltext)
     appendix = 1;
   if (maxtext) {
   if (normaltext)
     appendix = 1;
   if (maxtext) {
-    res = div (maxtext, 4);
+    res = div (maxtext-1, 4);
     appendix += res.quot;
     if (res.rem) appendix++;
   }
   i = 187 + (appendix * 29);
 
   /* Bail out if the number is too large, shouldn't be possible though */
     appendix += res.quot;
     if (res.rem) appendix++;
   }
   i = 187 + (appendix * 29);
 
   /* Bail out if the number is too large, shouldn't be possible though */
-  if (i>1000)
+  if (i >= 1000)
     exit (1);
 
   buf[1] = (i/100)+48;i-=(i/100)*100;
     exit (1);
 
   buf[1] = (i/100)+48;i-=(i/100)*100;
@@ -506,17 +521,28 @@ void dtaus_prepareC (char *buf, int normaltext, int maxtext)
   buf[3] = i+48;
   buf[4] = 'C';
 
   buf[3] = i+48;
   buf[4] = 'C';
 
-  for (i=31;i<31+13; i++) buf[i] = '0';
-  buf[49] = ' ';
-  for (i=79;i<79+11; i++) buf[i] = '0';
-  for (i=90;i<90+3; i++) buf[i] = ' ';
-  for (i=120;i<120+8; i++) buf[i] = ' ';
-  for (i=182;i<182+3; i++) buf[i] = ' ';
-  for (i=185;i<185+2; i++) buf[i] = '0';
-  for (i=187;i<187+(29*2); i++) buf[i] = ' ';
-  for (i=245;i<245+11; i++) buf[i] = ' ';
+  for (i=31;i<31+13; i++) buf[i] = '0';                /* C6 */
+  buf[49] = ' ';                               /* C8 */
+  for (i=50;i<50+11; i++) buf[i] = '0';                /* C9 (Betrag) */
+  for (i=79;i<79+11; i++) buf[i] = '0';                /* C12 (Euro) */
+  for (i=90;i<90+3; i++) buf[i] = ' ';         /* C13 */
+  for (i=93;i<90+27; i++) buf[i] = ' ';                /* C14a (Kunde) */
+  for (i=120;i<120+8; i++) buf[i] = ' ';       /* C14b */
+#ifdef DEFAULT_EURO
+  buf[182] = '1';                              /* C17a (Currency) */
+#else
+  buf[182] = ' ';                              /* C17a (Currency) */
+#endif
+  for (i=183;i<183+2; i++) buf[i] = ' ';       /* C17b */
+  for (i=185;i<185+2; i++) buf[i] = '0';       /* C18 (#Extension) */
+  for (i=187;i<187+(29*2); i++) buf[i] = ' ';  /* C19-C22 (misc text) */
+  for (i=245;i<245+11; i++) buf[i] = ' ';      /* C23 */
 }
 
 }
 
+/*
+ *  Prepare a record E according to the specs.
+ *  See dtaus.txt for explanation
+ */
 void dtaus_prepareE (char *buf)
 {
   int i;
 void dtaus_prepareE (char *buf)
 {
   int i;
@@ -527,9 +553,10 @@ void dtaus_prepareE (char *buf)
   buf[2] = '2';
   buf[3] = '8';
   buf[4] = 'E';
   buf[2] = '2';
   buf[3] = '8';
   buf[4] = 'E';
-  for (i=5;i<5+5; i++) buf[i] = ' ';
-  for (i=64;i<64+13; i++) buf[i] = '0';
-  for (i=77;i<77+51; i++) buf[i] = ' ';
+  for (i=5;i<5+5; i++) buf[i] = ' ';   /* E3 */
+  for (i=17;i<17+13; i++) buf[i] = '0';        /* E8 (Check Betrag) */
+  for (i=64;i<64+13; i++) buf[i] = '0';        /* E8 (Check Euro) */
+  for (i=77;i<77+51; i++) buf[i] = ' ';        /* E9 */
 }
 
 int dtaus_writeA(FILE *f, char **values)
 }
 
 int dtaus_writeA(FILE *f, char **values)
@@ -557,7 +584,7 @@ int dtaus_writeA(FILE *f, char **values)
   buf[6] = values[A_TRANS][1];
   sprintf (tmp, "%s", padzeroclip (strip_nondigits (values[A_BLZ]),8));
   for (i=0; i<8; i++) buf[recA[A_BLZ].pos+i] = tmp[i];
   buf[6] = values[A_TRANS][1];
   sprintf (tmp, "%s", padzeroclip (strip_nondigits (values[A_BLZ]),8));
   for (i=0; i<8; i++) buf[recA[A_BLZ].pos+i] = tmp[i];
-  sprintf (tmp, "%-27s", upcase(values[A_NAME]));
+  sprintf (tmp, "%-27.27s", upcase(values[A_NAME]));
   for (i=0; i<27; i++) buf[recA[A_NAME].pos+i] = tmp[i];
   sprintf (tmp, "%s", padzeroclip (strip_nondigits (values[A_KTO]),10));
   for (i=0; i<10; i++) buf[recA[A_KTO].pos+i] = tmp[i];
   for (i=0; i<27; i++) buf[recA[A_NAME].pos+i] = tmp[i];
   sprintf (tmp, "%s", padzeroclip (strip_nondigits (values[A_KTO]),10));
   for (i=0; i<10; i++) buf[recA[A_KTO].pos+i] = tmp[i];
@@ -626,25 +653,30 @@ int dtaus_writeC(FILE *f, char **valuesA, char **values, char **text)
   else
     sprintf (tmp, "%s", padzeroclip (strip_nondigits (valuesA[A_KTO]),10));
   for (i=0; i<recC[C_MYKTO].len; i++) buf[recC[C_MYKTO].pos+i] = tmp[i];
   else
     sprintf (tmp, "%s", padzeroclip (strip_nondigits (valuesA[A_KTO]),10));
   for (i=0; i<recC[C_MYKTO].len; i++) buf[recC[C_MYKTO].pos+i] = tmp[i];
-  sprintf (tmp, "%-27s", upcase(values[C_NAME]));
+  sprintf (tmp, "%-27.27s", upcase(values[C_NAME]));
   for (i=0; i<recC[C_NAME].len; i++) buf[recC[C_NAME].pos+i] = tmp[i];
   if (values[C_MYNAM])
   for (i=0; i<recC[C_NAME].len; i++) buf[recC[C_NAME].pos+i] = tmp[i];
   if (values[C_MYNAM])
-    sprintf (tmp, "%-27s", upcase(values[C_MYNAM]));
+    sprintf (tmp, "%-27.27s", upcase(values[C_MYNAM]));
   else
   else
-    sprintf (tmp, "%-27s", upcase(valuesA[A_NAME]));
+    sprintf (tmp, "%-27.27s", upcase(valuesA[A_NAME]));
   for (i=0; i<recC[C_MYNAM].len; i++) buf[recC[C_MYNAM].pos+i] = tmp[i];
   for (i=0; i<recC[C_MYNAM].len; i++) buf[recC[C_MYNAM].pos+i] = tmp[i];
-  sprintf (tmp, "%-27s", upcase(values[C_ZWECK]));
+  sprintf (tmp, "%-27.27s", upcase(values[C_ZWECK]));
   for (i=0; i<recC[C_ZWECK].len; i++) buf[recC[C_ZWECK].pos+i] = tmp[i];
 
   if (values[C_TEXT]) {
     buf[recC[C_TEXT].pos+0] = '0';
     buf[recC[C_TEXT].pos+1] = '2';
   for (i=0; i<recC[C_ZWECK].len; i++) buf[recC[C_ZWECK].pos+i] = tmp[i];
 
   if (values[C_TEXT]) {
     buf[recC[C_TEXT].pos+0] = '0';
     buf[recC[C_TEXT].pos+1] = '2';
-    sprintf (tmp, "%-27s", upcase(values[C_TEXT]));
+    sprintf (tmp, "%-27.27s", upcase(values[C_TEXT]));
     for (i=0; i<recC[C_TEXT].len-2; i++) buf[recC[C_TEXT].pos+2+i] = tmp[i];
   }
 
   if (text) {
     for (i=0; i<recC[C_TEXT].len-2; i++) buf[recC[C_TEXT].pos+2+i] = tmp[i];
   }
 
   if (text) {
-    res = div (maxtext, 4);
+    buf[recC[C_EXT].pos+0] = '0';
+    buf[recC[C_EXT].pos+1] = '2';
+    sprintf (tmp, "%-27.27s", upcase(text[0]));
+    for (i=0; i<recC[C_EXT].len-2; i++) buf[recC[C_EXT].pos+2+i] = tmp[i];
+
+    res = div (maxtext-1, 4);
     i=res.quot;
     if (res.rem) i++;
 
     i=res.quot;
     if (res.rem) i++;
 
@@ -654,13 +686,13 @@ int dtaus_writeC(FILE *f, char **valuesA, char **values, char **text)
 
   fputs(buf, f);
 
 
   fputs(buf, f);
 
-  if (text) {
-    fieldnr=0;
+  if (text && maxtext > 1) {
+    fieldnr=1;
     while (fieldnr<maxtext) {
       memset (appendix, ' ', 128);
     while (fieldnr<maxtext) {
       memset (appendix, ' ', 128);
-      appendix[128] = '0';
+      appendix[128] = '\0';
       for (k=0; k<4 && (fieldnr+k)<maxtext; k++) {
       for (k=0; k<4 && (fieldnr+k)<maxtext; k++) {
-       sprintf (tmp, "%-27s", upcase(text[fieldnr+k]));
+       sprintf (tmp, "%-27.27s", upcase(text[fieldnr+k]));
        appendix[k*29] = '0';
        appendix[(k*29)+1] = '2';
        for (i=0; i<recC[C_TEXT].len-2; i++) appendix[(k*29)+2+i] = tmp[i];
        appendix[k*29] = '0';
        appendix[(k*29)+1] = '2';
        for (i=0; i<recC[C_TEXT].len-2; i++) appendix[(k*29)+2+i] = tmp[i];
@@ -718,20 +750,28 @@ void dtaus2control (char *cdtaus, char *ccontrol)
   void *bufp;
   char tmp[30];
   char x[30];
   void *bufp;
   char tmp[30];
   char x[30];
-  int index, countC;
+  int index;
+  int extC;
 
 
-  if (!cdtaus)
+  if (!cdtaus) {
     if (!(fdtaus = fopen("DTAUS0.TXT", "r")))
       if (!(fdtaus = fopen("dtaus0.txt", "r")))
        return;
     if (!(fdtaus = fopen("DTAUS0.TXT", "r")))
       if (!(fdtaus = fopen("dtaus0.txt", "r")))
        return;
-  if (cdtaus)
-    if (!(fdtaus = fopen(cdtaus, "r")))
-      return;
-  if (!ccontrol)
+  } else {
+    if (!strcmp (cdtaus, "-"))
+      fdtaus = stdin;
+    else
+      if (!(fdtaus = fopen(cdtaus, "r")))
+       return;
+  }
+  if (!ccontrol) 
     fcontrol = stdout;
   else
     fcontrol = stdout;
   else
-    if (!(fcontrol = fopen(ccontrol, "w")))
-      return;
+    if (!strcmp (ccontrol, "-"))
+      fcontrol = stdout;
+    else
+      if (!(fcontrol = fopen(ccontrol, "w")))
+       return;
   if (!(buf = (char *)malloc (512)))
     return;
 
   if (!(buf = (char *)malloc (512)))
     return;
 
@@ -773,7 +813,10 @@ void dtaus2control (char *cdtaus, char *ccontrol)
   if (dtaus_nextrec(buf, fdtaus) == 1) {
     while (dtaus_char(buf,4) == 'C') {
       bufp = buf + 128;
   if (dtaus_nextrec(buf, fdtaus) == 1) {
     while (dtaus_char(buf,4) == 'C') {
       bufp = buf + 128;
-      if (dtaus_nextrec(bufp, fdtaus) == 1) {
+      if (dtaus_nextrec(bufp, fdtaus) != 1) {
+       fprintf (stderr, "Der zweite Teil der Transaktion ist kaputt.\n");
+       return;
+      } else {
        fprintf(fcontrol, "{\n");
 
        for (index=C_NAME; index <= C_EURO; index++) {
        fprintf(fcontrol, "{\n");
 
        for (index=C_NAME; index <= C_EURO; index++) {
@@ -796,18 +839,34 @@ void dtaus2control (char *cdtaus, char *ccontrol)
          }
        }
 
          }
        }
 
-       countC = dtaus_int(buf, 185, 2);
-       countC = countC<3?0:countC-2/4;
-       countC += countC%4>0?1:0;
-       /* FIXME: Erweiterungsfelder werden ignoriert */
+       /* Number of extension records for this C record */
+       extC = dtaus_int(buf, 185, 2);
 
 
-       fprintf(fcontrol, "}\n");
-      } else {
-       fprintf (stderr, "Der zweite Teil der Transaktion ist kaputt.\n");
-       return;
       }
       if (dtaus_nextrec(buf, fdtaus) != 1)
        bzero (buf, sizeof(buf));
       }
       if (dtaus_nextrec(buf, fdtaus) != 1)
        bzero (buf, sizeof(buf));
+
+      /*
+       * Are there extension records that we have to check?
+       *
+       * 2nd half of the AND is wrong, but since dtaus < 0.5 wrote 01
+       *     instead of 00 we should let it in so it can read its own
+       *     old files...  *sigh*
+       */
+      while (extC > 0 && dtaus_char(buf,4) != 'C' && dtaus_char(buf,4) != 'E') {
+       for (index=0; index < 4; index++) {
+         if ((dtaus_char(buf,index*29) != ' ')) {
+           bufp = buf + index*29;
+           memcpy(x, bufp, 2); tmp[2] = '\0'; bufp+=2;
+           memcpy(tmp, bufp, recC[C_TEXT].len-2); tmp[recC[C_TEXT].len-2] = '\0';
+           printctln(fcontrol, string2ext(x), strip_spaces(tmp));
+         }
+       }
+       if (dtaus_nextrec(buf, fdtaus) != 1)
+         bzero (buf, sizeof(buf));
+       extC--;
+      }
+      fprintf(fcontrol, "}\n");
     }
   }
 
     }
   }
 
@@ -817,7 +876,7 @@ void dtaus2control (char *cdtaus, char *ccontrol)
    */
   if (dtaus_char(buf,4) == 'E') {
     if (dtaus_char(buf,4) == 'E') {
    */
   if (dtaus_char(buf,4) == 'E') {
     if (dtaus_char(buf,4) == 'E') {
-      fprintf(fcontrol, "END {\n");
+      fprintf(fcontrol, "\nEND {\n");
 
       for (index=E_COUNT; index <= E_BLZ; index++) {
        bufp = buf + recE[index].pos;
 
       for (index=E_COUNT; index <= E_BLZ; index++) {
        bufp = buf + recE[index].pos;
@@ -856,25 +915,34 @@ int control2dtaus (char *ccontrol, char *cdtaus, char *cbeleg, char *ccheck)
   char *cp;
   int textindex = 0;
 
   char *cp;
   int textindex = 0;
 
-  if (!cdtaus)
+  if (!cdtaus) {
     if (!(fdtaus = fopen("dtaus0.txt", "w")))
       return 0;
     if (!(fdtaus = fopen("dtaus0.txt", "w")))
       return 0;
-  if (cdtaus)
-    if (!(fdtaus = fopen(cdtaus, "w")))
-      return 0;
-  if (!ccontrol)
+  } else {
+    if (!strcmp (cdtaus, "-"))
+      fdtaus = stdout;
+    else
+      if (!(fdtaus = fopen(cdtaus, "w")))
+       return 0;
+  }
+  if (!ccontrol) {
     if (!(fcontrol = fopen("dtaus0.ctl", "r")))
       if (!(fcontrol = fopen("DTAUS0.CTL", "r")))
        return 0;
     if (!(fcontrol = fopen("dtaus0.ctl", "r")))
       if (!(fcontrol = fopen("DTAUS0.CTL", "r")))
        return 0;
-  if (ccontrol)
-    if (!(fcontrol = fopen(ccontrol, "r")))
-      return 0;
-  if (!cbeleg)
+  } else {
+    if (!strcmp (ccontrol, "-"))
+      fcontrol = stdin;
+    else
+      if (!(fcontrol = fopen(ccontrol, "r")))
+       return 0;
+  }
+  if (!cbeleg) {
     if (!(fbeleg = fopen("dtaus0.doc", "w")))
       return 0;
     if (!(fbeleg = fopen("dtaus0.doc", "w")))
       return 0;
-  if (cbeleg)
+  } else {
     if (!(fbeleg = fopen(cbeleg, "w")))
       return 0;
     if (!(fbeleg = fopen(cbeleg, "w")))
       return 0;
+  }
   if (!ccheck)
     fcheck = stdout;
   else
   if (!ccheck)
     fcheck = stdout;
   else
@@ -922,7 +990,7 @@ int control2dtaus (char *ccontrol, char *cdtaus, char *cbeleg, char *ccheck)
 
     fprintf (fcheck, "\n\n\n");
     if (valA[recindex][0] == 'L')
 
     fprintf (fcheck, "\n\n\n");
     if (valA[recindex][0] == 'L')
-      fprintf (fcheck, "    Sammeleinzeiehungsauftrag\n\n");
+      fprintf (fcheck, "    Sammeleinziehungsauftrag\n\n");
     else if (valA[recindex][0] == 'G')
       fprintf (fcheck, "    Sammelueberweisungsauftrag\n\n");
     else
     else if (valA[recindex][0] == 'G')
       fprintf (fcheck, "    Sammelueberweisungsauftrag\n\n");
     else