2 * IR toolkit for tcl/tk
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
8 * Revision 1.10 1999-02-08 09:22:31 franck
9 * Added a grs mode for ir_tcl_get_marc which returns MARC records in a TCL
10 * structure similar to that of ir_tcl_get_grs.
12 * Revision 1.9 1996/07/03 13:31:13 adam
13 * The xmalloc/xfree functions from YAZ are used to manage memory.
15 * Revision 1.8 1995/11/14 16:48:00 adam
16 * Bug fix: record extraction in line mode merged lines with same tag.
18 * Revision 1.7 1995/11/09 15:24:02 adam
19 * Allow charsets [..] in record match.
21 * Revision 1.6 1995/08/28 12:21:22 adam
22 * Removed lines and list as synonyms of list in MARC extractron.
23 * Configure searches also for tk4.0 / tcl7.4.
25 * Revision 1.5 1995/06/30 12:39:26 adam
26 * Bug fix: loadFile didn't set record type.
27 * The MARC routines are a little less strict in the interpretation.
28 * Script display.tcl replaces the old marc.tcl.
29 * New interactive script: shell.tcl.
31 * Revision 1.4 1995/06/22 13:15:09 adam
32 * Feature: SUTRS. Setting getSutrs implemented.
33 * Work on display formats.
34 * Preferred record syntax can be set by the user.
36 * Revision 1.3 1995/05/29 08:44:26 adam
37 * Work on delete of objects.
39 * Revision 1.2 1995/05/26 11:44:11 adam
40 * Bugs fixed. More work on MARC utilities and queries. Test
41 * client is up-to-date again.
43 * Revision 1.1 1995/05/26 08:54:19 adam
44 * New MARC utilities. Uses prefix query.
55 #define ISO2709_RS 035
56 #define ISO2709_FS 036
57 #define ISO2709_IDFS 037
59 static int atoi_n (const char *buf, int len)
63 if (!isdigit (buf[len-1]))
68 val = val*10 + (*buf - '0');
74 static int marc_compare (const char *f, const char *p)
82 for (; (ch = *p) && *f; f++, p++)
111 char *ir_tcl_fread_marc (FILE *inf, size_t *size)
116 if (fread (length, 1, 5, inf) != 5)
118 *size = atoi_n (length, 5);
121 if (!(buf = xmalloc (*size+1)))
123 if (fread (buf+5, 1, *size-5, inf) != (*size-5))
128 memcpy (buf, length, 5);
133 int ir_tcl_get_marc (Tcl_Interp *interp, const char *buf,
134 int argc, char **argv)
138 int indicator_length;
139 int identifier_length;
141 int length_data_entry;
143 int length_implementation;
147 if (!strcmp (argv[3], "field"))
149 else if (!strcmp (argv[3], "line"))
151 else if (!strcmp (argv[3], "grs"))
155 Tcl_AppendResult (interp, "Unknown MARC extract mode", NULL);
160 Tcl_AppendResult (interp, "Not a MARC record", NULL);
163 record_length = atoi_n (buf, 5);
164 if (record_length < 25)
166 Tcl_AppendResult (interp, "Not a MARC record", NULL);
169 indicator_length = atoi_n (buf+10, 1);
170 identifier_length = atoi_n (buf+11, 1);
171 base_address = atoi_n (buf+12, 4);
173 length_data_entry = atoi_n (buf+20, 1);
174 length_starting = atoi_n (buf+21, 1);
175 length_implementation = atoi_n (buf+22, 1);
177 for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
178 entry_p += 3+length_data_entry+length_starting;
179 base_address = entry_p+1;
180 for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
188 char identifier[128];
191 memcpy (tag, buf+entry_p, 3);
194 data_length = atoi_n (buf+entry_p, length_data_entry);
195 entry_p += length_data_entry;
196 data_offset = atoi_n (buf+entry_p, length_starting);
197 entry_p += length_starting;
198 i = data_offset + base_address;
199 end_offset = i+data_length-1;
201 if (memcmp (tag, "00", 2) && indicator_length)
203 for (j = 0; j<indicator_length; j++)
204 indicator[j] = buf[i++];
207 if (marc_compare (tag, argv[4]) || marc_compare (indicator, argv[5]))
209 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS && i < end_offset)
213 if (memcmp (tag, "00", 2) && identifier_length)
216 for (j = 1; j<identifier_length; j++)
217 identifier[j-1] = buf[i++];
218 identifier[j-1] = '\0';
219 for (i0 = i; buf[i] != ISO2709_RS &&
220 buf[i] != ISO2709_IDFS &&
221 buf[i] != ISO2709_FS && i < end_offset;
227 for (i0 = i; buf[i] != ISO2709_RS &&
228 buf[i] != ISO2709_FS && i < end_offset;
233 if (marc_compare (identifier, argv[6])==0)
235 char *data = xmalloc (i-i0+1);
237 memcpy (data, buf+i0, i-i0);
241 if (strcmp (tag, ptag))
244 Tcl_AppendResult (interp, "}} ", NULL);
246 Tcl_AppendResult (interp, "{", tag, " {} {", NULL);
248 Tcl_AppendResult (interp, "{", tag, " {",
249 indicator, "} {", NULL);
253 Tcl_AppendResult (interp, "{{}", NULL);
255 Tcl_AppendResult (interp, "{", identifier, NULL);
256 Tcl_AppendElement (interp, data);
257 Tcl_AppendResult (interp, "} ", NULL);
259 else if (mode == 'g')
261 if (strcmp (tag, ptag))
264 Tcl_AppendResult (interp, "}} ", NULL);
266 Tcl_AppendResult (interp, "{ 0 numeric {", tag,
267 indicator, "} subtree {", NULL);
269 Tcl_AppendResult (interp, "{ 0 numeric ", tag,
274 Tcl_AppendResult (interp, "{3 string ", identifier,
277 Tcl_AppendResult (interp, "{1 numeric 19 string ",
279 Tcl_AppendElement (interp, data);
280 Tcl_AppendResult (interp, "} ", NULL);
283 Tcl_AppendElement (interp, data);
287 if (((mode == 'l') || (mode == 'g')) && *ptag)
288 Tcl_AppendResult (interp, "}} ", NULL);
290 logf (LOG_WARN, "MARC: separator but not at end of field");
291 if (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS)
292 logf (LOG_WARN, "MARC: no separator at end of field");