MARC line reader: allow control fields of size 1
[yaz-moved-to-github.git] / src / marc_read_line.c
index f6f58ea..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.5 2007-03-17 09:13:29 adam Exp $
  */
 
 /**
@@ -35,7 +33,6 @@ int yaz_gets(int (*getbyte)(void *client_data),
     size_t sz = 0;
     int ch = getbyte(client_data);
 
-    wrbuf_rewind(w);
     while (ch != '\0' && ch != '\r' && ch != '\n')
     {
         wrbuf_putc(w, ch);
@@ -60,6 +57,41 @@ int yaz_gets(int (*getbyte)(void *client_data),
     }
     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),
@@ -79,7 +111,7 @@ int yaz_marc_read_line(yaz_marc_t mt,
 
     yaz_marc_reset(mt);
 
-    while (yaz_gets(getbyte, ungetbyte, client_data, wrbuf_line))
+    while (yaz_marc_line_gets(getbyte, ungetbyte, client_data, wrbuf_line))
     {
         const char *line = wrbuf_cstr(wrbuf_line);
         int val;
@@ -112,11 +144,8 @@ 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];
@@ -202,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;
@@ -210,6 +238,10 @@ 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)
@@ -220,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