New module gip: Gateway IPc module.
[egate.git] / util / iso2709.c
index 927df07..812f8da 100644 (file)
@@ -1,14 +1,39 @@
 /*
-   gw-res.c: Iso2709 record management
-
-   Europagate, 1994-1995.
-
-   $Log: iso2709.c,v $
-   Revision 1.2  1995/02/10 16:50:32  adam
-   Indicator field moved to 'struct iso2709_dir' from 'struct
-   iso2709_field'.
-   Function iso2709_rm implemented - to delete a MARC record.
-
+ * Iso2709 record management
+ *
+ * Europagate, 1994-1995.
+ *
+ * $Log: iso2709.c,v $
+ * Revision 1.10  1995/03/10 09:10:56  adam
+ * Removed dbc2709_cvt function. Makes heuristic guess for DBC2709 records.
+ *
+ * Revision 1.9  1995/03/08  12:36:39  adam
+ * New function: dbc2709_cvt.
+ *
+ * Revision 1.8  1995/03/08  12:03:15  adam
+ * Hack: When tags 00? are used, every separator (DC[1-3]) marks
+ * the end of the data field.
+ *
+ * Revision 1.7  1995/02/22  21:28:03  adam
+ * Changed header.
+ *
+ * Revision 1.5  1995/02/22  15:24:14  adam
+ * Function iso2709_cvt makes a litte check for the format. It returns
+ * NULL if the buffer parameter can never be a MARC record.
+ *
+ * Revision 1.4  1995/02/15  17:45:44  adam
+ * Bug fix in iso2709 module.
+ *
+ * Revision 1.3  1995/02/10  17:05:18  adam
+ * New function iso2709_display to display MARC records in a
+ * line-by-line format. The iso2709_cvt function no longer
+ * prints the record to stderr.
+ *
+ * Revision 1.2  1995/02/10  16:50:32  adam
+ * Indicator field moved to 'struct iso2709_dir' from 'struct
+ * iso2709_field'.
+ * Function iso2709_rm implemented - to delete a MARC record.
+ *
  * Revision 1.1.1.1  1995/02/09  17:27:11  adam
  * Initial version of email gateway under CVS control.
  *
@@ -74,6 +99,7 @@ char *iso2709_read (FILE *inf)
     return buf;
 }
 
+
 Iso2709Rec iso2709_cvt (const char *buf)
 {
     struct iso2709_dir **dpp, *dp;
@@ -92,6 +118,11 @@ Iso2709Rec iso2709_cvt (const char *buf)
     p->base_address = atoin (buf+12, 4);
     strncpyx (p->user_systems, buf+17, 3);
 
+    if (p->record_length < 26)
+    {
+        free (p);
+        return NULL;
+    }
     p->length_data_entry = atoin (buf+20, 1);
     p->length_starting = atoin (buf+21, 1);
     p->length_implementation = atoin (buf+22, 1);
@@ -103,6 +134,7 @@ Iso2709Rec iso2709_cvt (const char *buf)
     while (buf[pos] != ISO2709_FS)
     {
         *dpp = malloc (sizeof(**dpp));
+        assert (*dpp);
         (*dpp)->next = NULL;
         strncpyx ((*dpp)->tag, buf+pos, 3);
         pos += 3;
@@ -115,68 +147,73 @@ Iso2709Rec iso2709_cvt (const char *buf)
     }
     pos++;
     /* deal with datafields */
-#if 0
-    fprintf (stderr, "indicator_len=%d, identifier_len=%d\n", 
-             p->indicator_length, p->identifier_length);
-#endif
     for (dp = p->directory; dp; dp = dp->next)
     {
-        int tag00;
+        int identifier_flag;
         struct iso2709_field **fpp;
         int dpos = pos+dp->offset;
 
         fpp = &dp->fields;
 
         *fpp = malloc (sizeof(**fpp));
+        assert (*fpp);
         (*fpp)->next = NULL;
-        if (p->indicator_length && memcmp (dp->tag, "00", 2))
+
+        identifier_flag = 1;
+        if (p->indicator_length)
+        {
+#if STUPID_ISO_DBC
+            if (buf[dpos+p->indicator_length] != ISO2709_IDFS)
+                identifier_flag = 0;
+#else
+            if (!memcmp (dp->tag, "00", 2))
+                identifier_flag = 0;
+#endif
+        }
+        else if (!memcmp (dp->tag, "00", 2))
+                identifier_flag = 0;
+        if (identifier_flag && p->indicator_length)
         {
             dp->indicator = malloc (p->indicator_length+1);
+            assert (dp->indicator);
             strncpyx (dp->indicator, buf+dpos, p->indicator_length);
             dpos += p->indicator_length;
         }
         else
             dp->indicator = NULL;
-
-        if (memcmp (dp->tag, "00", 2))
-            tag00 = 0;
-        else
-            tag00 = 1;
-        fprintf (stderr, "%s", dp->tag);
-        if (dp->indicator)
-            fprintf (stderr, " %s", dp->indicator);
         while (1)
         {
             int dpos_n;
-            if (p->identifier_length && !tag00)
+            if (p->identifier_length && identifier_flag)
             {
                 (*fpp)->identifier = malloc (p->identifier_length+1);
                 strncpyx ((*fpp)->identifier, buf+dpos+1,
                           p->identifier_length-1);
-                dpos += p->identifier_length;
+                dpos_n = dpos += p->identifier_length;
+                while (buf[dpos_n] != ISO2709_FS && buf[dpos_n] != ISO2709_RS
+                       && buf[dpos_n] != ISO2709_IDFS)
+                    dpos_n++;
             }
             else
+            {
                 (*fpp)->identifier = NULL;
-
-            dpos_n = dpos;
-            while (buf[dpos_n] != ISO2709_FS && buf[dpos_n] != ISO2709_IDFS)
-                dpos_n++;
-
+                dpos_n = dpos;
+                while (buf[dpos_n] != ISO2709_FS && buf[dpos_n] != ISO2709_RS)
+                    dpos_n++;
+            }
             (*fpp)->data = malloc (dpos_n - dpos + 1);
+            assert ((*fpp)->data);
             strncpyx ((*fpp)->data, buf+dpos, dpos_n - dpos);
             dpos = dpos_n;
             
-            if ((*fpp)->identifier)
-                fprintf (stderr, " *%s", (*fpp)->identifier);
-            fprintf (stderr, " %s", (*fpp)->data);
-            if (buf[dpos] == ISO2709_FS)
+            if (buf[dpos] == ISO2709_FS || buf[dpos] == ISO2709_RS)
                 break;
             
             fpp = &(*fpp)->next;
             *fpp = malloc (sizeof(**fpp));
+            assert (*fpp);
             (*fpp)->next = NULL;
         }
-        fprintf (stderr, "\n");
     }
     return p;
 }