2 * Iso2709 record management
4 * Europagate, 1994-1995.
7 * Revision 1.15 1995/03/31 10:42:41 adam
10 * Revision 1.14 1995/03/30 14:22:18 adam
11 * More work on new MARC anchor functions.
13 * Revision 1.13 1995/03/30 07:33:32 adam
14 * New 2709 function: iso2709_mk.
15 * First implementation of iso2709_a_insert.
17 * Revision 1.12 1995/03/29 16:08:56 adam
18 * Better error recovery when using bad records.
20 * Revision 1.11 1995/03/28 16:07:07 adam
21 * New function: iso2709_out. This function is the reverse of iso2709_cvt.
23 * Revision 1.10 1995/03/10 09:10:56 adam
24 * Removed dbc2709_cvt function. Makes heuristic guess for DBC2709 records.
26 * Revision 1.9 1995/03/08 12:36:39 adam
27 * New function: dbc2709_cvt.
29 * Revision 1.8 1995/03/08 12:03:15 adam
30 * Hack: When tags 00? are used, every separator (DC[1-3]) marks
31 * the end of the data field.
33 * Revision 1.7 1995/02/22 21:28:03 adam
36 * Revision 1.5 1995/02/22 15:24:14 adam
37 * Function iso2709_cvt makes a litte check for the format. It returns
38 * NULL if the buffer parameter can never be a MARC record.
40 * Revision 1.4 1995/02/15 17:45:44 adam
41 * Bug fix in iso2709 module.
43 * Revision 1.3 1995/02/10 17:05:18 adam
44 * New function iso2709_display to display MARC records in a
45 * line-by-line format. The iso2709_cvt function no longer
46 * prints the record to stderr.
48 * Revision 1.2 1995/02/10 16:50:32 adam
49 * Indicator field moved to 'struct iso2709_dir' from 'struct
51 * Function iso2709_rm implemented - to delete a MARC record.
53 * Revision 1.1.1.1 1995/02/09 17:27:11 adam
54 * Initial version of email gateway under CVS control.
66 static int atoin (const char *buf, int n)
72 val = val*10 + (*buf - '0');
78 static void strncpyx (char *d, const char *s, int n)
80 while (--n >= 0 && *s)
81 if (*s != ISO2709_IDFS)
91 char *iso2709_read (FILE *inf)
97 if (fread (length_str, 1, 5, inf) != 5)
99 size = atoin (length_str, 5);
102 if (!(buf = malloc (size+1)))
104 if (fread (buf+5, 1, size-5, inf) != (size-5))
109 memcpy (buf, length_str, 5);
114 Iso2709Rec iso2709_mk (void)
118 if (!(p = malloc (sizeof(*p))))
121 p->record_length = 0;
122 strncpyx (p->record_status, " ", 1);
123 strncpyx (p->implementation_codes, " ", 4);
124 p->indicator_length = 2;
125 p->identifier_length = 2;
127 strncpyx (p->user_systems, " ", 3);
128 p->length_data_entry = 4;
129 p->length_starting = 5;
130 p->length_implementation = 0;
131 strncpyx (p->future_use, " ", 1);
137 Iso2709Rec iso2709_cvt (const char *buf)
139 struct iso2709_dir **dpp, *dp;
143 if (!(p = malloc (sizeof(*p))))
146 /* deal with record label (24 characters) */
147 p->record_length = atoin (buf, 5);
148 strncpyx (p->record_status, buf+5, 1);
149 strncpyx (p->implementation_codes, buf+6, 4);
150 p->indicator_length = atoin (buf+10, 1);
151 p->identifier_length = atoin (buf+11, 1);
152 p->base_address = atoin (buf+12, 4);
153 strncpyx (p->user_systems, buf+17, 3);
155 if (p->record_length < 26)
160 p->length_data_entry = atoin (buf+20, 1);
161 p->length_starting = atoin (buf+21, 1);
162 p->length_implementation = atoin (buf+22, 1);
163 strncpyx (p->future_use, buf+23, 1);
165 /* deal with directory */
168 while (buf[pos] != ISO2709_FS)
170 if (!(*dpp = malloc (sizeof(**dpp))))
176 (*dpp)->fields = NULL;
177 (*dpp)->indicator = NULL;
178 strncpyx ((*dpp)->tag, buf+pos, 3);
180 (*dpp)->length = atoin (buf+pos, p->length_data_entry);
181 pos += p->length_data_entry;
182 (*dpp)->offset = atoin (buf+pos, p->length_starting);
183 pos += p->length_starting + p->length_implementation;
186 if (pos > p->record_length)
193 /* deal with datafields */
194 for (dp = p->directory; dp; dp = dp->next)
197 struct iso2709_field **fpp;
198 int dpos = pos+dp->offset;
199 int epos = pos+dp->offset+dp->length-1;
208 if (!(*fpp = malloc (sizeof(**fpp))))
216 if (p->indicator_length)
219 if (buf[dpos+p->indicator_length] != ISO2709_IDFS)
222 if (!memcmp (dp->tag, "00", 2))
226 else if (!memcmp (dp->tag, "00", 2))
228 if (identifier_flag && p->indicator_length)
230 if (!(dp->indicator = malloc (p->indicator_length+1)))
235 strncpyx (dp->indicator, buf+dpos, p->indicator_length);
236 dpos += p->indicator_length;
239 dp->indicator = NULL;
243 if (p->identifier_length && identifier_flag)
245 strncpyx ((*fpp)->identifier, buf+dpos+1,
246 p->identifier_length-1);
247 dpos_n = dpos += p->identifier_length;
248 while (buf[dpos_n] != ISO2709_FS && buf[dpos_n] != ISO2709_RS
249 && buf[dpos_n] != ISO2709_IDFS && dpos_n < epos)
254 *(*fpp)->identifier = '\0';
256 while (buf[dpos_n] != ISO2709_FS && buf[dpos_n] != ISO2709_RS
260 if (!((*fpp)->data = malloc (dpos_n - dpos + 1)))
265 strncpyx ((*fpp)->data, buf+dpos, dpos_n - dpos);
270 if (buf[dpos] != ISO2709_FS && buf[dpos] != ISO2709_RS)
271 fprintf (stderr, "Missing separator at end of field "
272 "in %s %s\n", dp->tag, (*fpp)->identifier);
275 if (buf[dpos] == ISO2709_FS || buf[dpos] == ISO2709_RS)
277 fprintf (stderr, "Unexpected separator inside field %s %s\n",
278 dp->tag, (*fpp)->identifier);
282 if (!(*fpp = malloc (sizeof(**fpp))))
293 void iso2709_rm (Iso2709Rec rec)
295 struct iso2709_dir *dir, *dir1;
297 for (dir = rec->directory; dir; dir = dir1)
299 struct iso2709_field *field, *field1;
301 for (field = dir->fields; field; field = field1)
304 field1 = field->next;
307 free (dir->indicator);