/[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 193 - (hide annotations)
Mon Feb 2 21:56:48 2004 UTC (20 years, 3 months ago) by frodo
File MIME type: text/plain
File size: 8906 byte(s)
(Frodo) Make the formats stuff work again

  * Amend the configure.in to select the right targets
  * Remove RTF support (it never worked anyway)
  * Update the links creation scripts

And in the psiconv program and library:
  * Some fixes to reduce compiler warnings
  * Emit tabs as spaces in the HTML4/XHTML generators

1 frodo 2 /*
2     psiconv.c - Part of psiconv, a PSION 5 file formats converter
3     Copyright (c) 1999 Frodo Looijaard <frodol@dds.nl>
4    
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 192 puts(" -e, --encoding=ENC Output encoding (default: UTF8)");
60 frodo 2 puts(" -h, --help Display this help and exit");
61 frodo 192 puts(" -n, --noise=LEVEL Select what to print on stderr (overrides psiconv.conf)");
62 frodo 2 puts(" -o, --outputfile Output to file instead of stdout");
63 frodo 192 puts(" -T, --type=FILETYPE Output type (default: XHTML or TIFF");
64 frodo 2 puts(" -V, --version Display the program version and exit");
65     puts("");
66 frodo 185 puts("The following encodings are currently supported:");
67     puts(" UTF8 Variable length Unicode encoding");
68     puts(" UCS2 Fixed 16-bit length Unicode encoding");
69     puts(" Psion The encoding your Psion uses (as in psiconv.conf)");
70     puts(" ASCII 7-bit ASCII (other symbols are substituted by '?')");
71     puts("");
72 frodo 192 puts("The following noise levels are currently supported:");
73     puts(" 1 or F: Fatal errors only");
74     puts(" 2 or E: Errors");
75     puts(" 3 or W: Warnings");
76     puts(" 4 or P: Progress indicators");
77     puts(" 5 or D: Debug data");
78     puts("");
79     puts("The following abbreviations are used below:");
80     puts(" C - processes ClipArt files");
81     puts(" c - processes ClipArt files containing only one image");
82     puts(" M - processes MBM files");
83     puts(" m - processes MBM files containing only one image");
84     puts(" S - processes Sketch files");
85     puts(" T - processes TextEd files");
86     puts(" W - processes Word files");
87     puts("");
88 frodo 2 puts("The following output types are known:");
89 frodo 34 for (i = 0; i < psiconv_list_length(fileformat_list); i ++) {
90     ff = psiconv_list_get(fileformat_list,i);
91     printf(" %s",ff->name);
92 frodo 192 for (j = strlen(ff->name); j < 10; j++)
93 frodo 34 putchar(' ');
94 frodo 192 printf("[%c%c%c%c%c] ",
95     ff->supported_format & FORMAT_CLIPART_MULTIPLE?'C':
96     ff->supported_format & FORMAT_CLIPART_SINGLE?'c':' ',
97     ff->supported_format & FORMAT_MBM_MULTIPLE?'M':
98     ff->supported_format & FORMAT_MBM_SINGLE?'m':' ',
99     ff->supported_format & FORMAT_SKETCH?'S':' ',
100     ff->supported_format & FORMAT_TEXTED?'T':' ',
101     ff->supported_format & FORMAT_WORD?'W':' ');
102 frodo 34 puts(ff->description);
103     }
104 frodo 147 puts("");
105     puts("When using UTF8 with LaTeX type, the resulting LaTeX source should be converted");
106     puts(" to a suitable encoding for your LaTeX installation before being typeset");
107 frodo 2 }
108    
109     void print_version(void)
110     {
111     printf("Version %s\n",VERSION);
112     }
113    
114     void strtoupper(char *str)
115     {
116     int i;
117     for (i = 0; i < strlen(str); i ++)
118     str[i] = toupper(str[i]);
119     }
120    
121     int main(int argc, char *argv[])
122     {
123     struct option long_options[] =
124     {
125     {"help",no_argument,NULL,'h'},
126     {"version",no_argument,NULL,'V'},
127 frodo 192 {"noise",required_argument,NULL,'n'},
128 frodo 2 {"outputfile",required_argument,NULL,'o'},
129     {"type",required_argument,NULL,'T'},
130 frodo 185 {"encoding",no_argument,NULL,'e'},
131 frodo 2 {0,0,0,0}
132     };
133 frodo 192 const char* short_options = "hVn:o:e:T:";
134 frodo 2 int option_index;
135     FILE * f;
136     struct stat fbuf;
137    
138     const char *inputfilename = "";
139     const char *outputfilename = "";
140 frodo 192 char *type = NULL;
141 frodo 185 encoding encoding_type=ENCODING_UTF8;
142     psiconv_list outputlist;
143 frodo 2
144 frodo 34 int c,i,res;
145 frodo 2 psiconv_buffer buf;
146     psiconv_file file;
147 frodo 185 fileformat ff = NULL;
148 frodo 2
149 frodo 180 psiconv_config config = psiconv_config_default();
150     psiconv_config_read(NULL,&config);
151 frodo 65
152 frodo 185 if (!(fileformat_list = psiconv_list_new(sizeof(struct fileformat_s)))) {
153     fputs("Out of memory error",stderr);
154     exit(1);
155     }
156    
157 frodo 34 init_txt();
158 frodo 192 init_xhtml();
159     init_html4();
160 frodo 187 init_image();
161 frodo 34
162 frodo 2 while(1) {
163     c = getopt_long(argc,argv,short_options, long_options, &option_index);
164     if (c == -1)
165     break;
166     switch(c) {
167     case 'h': print_help(); exit(0);
168     case 'V': print_version(); exit(0);
169 frodo 192 case 'n': switch(optarg[0]) {
170     case '1': case 'F':case 'f':
171     config->verbosity=PSICONV_VERB_FATAL;
172     break;
173     case '2': case 'E':case 'e':
174     config->verbosity=PSICONV_VERB_ERROR;
175     break;
176     case '3': case 'W':case 'w':
177     config->verbosity=PSICONV_VERB_WARN;
178     break;
179     case '4': case 'P':case 'p':
180     config->verbosity=PSICONV_VERB_PROGRESS;
181     break;
182     case '5': case 'D':case 'd':
183     config->verbosity=PSICONV_VERB_DEBUG;
184     break;
185     default:
186     fputs("Unknown noise level\n",stderr);
187     exit(1);
188     }
189 frodo 2 break;
190     case 'o': outputfilename = strdup(optarg); break;
191     case 'T': type = strdup(optarg); break;
192 frodo 185 case 'e': if(!strcmp(optarg,"UTF8"))
193     encoding_type = ENCODING_UTF8;
194     else if (!strcmp(optarg,"UCS2"))
195     encoding_type = ENCODING_UCS2;
196     else if (!strcmp(optarg,"ASCII"))
197     encoding_type = ENCODING_ASCII;
198     else if (!strcmp(optarg,"Psion"))
199     encoding_type = ENCODING_PSION;
200     else {
201     fputs("Unknown encoding type "
202     "(try '-h' for more information\n",stderr);
203     exit(1);
204     }
205     break;
206 frodo 34 case '?': case ':': fputs("Try `-h' for more information\n",stderr);
207 frodo 2 exit(1);
208     default: fprintf(stderr,"Internal error: getopt_long returned character "
209     "code 0%o ?? (contact the author)\n", c);
210     exit(1); break;
211     }
212     }
213     if (optind < argc-1) {
214     fputs("I can only convert one file!\n"
215     "Try `-h' for more information\n",stderr);
216     exit(1);
217     } else if (optind == argc-1)
218 frodo 185 if (!(inputfilename = strdup(argv[optind]))) {
219     fputs("Out of memory error",stderr);
220     exit(1);
221     }
222 frodo 2
223 frodo 65
224 frodo 2 /* Open inputfile for reading */
225    
226     if (strlen(inputfilename) != 0) {
227     if(stat(inputfilename,&fbuf) < 0) {
228     perror(inputfilename);
229     exit(1);
230     }
231     f = fopen(inputfilename,"r");
232     if (! f) {
233     perror(inputfilename);
234     exit(1);
235     }
236     } else
237     f = stdin;
238    
239 frodo 185 if (!(buf = psiconv_buffer_new())) {
240     fputs("Out of memory error",stderr);
241     exit(1);
242     }
243     if (psiconv_buffer_fread_all(buf,f)) {
244     fprintf(stderr,"Failure reading file");
245     exit(1);
246     }
247 frodo 2
248     if (strlen(inputfilename) != 0)
249     if (fclose(f)) {
250     perror(inputfilename);
251     exit(1);
252     }
253    
254 frodo 168 if (psiconv_parse(config,buf,&file) || (file->type == psiconv_unknown_file))
255 frodo 66 {
256     fprintf(stderr,"Parse error\n");
257     exit(1);
258     }
259 frodo 2
260 frodo 192 if (!type) {
261     switch(file->type) {
262     case psiconv_word_file:
263     case psiconv_texted_file:
264     default:
265     type = "XHTML"; break;
266     case psiconv_mbm_file:
267     case psiconv_clipart_file:
268     case psiconv_sketch_file:
269     type = "TIFF"; break;
270     }
271     } else
272     strtoupper(type);
273    
274 frodo 34 for (i = 0; i < psiconv_list_length(fileformat_list); i ++) {
275     ff = psiconv_list_get(fileformat_list,i);
276 frodo 147 if (! strcasecmp(type,ff->name)) {
277 frodo 34 break;
278     }
279     }
280    
281     if (i == psiconv_list_length(fileformat_list)) {
282 frodo 2 fprintf(stderr,"Unknown output type: `%s'\n",type);
283     exit(1);
284     }
285    
286 frodo 185 if (!(outputlist = psiconv_list_new(sizeof(psiconv_u8)))) {
287     fputs("Out of memory error\n",stderr);
288     exit(1);
289     }
290    
291     res = ff->output(config,outputlist,file,type,encoding_type);
292     if (res) {
293     fprintf(stderr,
294     "Output format `%s' not permitted for this file type\n",type);
295     exit(1);
296     }
297    
298 frodo 2 psiconv_free_file(file);
299    
300 frodo 185 if (strlen(outputfilename) != 0) {
301     f = fopen(outputfilename,"w");
302     if (! f) {
303     perror(inputfilename);
304     exit(1);
305     }
306     } else
307     f = stdout;
308    
309     psiconv_list_fwrite_all(outputlist,f);
310    
311     if (fclose(f)) {
312     perror(inputfilename);
313     exit(1);
314     }
315    
316     psiconv_list_free(outputlist);
317    
318 frodo 2 exit(0);
319     }

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