source: main/trunk/greenstone2/runtime-src/packages/d2m/D2Mmain.c@ 22398

Last change on this file since 22398 was 22398, checked in by kjdon, 14 years ago

added UNIMARC format

  • Property svn:keywords set to Author Date Id Revision
File size: 7.1 KB
Line 
1/* ------------------------------------------------------------------- */
2/* D2Mmain : standalone program for DC -> MARC conversion */
3/* */
4/* Syntax : d2main [File] [Options] */
5/* */
6/* File : This may be a local filename, or a URL (which is */
7/* recognized by the "http://" prefix). When a URL, */
8/* the remote file will be fetched using the builtin */
9/* HTTP "client". */
10/* */
11/* Options : -l : Format = line format */
12/* (Default is ISO 2709) */
13/* Options : -f [filename] : Outputfile */
14/* (Default is stdout) */
15/* Options : -m [marcformat] : Marcformat (upper case) */
16/* (Default is NORMARC) */
17/* Options : -t [filename] : Tracefile */
18/* (Default is no trace) */
19/* */
20/* Return : 0 : OK */
21/* 1 : Syntax error (no parms != 2) */
22/* 2 : Illegal MARC format */
23/* 3 : Cannot open output file */
24/* 8 : Error in MARC conversion */
25/* 9 : URL not found */
26/* */
27/* Author : Ole Husby */
28/* Updated : 1998-09-30 */
29/* ------------------------------------------------------------------- */
30
31#include <stdlib.h>
32#include <string.h>
33#include <stdio.h>
34#include <ctype.h>
35#include <unistd.h>
36#include "d2m.h"
37
38
39
40
41
42/* ------------------------------------------------------------------- */
43/* main() program */
44/* ------------------------------------------------------------------- */
45
46int main (int argc, char **argv)
47{
48 int rc, i, f2709, tracing, argtype, mformat, marcfile;
49 int linel, llen, slen;
50
51 FILE *df, *of;
52 char dfilename[64], tfilename[64], ofilename[64], line[50000];
53 char *pq, *p, *q, pre[16], *l, *u;
54 char marcformat[64], url[1000];
55 char buffer[50000];
56
57 char recstatus = 'n';
58 char rectype = 'm';
59 char biblevel = ' ';
60
61 enum options {OPT_NONE, OPT_FILE, OPT_TRAC, OPT_MARC};
62
63
64/* Initialize */
65
66 of = stdout;
67 *url = *ofilename = 0;
68 strcpy(marcformat, "NORMARC");
69
70 tracing = FALSE;
71 *tfilename = 0;
72 f2709 = TRUE;
73 marcfile = 0;
74
75 *line = 0;
76
77 linel = D2M_LINELEN;
78 if (!linel)
79 linel = 80;
80
81 if (linel > 200)
82 linel = 200;
83
84 llen = linel - 9;
85 slen = linel - 30;
86 if (slen < 10)
87 slen = 10;
88
89
90/* Read command line arguments */
91
92 if (argc < 2)
93 {
94 printf(" Syntax : d2main [File] [Options]\n\n");
95 printf(" File : This may be a local filename, or a URL (which is\n");
96 printf(" recognized by the \"http://\" prefix). When a URL,\n");
97 printf(" the remote file will be fetched using the builtin\n");
98 printf(" HTTP \"client\".\n\n");
99 printf(" Options : -l : Format = line format\n");
100 printf(" (Default is ISO 2709)\n");
101 printf(" Options : -f [filename] : Outputfile\n");
102 printf(" (Default is stdout)\n");
103 printf(" Options : -m [marcformat] : Marcformat (upper case)\n");
104 printf(" (Default is NORMARC)\n");
105 printf(" Options : -t [filename] : Tracefile\n");
106 printf(" (Default is no trace)\n\n");
107 printf(" Return : 0 : OK\n");
108 printf(" 1 : Syntax error\n");
109 printf(" 2 : Illegal MARC format\n");
110 printf(" 3 : Cannot open output file\n");
111 printf(" 8 : Error in MARC conversion\n");
112 printf(" 9 : URL not found\n");
113 exit(1);
114 }
115
116 strcpy(dfilename, argv[1]);
117
118
119/* Get options */
120
121 if (argc > 2)
122 {
123 argtype = OPT_NONE;
124
125 for (i = 1; i < argc; i++)
126 {
127 if (argtype == OPT_FILE)
128 {
129 strcpy(ofilename, argv[i]);
130 argtype = OPT_NONE;
131 }
132 else if (argtype == OPT_TRAC)
133 {
134 strcpy(tfilename, argv[i]);
135 tracing = TRUE;
136 argtype = OPT_NONE;
137 }
138 else if (argtype == OPT_MARC)
139 {
140 strcpy(marcformat, argv[i]);
141 argtype = OPT_NONE;
142 }
143 else if (strcmp(argv[i], "-l") == 0)
144 {
145 f2709 = FALSE;
146 argtype = OPT_NONE;
147 }
148 else if (strcmp(argv[i], "-m") == 0)
149 argtype = OPT_MARC;
150 else if (strcmp(argv[i], "-f") == 0)
151 argtype = OPT_FILE;
152 else if (strcmp(argv[i], "-t") == 0)
153 argtype = OPT_TRAC;
154 else
155 argtype = OPT_NONE;
156 }
157 }
158
159/* Verify MARC format */
160
161 if (strncasecmp(marcformat, "US", 2) == 0)
162 mformat = USMARC;
163 if (strncasecmp(marcformat, "UNI", 3) == 0)
164 mformat = UNIMARC;
165 else if (strncasecmp(marcformat, "DAN", 3) == 0)
166 mformat = DANMARC;
167 else if (strncasecmp(marcformat, "FIN", 3) == 0)
168 mformat = FINMARC;
169 else if (strncasecmp(marcformat, "IS", 2) == 0)
170 mformat = ISMARC;
171 else if (strncasecmp(marcformat, "NOR", 3) == 0)
172 mformat = NORMARC;
173 else if (strncasecmp(marcformat, "SWE", 3) == 0)
174 mformat = SWEMARC;
175 else
176 exit(2);
177
178
179/* Fetch URL if remote file */
180
181 if (strncmp(dfilename, "http://", 7) == 0)
182 {
183 strcpy(url, dfilename);
184 sprintf(dfilename, "%s.%d", D2M_TEMPFILE, getpid());
185 rc = wfetch(&url, &dfilename);
186 if (!rc)
187 {
188 remove(dfilename);
189 exit(9);
190 }
191 if (rc == 2)
192 marcfile = 1;
193 }
194
195
196
197
198/* Convert into buffer (line format) */
199
200 if (marcfile)
201 rc = M2Mconv((char *) dfilename, (char *) buffer,
202 (char *) tfilename, mformat);
203 else
204 rc = D2Mconv((char *) dfilename, (char *) buffer,
205 (char *) tfilename, (char *) url, mformat);
206
207 if (!rc)
208 {
209 fclose(df);
210 exit(8);
211 }
212
213 if (*url)
214 remove(dfilename);
215
216
217/* Prepare for output */
218
219 if (*ofilename)
220 {
221 of = fopen(ofilename, "w");
222 if (!of)
223 exit(3);
224 }
225 else
226 of = stdout;
227
228
229/* Write output, if necessary converted to ISO 27809 */
230
231 if (f2709)
232 fprintf(of, "%s",
233 (char *) MARC2709(buffer, 0, 6, recstatus, rectype, biblevel));
234
235 else
236 {
237 l = strtok((char *) buffer, "\n");
238
239 while (l)
240 {
241 write_field(l, of, linel, llen, slen);
242 l = strtok(NULL, "\n");
243 }
244 }
245
246 if (*ofilename)
247 fclose(of);
248}
Note: See TracBrowser for help on using the repository browser.