source: gsdl/trunk/runtime-src/packages/d2m/marc2line.c@ 16571

Last change on this file since 16571 was 10365, checked in by kjdon, 19 years ago

changed my mind, now adding these all individually instead of in a tar file

  • Property svn:keywords set to Author Date Id Revision
File size: 6.5 KB
Line 
1/* ------------------------------------------------------------------- */
2/* marc2line : Reads an ISO 2709 MARC record, converts to MARC */
3/* "Line format" */
4/* */
5/* author : Ole Husby, BIBSYS */
6/* updated : 1998-09-30 */
7/* ------------------------------------------------------------------- */
8
9#include <stdlib.h>
10#include <stdio.h>
11#include <string.h>
12#include <ctype.h>
13
14#define DIRL 3600
15
16#define LINEL 72
17#define SUBFIELD_CODE '\x1f'
18#define SUBFIELD_STR "\x1f"
19#define FIELD_TERMINATOR '\x1e'
20#define RECORD_TERMINATOR '\x1d'
21
22#define BIBSYS_TYPE 0
23#define NORMARC_TYPE 1
24
25union LDR {
26 char ldr_string[24+1];
27 struct {
28 char record_length[5],
29 record_status,
30 record_type,
31 bibliographic_level,
32 unused1[2],
33 indicator_count,
34 subfield_code_count,
35 base_adress[5],
36 unused2[3],
37 entry1,
38 entry2,
39 entry3,
40 entry4;
41 } ltab;
42} ldr;
43
44static char out_record[10240];
45static int out_type;
46
47
48
49/* ------------------------------------------------------------------- */
50/* One subfield string is split into output lines */
51/* ------------------------------------------------------------------- */
52
53static void out_subfield(data, prefix, slen)
54char *data, *prefix;
55int slen;
56
57{
58 char lineprefix[7], subprefix[4], *spre;
59 int i, more, len, max;
60
61 if (out_type == BIBSYS_TYPE)
62 strcpy(lineprefix, prefix);
63 else
64 {
65 if (prefix[0] == ' ') strcpy(lineprefix, " ");
66 else strcpy(lineprefix, "*");
67 strcat(lineprefix, prefix);
68 }
69
70 max = LINEL;
71
72 if (slen)
73 {
74 spre = (char *) subprefix;
75 strcpy(spre, "$"); /* Construct subfield */
76 strncat(spre, data, 1); /* prefix */
77 if (out_type == BIBSYS_TYPE)
78 strcat(spre, " ");
79 data++;
80 max = LINEL - 2;
81 if (out_type == BIBSYS_TYPE)
82 max--;
83 }
84 else spre = NULL;
85
86 more = 1;
87
88 while (more)
89 {
90 if (slen)
91 while (data[0] == ' ') data++; /* skip leading blanks */
92
93 strcat(out_record, lineprefix); /* Start with prefix */
94 strcpy(lineprefix, " ");
95 if (out_type == NORMARC_TYPE)
96 strcat(lineprefix, " ");
97
98 len = strlen(data);
99 if (len > max) /* Must fit into one line */
100 {
101 len = max;
102 for (i = max; i > max - 15; i--)
103 {
104 if (data[i] == ' ')
105 {
106 len = i;
107 break;
108 }
109 }
110 }
111 else more = 0;
112
113 if (spre)
114 {
115 strcat(out_record, spre); /* subfield pref needed */
116 spre = NULL; /* only first line */
117 max = LINEL;
118 }
119
120 strncat(out_record, data, len); /* append data */
121 if (more) data += len;
122 strcat(out_record, "\n");
123 }
124}
125
126
127
128/* ------------------------------------------------------------------- */
129/* One MARC field is split into subfields and given to output */
130/* ------------------------------------------------------------------- */
131
132static int out_field(data, ftag, find)
133char *data, *ftag, *find;
134
135{
136 char prefix[5];
137 char subfield[5120], *sub = subfield;
138 int subcode = 0;
139
140 strcpy(prefix, ftag);
141
142 if (!find) /* output entire field */
143 {
144 out_subfield(data, prefix, subcode);
145 return 1;
146 }
147
148 strcat(prefix, find);
149
150 if (data[0] != SUBFIELD_CODE) return 0; /* requires subf code in */
151 data++; /* first byte */
152
153 sub = strtok(data, SUBFIELD_STR); /* find first subfield */
154 if (!sub) return 0;
155
156 subcode = 1;
157 out_subfield(sub, prefix, subcode); /* output subfield */
158 strcpy(prefix, " "); /* reset prefix */
159
160 while (sub = strtok(NULL, SUBFIELD_STR)) /* find and output rest */
161 out_subfield(sub, prefix, subcode); /* of the subfields */
162
163 return 1;
164}
165
166
167
168/* ------------------------------------------------------------------- */
169/* Main program */
170/* ------------------------------------------------------------------- */
171
172char *marc2line(record, type)
173char *record, *type;
174{
175 char buffer[10240], tag[4], indicators[3];
176 char *cp, *fld, *buf = buffer, *cq;
177 char directory[DIRL+1], *dir = directory, *ind;
178 char sBase[6], fLen[5], fStart[6];
179 int dlen, base, x = 0, more = 1, len, start;
180
181 if (!record || !*record)
182 return (char *) NULL;
183
184 strcpy(buf, record);
185 *out_record = 0; /* reset out area */
186
187 if (strcmp(type, "bibsys") == 0) out_type = BIBSYS_TYPE;
188 else out_type = NORMARC_TYPE;
189
190 strncpy(ldr.ldr_string, buf, 24); /* get leader */
191 ldr.ldr_string[24] = '\0';
192 buf += 24;
193
194 strncpy(sBase, ldr.ltab.base_adress, 5);
195 sBase[5] = '\0';
196 base = atoi(sBase);
197
198 dlen = base - 24; /* get directory */
199 strncpy(dir, buf, dlen);
200 dir[dlen] = '\0';
201 buf += dlen;
202
203 cp = dir;
204
205 while (more) /* loop until EOdir */
206 {
207 if (*cp == FIELD_TERMINATOR) break;
208 if (strlen(cp) < 13) return (char *) NULL;
209
210 strncpy(tag, cp, 3); /* get tag */
211 tag[3] = '\0';
212 cp +=3;
213
214 strncpy(fLen, cp, 4); /* get length */
215 fLen[4] = '\0';
216 len = atoi(fLen) -1; /* discard field term. */
217 cp += 4;
218
219 strncpy(fStart, cp, 5); /* get start of field */
220 fStart[5] = '\0';
221 start = atoi(fStart);
222 cp += 5;
223
224 fld = buf + start; /* get field */
225 fld[len] = '\0';
226
227 if (strncmp(tag, "00", 2) != 0) /* get indicators */
228 {
229 indicators[0] = '\0';
230 strncat(indicators, fld, 2);
231 ind = (char *) indicators;
232 fld += 2;
233 }
234 else ind = NULL;
235
236 if (!out_field(fld, tag, ind)) /* output field */
237 return (char *) NULL;
238
239 }
240
241 return (char *) out_record;
242
243}
Note: See TracBrowser for help on using the repository browser.