2 * Iso2709 record management
4 * Europagate, 1994-1995.
7 * Revision 1.10 1995/03/10 09:10:56 adam
8 * Removed dbc2709_cvt function. Makes heuristic guess for DBC2709 records.
10 * Revision 1.9 1995/03/08 12:36:39 adam
11 * New function: dbc2709_cvt.
13 * Revision 1.8 1995/03/08 12:03:15 adam
14 * Hack: When tags 00? are used, every separator (DC[1-3]) marks
15 * the end of the data field.
17 * Revision 1.7 1995/02/22 21:28:03 adam
20 * Revision 1.5 1995/02/22 15:24:14 adam
21 * Function iso2709_cvt makes a litte check for the format. It returns
22 * NULL if the buffer parameter can never be a MARC record.
24 * Revision 1.4 1995/02/15 17:45:44 adam
25 * Bug fix in iso2709 module.
27 * Revision 1.3 1995/02/10 17:05:18 adam
28 * New function iso2709_display to display MARC records in a
29 * line-by-line format. The iso2709_cvt function no longer
30 * prints the record to stderr.
32 * Revision 1.2 1995/02/10 16:50:32 adam
33 * Indicator field moved to 'struct iso2709_dir' from 'struct
35 * Function iso2709_rm implemented - to delete a MARC record.
37 * Revision 1.1.1.1 1995/02/09 17:27:11 adam
38 * Initial version of email gateway under CVS control.
50 static int atoin (const char *buf, int n)
56 val = val*10 + (*buf - '0');
62 static void strncpyx (char *d, const char *s, int n)
64 while (--n >= 0 && *s)
65 if (*s != ISO2709_IDFS)
79 char *iso2709_read (FILE *inf)
85 if (fread (length_str, 1, 5, inf) != 5)
87 size = atoin (length_str, 5);
90 if (!(buf = malloc (size+1)))
92 if (fread (buf+5, 1, size-5, inf) != (size-5))
97 memcpy (buf, length_str, 5);
103 Iso2709Rec iso2709_cvt (const char *buf)
105 struct iso2709_dir **dpp, *dp;
109 if (!(p = malloc (sizeof(*p))))
112 /* deal with record label (24 characters) */
113 p->record_length = atoin (buf, 5);
114 strncpyx (p->record_status, buf+5, 1);
115 strncpyx (p->implementation_codes, buf+6, 4);
116 p->indicator_length = atoin (buf+10, 1);
117 p->identifier_length = atoin (buf+11, 1);
118 p->base_address = atoin (buf+12, 4);
119 strncpyx (p->user_systems, buf+17, 3);
121 if (p->record_length < 26)
126 p->length_data_entry = atoin (buf+20, 1);
127 p->length_starting = atoin (buf+21, 1);
128 p->length_implementation = atoin (buf+22, 1);
129 strncpyx (p->future_use, buf+23, 1);
131 /* deal with directory */
134 while (buf[pos] != ISO2709_FS)
136 *dpp = malloc (sizeof(**dpp));
139 strncpyx ((*dpp)->tag, buf+pos, 3);
141 (*dpp)->length = atoin (buf+pos, p->length_data_entry);
142 pos += p->length_data_entry;
143 (*dpp)->offset = atoin (buf+pos, p->length_starting);
144 pos += p->length_starting + p->length_implementation;
149 /* deal with datafields */
150 for (dp = p->directory; dp; dp = dp->next)
153 struct iso2709_field **fpp;
154 int dpos = pos+dp->offset;
158 *fpp = malloc (sizeof(**fpp));
163 if (p->indicator_length)
166 if (buf[dpos+p->indicator_length] != ISO2709_IDFS)
169 if (!memcmp (dp->tag, "00", 2))
173 else if (!memcmp (dp->tag, "00", 2))
175 if (identifier_flag && p->indicator_length)
177 dp->indicator = malloc (p->indicator_length+1);
178 assert (dp->indicator);
179 strncpyx (dp->indicator, buf+dpos, p->indicator_length);
180 dpos += p->indicator_length;
183 dp->indicator = NULL;
187 if (p->identifier_length && identifier_flag)
189 (*fpp)->identifier = malloc (p->identifier_length+1);
190 strncpyx ((*fpp)->identifier, buf+dpos+1,
191 p->identifier_length-1);
192 dpos_n = dpos += p->identifier_length;
193 while (buf[dpos_n] != ISO2709_FS && buf[dpos_n] != ISO2709_RS
194 && buf[dpos_n] != ISO2709_IDFS)
199 (*fpp)->identifier = NULL;
201 while (buf[dpos_n] != ISO2709_FS && buf[dpos_n] != ISO2709_RS)
204 (*fpp)->data = malloc (dpos_n - dpos + 1);
205 assert ((*fpp)->data);
206 strncpyx ((*fpp)->data, buf+dpos, dpos_n - dpos);
209 if (buf[dpos] == ISO2709_FS || buf[dpos] == ISO2709_RS)
213 *fpp = malloc (sizeof(**fpp));
221 void iso2709_rm (Iso2709Rec rec)
223 struct iso2709_dir *dir, *dir1;
225 for (dir = rec->directory; dir; dir = dir1)
227 struct iso2709_field *field, *field1;
229 for (field = dir->fields; field; field = field1)
231 free (field->identifier);
233 field1 = field->next;
236 free (dir->indicator);