2 * Iso2709 record management - anchor utilities
7 * Revision 1.3 1995/03/30 14:22:18 adam
8 * More work on new MARC anchor functions.
10 * Revision 1.2 1995/03/30 07:33:35 adam
11 * New 2709 function: iso2709_mk.
12 * First implementation of iso2709_a_insert.
14 * Revision 1.1 1995/03/29 11:44:29 adam
15 * New functions: iso2709_a_.. for record manipulation.
26 Iso2709Anchor iso2709_a_mk (Iso2709Rec rec)
30 anchor = malloc (sizeof(*anchor));
34 anchor->d0 = &rec->directory;
36 anchor->f0 = &(*anchor->d0)->fields;
42 void iso2709_a_rm (Iso2709Anchor anchor)
47 int iso2709_a_first (Iso2709Anchor anchor)
49 anchor->d0 = &anchor->rec->directory;
52 anchor->f0 = &(*anchor->d0)->fields;
58 int iso2709_a_next_line (Iso2709Anchor anchor)
62 anchor->d0 = &(*anchor->d0)->next;
64 anchor->f0 = &(*anchor->d0)->fields;
68 int iso2709_a_next_field (Iso2709Anchor anchor)
70 if (!*anchor->d0 || !*anchor->f0)
72 if (!(*anchor->f0)->next)
74 anchor->f0 = &(*anchor->f0)->next;
78 int iso2709_a_next (Iso2709Anchor anchor)
80 if (!*anchor->d0 || !*anchor->f0)
81 return iso2709_a_next_line (anchor);
82 anchor->f0 = &(*anchor->f0)->next;
84 return iso2709_a_next_line (anchor);
88 int iso2709_a_info_field (Iso2709Anchor anchor,
89 char **tag, char **indicator,
90 char **identifier, char **data)
92 if (!*anchor->d0 || !*anchor->f0)
95 *tag = (*anchor->d0)->tag;
98 *indicator = (*anchor->d0)->indicator;
99 if (*indicator && !**indicator)
104 *identifier = (*anchor->f0)->identifier;
105 if (*identifier && !**identifier)
109 *data = (*anchor->f0)->data;
113 int iso2709_a_info_line (Iso2709Anchor anchor,
114 char **tag, char **indicator)
118 assert (*anchor->f0);
119 return iso2709_a_info_field (anchor, tag, indicator, NULL, NULL);
122 int iso2709_a_delete_field (Iso2709Anchor anchor)
124 struct iso2709_field *field;
129 *anchor->f0 = field->next;
134 if (! (*anchor->d0)->fields)
135 iso2709_a_delete_line (anchor);
136 iso2709_a_next_line (anchor);
141 int iso2709_a_delete_line (Iso2709Anchor anchor)
143 struct iso2709_dir *dir;
148 *anchor->d0 = dir->next;
149 free (dir->indicator);
154 static int marc_cmp (const char *field, const char *pattern)
160 for (; *field && *pattern; field++, pattern++)
164 if (*pattern != *field)
167 return *field - *pattern;
170 int iso2709_a_search (Iso2709Anchor anchor,
171 const char *tag_p, const char *indicator_p,
172 const char *identifier_p)
179 if (!iso2709_a_info_field (anchor, &tag, &indicator,
182 if ((!tag_p || !marc_cmp (tag, tag_p)) &&
183 (!indicator_p || !marc_cmp (indicator, indicator_p)) &&
184 (!identifier_p || !marc_cmp (identifier, identifier_p)))
186 } while (iso2709_a_next (anchor));
190 int iso2709_a_insert (Iso2709Anchor a,
191 const char *tag, const char *indicator,
192 const char *identifier, const char *data)
194 struct iso2709_field *field;
199 if (! *a->d0 || strcmp (tag, (*a->d0)->tag))
201 struct iso2709_dir *dir;
203 if (!(dir = malloc (sizeof(*dir))))
205 strcpy (dir->tag, tag);
208 if (!(dir->indicator = malloc (strlen(indicator)+1)))
210 strcpy (dir->indicator, indicator);
213 dir->indicator = NULL;
215 a->d0 = &(*a->d0)->next;
219 a->f0 = &dir->fields;
221 if (!(field = malloc (sizeof(*field))))
224 strcpy (field->identifier, identifier);
226 *field->identifier = '\0';
227 if (!(field->data = malloc (strlen (data)+1)))
229 strcpy (field->data, data);
230 field->next = *a->f0;
232 a->f0 = &field->next;