2 gw-res.c: Iso2709 record management
7 Revision 1.2 1995/02/10 16:50:32 adam
8 Indicator field moved to 'struct iso2709_dir' from 'struct
10 Function iso2709_rm implemented - to delete a MARC record.
12 * Revision 1.1.1.1 1995/02/09 17:27:11 adam
13 * Initial version of email gateway under CVS control.
25 static int atoin (const char *buf, int n)
31 val = val*10 + (*buf - '0');
37 static void strncpyx (char *d, const char *s, int n)
39 while (--n >= 0 && *s)
40 if (*s != ISO2709_IDFS)
54 char *iso2709_read (FILE *inf)
60 if (fread (length_str, 1, 5, inf) != 5)
62 size = atoin (length_str, 5);
65 if (!(buf = malloc (size+1)))
67 if (fread (buf+5, 1, size-5, inf) != (size-5))
72 memcpy (buf, length_str, 5);
77 Iso2709Rec iso2709_cvt (const char *buf)
79 struct iso2709_dir **dpp, *dp;
83 if (!(p = malloc (sizeof(*p))))
86 /* deal with record label (24 characters) */
87 p->record_length = atoin (buf, 5);
88 strncpyx (p->record_status, buf+5, 1);
89 strncpyx (p->implementation_codes, buf+6, 4);
90 p->indicator_length = atoin (buf+10, 1);
91 p->identifier_length = atoin (buf+11, 1);
92 p->base_address = atoin (buf+12, 4);
93 strncpyx (p->user_systems, buf+17, 3);
95 p->length_data_entry = atoin (buf+20, 1);
96 p->length_starting = atoin (buf+21, 1);
97 p->length_implementation = atoin (buf+22, 1);
98 strncpyx (p->future_use, buf+23, 1);
100 /* deal with directory */
103 while (buf[pos] != ISO2709_FS)
105 *dpp = malloc (sizeof(**dpp));
107 strncpyx ((*dpp)->tag, buf+pos, 3);
109 (*dpp)->length = atoin (buf+pos, p->length_data_entry);
110 pos += p->length_data_entry;
111 (*dpp)->offset = atoin (buf+pos, p->length_starting);
112 pos += p->length_starting + p->length_implementation;
117 /* deal with datafields */
119 fprintf (stderr, "indicator_len=%d, identifier_len=%d\n",
120 p->indicator_length, p->identifier_length);
122 for (dp = p->directory; dp; dp = dp->next)
125 struct iso2709_field **fpp;
126 int dpos = pos+dp->offset;
130 *fpp = malloc (sizeof(**fpp));
132 if (p->indicator_length && memcmp (dp->tag, "00", 2))
134 dp->indicator = malloc (p->indicator_length+1);
135 strncpyx (dp->indicator, buf+dpos, p->indicator_length);
136 dpos += p->indicator_length;
139 dp->indicator = NULL;
141 if (memcmp (dp->tag, "00", 2))
145 fprintf (stderr, "%s", dp->tag);
147 fprintf (stderr, " %s", dp->indicator);
151 if (p->identifier_length && !tag00)
153 (*fpp)->identifier = malloc (p->identifier_length+1);
154 strncpyx ((*fpp)->identifier, buf+dpos+1,
155 p->identifier_length-1);
156 dpos += p->identifier_length;
159 (*fpp)->identifier = NULL;
162 while (buf[dpos_n] != ISO2709_FS && buf[dpos_n] != ISO2709_IDFS)
165 (*fpp)->data = malloc (dpos_n - dpos + 1);
166 strncpyx ((*fpp)->data, buf+dpos, dpos_n - dpos);
169 if ((*fpp)->identifier)
170 fprintf (stderr, " *%s", (*fpp)->identifier);
171 fprintf (stderr, " %s", (*fpp)->data);
172 if (buf[dpos] == ISO2709_FS)
176 *fpp = malloc (sizeof(**fpp));
179 fprintf (stderr, "\n");
184 void iso2709_rm (Iso2709Rec rec)
186 struct iso2709_dir *dir, *dir1;
188 for (dir = rec->directory; dir; dir = dir1)
190 struct iso2709_field *field, *field1;
192 for (field = dir->fields; field; field = field1)
194 free (field->identifier);
196 field1 = field->next;
199 free (dir->indicator);