1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2012 Index Data
3 * See the file LICENSE for details.
7 * \file marc_read_line.c
8 * \brief Implements reading of MARC in line format
23 #include <yaz/marcdisp.h>
24 #include <yaz/wrbuf.h>
25 #include <yaz/yaz-util.h>
27 int yaz_gets(int (*getbyte)(void *client_data),
28 void (*ungetbyte)(int b, void *client_data),
33 int ch = getbyte(client_data);
35 while (ch != '\0' && ch != '\r' && ch != '\n')
39 ch = getbyte(client_data);
43 ch = getbyte(client_data);
44 if (ch != '\n' && ch != '\0')
45 ungetbyte(ch, client_data);
49 ch = getbyte(client_data);
50 if (ch != '\r' && ch != '\0')
51 ungetbyte(ch, client_data);
60 static int yaz_marc_line_gets(int (*getbyte)(void *client_data),
61 void (*ungetbyte)(int b, void *client_data),
68 more = yaz_gets(getbyte, ungetbyte, client_data, w);
77 int ch = getbyte(client_data);
81 ungetbyte(ch, client_data);
85 if (wrbuf_len(w) > 60 && wrbuf_buf(w)[wrbuf_len(w)-1] == '=')
86 wrbuf_cut_right(w, 1);
89 more = yaz_gets(getbyte, ungetbyte, client_data, w);
95 int yaz_marc_read_line(yaz_marc_t mt,
96 int (*getbyte)(void *client_data),
97 void (*ungetbyte)(int b, void *client_data),
100 int indicator_length;
101 int identifier_length;
103 int length_data_entry;
105 int length_implementation;
108 int header_created = 0;
109 WRBUF wrbuf_line = wrbuf_alloc();
113 while (yaz_marc_line_gets(getbyte, ungetbyte, client_data, wrbuf_line))
115 const char *line = wrbuf_cstr(wrbuf_line);
117 size_t line_len = strlen(line);
118 if (line_len == 0) /* empty line indicates end of record */
123 else if (line[0] == '$') /* indicates beginning/end of record */
128 else if (line[0] == '(') /* annotation, skip it */
130 else if (line_len == 24 && atoi_n_check(line, 5, &val))
132 /* deal with header lines: 00366nam 22001698a 4500
137 yaz_marc_set_leader(mt, line,
143 &length_implementation);
146 else if (line_len > 4 && line[0] != ' ' && line[1] != ' '
147 && line[2] != ' ' && line[3] == ' ' )
149 /* deal with data/control lines: 245 12 ........ */
151 const char *datafield_start = line+6;
155 memcpy(tag, line, 3);
157 if (line_len >= 8) /* control - or datafield ? */
159 if (*datafield_start == ' ')
160 datafield_start++; /* skip blank after indicator */
162 if (strchr("$_*", *datafield_start))
164 marker_ch = *datafield_start;
165 if (datafield_start[2] == ' ')
166 marker_skip = 1; /* subfields has blank before data */
171 const char *leader = "01000cam 2200265 i 4500";
173 yaz_marc_set_leader(mt, leader,
179 &length_implementation);
184 { /* control field */
185 yaz_marc_add_controlfield(mt, tag, line+4, strlen(line+4));
189 const char *indicator = line+4;
190 int indicator_len = 2;
191 const char *cp = datafield_start;
193 yaz_marc_add_datafield(mt, tag, indicator, indicator_len);
199 assert(cp[0] == marker_ch);
202 while ((next = strchr(next, marker_ch)))
204 if ((next[1] >= 'A' && next[1] <= 'Z')
205 ||(next[1] >= 'a' && next[1] <= 'z'))
209 else if (next[2] == ' ')
216 len = next - cp - marker_skip;
220 /* remove ' ' after subfield marker */
221 char *cp_blank = strchr(cp, ' ');
225 while (cp_blank != cp)
227 cp_blank[0] = cp_blank[-1];
233 yaz_marc_add_subfield(mt, cp, len);
242 yaz_marc_cprintf(mt, "Ignoring line: %s", line);
245 wrbuf_destroy(wrbuf_line);
254 * c-file-style: "Stroustrup"
255 * indent-tabs-mode: nil
257 * vim: shiftwidth=4 tabstop=8 expandtab