MARC line reader: allow control fields of size 1
[yaz-moved-to-github.git] / src / marc_read_line.c
index 1b9aca9..eefeb84 100644 (file)
@@ -1,8 +1,6 @@
-/*
- * Copyright (C) 1995-2007, Index Data ApS
+/* This file is part of the YAZ toolkit.
+ * Copyright (C) 1995-2010 Index Data
  * See the file LICENSE for details.
- *
- * $Id: marc_read_line.c,v 1.4 2007-03-17 08:44:24 adam Exp $
  */
 
 /**
 int yaz_gets(int (*getbyte)(void *client_data),
              void (*ungetbyte)(int b, void *client_data),
              void *client_data,
-             char *buf, size_t len)
+             WRBUF w)
 {
     size_t sz = 0;
     int ch = getbyte(client_data);
+
     while (ch != '\0' && ch != '\r' && ch != '\n')
     {
-        if (sz < len-1)
-            buf[sz++] = ch;
+        wrbuf_putc(w, ch);
+        sz++;
         ch = getbyte(client_data);
     }
     if (ch == '\r')
@@ -52,11 +51,47 @@ int yaz_gets(int (*getbyte)(void *client_data),
         if (ch != '\r' && ch != '\0')
             ungetbyte(ch, client_data);
     }
-    buf[sz] = '\0';
     if (sz)
+    {
         return 1;
+    }
     return 0;
 }
+
+static int yaz_marc_line_gets(int (*getbyte)(void *client_data),
+                              void (*ungetbyte)(int b, void *client_data),
+                              void *client_data,
+                              WRBUF w)
+{
+    int more;
+
+    wrbuf_rewind(w);
+    more = yaz_gets(getbyte, ungetbyte, client_data, w);
+    if (!more)
+        return 0;
+
+    while (more)
+    {
+        int i;
+        for (i = 0; i<4; i++)
+        {
+            int ch = getbyte(client_data);
+            if (ch != ' ')
+            {
+                if (ch)
+                    ungetbyte(ch, client_data);
+                return 1;
+            }
+        }
+        if (wrbuf_len(w) > 60 && wrbuf_buf(w)[wrbuf_len(w)-1] == '=')
+            wrbuf_cut_right(w, 1);
+        else
+            wrbuf_puts(w, " ");
+        more = yaz_gets(getbyte, ungetbyte, client_data, w);
+    }
+    return 1;
+}
+
     
 int yaz_marc_read_line(yaz_marc_t mt,
                        int (*getbyte)(void *client_data),
@@ -72,17 +107,15 @@ int yaz_marc_read_line(yaz_marc_t mt,
     int marker_ch = 0;
     int marker_skip = 0;
     int header_created = 0;
-    char line[4096];
+    WRBUF wrbuf_line = wrbuf_alloc();
 
     yaz_marc_reset(mt);
 
-    while (yaz_gets(getbyte, ungetbyte, client_data, line, sizeof(line)))
+    while (yaz_marc_line_gets(getbyte, ungetbyte, client_data, wrbuf_line))
     {
+        const char *line = wrbuf_cstr(wrbuf_line);
         int val;
         size_t line_len = strlen(line);
-        /* see if have leader lines of the form:
-           00366nam  22001698a 4500
-        */
         if (line_len == 0)       /* empty line indicates end of record */
         {
             if (header_created)
@@ -97,6 +130,9 @@ int yaz_marc_read_line(yaz_marc_t mt,
             ;
         else if (line_len == 24 && atoi_n_check(line, 5, &val) && val >= 24)
         {
+            /* deal with header lines:  00366nam  22001698a 4500
+            */
+
             if (header_created)
                 break;
             yaz_marc_set_leader(mt, line,
@@ -108,14 +144,12 @@ int yaz_marc_read_line(yaz_marc_t mt,
                                 &length_implementation);
             header_created = 1;
         }
-        else if (line_len > 5 && memcmp(line, "    ", 4) == 0)
-        {  /* continuation line */
-            ;
-        }
-        else if (line_len > 5 && line[3] == ' ')
+        else if (line_len > 4 && line[0] != ' ' && line[1] != ' '
+                 && line[2] != ' ' && line[3] == ' ' )
         {
+            /* deal with data/control lines: 245 12 ........ */
             char tag[4];
-            char *datafield_start = line+6;
+            const char *datafield_start = line+6;
             marker_ch = 0;
             marker_skip = 0;
 
@@ -155,12 +189,12 @@ int yaz_marc_read_line(yaz_marc_t mt,
             {   /* data field */
                 const char *indicator = line+4;
                 int indicator_len = 2;
-                char *cp = datafield_start;
+                const char *cp = datafield_start;
 
                 yaz_marc_add_datafield(mt, tag, indicator, indicator_len);
                 for (;;)
                 {
-                    char *next;
+                    const char *next;
                     size_t len;
                     
                     assert(cp[0] == marker_ch);
@@ -197,7 +231,6 @@ int yaz_marc_read_line(yaz_marc_t mt,
                             cp++;
                         }
                     }
-                    assert(len >= 0);
                     yaz_marc_add_subfield(mt, cp, len);
                     if (!next)
                         break;
@@ -205,7 +238,12 @@ int yaz_marc_read_line(yaz_marc_t mt,
                 }
             }
         }
+        else
+        {
+            yaz_marc_cprintf(mt, "Ignoring line: %s", line);
+        }
     }
+    wrbuf_destroy(wrbuf_line);
     if (!header_created)
         return -1;
     return 0;
@@ -214,6 +252,7 @@ int yaz_marc_read_line(yaz_marc_t mt,
 /*
  * Local variables:
  * c-basic-offset: 4
+ * c-file-style: "Stroustrup"
  * indent-tabs-mode: nil
  * End:
  * vim: shiftwidth=4 tabstop=8 expandtab