2 gw-res.c: Iso2709 record management
7 Revision 1.1 1995/02/09 17:27:10 adam
20 static int atoin (const char *buf, int n)
26 val = val*10 + (*buf - '0');
32 static void strncpyx (char *d, const char *s, int n)
34 while (--n >= 0 && *s)
35 if (*s != ISO2709_IDFS)
49 char *iso2709_read (FILE *inf)
55 if (fread (length_str, 1, 5, inf) != 5)
57 size = atoin (length_str, 5);
60 if (!(buf = malloc (size+1)))
62 if (fread (buf+5, 1, size-5, inf) != (size-5))
67 memcpy (buf, length_str, 5);
72 Iso2709Rec iso2709_cvt (const char *buf)
74 struct iso2709_dir **dpp, *dp;
78 if (!(p = malloc (sizeof(*p))))
81 p->record_length = atoin (buf, 5);
82 strncpyx (p->record_status, buf+5, 1);
83 strncpyx (p->implementation_codes, buf+6, 4);
84 p->indicator_length = atoin (buf+10, 1);
85 p->identifier_length = atoin (buf+11, 1);
86 p->base_address = atoin (buf+12, 4);
87 strncpyx (p->user_systems, buf+17, 3);
89 p->length_data_entry = atoin (buf+20, 1);
90 p->length_starting = atoin (buf+21, 1);
91 p->length_implementation = atoin (buf+22, 1);
92 strncpyx (p->future_use, buf+23, 1);
97 while (buf[pos] != ISO2709_FS)
99 *dpp = malloc (sizeof(**dpp));
101 strncpyx ((*dpp)->tag, buf+pos, 3);
103 (*dpp)->length = atoin (buf+pos, p->length_data_entry);
104 pos += p->length_data_entry;
105 (*dpp)->offset = atoin (buf+pos, p->length_starting);
106 pos += p->length_starting + p->length_implementation;
112 fprintf (stderr, "indicator_len=%d, identifier_len=%d\n",
113 p->indicator_length, p->identifier_length);
115 for (dp = p->directory; dp; dp = dp->next)
118 struct iso2709_field **fpp;
119 int dpos = pos+dp->offset;
120 char *save_indicator = NULL;
124 *fpp = malloc (sizeof(**fpp));
126 if (p->indicator_length && memcmp (dp->tag, "00", 2))
128 (*fpp)->indicator = malloc (p->indicator_length+1);
129 strncpyx ((*fpp)->indicator, buf+dpos, p->indicator_length);
130 dpos += p->indicator_length;
133 (*fpp)->indicator = NULL;
135 if (memcmp (dp->tag, "00", 2))
139 fprintf (stderr, "%s", dp->tag);
143 if (p->identifier_length && !tag00)
145 (*fpp)->identifier = malloc (p->identifier_length+1);
146 strncpyx ((*fpp)->identifier, buf+dpos+1,
147 p->identifier_length-1);
148 dpos += p->identifier_length;
151 (*fpp)->identifier = NULL;
154 while (buf[dpos_n] != ISO2709_FS && buf[dpos_n] != ISO2709_IDFS)
157 (*fpp)->data = malloc (dpos_n - dpos + 1);
158 strncpyx ((*fpp)->data, buf+dpos, dpos_n - dpos);
161 if (!save_indicator && (*fpp)->indicator)
162 fprintf (stderr, " %s", (*fpp)->indicator);
163 if ((*fpp)->identifier)
164 fprintf (stderr, " *%s", (*fpp)->identifier);
165 fprintf (stderr, " %s", (*fpp)->data);
166 if (buf[dpos] == ISO2709_FS)
169 save_indicator = (*fpp)->indicator;
171 *fpp = malloc (sizeof(**fpp));
173 (*fpp)->indicator = save_indicator;
175 fprintf (stderr, "\n");