2 * IR toolkit for tcl/tk
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
8 * Revision 1.9 1996-07-03 13:31:13 adam
9 * The xmalloc/xfree functions from YAZ are used to manage memory.
11 * Revision 1.8 1995/11/14 16:48:00 adam
12 * Bug fix: record extraction in line mode merged lines with same tag.
14 * Revision 1.7 1995/11/09 15:24:02 adam
15 * Allow charsets [..] in record match.
17 * Revision 1.6 1995/08/28 12:21:22 adam
18 * Removed lines and list as synonyms of list in MARC extractron.
19 * Configure searches also for tk4.0 / tcl7.4.
21 * Revision 1.5 1995/06/30 12:39:26 adam
22 * Bug fix: loadFile didn't set record type.
23 * The MARC routines are a little less strict in the interpretation.
24 * Script display.tcl replaces the old marc.tcl.
25 * New interactive script: shell.tcl.
27 * Revision 1.4 1995/06/22 13:15:09 adam
28 * Feature: SUTRS. Setting getSutrs implemented.
29 * Work on display formats.
30 * Preferred record syntax can be set by the user.
32 * Revision 1.3 1995/05/29 08:44:26 adam
33 * Work on delete of objects.
35 * Revision 1.2 1995/05/26 11:44:11 adam
36 * Bugs fixed. More work on MARC utilities and queries. Test
37 * client is up-to-date again.
39 * Revision 1.1 1995/05/26 08:54:19 adam
40 * New MARC utilities. Uses prefix query.
51 #define ISO2709_RS 035
52 #define ISO2709_FS 036
53 #define ISO2709_IDFS 037
55 static int atoi_n (const char *buf, int len)
59 if (!isdigit (buf[len-1]))
64 val = val*10 + (*buf - '0');
70 static int marc_compare (const char *f, const char *p)
78 for (; (ch = *p) && *f; f++, p++)
107 char *ir_tcl_fread_marc (FILE *inf, size_t *size)
112 if (fread (length, 1, 5, inf) != 5)
114 *size = atoi_n (length, 5);
117 if (!(buf = xmalloc (*size+1)))
119 if (fread (buf+5, 1, *size-5, inf) != (*size-5))
124 memcpy (buf, length, 5);
129 int ir_tcl_get_marc (Tcl_Interp *interp, const char *buf,
130 int argc, char **argv)
134 int indicator_length;
135 int identifier_length;
137 int length_data_entry;
139 int length_implementation;
143 if (!strcmp (argv[3], "field"))
145 else if (!strcmp (argv[3], "line"))
149 Tcl_AppendResult (interp, "Unknown MARC extract mode", NULL);
154 Tcl_AppendResult (interp, "Not a MARC record", NULL);
157 record_length = atoi_n (buf, 5);
158 if (record_length < 25)
160 Tcl_AppendResult (interp, "Not a MARC record", NULL);
163 indicator_length = atoi_n (buf+10, 1);
164 identifier_length = atoi_n (buf+11, 1);
165 base_address = atoi_n (buf+12, 4);
167 length_data_entry = atoi_n (buf+20, 1);
168 length_starting = atoi_n (buf+21, 1);
169 length_implementation = atoi_n (buf+22, 1);
171 for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
172 entry_p += 3+length_data_entry+length_starting;
173 base_address = entry_p+1;
174 for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
182 char identifier[128];
185 memcpy (tag, buf+entry_p, 3);
188 data_length = atoi_n (buf+entry_p, length_data_entry);
189 entry_p += length_data_entry;
190 data_offset = atoi_n (buf+entry_p, length_starting);
191 entry_p += length_starting;
192 i = data_offset + base_address;
193 end_offset = i+data_length-1;
195 if (memcmp (tag, "00", 2) && indicator_length)
197 for (j = 0; j<indicator_length; j++)
198 indicator[j] = buf[i++];
201 if (marc_compare (tag, argv[4]) || marc_compare (indicator, argv[5]))
203 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS && i < end_offset)
207 if (memcmp (tag, "00", 2) && identifier_length)
210 for (j = 1; j<identifier_length; j++)
211 identifier[j-1] = buf[i++];
212 identifier[j-1] = '\0';
213 for (i0 = i; buf[i] != ISO2709_RS &&
214 buf[i] != ISO2709_IDFS &&
215 buf[i] != ISO2709_FS && i < end_offset;
221 for (i0 = i; buf[i] != ISO2709_RS &&
222 buf[i] != ISO2709_FS && i < end_offset;
227 if (marc_compare (identifier, argv[6])==0)
229 char *data = xmalloc (i-i0+1);
231 memcpy (data, buf+i0, i-i0);
235 if (strcmp (tag, ptag))
238 Tcl_AppendResult (interp, "}} ", NULL);
240 Tcl_AppendResult (interp, "{", tag, " {} {", NULL);
242 Tcl_AppendResult (interp, "{", tag, " {",
243 indicator, "} {", NULL);
247 Tcl_AppendResult (interp, "{{}", NULL);
249 Tcl_AppendResult (interp, "{", identifier, NULL);
250 Tcl_AppendElement (interp, data);
251 Tcl_AppendResult (interp, "} ", NULL);
254 Tcl_AppendElement (interp, data);
258 if (mode == 'l' && *ptag)
259 Tcl_AppendResult (interp, "}} ", NULL);
261 logf (LOG_WARN, "MARC: separator but not at end of field");
262 if (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS)
263 logf (LOG_WARN, "MARC: no separator at end of field");