/[public]/psiconv/trunk/program/psiconv/psiconv.c
ViewVC logotype

Annotation of /psiconv/trunk/program/psiconv/psiconv.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 219 - (hide annotations)
Mon Feb 23 13:08:32 2004 UTC (20 years, 2 months ago) by frodo
File MIME type: text/plain
File size: 9209 byte(s)
(Frodo) Psiconv program work
  * Added -c option

1 frodo 2 /*
2     psiconv.c - Part of psiconv, a PSION 5 file formats converter
3 frodo 196 Copyright (c) 1999-2004 Frodo Looijaard <frodol@dds.nl>
4 frodo 2
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9    
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13     GNU General Public License for more details.
14    
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18     */
19    
20     /* Driver program */
21    
22     #include "config.h"
23 frodo 20 #include "compat.h"
24 frodo 2 #include <getopt.h>
25     #include <stdio.h>
26     #include <sys/stat.h>
27     #include <stdlib.h>
28 frodo 34 #include <string.h>
29 frodo 193 #include <strings.h>
30 frodo 34 #include <ctype.h>
31 frodo 2
32     #ifdef HAVE_UNISTD_H
33     #include <unistd.h>
34     #endif
35    
36 frodo 34 #ifdef IMAGEMAGICK
37 frodo 152 #include "magick-aux.h"
38 frodo 34 #endif
39    
40 frodo 185 #include <psiconv/data.h>
41     #include <psiconv/list.h>
42     #include <psiconv/parse.h>
43     #include <psiconv/configuration.h>
44     #include "psiconv.h"
45 frodo 2 #include "gen.h"
46    
47     static void print_help(void);
48     static void print_version(void);
49     static void strtoupper(char *str);
50    
51     void print_help(void)
52     {
53 frodo 185 fileformat ff;
54 frodo 34 int i,j;
55    
56 frodo 2 puts("Syntax: psiconv [OPTIONS..] [FILE]");
57 frodo 185 puts("Convert the psion file FILE to other formats");
58 frodo 2 puts("If FILE is not specified, use stdin");
59 frodo 219 puts(" -c, --configfile=FILE Read extra configuration file after normal ones");
60     puts(" -e, --encoding=ENC Output encoding (default: UTF8)");
61     puts(" -h, --help Display this help and exit");
62     puts(" -n, --noise=LEVEL Select what to print on stderr (overrides psiconv.conf)");
63     puts(" -o, --outputfile Output to file instead of stdout");
64     puts(" -T, --type=FILETYPE Output type (default: XHTML or TIFF");
65     puts(" -V, --version Display the program version and exit");
66 frodo 2 puts("");
67 frodo 185 puts("The following encodings are currently supported:");
68     puts(" UTF8 Variable length Unicode encoding");
69     puts(" UCS2 Fixed 16-bit length Unicode encoding");
70     puts(" Psion The encoding your Psion uses (as in psiconv.conf)");
71     puts(" ASCII 7-bit ASCII (other symbols are substituted by '?')");
72     puts("");
73 frodo 192 puts("The following noise levels are currently supported:");
74     puts(" 1 or F: Fatal errors only");
75     puts(" 2 or E: Errors");
76     puts(" 3 or W: Warnings");
77     puts(" 4 or P: Progress indicators");
78     puts(" 5 or D: Debug data");
79     puts("");
80 frodo 219 puts("The following abbreviations are used in the output types list:");
81 frodo 192 puts(" C - processes ClipArt files");
82     puts(" c - processes ClipArt files containing only one image");
83     puts(" M - processes MBM files");
84     puts(" m - processes MBM files containing only one image");
85     puts(" S - processes Sketch files");
86     puts(" T - processes TextEd files");
87     puts(" W - processes Word files");
88     puts("");
89 frodo 2 puts("The following output types are known:");
90 frodo 34 for (i = 0; i < psiconv_list_length(fileformat_list); i ++) {
91     ff = psiconv_list_get(fileformat_list,i);
92     printf(" %s",ff->name);
93 frodo 192 for (j = strlen(ff->name); j < 10; j++)
94 frodo 34 putchar(' ');
95 frodo 192 printf("[%c%c%c%c%c] ",
96     ff->supported_format & FORMAT_CLIPART_MULTIPLE?'C':
97     ff->supported_format & FORMAT_CLIPART_SINGLE?'c':' ',
98     ff->supported_format & FORMAT_MBM_MULTIPLE?'M':
99     ff->supported_format & FORMAT_MBM_SINGLE?'m':' ',
100     ff->supported_format & FORMAT_SKETCH?'S':' ',
101     ff->supported_format & FORMAT_TEXTED?'T':' ',
102     ff->supported_format & FORMAT_WORD?'W':' ');
103 frodo 34 puts(ff->description);
104     }
105 frodo 147 puts("");
106     puts("When using UTF8 with LaTeX type, the resulting LaTeX source should be converted");
107     puts(" to a suitable encoding for your LaTeX installation before being typeset");
108 frodo 2 }
109    
110     void print_version(void)
111     {
112     printf("Version %s\n",VERSION);
113     }
114    
115     void strtoupper(char *str)
116     {
117     int i;
118     for (i = 0; i < strlen(str); i ++)
119     str[i] = toupper(str[i]);
120     }
121    
122     int main(int argc, char *argv[])
123     {
124     struct option long_options[] =
125     {
126     {"help",no_argument,NULL,'h'},
127     {"version",no_argument,NULL,'V'},
128 frodo 219 {"configfile",required_argument,NULL,'c'},
129 frodo 192 {"noise",required_argument,NULL,'n'},
130 frodo 2 {"outputfile",required_argument,NULL,'o'},
131     {"type",required_argument,NULL,'T'},
132 frodo 185 {"encoding",no_argument,NULL,'e'},
133 frodo 2 {0,0,0,0}
134     };
135 frodo 219 const char* short_options = "hVn:o:e:T:c:";
136 frodo 2 int option_index;
137     FILE * f;
138     struct stat fbuf;
139    
140     const char *inputfilename = "";
141     const char *outputfilename = "";
142 frodo 219 const char *extra_configfile = NULL;
143 frodo 192 char *type = NULL;
144 frodo 185 encoding encoding_type=ENCODING_UTF8;
145     psiconv_list outputlist;
146 frodo 219 int verbosity = 0;
147 frodo 2
148 frodo 34 int c,i,res;
149 frodo 2 psiconv_buffer buf;
150     psiconv_file file;
151 frodo 185 fileformat ff = NULL;
152 frodo 2
153 frodo 185 if (!(fileformat_list = psiconv_list_new(sizeof(struct fileformat_s)))) {
154     fputs("Out of memory error",stderr);
155     exit(1);
156     }
157    
158 frodo 34 init_txt();
159 frodo 192 init_xhtml();
160     init_html4();
161 frodo 187 init_image();
162 frodo 34
163 frodo 2 while(1) {
164     c = getopt_long(argc,argv,short_options, long_options, &option_index);
165     if (c == -1)
166     break;
167     switch(c) {
168     case 'h': print_help(); exit(0);
169     case 'V': print_version(); exit(0);
170 frodo 192 case 'n': switch(optarg[0]) {
171     case '1': case 'F':case 'f':
172 frodo 219 verbosity=PSICONV_VERB_FATAL;
173 frodo 192 break;
174     case '2': case 'E':case 'e':
175 frodo 219 verbosity=PSICONV_VERB_ERROR;
176 frodo 192 break;
177     case '3': case 'W':case 'w':
178 frodo 219 verbosity=PSICONV_VERB_WARN;
179 frodo 192 break;
180     case '4': case 'P':case 'p':
181 frodo 219 verbosity=PSICONV_VERB_PROGRESS;
182 frodo 192 break;
183     case '5': case 'D':case 'd':
184 frodo 219 verbosity=PSICONV_VERB_DEBUG;
185 frodo 192 break;
186     default:
187     fputs("Unknown noise level\n",stderr);
188     exit(1);
189     }
190 frodo 2 break;
191     case 'o': outputfilename = strdup(optarg); break;
192     case 'T': type = strdup(optarg); break;
193 frodo 185 case 'e': if(!strcmp(optarg,"UTF8"))
194     encoding_type = ENCODING_UTF8;
195     else if (!strcmp(optarg,"UCS2"))
196     encoding_type = ENCODING_UCS2;
197     else if (!strcmp(optarg,"ASCII"))
198     encoding_type = ENCODING_ASCII;
199     else if (!strcmp(optarg,"Psion"))
200     encoding_type = ENCODING_PSION;
201     else {
202     fputs("Unknown encoding type "
203     "(try '-h' for more information\n",stderr);
204     exit(1);
205     }
206     break;
207 frodo 219 case 'c': extra_configfile = strdup(optarg); break;
208 frodo 34 case '?': case ':': fputs("Try `-h' for more information\n",stderr);
209 frodo 2 exit(1);
210     default: fprintf(stderr,"Internal error: getopt_long returned character "
211     "code 0%o ?? (contact the author)\n", c);
212     exit(1); break;
213     }
214     }
215     if (optind < argc-1) {
216     fputs("I can only convert one file!\n"
217     "Try `-h' for more information\n",stderr);
218     exit(1);
219     } else if (optind == argc-1)
220 frodo 185 if (!(inputfilename = strdup(argv[optind]))) {
221     fputs("Out of memory error",stderr);
222     exit(1);
223     }
224 frodo 2
225 frodo 219 psiconv_config config = psiconv_config_default();
226     psiconv_config_read(extra_configfile,&config);
227     if (verbosity)
228     config->verbosity = verbosity;
229 frodo 65
230 frodo 2 /* Open inputfile for reading */
231    
232     if (strlen(inputfilename) != 0) {
233     if(stat(inputfilename,&fbuf) < 0) {
234     perror(inputfilename);
235     exit(1);
236     }
237     f = fopen(inputfilename,"r");
238     if (! f) {
239     perror(inputfilename);
240     exit(1);
241     }
242     } else
243     f = stdin;
244    
245 frodo 185 if (!(buf = psiconv_buffer_new())) {
246     fputs("Out of memory error",stderr);
247     exit(1);
248     }
249     if (psiconv_buffer_fread_all(buf,f)) {
250     fprintf(stderr,"Failure reading file");
251     exit(1);
252     }
253 frodo 2
254     if (strlen(inputfilename) != 0)
255     if (fclose(f)) {
256     perror(inputfilename);
257     exit(1);
258     }
259    
260 frodo 168 if (psiconv_parse(config,buf,&file) || (file->type == psiconv_unknown_file))
261 frodo 66 {
262     fprintf(stderr,"Parse error\n");
263     exit(1);
264     }
265 frodo 2
266 frodo 192 if (!type) {
267     switch(file->type) {
268     case psiconv_word_file:
269     case psiconv_texted_file:
270     default:
271     type = "XHTML"; break;
272     case psiconv_mbm_file:
273     case psiconv_clipart_file:
274     case psiconv_sketch_file:
275     type = "TIFF"; break;
276     }
277     } else
278     strtoupper(type);
279    
280 frodo 34 for (i = 0; i < psiconv_list_length(fileformat_list); i ++) {
281     ff = psiconv_list_get(fileformat_list,i);
282 frodo 147 if (! strcasecmp(type,ff->name)) {
283 frodo 34 break;
284     }
285     }
286    
287     if (i == psiconv_list_length(fileformat_list)) {
288 frodo 2 fprintf(stderr,"Unknown output type: `%s'\n",type);
289     exit(1);
290     }
291    
292 frodo 185 if (!(outputlist = psiconv_list_new(sizeof(psiconv_u8)))) {
293     fputs("Out of memory error\n",stderr);
294     exit(1);
295     }
296    
297     res = ff->output(config,outputlist,file,type,encoding_type);
298     if (res) {
299     fprintf(stderr,
300     "Output format `%s' not permitted for this file type\n",type);
301     exit(1);
302     }
303    
304 frodo 2 psiconv_free_file(file);
305    
306 frodo 185 if (strlen(outputfilename) != 0) {
307     f = fopen(outputfilename,"w");
308     if (! f) {
309     perror(inputfilename);
310     exit(1);
311     }
312     } else
313     f = stdout;
314    
315     psiconv_list_fwrite_all(outputlist,f);
316    
317     if (fclose(f)) {
318     perror(inputfilename);
319     exit(1);
320     }
321    
322     psiconv_list_free(outputlist);
323    
324 frodo 2 exit(0);
325     }

frodo@frodo.looijaard.name
ViewVC Help
Powered by ViewVC 1.1.26