2 * IR toolkit for tcl/tk
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
8 * Revision 1.5 1995-06-30 12:39:26 adam
9 * Bug fix: loadFile didn't set record type.
10 * The MARC routines are a little less strict in the interpretation.
11 * Script display.tcl replaces the old marc.tcl.
12 * New interactive script: shell.tcl.
14 * Revision 1.4 1995/06/22 13:15:09 adam
15 * Feature: SUTRS. Setting getSutrs implemented.
16 * Work on display formats.
17 * Preferred record syntax can be set by the user.
19 * Revision 1.3 1995/05/29 08:44:26 adam
20 * Work on delete of objects.
22 * Revision 1.2 1995/05/26 11:44:11 adam
23 * Bugs fixed. More work on MARC utilities and queries. Test
24 * client is up-to-date again.
26 * Revision 1.1 1995/05/26 08:54:19 adam
27 * New MARC utilities. Uses prefix query.
38 #define ISO2709_RS 035
39 #define ISO2709_FS 036
40 #define ISO2709_IDFS 037
42 static int atoi_n (const char *buf, int len)
46 if (!isdigit (buf[len-1]))
51 val = val*10 + (*buf - '0');
57 static int marc_compare (const char *f, const char *p)
63 for (; *f && *p; f++, p++)
73 char *ir_tcl_fread_marc (FILE *inf, size_t *size)
78 if (fread (length, 1, 5, inf) != 5)
80 *size = atoi_n (length, 5);
83 if (!(buf = malloc (*size+1)))
85 if (fread (buf+5, 1, *size-5, inf) != (*size-5))
90 memcpy (buf, length, 5);
95 int ir_tcl_get_marc (Tcl_Interp *interp, const char *buf,
96 int argc, char **argv)
100 int indicator_length;
101 int identifier_length;
103 int length_data_entry;
105 int length_implementation;
110 if (!strcmp (argv[3], "field"))
112 else if (!strcmp (argv[3], "lines") || !strcmp (argv[3], "list"))
116 Tcl_AppendResult (interp, "Unknown MARC extract mode", NULL);
121 Tcl_AppendResult (interp, "Not a MARC record", NULL);
124 record_length = atoi_n (buf, 5);
125 if (record_length < 25)
127 Tcl_AppendResult (interp, "Not a MARC record", NULL);
130 indicator_length = atoi_n (buf+10, 1);
131 identifier_length = atoi_n (buf+11, 1);
132 base_address = atoi_n (buf+12, 4);
134 length_data_entry = atoi_n (buf+20, 1);
135 length_starting = atoi_n (buf+21, 1);
136 length_implementation = atoi_n (buf+22, 1);
138 for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
139 entry_p += 3+length_data_entry+length_starting;
140 base_address = entry_p+1;
141 for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
149 char identifier[128];
151 memcpy (tag, buf+entry_p, 3);
154 data_length = atoi_n (buf+entry_p, length_data_entry);
155 entry_p += length_data_entry;
156 data_offset = atoi_n (buf+entry_p, length_starting);
157 entry_p += length_starting;
158 i = data_offset + base_address;
159 end_offset = i+data_length-1;
161 if (memcmp (tag, "00", 2) && indicator_length)
163 for (j = 0; j<indicator_length; j++)
164 indicator[j] = buf[i++];
167 if (marc_compare (tag, argv[4]) || marc_compare (indicator, argv[5]))
169 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS && i < end_offset)
173 if (memcmp (tag, "00", 2) && identifier_length)
176 for (j = 1; j<identifier_length; j++)
177 identifier[j-1] = buf[i++];
178 identifier[j-1] = '\0';
179 for (i0 = i; buf[i] != ISO2709_RS &&
180 buf[i] != ISO2709_IDFS &&
181 buf[i] != ISO2709_FS && i < end_offset;
187 for (i0 = i; buf[i] != ISO2709_RS &&
188 buf[i] != ISO2709_FS && i < end_offset;
193 if (marc_compare (identifier, argv[6])==0)
195 char *data = malloc (i-i0+1);
197 memcpy (data, buf+i0, i-i0);
201 if (strcmp (tag, ptag))
204 Tcl_AppendResult (interp, "}} ", NULL);
206 Tcl_AppendResult (interp, "{", tag, " {} {", NULL);
208 Tcl_AppendResult (interp, "{", tag, " {",
209 indicator, "} {", NULL);
213 Tcl_AppendResult (interp, "{{}", NULL);
215 Tcl_AppendResult (interp, "{", identifier, NULL);
216 Tcl_AppendElement (interp, data);
217 Tcl_AppendResult (interp, "} ", NULL);
220 Tcl_AppendElement (interp, data);
225 logf (LOG_WARN, "MARC: separator but not at end of field");
226 if (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS)
227 logf (LOG_WARN, "MARC: no separator at end of field");
229 if (mode == 'l' && *ptag)
230 Tcl_AppendResult (interp, "}} ", NULL);