2 * IR toolkit for tcl/tk
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
8 * Revision 1.6 1995-08-28 12:21:22 adam
9 * Removed lines and list as synonyms of list in MARC extractron.
10 * Configure searches also for tk4.0 / tcl7.4.
12 * Revision 1.5 1995/06/30 12:39:26 adam
13 * Bug fix: loadFile didn't set record type.
14 * The MARC routines are a little less strict in the interpretation.
15 * Script display.tcl replaces the old marc.tcl.
16 * New interactive script: shell.tcl.
18 * Revision 1.4 1995/06/22 13:15:09 adam
19 * Feature: SUTRS. Setting getSutrs implemented.
20 * Work on display formats.
21 * Preferred record syntax can be set by the user.
23 * Revision 1.3 1995/05/29 08:44:26 adam
24 * Work on delete of objects.
26 * Revision 1.2 1995/05/26 11:44:11 adam
27 * Bugs fixed. More work on MARC utilities and queries. Test
28 * client is up-to-date again.
30 * Revision 1.1 1995/05/26 08:54:19 adam
31 * New MARC utilities. Uses prefix query.
42 #define ISO2709_RS 035
43 #define ISO2709_FS 036
44 #define ISO2709_IDFS 037
46 static int atoi_n (const char *buf, int len)
50 if (!isdigit (buf[len-1]))
55 val = val*10 + (*buf - '0');
61 static int marc_compare (const char *f, const char *p)
67 for (; *f && *p; f++, p++)
77 char *ir_tcl_fread_marc (FILE *inf, size_t *size)
82 if (fread (length, 1, 5, inf) != 5)
84 *size = atoi_n (length, 5);
87 if (!(buf = malloc (*size+1)))
89 if (fread (buf+5, 1, *size-5, inf) != (*size-5))
94 memcpy (buf, length, 5);
99 int ir_tcl_get_marc (Tcl_Interp *interp, const char *buf,
100 int argc, char **argv)
104 int indicator_length;
105 int identifier_length;
107 int length_data_entry;
109 int length_implementation;
114 if (!strcmp (argv[3], "field"))
116 else if (!strcmp (argv[3], "line"))
120 Tcl_AppendResult (interp, "Unknown MARC extract mode", NULL);
125 Tcl_AppendResult (interp, "Not a MARC record", NULL);
128 record_length = atoi_n (buf, 5);
129 if (record_length < 25)
131 Tcl_AppendResult (interp, "Not a MARC record", NULL);
134 indicator_length = atoi_n (buf+10, 1);
135 identifier_length = atoi_n (buf+11, 1);
136 base_address = atoi_n (buf+12, 4);
138 length_data_entry = atoi_n (buf+20, 1);
139 length_starting = atoi_n (buf+21, 1);
140 length_implementation = atoi_n (buf+22, 1);
142 for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
143 entry_p += 3+length_data_entry+length_starting;
144 base_address = entry_p+1;
145 for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
153 char identifier[128];
155 memcpy (tag, buf+entry_p, 3);
158 data_length = atoi_n (buf+entry_p, length_data_entry);
159 entry_p += length_data_entry;
160 data_offset = atoi_n (buf+entry_p, length_starting);
161 entry_p += length_starting;
162 i = data_offset + base_address;
163 end_offset = i+data_length-1;
165 if (memcmp (tag, "00", 2) && indicator_length)
167 for (j = 0; j<indicator_length; j++)
168 indicator[j] = buf[i++];
171 if (marc_compare (tag, argv[4]) || marc_compare (indicator, argv[5]))
173 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS && i < end_offset)
177 if (memcmp (tag, "00", 2) && identifier_length)
180 for (j = 1; j<identifier_length; j++)
181 identifier[j-1] = buf[i++];
182 identifier[j-1] = '\0';
183 for (i0 = i; buf[i] != ISO2709_RS &&
184 buf[i] != ISO2709_IDFS &&
185 buf[i] != ISO2709_FS && i < end_offset;
191 for (i0 = i; buf[i] != ISO2709_RS &&
192 buf[i] != ISO2709_FS && i < end_offset;
197 if (marc_compare (identifier, argv[6])==0)
199 char *data = malloc (i-i0+1);
201 memcpy (data, buf+i0, i-i0);
205 if (strcmp (tag, ptag))
208 Tcl_AppendResult (interp, "}} ", NULL);
210 Tcl_AppendResult (interp, "{", tag, " {} {", NULL);
212 Tcl_AppendResult (interp, "{", tag, " {",
213 indicator, "} {", NULL);
217 Tcl_AppendResult (interp, "{{}", NULL);
219 Tcl_AppendResult (interp, "{", identifier, NULL);
220 Tcl_AppendElement (interp, data);
221 Tcl_AppendResult (interp, "} ", NULL);
224 Tcl_AppendElement (interp, data);
229 logf (LOG_WARN, "MARC: separator but not at end of field");
230 if (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS)
231 logf (LOG_WARN, "MARC: no separator at end of field");
233 if (mode == 'l' && *ptag)
234 Tcl_AppendResult (interp, "}} ", NULL);