2 * Iso2709 record management
4 * Europagate, 1994-1995.
7 * Revision 1.9 1995/03/08 12:36:39 adam
8 * New function: dbc2709_cvt.
10 * Revision 1.8 1995/03/08 12:03:15 adam
11 * Hack: When tags 00? are used, every separator (DC[1-3]) marks
12 * the end of the data field.
14 * Revision 1.7 1995/02/22 21:28:03 adam
17 * Revision 1.5 1995/02/22 15:24:14 adam
18 * Function iso2709_cvt makes a litte check for the format. It returns
19 * NULL if the buffer parameter can never be a MARC record.
21 * Revision 1.4 1995/02/15 17:45:44 adam
22 * Bug fix in iso2709 module.
24 * Revision 1.3 1995/02/10 17:05:18 adam
25 * New function iso2709_display to display MARC records in a
26 * line-by-line format. The iso2709_cvt function no longer
27 * prints the record to stderr.
29 * Revision 1.2 1995/02/10 16:50:32 adam
30 * Indicator field moved to 'struct iso2709_dir' from 'struct
32 * Function iso2709_rm implemented - to delete a MARC record.
34 * Revision 1.1.1.1 1995/02/09 17:27:11 adam
35 * Initial version of email gateway under CVS control.
47 static int atoin (const char *buf, int n)
53 val = val*10 + (*buf - '0');
59 static void strncpyx (char *d, const char *s, int n)
61 while (--n >= 0 && *s)
62 if (*s != ISO2709_IDFS)
76 char *iso2709_read (FILE *inf)
82 if (fread (length_str, 1, 5, inf) != 5)
84 size = atoin (length_str, 5);
87 if (!(buf = malloc (size+1)))
89 if (fread (buf+5, 1, size-5, inf) != (size-5))
94 memcpy (buf, length_str, 5);
100 static Iso2709Rec iso2709_cvt_g (const char *buf, int use_dbc)
102 struct iso2709_dir **dpp, *dp;
106 if (!(p = malloc (sizeof(*p))))
109 /* deal with record label (24 characters) */
110 p->record_length = atoin (buf, 5);
111 strncpyx (p->record_status, buf+5, 1);
112 strncpyx (p->implementation_codes, buf+6, 4);
113 p->indicator_length = atoin (buf+10, 1);
114 p->identifier_length = atoin (buf+11, 1);
115 p->base_address = atoin (buf+12, 4);
116 strncpyx (p->user_systems, buf+17, 3);
118 if (p->record_length < 26)
123 p->length_data_entry = atoin (buf+20, 1);
124 p->length_starting = atoin (buf+21, 1);
125 p->length_implementation = atoin (buf+22, 1);
126 strncpyx (p->future_use, buf+23, 1);
128 /* deal with directory */
131 while (buf[pos] != ISO2709_FS)
133 *dpp = malloc (sizeof(**dpp));
135 strncpyx ((*dpp)->tag, buf+pos, 3);
137 (*dpp)->length = atoin (buf+pos, p->length_data_entry);
138 pos += p->length_data_entry;
139 (*dpp)->offset = atoin (buf+pos, p->length_starting);
140 pos += p->length_starting + p->length_implementation;
145 /* deal with datafields */
146 for (dp = p->directory; dp; dp = dp->next)
149 struct iso2709_field **fpp;
150 int dpos = pos+dp->offset;
154 *fpp = malloc (sizeof(**fpp));
156 if (p->indicator_length && (memcmp (dp->tag, "00", 2) || use_dbc))
158 dp->indicator = malloc (p->indicator_length+1);
159 strncpyx (dp->indicator, buf+dpos, p->indicator_length);
160 dpos += p->indicator_length;
163 dp->indicator = NULL;
164 if (memcmp (dp->tag, "00", 2))
171 if (p->identifier_length && (!tag00 || use_dbc))
173 (*fpp)->identifier = malloc (p->identifier_length+1);
174 strncpyx ((*fpp)->identifier, buf+dpos+1,
175 p->identifier_length-1);
176 dpos += p->identifier_length;
179 (*fpp)->identifier = NULL;
182 while (buf[dpos_n] != ISO2709_FS && buf[dpos_n] != ISO2709_IDFS &&
183 buf[dpos_n] != ISO2709_RS)
186 (*fpp)->data = malloc (dpos_n - dpos + 1);
187 strncpyx ((*fpp)->data, buf+dpos, dpos_n - dpos);
190 if (buf[dpos] == ISO2709_FS || buf[dpos] == ISO2709_RS)
194 *fpp = malloc (sizeof(**fpp));
201 Iso2709Rec iso2709_cvt (const char *buf)
203 return iso2709_cvt_g (buf, 0);
206 Iso2709Rec dbc2709_cvt (const char *buf)
208 return iso2709_cvt_g (buf, 1);
211 void iso2709_rm (Iso2709Rec rec)
213 struct iso2709_dir *dir, *dir1;
215 for (dir = rec->directory; dir; dir = dir1)
217 struct iso2709_field *field, *field1;
219 for (field = dir->fields; field; field = field1)
221 free (field->identifier);
223 field1 = field->next;
226 free (dir->indicator);