+/*
+ * Iso2709 record management - anchor utilities
+ *
+ * Europagate, 1995.
+ *
+ * $Log: iso2709a.c,v $
+ * Revision 1.1 1995/03/29 11:44:29 adam
+ * New functions: iso2709_a_.. for record manipulation.
+ *
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <assert.h>
+
+#include <iso2709p.h>
+
+Iso2709Anchor iso2709_a_mk (Iso2709Rec rec)
+{
+ Iso2709Anchor anchor;
+
+ anchor = malloc (sizeof(*anchor));
+ if (!anchor)
+ return NULL;
+ anchor->rec = rec;
+ anchor->d0 = &rec->directory;
+ if (*anchor->d0)
+ anchor->f0 = &(*anchor->d0)->fields;
+ return anchor;
+}
+
+void iso2709_a_rm (Iso2709Anchor anchor)
+{
+ free (anchor);
+}
+
+int iso2709_a_first (Iso2709Anchor anchor)
+{
+ anchor->d0 = &anchor->rec->directory;
+ if (*anchor->d0)
+ {
+ anchor->f0 = &(*anchor->d0)->fields;
+ return 1;
+ }
+ return 0;
+}
+
+int iso2709_a_next_line (Iso2709Anchor anchor)
+{
+ if (! *anchor->d0)
+ return 0;
+ anchor->d0 = &(*anchor->d0)->next;
+ if (*anchor->d0)
+ anchor->f0 = &(*anchor->d0)->fields;
+ return 1;
+}
+
+int iso2709_a_next_field (Iso2709Anchor anchor)
+{
+ if (!*anchor->d0 || !*anchor->f0)
+ return 0;
+ if (!(*anchor->f0)->next)
+ return 0;
+ anchor->f0 = &(*anchor->f0)->next;
+ return 1;
+}
+
+int iso2709_a_next (Iso2709Anchor anchor)
+{
+ if (!*anchor->d0 || !*anchor->f0)
+ return iso2709_a_next_line (anchor);
+ anchor->f0 = &(*anchor->f0)->next;
+ if (! *anchor->f0)
+ return iso2709_a_next_line (anchor);
+ return 2;
+}
+
+int iso2709_a_info_field (Iso2709Anchor anchor,
+ char **tag, char **indicator,
+ char **identifier, char **data)
+{
+ if (!*anchor->d0 || !*anchor->f0)
+ return 0;
+ if (tag)
+ *tag = (*anchor->d0)->tag;
+ if (indicator)
+ *indicator = (*anchor->d0)->indicator;
+ if (identifier)
+ *identifier = (*anchor->f0)->identifier;
+ if (data)
+ *data = (*anchor->f0)->data;
+ return 1;
+}
+
+int iso2709_a_info_line (Iso2709Anchor anchor,
+ char **tag, char **indicator)
+{
+ if (!*anchor->d0)
+ return 0;
+ assert (*anchor->f0);
+ return iso2709_a_info_field (anchor, tag, indicator, NULL, NULL);
+}
+
+int iso2709_a_delete_field (Iso2709Anchor anchor)
+{
+ struct iso2709_field *field;
+
+ if (!*anchor->d0)
+ return 0;
+ field = *anchor->f0;
+ *anchor->f0 = field->next;
+ free (field->identifier);
+ free (field->data);
+ free (field);
+ if (!*anchor->f0)
+ {
+ if (! (*anchor->d0)->fields)
+ iso2709_a_delete_line (anchor);
+ iso2709_a_next_line (anchor);
+ }
+ return 1;
+}
+
+int iso2709_a_delete_line (Iso2709Anchor anchor)
+{
+ struct iso2709_dir *dir;
+
+ if (!*anchor->d0)
+ return 0;
+ dir = *anchor->d0;
+ *anchor->d0 = dir->next;
+ free (dir->indicator);
+ free (dir);
+ return 1;
+}
+
+static int marc_cmp (const char *field, const char *pattern)
+{
+ if (*pattern == '*')
+ return 0;
+ if (!field)
+ return -*pattern;
+ for (; *field && *pattern; field++, pattern++)
+ {
+ if (*pattern == '?')
+ continue;
+ if (*pattern != *field)
+ break;
+ }
+ return *field - *pattern;
+}
+
+int iso2709_a_search (Iso2709Anchor anchor,
+ const char *tag_p, const char *indicator_p,
+ const char *identifier_p)
+{
+ char *tag;
+ char *indicator;
+ char *identifier;
+ do
+ {
+ if (!iso2709_a_info_field (anchor, &tag, &indicator,
+ &identifier, NULL))
+ return 0;
+ if ((!tag_p || !marc_cmp (tag, tag_p)) &&
+ (!indicator_p || !marc_cmp (indicator, indicator_p)) &&
+ (!identifier_p || !marc_cmp (identifier, identifier_p)))
+ return 1;
+ } while (iso2709_a_next (anchor));
+ return 0;
+}
+
+int iso2709_a_insert (Iso2709Anchor anchor,
+ const char *tag, const char *indicator,
+ const char *identifier, const char *data)
+{
+ return 0;
+}
+