2 * IR toolkit for tcl/tk
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
8 * Revision 1.4 1995-06-22 13:15:09 adam
9 * Feature: SUTRS. Setting getSutrs implemented.
10 * Work on display formats.
11 * Preferred record syntax can be set by the user.
13 * Revision 1.3 1995/05/29 08:44:26 adam
14 * Work on delete of objects.
16 * Revision 1.2 1995/05/26 11:44:11 adam
17 * Bugs fixed. More work on MARC utilities and queries. Test
18 * client is up-to-date again.
20 * Revision 1.1 1995/05/26 08:54:19 adam
21 * New MARC utilities. Uses prefix query.
32 #define ISO2709_RS 035
33 #define ISO2709_FS 036
34 #define ISO2709_IDFS 037
36 static int atoi_n (const char *buf, int len)
43 val = val*10 + (*buf - '0');
51 static int marc_compare (const char *f, const char *p)
57 for (; *f && *p; f++, p++)
67 char *ir_tcl_fread_marc (FILE *inf, size_t *size)
72 if (fread (length, 1, 5, inf) != 5)
74 *size = atoi_n (length, 5);
77 if (!(buf = malloc (*size+1)))
79 if (fread (buf+5, 1, *size-5, inf) != (*size-5))
84 memcpy (buf, length, 5);
89 int ir_tcl_get_marc (Tcl_Interp *interp, const char *buf,
90 int argc, char **argv)
95 int identifier_length;
97 int length_data_entry;
99 int length_implementation;
104 if (!strcmp (argv[3], "field"))
106 else if (!strcmp (argv[3], "lines") || !strcmp (argv[3], "list"))
110 Tcl_AppendResult (interp, "Unknown MARC extract mode", NULL);
115 Tcl_AppendResult (interp, "Not a MARC record", NULL);
118 record_length = atoi_n (buf, 5);
119 if (record_length < 25)
121 Tcl_AppendResult (interp, "Not a MARC record", NULL);
124 indicator_length = atoi_n (buf+10, 1);
125 identifier_length = atoi_n (buf+11, 1);
126 base_address = atoi_n (buf+12, 4);
128 length_data_entry = atoi_n (buf+20, 1);
129 length_starting = atoi_n (buf+21, 1);
130 length_implementation = atoi_n (buf+22, 1);
132 for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
133 entry_p += 3+length_data_entry+length_starting;
134 base_address = entry_p+1;
135 for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
143 char identifier[128];
145 memcpy (tag, buf+entry_p, 3);
148 data_length = atoi_n (buf+entry_p, length_data_entry);
149 entry_p += length_data_entry;
150 data_offset = atoi_n (buf+entry_p, length_starting);
151 entry_p += length_starting;
152 i = data_offset + base_address;
153 end_offset = i+data_length-1;
155 if (memcmp (tag, "00", 2) && indicator_length)
157 for (j = 0; j<indicator_length; j++)
158 indicator[j] = buf[i++];
161 if (marc_compare (tag, argv[4]) || marc_compare (indicator, argv[5]))
163 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS && i < end_offset)
167 if (memcmp (tag, "00", 2) && identifier_length)
170 for (j = 1; j<identifier_length; j++)
171 identifier[j-1] = buf[i++];
172 identifier[j-1] = '\0';
173 for (i0 = i; buf[i] != ISO2709_RS &&
174 buf[i] != ISO2709_IDFS &&
175 buf[i] != ISO2709_FS && i < end_offset;
181 for (i0 = i; buf[i] != ISO2709_RS &&
182 buf[i] != ISO2709_FS && i < end_offset;
187 if (marc_compare (identifier, argv[6])==0)
189 char *data = malloc (i-i0+1);
191 memcpy (data, buf+i0, i-i0);
195 if (strcmp (tag, ptag))
198 Tcl_AppendResult (interp, "}} ", NULL);
200 Tcl_AppendResult (interp, "{", tag, " {} {", NULL);
202 Tcl_AppendResult (interp, "{", tag, " {",
203 indicator, "} {", NULL);
207 Tcl_AppendResult (interp, "{{}", NULL);
209 Tcl_AppendResult (interp, "{", identifier, NULL);
210 Tcl_AppendElement (interp, data);
211 Tcl_AppendResult (interp, "} ", NULL);
214 Tcl_AppendElement (interp, data);
219 logf (LOG_WARN, "MARC: separator but not at end of field");
220 if (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS)
221 logf (LOG_WARN, "MARC: no separator at end of field");
223 if (mode == 'l' && *ptag)
224 Tcl_AppendResult (interp, "}} ", NULL);