2 * IR toolkit for tcl/tk
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.1 1995-05-26 08:54:19 adam
8 * New MARC utilities. Uses prefix query.
19 #define ISO2709_RS 035
20 #define ISO2709_FS 036
21 #define ISO2709_IDFS 037
23 int atoi_n (const char *buf, int len)
30 val = val*10 + (*buf - '0');
36 static int marc_compare (const char *f, const char *p)
42 for (; *f && *p; f++, p++)
54 char *ir_tcl_fread_marc (FILE *inf, size_t *size)
59 if (fread (length, 1, 5, inf) != 5)
61 *size = atoi_n (length, 5);
64 if (!(buf = malloc (*size+1)))
66 if (fread (buf+5, 1, *size-5, inf) != (*size-5))
71 memcpy (buf, length, 5);
76 int ir_tcl_get_marc (Tcl_Interp *interp, const char *buf,
77 int argc, char **argv)
82 int identifier_length;
84 int length_data_entry;
86 int length_implementation;
89 if (!strcmp (argv[3], "field"))
91 else if (!strcmp (argv[3], "lines"))
95 Tcl_AppendResult (interp, "Unknown MARC extract mode", NULL);
98 record_length = atoi_n (buf, 5);
99 if (record_length < 25)
101 Tcl_AppendResult (interp, "Not a MARC record", NULL);
104 indicator_length = atoi_n (buf+10, 1);
105 identifier_length = atoi_n (buf+11, 1);
106 base_address = atoi_n (buf+12, 4);
108 length_data_entry = atoi_n (buf+20, 1);
109 length_starting = atoi_n (buf+21, 1);
110 length_implementation = atoi_n (buf+22, 1);
112 for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
113 entry_p += 3+length_data_entry+length_starting;
114 base_address = entry_p+1;
115 for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
123 char identifier[128];
125 memcpy (tag, buf+entry_p, 3);
128 data_length = atoi_n (buf+entry_p, length_data_entry);
129 entry_p += length_data_entry;
130 data_offset = atoi_n (buf+entry_p, length_starting);
131 entry_p += length_starting;
132 i = data_offset + base_address;
133 end_offset = i+data_length-1;
135 if (memcmp (tag, "00", 2) && indicator_length)
137 for (j = 0; j<indicator_length; j++)
138 indicator[j] = buf[i++];
141 if (marc_compare (tag, argv[4]) || marc_compare (indicator, argv[5]))
143 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS && i < end_offset)
145 if (memcmp (tag, "00", 2) && identifier_length)
149 for (j = 1; j<identifier_length; j++)
150 identifier[j-1] = buf[i++];
151 identifier[j-1] = '\0';
152 for (i0 = i; buf[i] != ISO2709_RS &&
153 buf[i] != ISO2709_IDFS &&
154 buf[i] != ISO2709_FS && i < end_offset;
157 if (marc_compare (identifier, argv[6])==0)
159 char *data = malloc (i-i0+1);
161 memcpy (data, buf+i0, i-i0);
163 Tcl_AppendElement (interp, data);
171 for (i0 = i; buf[i] != ISO2709_RS &&
172 buf[i] != ISO2709_FS && i < end_offset;
175 if (marc_compare (NULL, argv[6])==0)
177 char *data = malloc (i-i0+1);
179 memcpy (data, buf+i0, i-i0);
181 Tcl_AppendElement (interp, data);
188 fprintf (outf, "-- separator but not at end of field\n");
189 if (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS)
190 fprintf (outf, "-- no separator at end of field\n");
196 int ir_tcl_get_marc_fields(Tcl_Interp *interp, const char *buf,
197 size_t size, int argc, char **argv)
204 a = iso2709_a_mk (rec);
205 while (iso2709_a_search (a, argv[4], argv[5], argv[6]))
207 if (!(iso2709_a_info_field (a, NULL, NULL, NULL, &data)))
209 Tcl_AppendElement (interp, data);
216 int ir_tcl_get_marc_lines(Tcl_Interp *interp, const char *buf,
217 size_t size, int argc, char **argv)
228 a = iso2709_a_mk (rec);
229 while (iso2709_a_search (a, argv[4], argv[5], argv[6]))
231 if (!(iso2709_a_info_field (a, &tag, &indicator, &identifier, &data)))
233 if (strcmp (tag, ptag))
236 Tcl_AppendResult (interp, "}} ", NULL);
238 Tcl_AppendResult (interp, "{", tag, " {} {", NULL);
240 Tcl_AppendResult (interp, "{", tag, " {", indicator,
245 Tcl_AppendResult (interp, "{{}", NULL);
247 Tcl_AppendResult (interp, "{", identifier, NULL);
248 Tcl_AppendElement (interp, data);
249 Tcl_AppendResult (interp, "} ", NULL);
253 Tcl_AppendResult (interp, "}} ", NULL);