2 * Iso2709 record management
4 * Europagate, 1994-1995.
7 * Revision 1.8 1995/03/08 12:03:15 adam
8 * Hack: When tags 00? are used, every separator (DC[1-3]) marks
9 * the end of the data field.
11 * Revision 1.7 1995/02/22 21:28:03 adam
14 * Revision 1.5 1995/02/22 15:24:14 adam
15 * Function iso2709_cvt makes a litte check for the format. It returns
16 * NULL if the buffer parameter can never be a MARC record.
18 * Revision 1.4 1995/02/15 17:45:44 adam
19 * Bug fix in iso2709 module.
21 * Revision 1.3 1995/02/10 17:05:18 adam
22 * New function iso2709_display to display MARC records in a
23 * line-by-line format. The iso2709_cvt function no longer
24 * prints the record to stderr.
26 * Revision 1.2 1995/02/10 16:50:32 adam
27 * Indicator field moved to 'struct iso2709_dir' from 'struct
29 * Function iso2709_rm implemented - to delete a MARC record.
31 * Revision 1.1.1.1 1995/02/09 17:27:11 adam
32 * Initial version of email gateway under CVS control.
44 static int atoin (const char *buf, int n)
50 val = val*10 + (*buf - '0');
56 static void strncpyx (char *d, const char *s, int n)
58 while (--n >= 0 && *s)
59 if (*s != ISO2709_IDFS)
73 char *iso2709_read (FILE *inf)
79 if (fread (length_str, 1, 5, inf) != 5)
81 size = atoin (length_str, 5);
84 if (!(buf = malloc (size+1)))
86 if (fread (buf+5, 1, size-5, inf) != (size-5))
91 memcpy (buf, length_str, 5);
96 Iso2709Rec iso2709_cvt (const char *buf)
98 struct iso2709_dir **dpp, *dp;
102 if (!(p = malloc (sizeof(*p))))
105 /* deal with record label (24 characters) */
106 p->record_length = atoin (buf, 5);
107 strncpyx (p->record_status, buf+5, 1);
108 strncpyx (p->implementation_codes, buf+6, 4);
109 p->indicator_length = atoin (buf+10, 1);
110 p->identifier_length = atoin (buf+11, 1);
111 p->base_address = atoin (buf+12, 4);
112 strncpyx (p->user_systems, buf+17, 3);
114 if (p->record_length < 26)
119 p->length_data_entry = atoin (buf+20, 1);
120 p->length_starting = atoin (buf+21, 1);
121 p->length_implementation = atoin (buf+22, 1);
122 strncpyx (p->future_use, buf+23, 1);
124 /* deal with directory */
127 while (buf[pos] != ISO2709_FS)
129 *dpp = malloc (sizeof(**dpp));
131 strncpyx ((*dpp)->tag, buf+pos, 3);
133 (*dpp)->length = atoin (buf+pos, p->length_data_entry);
134 pos += p->length_data_entry;
135 (*dpp)->offset = atoin (buf+pos, p->length_starting);
136 pos += p->length_starting + p->length_implementation;
141 /* deal with datafields */
142 for (dp = p->directory; dp; dp = dp->next)
145 struct iso2709_field **fpp;
146 int dpos = pos+dp->offset;
150 *fpp = malloc (sizeof(**fpp));
152 if (p->indicator_length && memcmp (dp->tag, "00", 2))
154 dp->indicator = malloc (p->indicator_length+1);
155 strncpyx (dp->indicator, buf+dpos, p->indicator_length);
156 dpos += p->indicator_length;
159 dp->indicator = NULL;
160 printf ("tag=%.3s, dpos = %d, (0%o)\n", dp->tag, dpos, dpos);
161 if (memcmp (dp->tag, "00", 2))
168 if (p->identifier_length && !tag00)
170 (*fpp)->identifier = malloc (p->identifier_length+1);
171 strncpyx ((*fpp)->identifier, buf+dpos+1,
172 p->identifier_length-1);
173 dpos += p->identifier_length;
176 (*fpp)->identifier = NULL;
179 while (buf[dpos_n] != ISO2709_FS && buf[dpos_n] != ISO2709_IDFS &&
180 buf[dpos_n] != ISO2709_RS)
183 (*fpp)->data = malloc (dpos_n - dpos + 1);
184 strncpyx ((*fpp)->data, buf+dpos, dpos_n - dpos);
187 if (buf[dpos] == ISO2709_FS || buf[dpos] == ISO2709_RS || tag00)
191 *fpp = malloc (sizeof(**fpp));
198 void iso2709_rm (Iso2709Rec rec)
200 struct iso2709_dir *dir, *dir1;
202 for (dir = rec->directory; dir; dir = dir1)
204 struct iso2709_field *field, *field1;
206 for (field = dir->fields; field; field = field1)
208 free (field->identifier);
210 field1 = field->next;
213 free (dir->indicator);