Added ir-log command.
[ir-tcl-moved-to-github.git] / marc.c
diff --git a/marc.c b/marc.c
index d98ecd5..d7a3624 100644 (file)
--- a/marc.c
+++ b/marc.c
@@ -5,7 +5,16 @@
  * Sebastian Hammer, Adam Dickmeiss
  *
  * $Log: marc.c,v $
- * Revision 1.6  1995-08-28 12:21:22  adam
+ * Revision 1.9  1996-07-03 13:31:13  adam
+ * The xmalloc/xfree functions from YAZ are used to manage memory.
+ *
+ * Revision 1.8  1995/11/14  16:48:00  adam
+ * Bug fix: record extraction in line mode merged lines with same tag.
+ *
+ * Revision 1.7  1995/11/09  15:24:02  adam
+ * Allow charsets [..] in record match.
+ *
+ * Revision 1.6  1995/08/28  12:21:22  adam
  * Removed lines and list as synonyms of list in MARC extractron.
  * Configure searches also for tk4.0 / tcl7.4.
  *
@@ -60,18 +69,39 @@ static int atoi_n (const char *buf, int len)
 
 static int marc_compare (const char *f, const char *p)
 {
+    int ch;
+
     if (*p == '*')
         return 0;
     if (!f)
         return -*p;
-    for (; *f && *p; f++, p++)
-    {
-        if (*p == '?')
-           continue;
-       if (*p != *f)
-           break;
-    }
-    return *f - *p;
+    for (; (ch = *p) && *f; f++, p++)
+        switch (*p)
+        {
+        case '*':
+            return 0;
+        case '?':
+            ch = *f;
+            break;
+        case '[':
+            while (1)
+                if (!*++p)
+                    break;
+                else if (*p == ']')
+                {
+                    p++;
+                    break;
+                }
+                else if (*p == *f)
+                    ch = *p;
+            if (ch != *p)
+                return *f - ch;
+            break;
+        default:
+            if (ch != *f)
+                return *f - ch;
+        }
+    return *f - ch;
 }
 
 char *ir_tcl_fread_marc (FILE *inf, size_t *size)
@@ -84,11 +114,11 @@ char *ir_tcl_fread_marc (FILE *inf, size_t *size)
     *size = atoi_n (length, 5);
     if (*size <= 6)
         return NULL;
-    if (!(buf = malloc (*size+1)))
+    if (!(buf = xmalloc (*size+1)))
         return NULL;
     if (fread (buf+5, 1, *size-5, inf) != (*size-5))
     {
-        free (buf);
+        xfree (buf);
        return NULL;
     }
     memcpy (buf, length, 5);
@@ -110,7 +140,6 @@ int ir_tcl_get_marc (Tcl_Interp *interp, const char *buf,
     char ptag[4];
     int mode = 0;
 
-    *ptag = '\0';
     if (!strcmp (argv[3], "field"))
         mode = 'f';
     else if (!strcmp (argv[3], "line"))
@@ -152,6 +181,7 @@ int ir_tcl_get_marc (Tcl_Interp *interp, const char *buf,
        char indicator[128];
        char identifier[128];
 
+        *ptag = '\0';
         memcpy (tag, buf+entry_p, 3);
        entry_p += 3;
         tag[3] = '\0';
@@ -196,7 +226,7 @@ int ir_tcl_get_marc (Tcl_Interp *interp, const char *buf,
            }
             if (marc_compare (identifier, argv[6])==0)
             {
-                char *data = malloc (i-i0+1);
+                char *data = xmalloc (i-i0+1);
              
                 memcpy (data, buf+i0, i-i0);
                 data[i-i0] = '\0';
@@ -222,16 +252,16 @@ int ir_tcl_get_marc (Tcl_Interp *interp, const char *buf,
                 }
                 else
                     Tcl_AppendElement (interp, data);
-                free (data);
+                xfree (data);
             }
        }
+        if (mode == 'l' && *ptag)
+            Tcl_AppendResult (interp, "}} ", NULL);
        if (i < end_offset)
             logf (LOG_WARN, "MARC: separator but not at end of field");
        if (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS)
             logf (LOG_WARN, "MARC: no separator at end of field");
     }
-    if (mode == 'l' && *ptag)
-        Tcl_AppendResult (interp, "}} ", NULL);
     return TCL_OK;
 }