2 Iso2709 record management
7 Revision 1.5 1995/02/22 15:24:14 adam
8 Function iso2709_cvt makes a litte check for the format. It returns
9 NULL if the buffer parameter can never be a MARC record.
11 * Revision 1.4 1995/02/15 17:45:44 adam
12 * Bug fix in iso2709 module.
14 * Revision 1.3 1995/02/10 17:05:18 adam
15 * New function iso2709_display to display MARC records in a
16 * line-by-line format. The iso2709_cvt function no longer
17 * prints the record to stderr.
19 * Revision 1.2 1995/02/10 16:50:32 adam
20 * Indicator field moved to 'struct iso2709_dir' from 'struct
22 * Function iso2709_rm implemented - to delete a MARC record.
24 * Revision 1.1.1.1 1995/02/09 17:27:11 adam
25 * Initial version of email gateway under CVS control.
37 static int atoin (const char *buf, int n)
43 val = val*10 + (*buf - '0');
49 static void strncpyx (char *d, const char *s, int n)
51 while (--n >= 0 && *s)
52 if (*s != ISO2709_IDFS)
66 char *iso2709_read (FILE *inf)
72 if (fread (length_str, 1, 5, inf) != 5)
74 size = atoin (length_str, 5);
77 if (!(buf = malloc (size+1)))
79 if (fread (buf+5, 1, size-5, inf) != (size-5))
84 memcpy (buf, length_str, 5);
89 Iso2709Rec iso2709_cvt (const char *buf)
91 struct iso2709_dir **dpp, *dp;
95 if (!(p = malloc (sizeof(*p))))
98 /* deal with record label (24 characters) */
99 p->record_length = atoin (buf, 5);
100 strncpyx (p->record_status, buf+5, 1);
101 strncpyx (p->implementation_codes, buf+6, 4);
102 p->indicator_length = atoin (buf+10, 1);
103 p->identifier_length = atoin (buf+11, 1);
104 p->base_address = atoin (buf+12, 4);
105 strncpyx (p->user_systems, buf+17, 3);
107 if (p->record_length < 26)
112 p->length_data_entry = atoin (buf+20, 1);
113 p->length_starting = atoin (buf+21, 1);
114 p->length_implementation = atoin (buf+22, 1);
115 strncpyx (p->future_use, buf+23, 1);
117 /* deal with directory */
120 while (buf[pos] != ISO2709_FS)
122 *dpp = malloc (sizeof(**dpp));
124 strncpyx ((*dpp)->tag, buf+pos, 3);
126 (*dpp)->length = atoin (buf+pos, p->length_data_entry);
127 pos += p->length_data_entry;
128 (*dpp)->offset = atoin (buf+pos, p->length_starting);
129 pos += p->length_starting + p->length_implementation;
134 /* deal with datafields */
135 for (dp = p->directory; dp; dp = dp->next)
138 struct iso2709_field **fpp;
139 int dpos = pos+dp->offset;
143 *fpp = malloc (sizeof(**fpp));
145 if (p->indicator_length && memcmp (dp->tag, "00", 2))
147 dp->indicator = malloc (p->indicator_length+1);
148 strncpyx (dp->indicator, buf+dpos, p->indicator_length);
149 dpos += p->indicator_length;
152 dp->indicator = NULL;
154 if (memcmp (dp->tag, "00", 2))
161 if (p->identifier_length && !tag00)
163 (*fpp)->identifier = malloc (p->identifier_length+1);
164 strncpyx ((*fpp)->identifier, buf+dpos+1,
165 p->identifier_length-1);
166 dpos += p->identifier_length;
169 (*fpp)->identifier = NULL;
172 while (buf[dpos_n] != ISO2709_FS && buf[dpos_n] != ISO2709_IDFS &&
173 buf[dpos_n] != ISO2709_RS)
176 (*fpp)->data = malloc (dpos_n - dpos + 1);
177 strncpyx ((*fpp)->data, buf+dpos, dpos_n - dpos);
180 if (buf[dpos] == ISO2709_FS || buf[dpos] == ISO2709_RS)
184 *fpp = malloc (sizeof(**fpp));
191 void iso2709_rm (Iso2709Rec rec)
193 struct iso2709_dir *dir, *dir1;
195 for (dir = rec->directory; dir; dir = dir1)
197 struct iso2709_field *field, *field1;
199 for (field = dir->fields; field; field = field1)
201 free (field->identifier);
203 field1 = field->next;
206 free (dir->indicator);