2 Iso2709 record management
7 Revision 1.4 1995/02/15 17:45:44 adam
8 Bug fix in iso2709 module.
10 * Revision 1.3 1995/02/10 17:05:18 adam
11 * New function iso2709_display to display MARC records in a
12 * line-by-line format. The iso2709_cvt function no longer
13 * prints the record to stderr.
15 * Revision 1.2 1995/02/10 16:50:32 adam
16 * Indicator field moved to 'struct iso2709_dir' from 'struct
18 * Function iso2709_rm implemented - to delete a MARC record.
20 * Revision 1.1.1.1 1995/02/09 17:27:11 adam
21 * Initial version of email gateway under CVS control.
33 static int atoin (const char *buf, int n)
39 val = val*10 + (*buf - '0');
45 static void strncpyx (char *d, const char *s, int n)
47 while (--n >= 0 && *s)
48 if (*s != ISO2709_IDFS)
62 char *iso2709_read (FILE *inf)
68 if (fread (length_str, 1, 5, inf) != 5)
70 size = atoin (length_str, 5);
73 if (!(buf = malloc (size+1)))
75 if (fread (buf+5, 1, size-5, inf) != (size-5))
80 memcpy (buf, length_str, 5);
85 Iso2709Rec iso2709_cvt (const char *buf)
87 struct iso2709_dir **dpp, *dp;
91 if (!(p = malloc (sizeof(*p))))
94 /* deal with record label (24 characters) */
95 p->record_length = atoin (buf, 5);
96 strncpyx (p->record_status, buf+5, 1);
97 strncpyx (p->implementation_codes, buf+6, 4);
98 p->indicator_length = atoin (buf+10, 1);
99 p->identifier_length = atoin (buf+11, 1);
100 p->base_address = atoin (buf+12, 4);
101 strncpyx (p->user_systems, buf+17, 3);
103 p->length_data_entry = atoin (buf+20, 1);
104 p->length_starting = atoin (buf+21, 1);
105 p->length_implementation = atoin (buf+22, 1);
106 strncpyx (p->future_use, buf+23, 1);
108 /* deal with directory */
111 while (buf[pos] != ISO2709_FS)
113 *dpp = malloc (sizeof(**dpp));
115 strncpyx ((*dpp)->tag, buf+pos, 3);
117 (*dpp)->length = atoin (buf+pos, p->length_data_entry);
118 pos += p->length_data_entry;
119 (*dpp)->offset = atoin (buf+pos, p->length_starting);
120 pos += p->length_starting + p->length_implementation;
125 /* deal with datafields */
126 for (dp = p->directory; dp; dp = dp->next)
129 struct iso2709_field **fpp;
130 int dpos = pos+dp->offset;
134 *fpp = malloc (sizeof(**fpp));
136 if (p->indicator_length && memcmp (dp->tag, "00", 2))
138 dp->indicator = malloc (p->indicator_length+1);
139 strncpyx (dp->indicator, buf+dpos, p->indicator_length);
140 dpos += p->indicator_length;
143 dp->indicator = NULL;
145 if (memcmp (dp->tag, "00", 2))
152 if (p->identifier_length && !tag00)
154 (*fpp)->identifier = malloc (p->identifier_length+1);
155 strncpyx ((*fpp)->identifier, buf+dpos+1,
156 p->identifier_length-1);
157 dpos += p->identifier_length;
160 (*fpp)->identifier = NULL;
163 while (buf[dpos_n] != ISO2709_FS && buf[dpos_n] != ISO2709_IDFS &&
164 buf[dpos_n] != ISO2709_RS)
167 (*fpp)->data = malloc (dpos_n - dpos + 1);
168 strncpyx ((*fpp)->data, buf+dpos, dpos_n - dpos);
171 if (buf[dpos] == ISO2709_FS || buf[dpos] == ISO2709_RS)
175 *fpp = malloc (sizeof(**fpp));
182 void iso2709_rm (Iso2709Rec rec)
184 struct iso2709_dir *dir, *dir1;
186 for (dir = rec->directory; dir; dir = dir1)
188 struct iso2709_field *field, *field1;
190 for (field = dir->fields; field; field = field1)
192 free (field->identifier);
194 field1 = field->next;
197 free (dir->indicator);