2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: marc_read_line.c,v 1.6 2007-03-18 13:00:37 adam Exp $
9 * \file marc_read_line.c
10 * \brief Implements reading of MARC in line format
26 #include <yaz/marcdisp.h>
27 #include <yaz/wrbuf.h>
28 #include <yaz/yaz-util.h>
30 int yaz_gets(int (*getbyte)(void *client_data),
31 void (*ungetbyte)(int b, void *client_data),
36 int ch = getbyte(client_data);
38 while (ch != '\0' && ch != '\r' && ch != '\n')
42 ch = getbyte(client_data);
46 ch = getbyte(client_data);
47 if (ch != '\n' && ch != '\0')
48 ungetbyte(ch, client_data);
52 ch = getbyte(client_data);
53 if (ch != '\r' && ch != '\0')
54 ungetbyte(ch, client_data);
63 static int yaz_marc_line_gets(int (*getbyte)(void *client_data),
64 void (*ungetbyte)(int b, void *client_data),
71 more = yaz_gets(getbyte, ungetbyte, client_data, w);
80 int ch = getbyte(client_data);
84 ungetbyte(ch, client_data);
88 if (wrbuf_len(w) > 60 && wrbuf_buf(w)[wrbuf_len(w)-1] == '=')
89 wrbuf_cut_right(w, 1);
92 more = yaz_gets(getbyte, ungetbyte, client_data, w);
98 int yaz_marc_read_line(yaz_marc_t mt,
99 int (*getbyte)(void *client_data),
100 void (*ungetbyte)(int b, void *client_data),
103 int indicator_length;
104 int identifier_length;
106 int length_data_entry;
108 int length_implementation;
111 int header_created = 0;
112 WRBUF wrbuf_line = wrbuf_alloc();
116 while (yaz_marc_line_gets(getbyte, ungetbyte, client_data, wrbuf_line))
118 const char *line = wrbuf_cstr(wrbuf_line);
120 size_t line_len = strlen(line);
121 if (line_len == 0) /* empty line indicates end of record */
126 else if (line[0] == '$') /* indicates beginning/end of record */
131 else if (line[0] == '(') /* annotation, skip it */
133 else if (line_len == 24 && atoi_n_check(line, 5, &val) && val >= 24)
135 /* deal with header lines: 00366nam 22001698a 4500
140 yaz_marc_set_leader(mt, line,
146 &length_implementation);
149 else if (line_len > 5 && line[0] != ' ' && line[1] != ' '
150 && line[2] != ' ' && line[3] == ' ' )
152 /* deal with data/control lines: 245 12 ........ */
154 const char *datafield_start = line+6;
158 memcpy(tag, line, 3);
160 if (line_len >= 8) /* control - or datafield ? */
162 if (*datafield_start == ' ')
163 datafield_start++; /* skip blank after indicator */
165 if (strchr("$_*", *datafield_start))
167 marker_ch = *datafield_start;
168 if (datafield_start[2] == ' ')
169 marker_skip = 1; /* subfields has blank before data */
174 const char *leader = "01000cam 2200265 i 4500";
176 yaz_marc_set_leader(mt, leader,
182 &length_implementation);
187 { /* control field */
188 yaz_marc_add_controlfield(mt, tag, line+4, strlen(line+4));
192 const char *indicator = line+4;
193 int indicator_len = 2;
194 const char *cp = datafield_start;
196 yaz_marc_add_datafield(mt, tag, indicator, indicator_len);
202 assert(cp[0] == marker_ch);
205 while ((next = strchr(next, marker_ch)))
207 if ((next[1] >= 'A' && next[1] <= 'Z')
208 ||(next[1] >= 'a' && next[1] <= 'z'))
212 else if (next[2] == ' ')
219 len = next - cp - marker_skip;
223 /* remove ' ' after subfield marker */
224 char *cp_blank = strchr(cp, ' ');
228 while (cp_blank != cp)
230 cp_blank[0] = cp_blank[-1];
237 yaz_marc_add_subfield(mt, cp, len);
246 yaz_marc_cprintf(mt, "Ignoring line: %s", line);
249 wrbuf_destroy(wrbuf_line);
258 * indent-tabs-mode: nil
260 * vim: shiftwidth=4 tabstop=8 expandtab