source: main/tags/2.70-branch-merged/gsdl/packages/d2m/D2Mmain.c@ 26198

Last change on this file since 26198 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: 7.0 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 else if (strncasecmp(marcformat, "DAN", 3) == 0)
164 mformat = DANMARC;
165 else if (strncasecmp(marcformat, "FIN", 3) == 0)
166 mformat = FINMARC;
167 else if (strncasecmp(marcformat, "IS", 2) == 0)
168 mformat = ISMARC;
169 else if (strncasecmp(marcformat, "NOR", 3) == 0)
170 mformat = NORMARC;
171 else if (strncasecmp(marcformat, "SWE", 3) == 0)
172 mformat = SWEMARC;
173 else
174 exit(2);
175
176
177/* Fetch URL if remote file */
178
179 if (strncmp(dfilename, "http://", 7) == 0)
180 {
181 strcpy(url, dfilename);
182 sprintf(dfilename, "%s.%d", D2M_TEMPFILE, getpid());
183 rc = wfetch(&url, &dfilename);
184 if (!rc)
185 {
186 remove(dfilename);
187 exit(9);
188 }
189 if (rc == 2)
190 marcfile = 1;
191 }
192
193
194
195
196/* Convert into buffer (line format) */
197
198 if (marcfile)
199 rc = M2Mconv((char *) dfilename, (char *) buffer,
200 (char *) tfilename, mformat);
201 else
202 rc = D2Mconv((char *) dfilename, (char *) buffer,
203 (char *) tfilename, (char *) url, mformat);
204
205 if (!rc)
206 {
207 fclose(df);
208 exit(8);
209 }
210
211 if (*url)
212 remove(dfilename);
213
214
215/* Prepare for output */
216
217 if (*ofilename)
218 {
219 of = fopen(ofilename, "w");
220 if (!of)
221 exit(3);
222 }
223 else
224 of = stdout;
225
226
227/* Write output, if necessary converted to ISO 27809 */
228
229 if (f2709)
230 fprintf(of, "%s",
231 (char *) MARC2709(buffer, 0, 6, recstatus, rectype, biblevel));
232
233 else
234 {
235 l = strtok((char *) buffer, "\n");
236
237 while (l)
238 {
239 write_field(l, of, linel, llen, slen);
240 l = strtok(NULL, "\n");
241 }
242 }
243
244 if (*ofilename)
245 fclose(of);
246}
Note: See TracBrowser for help on using the repository browser.