1 | /* ------------------------------------------------------------------- */
|
---|
2 | /* util : Several utilities for the d2m converter */
|
---|
3 | /* */
|
---|
4 | /* Author : Ole Husby */
|
---|
5 | /* Updated : 1998-09-30 */
|
---|
6 | /* ------------------------------------------------------------------- */
|
---|
7 |
|
---|
8 |
|
---|
9 | #include <stdlib.h>
|
---|
10 | #include <stdio.h>
|
---|
11 | #include <string.h>
|
---|
12 | #include <ctype.h>
|
---|
13 | #include "d2m.h"
|
---|
14 |
|
---|
15 |
|
---|
16 |
|
---|
17 |
|
---|
18 | /* ------------------------------------------------------------------- */
|
---|
19 | /* Produce MARC line-format output */
|
---|
20 | /* ------------------------------------------------------------------- */
|
---|
21 |
|
---|
22 | void write_field(char *l, FILE *of, int linel, int llen, int slen)
|
---|
23 | {
|
---|
24 | char pre[64], line[256], *p, *q, *n;
|
---|
25 | int more, i, spos, compact, ctr;
|
---|
26 |
|
---|
27 | ctr = 0;
|
---|
28 | *pre = *line = 0;
|
---|
29 | p = strdup(l);
|
---|
30 | q = p;
|
---|
31 |
|
---|
32 | more = TRUE;
|
---|
33 | compact = FALSE;
|
---|
34 |
|
---|
35 | while (more)
|
---|
36 | {
|
---|
37 |
|
---|
38 | ctr++;
|
---|
39 | if (ctr > 20) more = FALSE;
|
---|
40 |
|
---|
41 | if (strlen(q) <= linel - 8) /* String fits into 1 line */
|
---|
42 | {
|
---|
43 | if (compact)
|
---|
44 | {
|
---|
45 | fprintf(of, "%s%s\n", pre, q);
|
---|
46 | more = FALSE;
|
---|
47 | }
|
---|
48 | else
|
---|
49 | {
|
---|
50 | n = strstr(q + 6, "$");
|
---|
51 |
|
---|
52 | if ( (n - q ) > 5)
|
---|
53 | {
|
---|
54 | spos = (n - q);
|
---|
55 | strncat(line, q, spos);
|
---|
56 | q = n;
|
---|
57 | fprintf(of, "%s%s\n", pre, line);
|
---|
58 | strcpy(pre, " ");
|
---|
59 | *line = 0;
|
---|
60 | }
|
---|
61 | else
|
---|
62 | {
|
---|
63 | fprintf(of, "%s%s\n", pre, q);
|
---|
64 | more = FALSE;
|
---|
65 | }
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | else /* Look for splitting point */
|
---|
70 | {
|
---|
71 | spos = 0;
|
---|
72 |
|
---|
73 | if (!compact)
|
---|
74 | {
|
---|
75 | for (i = 6; i < linel - 8; i++)
|
---|
76 | {
|
---|
77 | if (q[i] == '$')
|
---|
78 | {
|
---|
79 | spos = i + 1;
|
---|
80 | break;
|
---|
81 | }
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 |
|
---|
86 | if (!spos)
|
---|
87 | {
|
---|
88 | for (i = linel - 9; i > slen; i--)
|
---|
89 | {
|
---|
90 | if ( !compact && (q[i] == '$'))
|
---|
91 | {
|
---|
92 | spos = i + 1;
|
---|
93 | break;
|
---|
94 | }
|
---|
95 | else if (q[i] == ' ')
|
---|
96 | {
|
---|
97 | spos = i;
|
---|
98 | break;
|
---|
99 | }
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | if (spos)
|
---|
104 | {
|
---|
105 | strncat(line, q, spos);
|
---|
106 | q += spos + 1;
|
---|
107 | }
|
---|
108 | else
|
---|
109 | {
|
---|
110 | strncat(line, q, llen);
|
---|
111 | strcat(line, "-");
|
---|
112 | q += llen;
|
---|
113 | }
|
---|
114 |
|
---|
115 | fprintf(of, "%s%s\n", pre, line);
|
---|
116 | strcpy(pre, " ");
|
---|
117 | *line = 0;
|
---|
118 | }
|
---|
119 | }
|
---|
120 | free(p);
|
---|
121 | }
|
---|
122 |
|
---|
123 |
|
---|
124 |
|
---|
125 | /* ------------------------------------------------------------------- */
|
---|
126 | /* Fetching HTTP POST data */
|
---|
127 | /* ------------------------------------------------------------------- */
|
---|
128 |
|
---|
129 | char *poq()
|
---|
130 | {
|
---|
131 | int m=0, c, cl;
|
---|
132 | char *rec;
|
---|
133 |
|
---|
134 | if(strcmp(getenv("REQUEST_METHOD"),"POST"))
|
---|
135 | return NULL;
|
---|
136 |
|
---|
137 | cl = atoi(getenv("CONTENT_LENGTH"));
|
---|
138 |
|
---|
139 | if (cl)
|
---|
140 | rec = (char*) malloc(cl + 1);
|
---|
141 | else
|
---|
142 | return NULL;
|
---|
143 |
|
---|
144 | while (c = fgetc(stdin))
|
---|
145 | {
|
---|
146 | rec[m] = c;
|
---|
147 | m++;
|
---|
148 | if (m >= cl) break;
|
---|
149 | }
|
---|
150 |
|
---|
151 | rec[m] = 0;
|
---|
152 |
|
---|
153 | return rec;
|
---|
154 | }
|
---|
155 |
|
---|
156 |
|
---|
157 |
|
---|
158 | /* ------------------------------------------------------------------- */
|
---|
159 | /* ------------------------------------------------------------------- */
|
---|
160 |
|
---|
161 | int unesc (char h, char l)
|
---|
162 | {
|
---|
163 | char buf[3];
|
---|
164 | int i;
|
---|
165 |
|
---|
166 | buf[0]=h;
|
---|
167 | buf[1]=l;
|
---|
168 | buf[2]='\0';
|
---|
169 | sscanf(buf,"%x",&i);
|
---|
170 | return (i);
|
---|
171 | }
|
---|
172 |
|
---|
173 |
|
---|
174 | /* ------------------------------------------------------------------- */
|
---|
175 | /* Replace '+' with space and unescape hex coded characters. */
|
---|
176 | /* ------------------------------------------------------------------- */
|
---|
177 |
|
---|
178 | void unescape (char *s)
|
---|
179 | {
|
---|
180 | char *rp, *wp; /* read position and write position in string */
|
---|
181 | rp=wp=s;
|
---|
182 | while (*rp != '\0') {
|
---|
183 | if (*rp=='+') {
|
---|
184 | *(wp++)=' ';
|
---|
185 | rp++;
|
---|
186 | }
|
---|
187 | else if (*rp=='%') {
|
---|
188 | int a,b;
|
---|
189 | rp++; /* skip the '%' */
|
---|
190 | a = (unsigned char) *(rp++);
|
---|
191 | b = (unsigned char) *(rp++);
|
---|
192 | *(wp++)=unesc (a,b);
|
---|
193 | } else
|
---|
194 | *(wp++)=*(rp++);
|
---|
195 | }
|
---|
196 | *wp = '\0';
|
---|
197 | }
|
---|
198 |
|
---|
199 |
|
---|
200 |
|
---|
201 | /* ------------------------------------------------------------------- */
|
---|
202 | /* Case independent string search. */
|
---|
203 | /* */
|
---|
204 | /* Call syntax like strstr() */
|
---|
205 | /* ------------------------------------------------------------------- */
|
---|
206 |
|
---|
207 | char *cstr(char *h, char *n)
|
---|
208 | {
|
---|
209 | char *nn;
|
---|
210 | char *hh;
|
---|
211 | char *p, *s;
|
---|
212 | long i;
|
---|
213 |
|
---|
214 |
|
---|
215 | /* Returns NULL if either of strings are NULL */
|
---|
216 |
|
---|
217 | if (!*h || !*n)
|
---|
218 | return (char *) NULL;
|
---|
219 |
|
---|
220 |
|
---|
221 | /* Make a copy hh of h and nn of n */
|
---|
222 |
|
---|
223 | hh = (char *) strdup(h);
|
---|
224 | nn = (char *) strdup(n);
|
---|
225 |
|
---|
226 | /* Converts hh and nn to lower */
|
---|
227 |
|
---|
228 | for (i = 0; i < strlen(nn); i++)
|
---|
229 | nn[i] = tolower(n[i]);
|
---|
230 |
|
---|
231 | for (i = 0; i < strlen(hh); i++)
|
---|
232 | hh[i] = tolower(h[i]);
|
---|
233 |
|
---|
234 | /* Searches for nn in hh using strstr */
|
---|
235 |
|
---|
236 | p = (char *) strstr(hh, nn);
|
---|
237 |
|
---|
238 | /* If found, calculates corresponding pointer into h */
|
---|
239 |
|
---|
240 | if (!p)
|
---|
241 | s = (char *) NULL;
|
---|
242 | else
|
---|
243 | {
|
---|
244 | i = strlen(hh) - strlen(p);
|
---|
245 | s = h + i;
|
---|
246 | }
|
---|
247 |
|
---|
248 | /* Frees string copies and returns */
|
---|
249 |
|
---|
250 | free(hh);
|
---|
251 | free(nn);
|
---|
252 |
|
---|
253 | return s;
|
---|
254 | }
|
---|
255 |
|
---|
256 |
|
---|
257 |
|
---|
258 | /* ------------------------------------------------------------------- */
|
---|
259 | /* Write and remove tracefile */
|
---|
260 | /* ------------------------------------------------------------------- */
|
---|
261 |
|
---|
262 | int write_trace(char *tracefile, FILE *of)
|
---|
263 | {
|
---|
264 | char line[10000], *l;
|
---|
265 | FILE *tf;
|
---|
266 |
|
---|
267 | tf = fopen(tracefile, "r");
|
---|
268 | if (!tf)
|
---|
269 | return 0;
|
---|
270 |
|
---|
271 | while (l = fgets(line, 9999, tf))
|
---|
272 | fprintf(of, "%s", l);
|
---|
273 |
|
---|
274 | fclose(tf);
|
---|
275 | remove(tracefile);
|
---|
276 | return 1;
|
---|
277 | }
|
---|
278 |
|
---|
279 |
|
---|
280 | /* ------------------------------------------------------------------- */
|
---|
281 | /* Insert field into 008 */
|
---|
282 | /* ------------------------------------------------------------------- */
|
---|
283 |
|
---|
284 | int put008(char *s8, char *str, int f8)
|
---|
285 | {
|
---|
286 | int i, len, pos, nlen, npos, append=0;
|
---|
287 |
|
---|
288 | if (strlen(s8) != 40)
|
---|
289 | return 0;
|
---|
290 |
|
---|
291 | switch (f8)
|
---|
292 | {
|
---|
293 | case (F008_DATE_ENTERED) :
|
---|
294 | {
|
---|
295 | pos = 0;
|
---|
296 | len = 6;
|
---|
297 | break;
|
---|
298 | }
|
---|
299 | case (F008_TYPE_OF_DATE) :
|
---|
300 | {
|
---|
301 | pos = 6;
|
---|
302 | len = 1;
|
---|
303 | break;
|
---|
304 | }
|
---|
305 | case (F008_DATE1) :
|
---|
306 | {
|
---|
307 | pos = 7;
|
---|
308 | len = 4;
|
---|
309 | break;
|
---|
310 | }
|
---|
311 | case (F008_FORM_OF_PUBLICATION) :
|
---|
312 | {
|
---|
313 | pos = 24;
|
---|
314 | len = 4;
|
---|
315 | append = 1;
|
---|
316 | break;
|
---|
317 | }
|
---|
318 | case (F008_LANGUAGE) :
|
---|
319 | {
|
---|
320 | pos = 35;
|
---|
321 | len = 3;
|
---|
322 | break;
|
---|
323 | }
|
---|
324 | default :
|
---|
325 | return 0;
|
---|
326 | }
|
---|
327 |
|
---|
328 |
|
---|
329 | npos = pos;
|
---|
330 | nlen = strlen(str);
|
---|
331 | if (nlen > len)
|
---|
332 | return 0;
|
---|
333 |
|
---|
334 | if (nlen < len)
|
---|
335 | {
|
---|
336 | if (append)
|
---|
337 | {
|
---|
338 | npos = 0;
|
---|
339 | for (i = pos; i < pos + len; i++)
|
---|
340 | {
|
---|
341 | if ( s8[i] == ' ' )
|
---|
342 | {
|
---|
343 | npos = i;
|
---|
344 | break;
|
---|
345 | }
|
---|
346 | }
|
---|
347 | if (npos == 0)
|
---|
348 | return 0;
|
---|
349 | if (npos >= pos + len)
|
---|
350 | return 0;
|
---|
351 | if (npos + nlen > pos + len)
|
---|
352 | return 0;
|
---|
353 | }
|
---|
354 | else
|
---|
355 | return 0;
|
---|
356 | }
|
---|
357 | /* printf
|
---|
358 | ("pos %d len %d npos %d nlen %d str %s\n",
|
---|
359 | pos, len, npos, nlen, str);
|
---|
360 | */
|
---|
361 |
|
---|
362 | for (i = 0; i < nlen; i++)
|
---|
363 | s8[npos+i] = str[i];
|
---|
364 |
|
---|
365 | return 1;
|
---|
366 | }
|
---|
367 |
|
---|
368 |
|
---|
369 | /* ------------------------------------------------------------------- */
|
---|
370 | /* Converts date to YYYY */
|
---|
371 | /* ------------------------------------------------------------------- */
|
---|
372 |
|
---|
373 | int find_year(char *str)
|
---|
374 | {
|
---|
375 | int i;
|
---|
376 |
|
---|
377 | if (strlen(str) < 4)
|
---|
378 | return 0;
|
---|
379 |
|
---|
380 | str[4] = 0;
|
---|
381 |
|
---|
382 | for (i = 0; i < 4; i++)
|
---|
383 | {
|
---|
384 | if (!isdigit(str[i]))
|
---|
385 | return 0;
|
---|
386 | }
|
---|
387 |
|
---|
388 | return 1;
|
---|
389 | }
|
---|
390 |
|
---|
391 |
|
---|
392 |
|
---|
393 |
|
---|
394 | /* ------------------------------------------------------------------- */
|
---|
395 | /* Splits personal name at first "," into $a and $h */
|
---|
396 | /* ------------------------------------------------------------------- */
|
---|
397 |
|
---|
398 | int split_name(char *str)
|
---|
399 | {
|
---|
400 | char *p, *q, subtag[5];
|
---|
401 |
|
---|
402 | if (!strstr(str, ","))
|
---|
403 | return 1;
|
---|
404 |
|
---|
405 | p = (char *) malloc(strlen(str) + 50);
|
---|
406 | *p = 0;
|
---|
407 | *subtag = 0;
|
---|
408 |
|
---|
409 | q = strtok(str, ",");
|
---|
410 | if (q)
|
---|
411 | {
|
---|
412 | strcpy(p, q);
|
---|
413 | q = strtok(NULL, "\0");
|
---|
414 | if (q)
|
---|
415 | {
|
---|
416 | while ( ( q[0] == ' ' ) && ( strlen(q) ) )
|
---|
417 | q++;
|
---|
418 | strcat(p, " $h ");
|
---|
419 | strcat(p, q);
|
---|
420 | }
|
---|
421 | strcpy(str, p);
|
---|
422 | }
|
---|
423 |
|
---|
424 | free(p);
|
---|
425 |
|
---|
426 | return 1;
|
---|
427 | }
|
---|
428 |
|
---|
429 |
|
---|
430 |
|
---|
431 | /* ------------------------------------------------------------------- */
|
---|
432 | /* Splits subject heading on "-". First occurence go into $a, second */
|
---|
433 | /* into $b, all the rest into $x */
|
---|
434 | /* ------------------------------------------------------------------- */
|
---|
435 |
|
---|
436 | int split_subj(char *str)
|
---|
437 | {
|
---|
438 | char *p, *q, subtag[5];
|
---|
439 |
|
---|
440 | if (!strstr(str, "-"))
|
---|
441 | return 1;
|
---|
442 |
|
---|
443 | p = (char *) malloc(strlen(str) + 50);
|
---|
444 | *p = 0;
|
---|
445 | *subtag = 0;
|
---|
446 |
|
---|
447 | q = strtok(str, "-");
|
---|
448 | while (q)
|
---|
449 | {
|
---|
450 | while ( ( q[0] == ' ' ) && ( strlen(q) ) )
|
---|
451 | q++;
|
---|
452 |
|
---|
453 | while( ( q[strlen(q) - 1] == ' ' ) && ( strlen(q) ) )
|
---|
454 | q[strlen(q) - 1] = '\0';
|
---|
455 |
|
---|
456 | if (*q)
|
---|
457 | {
|
---|
458 | strcat(p, subtag);
|
---|
459 | strcat(p, q);
|
---|
460 | if (!*subtag)
|
---|
461 | strcpy(subtag, " $b ");
|
---|
462 | else
|
---|
463 | strcpy(subtag, " $x ");
|
---|
464 | }
|
---|
465 | q = strtok(NULL, "-");
|
---|
466 | }
|
---|
467 |
|
---|
468 | strcpy(str, p);
|
---|
469 | free(p);
|
---|
470 |
|
---|
471 | return 1;
|
---|
472 | }
|
---|