* While implementing zero-padded fields I've also implemented
[infodrom/dtaus] / dtaus.c
diff --git a/dtaus.c b/dtaus.c
index e79f31b..079bc2c 100644 (file)
--- a/dtaus.c
+++ b/dtaus.c
@@ -1,6 +1,6 @@
 /*
     dtaus.c - Datenträgeraustausch mit einer Bank
-    Copyright (c) 1996  Martin Schulze <joey@artis.uni-oldenburg.de>
+    Copyright (c) 1996,8  Martin Schulze <joey@artis.uni-oldenburg.de>
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -14,7 +14,7 @@
 
     You should have received a copy of the GNU General Public License
     along with this program; if not, write to the Free Software
-    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
 
     $Id$
  */
@@ -54,6 +54,7 @@ dtaus_record recA[] = {
   {"BLZ", 7, 8, REQ},
   {"Referenz", 70, 10, OPT},
   {"Datum", 50, 6, IGN},
+  {"Euro", 127, 1, OPT},
   {NULL, 0, 0}
 };
 
@@ -63,7 +64,8 @@ dtaus_record recA[] = {
 #define A_BLZ  3
 #define A_REF  4
 #define A_DATE 5
-#define A_LEN  6
+#define A_EURO 6
+#define A_LEN  7
 
 dtaus_record recC[] = {
   {"Name", 93, 27, REQ},
@@ -75,6 +77,7 @@ dtaus_record recC[] = {
   {"myName", 128, 27, OPT},
   {"myKonto", 69, 10, OPT},
   {"myBLZ", 61, 8, OPT},
+  {"Euro", 182, 1, IGN},
   {"Text", 187, 29, OPT},
   {"Extension", 216, 29, OPT},
   {NULL, 0, 0}
@@ -89,9 +92,10 @@ dtaus_record recC[] = {
 #define C_MYNAM        6
 #define C_MYKTO        7
 #define C_MYBLZ        8
-#define C_TEXT 9
-#define C_EXT  10
-#define C_LEN  11
+#define C_EURO  9
+#define C_TEXT 10
+#define C_EXT  11
+#define C_LEN  12
 
 dtaus_record recE[] = {
   {"Anzahl", 10, 7, IGN},
@@ -339,6 +343,28 @@ char *extract_ident (char *line)
   return line;
 }
 
+/*
+ * Pads a string with zero's on the left side.
+ */
+char *padzeroclip (char *s, int len)
+{
+  char *p, *q;
+
+  if (strlen(s) == len) return s;
+  if (strlen(s) > len) {
+      q=s+len;
+      *(q--) = '\0';
+      return s;
+  }
+
+  q=s+len;
+  *(q--) = '\0';
+  for (p=s+strlen(s)-1;p>=s;p--)
+    *(q--)=*p;
+  for (;q>=s;) *(q--)='0';
+  return s;
+}
+
 int rec_index(char *ident, int type)
 {
   int i;
@@ -485,14 +511,17 @@ int dtaus_writeA(FILE *f, char **values)
     return 0;
   }
 
+  i=A_NAME;if (values[i] && strlen(values[i]) > recA[i].len)
+    values[i][recA[i].len] = '\0';
+
   dtaus_prepareA(buf);
   buf[5] = values[A_TRANS][0];
   buf[6] = values[A_TRANS][1];
-  sprintf (tmp, "%08s", values[A_BLZ]);
+  sprintf (tmp, "%s", padzeroclip(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]));
   for (i=0; i<27; i++) buf[recA[A_NAME].pos+i] = tmp[i];
-  sprintf (tmp, "%010s", values[A_KTO]);
+  sprintf (tmp, "%s", padzeroclip(values[A_KTO],10));
   for (i=0; i<10; i++) buf[recA[A_KTO].pos+i] = tmp[i];
 
   fputs(buf, f);
@@ -522,6 +551,17 @@ int dtaus_writeC(FILE *f, char **valuesA, char **values)
     return 0;
   }
 
+  i=C_TEXT;if (values[i] && strlen(values[i]) > recC[i].len)
+    values[i][recC[i].len] = '\0';
+  i=C_ZWECK;if (values[i] && strlen(values[i]) > recC[i].len)
+    values[i][recC[i].len] = '\0';
+  i=C_MYNAM;if (values[i] && strlen(values[i]) > recC[i].len)
+    values[i][recC[i].len] = '\0';
+  i=C_TEXT;if (values[i] && strlen(values[i]) > recC[i].len)
+    values[i][recC[i].len] = '\0';
+  i=C_EXT;if (values[i] && strlen(values[i]) > recC[i].len)
+    values[i][recC[i].len] = '\0';
+
   dtaus_prepareC(buf);
   if (!values[C_TEXT]) {
     buf[1] = '1';
@@ -530,21 +570,21 @@ int dtaus_writeC(FILE *f, char **valuesA, char **values)
   }
   for (i=0; i<5; i++) buf[recC[C_TRANS].pos+i] = tmp[i];
   if (values[C_MYBLZ])
-    sprintf (tmp, "%08s", values[C_MYBLZ]);
+    sprintf (tmp, "%s", padzeroclip(values[C_MYBLZ],8));
   else
-    sprintf (tmp, "%08s", valuesA[A_BLZ]);
+    sprintf (tmp, "%s", padzeroclip(valuesA[A_BLZ],8));
   for (i=0; i<recC[C_MYBLZ].len; i++) buf[5+i] = tmp[i];
   for (i=0; i<recC[C_MYBLZ].len; i++) buf[recC[C_MYBLZ].pos+i] = tmp[i];
-  sprintf (tmp, "%08s", values[C_BLZ]);
+  sprintf (tmp, "%s", padzeroclip(values[C_BLZ],8));
   for (i=0; i<recC[C_BLZ].len; i++) buf[recC[C_BLZ].pos+i] = tmp[i];
-  sprintf (tmp, "%010s", values[C_KTO]);
+  sprintf (tmp, "%s", padzeroclip(values[C_KTO],10));
   for (i=0; i<recC[C_KTO].len; i++) buf[recC[C_KTO].pos+i] = tmp[i];
-  sprintf (tmp, "%011s", real2string(values[C_VAL]));
+  sprintf (tmp, "%s", padzeroclip(real2string(values[C_VAL]),11));
   for (i=0; i<recC[C_VAL].len; i++) buf[recC[C_VAL].pos+i] = tmp[i];
   if (values[C_MYKTO])
-    sprintf (tmp, "%010s", values[C_MYKTO]);
+    sprintf (tmp, "%s", padzeroclip(values[C_MYKTO],10));
   else
-    sprintf (tmp, "%010s", valuesA[A_KTO]);
+    sprintf (tmp, "%s", padzeroclip(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]));
   for (i=0; i<recC[C_NAME].len; i++) buf[recC[C_NAME].pos+i] = tmp[i];
@@ -581,19 +621,30 @@ int dtaus_writeE(FILE *f, int count, bigint sum, bigint blz, bigint kto)
 
   sprintf (tmp, "%07d", count);
   for (i=0; i<recE[E_COUNT].len; i++) buf[recE[E_COUNT].pos+i] = tmp[i];
-  bigint_sprintf (tmp, "%013s", sum);
+  bigint_sprintf (tmp, "%s", sum);
+  padzeroclip(tmp,13);
   for (i=0; i<recE[E_VAL].len; i++) buf[recE[E_VAL].pos+i] = tmp[i];
-  bigint_sprintf (tmp, "%013s", sum);
+  bigint_sprintf (tmp, "%s", sum);
+  padzeroclip(tmp,13);
   for (i=0; i<recE[E_VAL].len; i++) buf[recE[E_VAL].pos+i] = tmp[i];
-  bigint_sprintf (tmp, "%017s", kto);
+  bigint_sprintf (tmp, "%s", kto);
+  padzeroclip(tmp,17);
   for (i=0; i<recE[E_KTO].len; i++) buf[recE[E_KTO].pos+i] = tmp[i];
-  bigint_sprintf (tmp, "%017s", blz);
+  bigint_sprintf (tmp, "%s", blz);
+  padzeroclip(tmp,17);
   for (i=0; i<recE[E_BLZ].len; i++) buf[recE[E_BLZ].pos+i] = tmp[i];
 
   fputs(buf, f);
   return 1;
 }
 
+void printctln(FILE *f, char *field, char *value)
+{
+  if (strlen(field) && strlen(value))
+    fprintf(f, "  %s\t%s\n", field, value);
+}
+
+
 /*
  *  Third: Some high level routines
  */
@@ -633,12 +684,16 @@ void dtaus2control (char *cdtaus, char *ccontrol)
       for (index=A_TRANS; index < A_DATE; index++) {
        bufp = buf + recA[index].pos;
        memcpy(tmp, bufp, recA[index].len); tmp[recA[index].len] = '\0';
-       fprintf(fcontrol, "  %s\t%s\n", recA[index].name, strip_zeros(strip_spaces(tmp)));
+       printctln(fcontrol, recA[index].name, strip_zeros(strip_spaces(tmp)));
       }
 
       index = A_DATE; bufp = buf + recA[index].pos;
       memcpy(tmp, bufp, recA[index].len); tmp[recA[index].len] = '\0';
-      fprintf(fcontrol, "  %s\t%s\n", recA[index].name, strip_spaces(tmp));
+      printctln(fcontrol, recA[index].name, strip_spaces(tmp));
+
+      index = A_EURO; bufp = buf + recA[index].pos;
+      memcpy(tmp, bufp, recA[index].len); tmp[recA[index].len] = '\0';
+      printctln(fcontrol, recA[index].name, strip_zeros(strip_spaces(tmp)));
 
       fprintf(fcontrol, "}\n\n");
     } else {
@@ -659,15 +714,15 @@ void dtaus2control (char *cdtaus, char *ccontrol)
       if (dtaus_nextrec(bufp, fdtaus) == 1) {
        fprintf(fcontrol, "{\n");
 
-       for (index=C_NAME; index <= C_MYBLZ; index++) {
+       for (index=C_NAME; index <= C_EURO; index++) {
          bufp = buf + recC[index].pos;
          memcpy(tmp, bufp, recC[index].len); tmp[recC[index].len] = '\0';
          if (index == C_VAL)
-           fprintf(fcontrol, "  %s\t%s\n", recC[index].name, strip_zeros(string2real(tmp)));
+           printctln(fcontrol, recC[index].name, strip_zeros(string2real(tmp)));
          else if (index == C_TRANS)
-           fprintf(fcontrol, "  %s\t%s\n", recC[index].name, string2trans(tmp));
+           printctln(fcontrol, recC[index].name, strip_zeros(string2trans(tmp)));
          else
-           fprintf(fcontrol, "  %s\t%s\n", recC[index].name, strip_zeros(strip_spaces(tmp)));
+           printctln(fcontrol, recC[index].name, strip_zeros(strip_spaces(tmp)));
        }
 
        for (index=C_TEXT; index <= C_EXT; index++) {
@@ -675,7 +730,7 @@ void dtaus2control (char *cdtaus, char *ccontrol)
            bufp = buf + recC[index].pos;
            memcpy(x, bufp, 2); tmp[2] = '\0'; bufp+=2;
            memcpy(tmp, bufp, recC[index].len-2); tmp[recC[index].len-2] = '\0';
-           fprintf(fcontrol, "  %s\t%s\n", string2ext(x), strip_spaces(tmp));
+           printctln(fcontrol, string2ext(x), strip_spaces(tmp));
          }
        }
 
@@ -706,9 +761,9 @@ void dtaus2control (char *cdtaus, char *ccontrol)
        bufp = buf + recE[index].pos;
        memcpy(tmp, bufp, recE[index].len); tmp[recE[index].len] = '\0';
        if (index == E_VAL)
-         fprintf(fcontrol, "  %s\t%s\n", recE[index].name, strip_zeros(string2real(tmp)));
+         printctln(fcontrol, recE[index].name, strip_zeros(string2real(tmp)));
        else
-         fprintf(fcontrol, "  %s\t%s\n", recE[index].name, strip_zeros(tmp));
+         printctln(fcontrol, recE[index].name, strip_zeros(tmp));
       }
 
       fprintf(fcontrol, "}\n");
@@ -821,6 +876,8 @@ int control2dtaus (char *ccontrol, char *cdtaus, char *cbeleg, char *ccheck)
          if (recC[recindex].type != IGN)
            if ((valC[recindex] = (char *)malloc (strlen(line)+1)))
              strcpy(valC[recindex], line);
+           else
+             return 0;
        control_nextline ((void *)line, 100, fcontrol);
       }
       if (!dtaus_writeC(fdtaus, valA, valC)) {