2 * IR toolkit for tcl/tk
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
8 * Revision 1.8 1995-11-14 16:48:00 adam
9 * Bug fix: record extraction in line mode merged lines with same tag.
11 * Revision 1.7 1995/11/09 15:24:02 adam
12 * Allow charsets [..] in record match.
14 * Revision 1.6 1995/08/28 12:21:22 adam
15 * Removed lines and list as synonyms of list in MARC extractron.
16 * Configure searches also for tk4.0 / tcl7.4.
18 * Revision 1.5 1995/06/30 12:39:26 adam
19 * Bug fix: loadFile didn't set record type.
20 * The MARC routines are a little less strict in the interpretation.
21 * Script display.tcl replaces the old marc.tcl.
22 * New interactive script: shell.tcl.
24 * Revision 1.4 1995/06/22 13:15:09 adam
25 * Feature: SUTRS. Setting getSutrs implemented.
26 * Work on display formats.
27 * Preferred record syntax can be set by the user.
29 * Revision 1.3 1995/05/29 08:44:26 adam
30 * Work on delete of objects.
32 * Revision 1.2 1995/05/26 11:44:11 adam
33 * Bugs fixed. More work on MARC utilities and queries. Test
34 * client is up-to-date again.
36 * Revision 1.1 1995/05/26 08:54:19 adam
37 * New MARC utilities. Uses prefix query.
48 #define ISO2709_RS 035
49 #define ISO2709_FS 036
50 #define ISO2709_IDFS 037
52 static int atoi_n (const char *buf, int len)
56 if (!isdigit (buf[len-1]))
61 val = val*10 + (*buf - '0');
67 static int marc_compare (const char *f, const char *p)
75 for (; (ch = *p) && *f; f++, p++)
104 char *ir_tcl_fread_marc (FILE *inf, size_t *size)
109 if (fread (length, 1, 5, inf) != 5)
111 *size = atoi_n (length, 5);
114 if (!(buf = malloc (*size+1)))
116 if (fread (buf+5, 1, *size-5, inf) != (*size-5))
121 memcpy (buf, length, 5);
126 int ir_tcl_get_marc (Tcl_Interp *interp, const char *buf,
127 int argc, char **argv)
131 int indicator_length;
132 int identifier_length;
134 int length_data_entry;
136 int length_implementation;
140 if (!strcmp (argv[3], "field"))
142 else if (!strcmp (argv[3], "line"))
146 Tcl_AppendResult (interp, "Unknown MARC extract mode", NULL);
151 Tcl_AppendResult (interp, "Not a MARC record", NULL);
154 record_length = atoi_n (buf, 5);
155 if (record_length < 25)
157 Tcl_AppendResult (interp, "Not a MARC record", NULL);
160 indicator_length = atoi_n (buf+10, 1);
161 identifier_length = atoi_n (buf+11, 1);
162 base_address = atoi_n (buf+12, 4);
164 length_data_entry = atoi_n (buf+20, 1);
165 length_starting = atoi_n (buf+21, 1);
166 length_implementation = atoi_n (buf+22, 1);
168 for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
169 entry_p += 3+length_data_entry+length_starting;
170 base_address = entry_p+1;
171 for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
179 char identifier[128];
182 memcpy (tag, buf+entry_p, 3);
185 data_length = atoi_n (buf+entry_p, length_data_entry);
186 entry_p += length_data_entry;
187 data_offset = atoi_n (buf+entry_p, length_starting);
188 entry_p += length_starting;
189 i = data_offset + base_address;
190 end_offset = i+data_length-1;
192 if (memcmp (tag, "00", 2) && indicator_length)
194 for (j = 0; j<indicator_length; j++)
195 indicator[j] = buf[i++];
198 if (marc_compare (tag, argv[4]) || marc_compare (indicator, argv[5]))
200 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS && i < end_offset)
204 if (memcmp (tag, "00", 2) && identifier_length)
207 for (j = 1; j<identifier_length; j++)
208 identifier[j-1] = buf[i++];
209 identifier[j-1] = '\0';
210 for (i0 = i; buf[i] != ISO2709_RS &&
211 buf[i] != ISO2709_IDFS &&
212 buf[i] != ISO2709_FS && i < end_offset;
218 for (i0 = i; buf[i] != ISO2709_RS &&
219 buf[i] != ISO2709_FS && i < end_offset;
224 if (marc_compare (identifier, argv[6])==0)
226 char *data = malloc (i-i0+1);
228 memcpy (data, buf+i0, i-i0);
232 if (strcmp (tag, ptag))
235 Tcl_AppendResult (interp, "}} ", NULL);
237 Tcl_AppendResult (interp, "{", tag, " {} {", NULL);
239 Tcl_AppendResult (interp, "{", tag, " {",
240 indicator, "} {", NULL);
244 Tcl_AppendResult (interp, "{{}", NULL);
246 Tcl_AppendResult (interp, "{", identifier, NULL);
247 Tcl_AppendElement (interp, data);
248 Tcl_AppendResult (interp, "} ", NULL);
251 Tcl_AppendElement (interp, data);
255 if (mode == 'l' && *ptag)
256 Tcl_AppendResult (interp, "}} ", NULL);
258 logf (LOG_WARN, "MARC: separator but not at end of field");
259 if (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS)
260 logf (LOG_WARN, "MARC: no separator at end of field");