/[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 192 - (hide annotations)
Mon Feb 2 20:43:04 2004 UTC (20 years, 2 months ago) by frodo
File MIME type: text/plain
File size: 8885 byte(s)
(Frodo) Psiconv program update
  * Created html4 target
  * Update of xhtml target (print entities if ASCII, and others)
  * Made everything static that should not be exported
  * Renamed stuff to xhtml were appropriate
  * The fileformat data does now contain the supported Psion files to convert
  * This is also printed in the help text
  * ENCODING_ASCII_HTML introduced (only used internally)
  * Replaced debug, silent, verbose options with noise option
  * Default targets are XHTML and TIFF

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

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