2 * Copyright (c) 1995, the EUROPAGATE consortium (see below).
4 * The EUROPAGATE consortium members are:
6 * University College Dublin
7 * Danmarks Teknologiske Videnscenter
8 * An Chomhairle Leabharlanna
9 * Consejo Superior de Investigaciones Cientificas
11 * Permission to use, copy, modify, distribute, and sell this software and
12 * its documentation, in whole or in part, for any purpose, is hereby granted,
15 * 1. This copyright and permission notice appear in all copies of the
16 * software and its documentation. Notices of copyright or attribution
17 * which appear at the beginning of any file must remain unchanged.
19 * 2. The names of EUROPAGATE or the project partners may not be used to
20 * endorse or promote products derived from this software without specific
21 * prior written permission.
23 * 3. Users of this software (implementors and gateway operators) agree to
24 * inform the EUROPAGATE consortium of their use of the software. This
25 * information will be used to evaluate the EUROPAGATE project and the
26 * software, and to plan further developments. The consortium may use
27 * the information in later publications.
29 * 4. Users of this software agree to make their best efforts, when
30 * documenting their use of the software, to acknowledge the EUROPAGATE
31 * consortium, and the role played by the software in their work.
33 * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
34 * EXPRESS, IMPLIED, OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
35 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
36 * IN NO EVENT SHALL THE EUROPAGATE CONSORTIUM OR ITS MEMBERS BE LIABLE
37 * FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF
38 * ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
39 * OR PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND
40 * ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
41 * USE OR PERFORMANCE OF THIS SOFTWARE.
45 * Iso2709 record management
47 * Europagate, 1994-1995.
50 * Revision 1.16 1995/05/16 09:40:53 adam
53 * Revision 1.15 1995/03/31 10:42:41 adam
56 * Revision 1.14 1995/03/30 14:22:18 adam
57 * More work on new MARC anchor functions.
59 * Revision 1.13 1995/03/30 07:33:32 adam
60 * New 2709 function: iso2709_mk.
61 * First implementation of iso2709_a_insert.
63 * Revision 1.12 1995/03/29 16:08:56 adam
64 * Better error recovery when using bad records.
66 * Revision 1.11 1995/03/28 16:07:07 adam
67 * New function: iso2709_out. This function is the reverse of iso2709_cvt.
69 * Revision 1.10 1995/03/10 09:10:56 adam
70 * Removed dbc2709_cvt function. Makes heuristic guess for DBC2709 records.
72 * Revision 1.9 1995/03/08 12:36:39 adam
73 * New function: dbc2709_cvt.
75 * Revision 1.8 1995/03/08 12:03:15 adam
76 * Hack: When tags 00? are used, every separator (DC[1-3]) marks
77 * the end of the data field.
79 * Revision 1.7 1995/02/22 21:28:03 adam
82 * Revision 1.5 1995/02/22 15:24:14 adam
83 * Function iso2709_cvt makes a litte check for the format. It returns
84 * NULL if the buffer parameter can never be a MARC record.
86 * Revision 1.4 1995/02/15 17:45:44 adam
87 * Bug fix in iso2709 module.
89 * Revision 1.3 1995/02/10 17:05:18 adam
90 * New function iso2709_display to display MARC records in a
91 * line-by-line format. The iso2709_cvt function no longer
92 * prints the record to stderr.
94 * Revision 1.2 1995/02/10 16:50:32 adam
95 * Indicator field moved to 'struct iso2709_dir' from 'struct
97 * Function iso2709_rm implemented - to delete a MARC record.
99 * Revision 1.1.1.1 1995/02/09 17:27:11 adam
100 * Initial version of email gateway under CVS control.
110 #include <iso2709p.h>
112 static int atoin (const char *buf, int n)
118 val = val*10 + (*buf - '0');
124 static void strncpyx (char *d, const char *s, int n)
126 while (--n >= 0 && *s)
127 if (*s != ISO2709_IDFS)
137 char *iso2709_read (FILE *inf)
143 if (fread (length_str, 1, 5, inf) != 5)
145 size = atoin (length_str, 5);
148 if (!(buf = malloc (size+1)))
150 if (fread (buf+5, 1, size-5, inf) != (size-5))
155 memcpy (buf, length_str, 5);
160 Iso2709Rec iso2709_mk (void)
164 if (!(p = malloc (sizeof(*p))))
167 p->record_length = 0;
168 strncpyx (p->record_status, " ", 1);
169 strncpyx (p->implementation_codes, " ", 4);
170 p->indicator_length = 2;
171 p->identifier_length = 2;
173 strncpyx (p->user_systems, " ", 3);
174 p->length_data_entry = 4;
175 p->length_starting = 5;
176 p->length_implementation = 0;
177 strncpyx (p->future_use, " ", 1);
183 Iso2709Rec iso2709_cvt (const char *buf)
185 struct iso2709_dir **dpp, *dp;
189 if (!(p = malloc (sizeof(*p))))
192 /* deal with record label (24 characters) */
193 p->record_length = atoin (buf, 5);
194 strncpyx (p->record_status, buf+5, 1);
195 strncpyx (p->implementation_codes, buf+6, 4);
196 p->indicator_length = atoin (buf+10, 1);
197 p->identifier_length = atoin (buf+11, 1);
198 p->base_address = atoin (buf+12, 4);
199 strncpyx (p->user_systems, buf+17, 3);
201 if (p->record_length < 26)
206 p->length_data_entry = atoin (buf+20, 1);
207 p->length_starting = atoin (buf+21, 1);
208 p->length_implementation = atoin (buf+22, 1);
209 strncpyx (p->future_use, buf+23, 1);
211 /* deal with directory */
214 while (buf[pos] != ISO2709_FS)
216 if (!(*dpp = malloc (sizeof(**dpp))))
222 (*dpp)->fields = NULL;
223 (*dpp)->indicator = NULL;
224 strncpyx ((*dpp)->tag, buf+pos, 3);
226 (*dpp)->length = atoin (buf+pos, p->length_data_entry);
227 pos += p->length_data_entry;
228 (*dpp)->offset = atoin (buf+pos, p->length_starting);
229 pos += p->length_starting + p->length_implementation;
232 if (pos > p->record_length)
239 /* deal with datafields */
240 for (dp = p->directory; dp; dp = dp->next)
243 struct iso2709_field **fpp;
244 int dpos = pos+dp->offset;
245 int epos = pos+dp->offset+dp->length-1;
254 if (!(*fpp = malloc (sizeof(**fpp))))
262 if (p->indicator_length)
265 if (buf[dpos+p->indicator_length] != ISO2709_IDFS)
268 if (!memcmp (dp->tag, "00", 2))
272 else if (!memcmp (dp->tag, "00", 2))
274 if (identifier_flag && p->indicator_length)
276 if (!(dp->indicator = malloc (p->indicator_length+1)))
281 strncpyx (dp->indicator, buf+dpos, p->indicator_length);
282 dpos += p->indicator_length;
285 dp->indicator = NULL;
289 if (p->identifier_length && identifier_flag)
291 strncpyx ((*fpp)->identifier, buf+dpos+1,
292 p->identifier_length-1);
293 dpos_n = dpos += p->identifier_length;
294 while (buf[dpos_n] != ISO2709_FS && buf[dpos_n] != ISO2709_RS
295 && buf[dpos_n] != ISO2709_IDFS && dpos_n < epos)
300 *(*fpp)->identifier = '\0';
302 while (buf[dpos_n] != ISO2709_FS && buf[dpos_n] != ISO2709_RS
306 if (!((*fpp)->data = malloc (dpos_n - dpos + 1)))
311 strncpyx ((*fpp)->data, buf+dpos, dpos_n - dpos);
316 if (buf[dpos] != ISO2709_FS && buf[dpos] != ISO2709_RS)
317 fprintf (stderr, "Missing separator at end of field "
318 "in %s %s\n", dp->tag, (*fpp)->identifier);
321 if (buf[dpos] == ISO2709_FS || buf[dpos] == ISO2709_RS)
323 fprintf (stderr, "Unexpected separator inside field %s %s\n",
324 dp->tag, (*fpp)->identifier);
328 if (!(*fpp = malloc (sizeof(**fpp))))
339 void iso2709_rm (Iso2709Rec rec)
341 struct iso2709_dir *dir, *dir1;
343 for (dir = rec->directory; dir; dir = dir1)
345 struct iso2709_field *field, *field1;
347 for (field = dir->fields; field; field = field1)
350 field1 = field->next;
353 free (dir->indicator);