2 * IR toolkit for tcl/tk
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
8 * Revision 1.3 1995-05-29 08:44:26 adam
9 * Work on delete of objects.
11 * Revision 1.2 1995/05/26 11:44:11 adam
12 * Bugs fixed. More work on MARC utilities and queries. Test
13 * client is up-to-date again.
15 * Revision 1.1 1995/05/26 08:54:19 adam
16 * New MARC utilities. Uses prefix query.
27 #define ISO2709_RS 035
28 #define ISO2709_FS 036
29 #define ISO2709_IDFS 037
31 static int atoi_n (const char *buf, int len)
38 val = val*10 + (*buf - '0');
44 static int marc_compare (const char *f, const char *p)
50 for (; *f && *p; f++, p++)
60 char *ir_tcl_fread_marc (FILE *inf, size_t *size)
65 if (fread (length, 1, 5, inf) != 5)
67 *size = atoi_n (length, 5);
70 if (!(buf = malloc (*size+1)))
72 if (fread (buf+5, 1, *size-5, inf) != (*size-5))
77 memcpy (buf, length, 5);
82 int ir_tcl_get_marc (Tcl_Interp *interp, const char *buf,
83 int argc, char **argv)
88 int identifier_length;
90 int length_data_entry;
92 int length_implementation;
97 if (!strcmp (argv[3], "field"))
99 else if (!strcmp (argv[3], "lines") || !strcmp (argv[3], "list"))
103 Tcl_AppendResult (interp, "Unknown MARC extract mode", NULL);
106 record_length = atoi_n (buf, 5);
107 if (record_length < 25)
109 Tcl_AppendResult (interp, "Not a MARC record", NULL);
112 indicator_length = atoi_n (buf+10, 1);
113 identifier_length = atoi_n (buf+11, 1);
114 base_address = atoi_n (buf+12, 4);
116 length_data_entry = atoi_n (buf+20, 1);
117 length_starting = atoi_n (buf+21, 1);
118 length_implementation = atoi_n (buf+22, 1);
120 for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
121 entry_p += 3+length_data_entry+length_starting;
122 base_address = entry_p+1;
123 for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
131 char identifier[128];
133 memcpy (tag, buf+entry_p, 3);
136 data_length = atoi_n (buf+entry_p, length_data_entry);
137 entry_p += length_data_entry;
138 data_offset = atoi_n (buf+entry_p, length_starting);
139 entry_p += length_starting;
140 i = data_offset + base_address;
141 end_offset = i+data_length-1;
143 if (memcmp (tag, "00", 2) && indicator_length)
145 for (j = 0; j<indicator_length; j++)
146 indicator[j] = buf[i++];
149 if (marc_compare (tag, argv[4]) || marc_compare (indicator, argv[5]))
151 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS && i < end_offset)
155 if (memcmp (tag, "00", 2) && identifier_length)
158 for (j = 1; j<identifier_length; j++)
159 identifier[j-1] = buf[i++];
160 identifier[j-1] = '\0';
161 for (i0 = i; buf[i] != ISO2709_RS &&
162 buf[i] != ISO2709_IDFS &&
163 buf[i] != ISO2709_FS && i < end_offset;
169 for (i0 = i; buf[i] != ISO2709_RS &&
170 buf[i] != ISO2709_FS && i < end_offset;
175 if (marc_compare (identifier, argv[6])==0)
177 char *data = malloc (i-i0+1);
179 memcpy (data, buf+i0, i-i0);
183 if (strcmp (tag, ptag))
186 Tcl_AppendResult (interp, "}} ", NULL);
188 Tcl_AppendResult (interp, "{", tag, " {} {", NULL);
190 Tcl_AppendResult (interp, "{", tag, " {",
191 indicator, "} {", NULL);
195 Tcl_AppendResult (interp, "{{}", NULL);
197 Tcl_AppendResult (interp, "{", identifier, NULL);
198 Tcl_AppendElement (interp, data);
199 Tcl_AppendResult (interp, "} ", NULL);
202 Tcl_AppendElement (interp, data);
207 logf (LOG_WARN, "MARC: separator but not at end of field");
208 if (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS)
209 logf (LOG_WARN, "MARC: no separator at end of field");
211 if (mode == 'l' && *ptag)
212 Tcl_AppendResult (interp, "}} ", NULL);