2 * Iso2709 record management
4 * Europagate, 1994-1995.
7 * Revision 1.7 1995/02/22 21:28:03 adam
10 * Revision 1.5 1995/02/22 15:24:14 adam
11 * Function iso2709_cvt makes a litte check for the format. It returns
12 * NULL if the buffer parameter can never be a MARC record.
14 * Revision 1.4 1995/02/15 17:45:44 adam
15 * Bug fix in iso2709 module.
17 * Revision 1.3 1995/02/10 17:05:18 adam
18 * New function iso2709_display to display MARC records in a
19 * line-by-line format. The iso2709_cvt function no longer
20 * prints the record to stderr.
22 * Revision 1.2 1995/02/10 16:50:32 adam
23 * Indicator field moved to 'struct iso2709_dir' from 'struct
25 * Function iso2709_rm implemented - to delete a MARC record.
27 * Revision 1.1.1.1 1995/02/09 17:27:11 adam
28 * Initial version of email gateway under CVS control.
40 static int atoin (const char *buf, int n)
46 val = val*10 + (*buf - '0');
52 static void strncpyx (char *d, const char *s, int n)
54 while (--n >= 0 && *s)
55 if (*s != ISO2709_IDFS)
69 char *iso2709_read (FILE *inf)
75 if (fread (length_str, 1, 5, inf) != 5)
77 size = atoin (length_str, 5);
80 if (!(buf = malloc (size+1)))
82 if (fread (buf+5, 1, size-5, inf) != (size-5))
87 memcpy (buf, length_str, 5);
92 Iso2709Rec iso2709_cvt (const char *buf)
94 struct iso2709_dir **dpp, *dp;
98 if (!(p = malloc (sizeof(*p))))
101 /* deal with record label (24 characters) */
102 p->record_length = atoin (buf, 5);
103 strncpyx (p->record_status, buf+5, 1);
104 strncpyx (p->implementation_codes, buf+6, 4);
105 p->indicator_length = atoin (buf+10, 1);
106 p->identifier_length = atoin (buf+11, 1);
107 p->base_address = atoin (buf+12, 4);
108 strncpyx (p->user_systems, buf+17, 3);
110 if (p->record_length < 26)
115 p->length_data_entry = atoin (buf+20, 1);
116 p->length_starting = atoin (buf+21, 1);
117 p->length_implementation = atoin (buf+22, 1);
118 strncpyx (p->future_use, buf+23, 1);
120 /* deal with directory */
123 while (buf[pos] != ISO2709_FS)
125 *dpp = malloc (sizeof(**dpp));
127 strncpyx ((*dpp)->tag, buf+pos, 3);
129 (*dpp)->length = atoin (buf+pos, p->length_data_entry);
130 pos += p->length_data_entry;
131 (*dpp)->offset = atoin (buf+pos, p->length_starting);
132 pos += p->length_starting + p->length_implementation;
137 /* deal with datafields */
138 for (dp = p->directory; dp; dp = dp->next)
141 struct iso2709_field **fpp;
142 int dpos = pos+dp->offset;
146 *fpp = malloc (sizeof(**fpp));
148 if (p->indicator_length && memcmp (dp->tag, "00", 2))
150 dp->indicator = malloc (p->indicator_length+1);
151 strncpyx (dp->indicator, buf+dpos, p->indicator_length);
152 dpos += p->indicator_length;
155 dp->indicator = NULL;
157 if (memcmp (dp->tag, "00", 2))
164 if (p->identifier_length && !tag00)
166 (*fpp)->identifier = malloc (p->identifier_length+1);
167 strncpyx ((*fpp)->identifier, buf+dpos+1,
168 p->identifier_length-1);
169 dpos += p->identifier_length;
172 (*fpp)->identifier = NULL;
175 while (buf[dpos_n] != ISO2709_FS && buf[dpos_n] != ISO2709_IDFS &&
176 buf[dpos_n] != ISO2709_RS)
179 (*fpp)->data = malloc (dpos_n - dpos + 1);
180 strncpyx ((*fpp)->data, buf+dpos, dpos_n - dpos);
183 if (buf[dpos] == ISO2709_FS || buf[dpos] == ISO2709_RS)
187 *fpp = malloc (sizeof(**fpp));
194 void iso2709_rm (Iso2709Rec rec)
196 struct iso2709_dir *dir, *dir1;
198 for (dir = rec->directory; dir; dir = dir1)
200 struct iso2709_field *field, *field1;
202 for (field = dir->fields; field; field = field1)
204 free (field->identifier);
206 field1 = field->next;
209 free (dir->indicator);