Omit CVS Id. Update copyright year.
[idzebra-moved-to-github.git] / index / marcread.c
index e030cf9..52b59e8 100644 (file)
@@ -1,8 +1,5 @@
-/* $Id: marcread.c,v 1.1 2006-07-03 14:27:09 adam Exp $
-   Copyright (C) 1995-2005
-   Index Data ApS
-
-This file is part of the Zebra server.
+/* This file is part of the Zebra server.
+   Copyright (C) 1995-2008 Index Data
 
 Zebra is free software; you can redistribute it and/or modify it under
 the terms of the GNU General Public License as published by the Free
@@ -15,9 +12,9 @@ FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 for more details.
 
 You should have received a copy of the GNU General Public License
-along with Zebra; see the file LICENSE.zebra.  If not, write to the
-Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
-02111-1307, USA.
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
 */
 
 #include <stdio.h>
@@ -58,7 +55,7 @@ static data1_node *grs_read_iso2709 (struct grs_read_info *p, int marc_xml)
     char *absynName;
     data1_marctab *marctab;
 
-    if ((*p->readf)(p->fh, buf, 5) != 5)
+    if (p->stream->readf(p->stream, buf, 5) != 5)
         return NULL;
     while (*buf < '0' || *buf > '9')
     {
@@ -69,7 +66,7 @@ static data1_node *grs_read_iso2709 (struct grs_read_info *p, int marc_xml)
        for (i = 0; i<4; i++)
            buf[i] = buf[i+1];
 
-       if ((*p->readf)(p->fh, buf+4, 1) != 1)
+       if (p->stream->readf(p->stream, buf+4, 1) != 1)
            return NULL;
     }
     record_length = atoi_n (buf, 5);
@@ -78,21 +75,35 @@ static data1_node *grs_read_iso2709 (struct grs_read_info *p, int marc_xml)
         yaz_log (YLOG_WARN, "MARC record length < 25, is %d", record_length);
         return NULL;
     }
-    /* read remaining part - attempt to read one byte furhter... */
-    read_bytes = (*p->readf)(p->fh, buf+5, record_length-4);
+
+    read_bytes = p->stream->readf(p->stream, buf+5, record_length-5);
     if (read_bytes < record_length-5)
     {
         yaz_log (YLOG_WARN, "Couldn't read whole MARC record");
         return NULL;
     }
-    if (read_bytes == record_length - 4)
+    /* skip until we meet a record separator */
+    while (buf[record_length-1] != ISO2709_RS)
     {
-        off_t cur_offset = (*p->tellf)(p->fh);
-       if (cur_offset <= 27)
-           return NULL;
-       if (p->endf)
-           (*p->endf)(p->fh, cur_offset - 1);
+        if (record_length > sizeof(buf)-2)
+            break;
+        read_bytes = p->stream->readf(p->stream, buf+record_length, 1);
+        if (read_bytes != 1)
+            break;
+        record_length++;
     }
+    /* read one byte ahead to see if there is more ... */
+    read_bytes = p->stream->readf(p->stream, buf+record_length, 1);
+    if (read_bytes == 1)
+    {
+        off_t cur_offset = p->stream->tellf(p->stream);
+       if (p->stream->endf)
+        {
+            off_t end_offset = cur_offset - 1;
+           p->stream->endf(p->stream, &end_offset);
+        }
+    }
+
     absynName = mi->type;
     res_root = data1_mk_root (p->dh, p->mem, absynName);
     if (!res_root)
@@ -113,7 +124,7 @@ static data1_node *grs_read_iso2709 (struct grs_read_info *p, int marc_xml)
     else
        res_top = data1_mk_tag (p->dh, p->mem, absynName, 0, res_root);
 
-    if ((marctab = data1_absyn_getmarctab(p->dh, res_root->u.root.absyn)))
+    if ((marctab = data1_absyn_getmarctab(p->dh, res_root)))
     {
        memcpy(marctab->leader, buf, 24);
         memcpy(marctab->implementation_codes, buf+6, 4);
@@ -703,7 +714,7 @@ static int is_empty(char *s)
 static void parse_data1_tree(struct grs_read_info *p, const char *mc_stmnt,
                             data1_node *root)
 {
-    data1_marctab *marctab = data1_absyn_getmarctab(p->dh, root->u.root.absyn);
+    data1_marctab *marctab = data1_absyn_getmarctab(p->dh, root);
     data1_node *top = root->child;
     data1_node *field;
     mc_context *c;
@@ -733,9 +744,12 @@ static void parse_data1_tree(struct grs_read_info *p, const char *mc_stmnt,
        yaz_log(YLOG_LOG,"parse_data1_tree(): try LEADER from {%d} to {%d} positions",
            pf->interval.start, pf->interval.end);
 #endif 
-       new = data1_mk_tag_n(p->dh, p->mem, mc_stmnt, strlen(mc_stmnt), 0, top);
-       data1_mk_text_n(p->dh, p->mem, marctab->leader+pf->interval.start,
-           pf->interval.end-pf->interval.start+1, new);
+        if (marctab)
+        {
+            new = data1_mk_tag_n(p->dh, p->mem, mc_stmnt, strlen(mc_stmnt), 0, top);
+            data1_mk_text_n(p->dh, p->mem, marctab->leader+pf->interval.start,
+                            pf->interval.end-pf->interval.start+1, new);
+        }
     }
     else
     {
@@ -755,14 +769,15 @@ static void parse_data1_tree(struct grs_read_info *p, const char *mc_stmnt,
 
                field = cat_field(p, pf, buf, field);
                
-               pb = wrbuf_buf(buf);
+               wrbuf_cstr(buf);
+                pb = wrbuf_buf(buf);
                for (pb = strtok(pb, "\n"); pb; pb = strtok(NULL, "\n"))
                {
-                       if (!is_empty(pb))
-                       {
-                               new = data1_mk_tag_n(p->dh, p->mem, mc_stmnt, strlen(mc_stmnt), 0, top);
-                               data1_mk_text_n(p->dh, p->mem, pb, strlen(pb), new);
-                       }
+                    if (!is_empty(pb))
+                    {
+                        new = data1_mk_tag_n(p->dh, p->mem, mc_stmnt, strlen(mc_stmnt), 0, top);
+                        data1_mk_text_n(p->dh, p->mem, pb, strlen(pb), new);
+                    }
                }
            }
            else
@@ -773,7 +788,7 @@ static void parse_data1_tree(struct grs_read_info *p, const char *mc_stmnt,
     }
     mc_destroy_field(pf);
     mc_destroy_context(c);
-    wrbuf_free(buf, 1);
+    wrbuf_destroy(buf);
 }
 
 data1_node *grs_read_marcxml(struct grs_read_info *p)
@@ -784,7 +799,7 @@ data1_node *grs_read_marcxml(struct grs_read_info *p)
     if (!root)
        return 0;
        
-    for (e = data1_absyn_getelements(p->dh, root->u.root.absyn); e; e=e->next)
+    for (e = data1_absyn_getelements(p->dh, root); e; e=e->next)
     {
        data1_tag *tag = e->tag;
        
@@ -803,7 +818,7 @@ data1_node *grs_read_marc(struct grs_read_info *p)
     if (!root)
        return 0;
        
-    for (e = data1_absyn_getelements(p->dh, root->u.root.absyn); e; e=e->next)
+    for (e = data1_absyn_getelements(p->dh, root); e; e=e->next)
     {
        data1_tag *tag = e->tag;