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

Diff of /psiconv/trunk/program/psiconv/psiconv.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 184 Revision 185
50 50
51#ifdef IMAGEMAGICK 51#ifdef IMAGEMAGICK
52#include "magick-aux.h" 52#include "magick-aux.h"
53#endif 53#endif
54 54
55#include "psiconv/data.h" 55#include <psiconv/data.h>
56#include <psiconv/list.h>
56#include "psiconv/parse.h" 57#include <psiconv/parse.h>
57#include "psiconv/configuration.h" 58#include <psiconv/configuration.h>
59#include "psiconv.h"
58#include "gen.h" 60#include "gen.h"
59#include "psiconv.h"
60 61
61static void print_help(void); 62static void print_help(void);
62static void print_version(void); 63static void print_version(void);
63static void strtoupper(char *str); 64static void strtoupper(char *str);
64 65
65void print_help(void) 66void print_help(void)
66{ 67{
67 psiconv_fileformat ff; 68 fileformat ff;
68 int i,j; 69 int i,j;
69 70
70 puts("Syntax: psiconv [OPTIONS..] [FILE]"); 71 puts("Syntax: psiconv [OPTIONS..] [FILE]");
71 puts("Convert the psion Word file FILE to other formats"); 72 puts("Convert the psion file FILE to other formats");
72 puts("If FILE is not specified, use stdin"); 73 puts("If FILE is not specified, use stdin");
73 puts(" -d, --debug Show debug information on stderr"); 74 puts(" -d, --debug Show debug information on stderr");
75 puts(" -e, --encoding Output encoding (default: UTF8)");
74 puts(" -h, --help Display this help and exit"); 76 puts(" -h, --help Display this help and exit");
75 puts(" -o, --outputfile Output to file instead of stdout"); 77 puts(" -o, --outputfile Output to file instead of stdout");
76 puts(" -s, --silent Do not even show warnings on stderr"); 78 puts(" -s, --silent Do not even show warnings on stderr");
77 puts(" -T, --type=FILETYPE Output type"); 79 puts(" -T, --type=FILETYPE Output type");
78 puts(" -V, --version Display the program version and exit"); 80 puts(" -V, --version Display the program version and exit");
79 puts(" -v, --verbose Show progress indicators on stderr"); 81 puts(" -v, --verbose Show progress indicators on stderr");
80 puts(" -u, --UTF8 Input file is encoded in UTF8"); 82 puts("");
83 puts("The following encodings are currently supported:");
84 puts(" UTF8 Variable length Unicode encoding");
85 puts(" UCS2 Fixed 16-bit length Unicode encoding");
86 puts(" Psion The encoding your Psion uses (as in psiconv.conf)");
87 puts(" ASCII 7-bit ASCII (other symbols are substituted by '?')");
81 puts(""); 88 puts("");
82 puts("The following output types are known:"); 89 puts("The following output types are known:");
83 for (i = 0; i < psiconv_list_length(fileformat_list); i ++) { 90 for (i = 0; i < psiconv_list_length(fileformat_list); i ++) {
84 ff = psiconv_list_get(fileformat_list,i); 91 ff = psiconv_list_get(fileformat_list,i);
85 printf(" %s",ff->name); 92 printf(" %s",ff->name);
113 {"verbose",no_argument,NULL,'v'}, 120 {"verbose",no_argument,NULL,'v'},
114 {"debug",no_argument,NULL,'d'}, 121 {"debug",no_argument,NULL,'d'},
115 {"silent",no_argument,NULL,'s'}, 122 {"silent",no_argument,NULL,'s'},
116 {"outputfile",required_argument,NULL,'o'}, 123 {"outputfile",required_argument,NULL,'o'},
117 {"type",required_argument,NULL,'T'}, 124 {"type",required_argument,NULL,'T'},
118 {"UTF8",no_argument,NULL,'u'}, 125 {"encoding",no_argument,NULL,'e'},
119 {0,0,0,0} 126 {0,0,0,0}
120 }; 127 };
121 const char* short_options = "hVvsdo:uT:"; 128 const char* short_options = "hVvsdo:e:T:";
122 int option_index; 129 int option_index;
123 FILE * f; 130 FILE * f;
124 struct stat fbuf; 131 struct stat fbuf;
125 132
126 const char *inputfilename = ""; 133 const char *inputfilename = "";
127 const char *outputfilename = ""; 134 const char *outputfilename = "";
128 char *type = strdup("HTML3"); 135 char *type = strdup("HTML3");
129 psiconv_encoding encoding_type=PSICONV_ENCODING_CP1252; 136 encoding encoding_type=ENCODING_UTF8;
137 psiconv_list outputlist;
130 138
131 int c,i,res; 139 int c,i,res;
132 psiconv_buffer buf; 140 psiconv_buffer buf;
133 psiconv_file file; 141 psiconv_file file;
134 psiconv_fileformat ff; 142 fileformat ff = NULL;
135 143
136 psiconv_config config = psiconv_config_default(); 144 psiconv_config config = psiconv_config_default();
137 psiconv_config_read(NULL,&config); 145 psiconv_config_read(NULL,&config);
138 146
139 fileformat_list = psiconv_list_new(sizeof(struct psiconv_fileformat_s)); 147 if (!(fileformat_list = psiconv_list_new(sizeof(struct fileformat_s)))) {
148 fputs("Out of memory error",stderr);
149 exit(1);
150 }
151
140 init_txt(); 152 init_txt();
153#if 0
141 init_html(); 154 init_html();
142 init_html4(); 155 init_html4();
143 init_latex(); 156 init_latex();
144 init_rtf(); 157 init_rtf();
145 init_image(); 158 init_image();
159#endif
146 160
147 while(1) { 161 while(1) {
148 c = getopt_long(argc,argv,short_options, long_options, &option_index); 162 c = getopt_long(argc,argv,short_options, long_options, &option_index);
149 if (c == -1) 163 if (c == -1)
150 break; 164 break;
153 case 'V': print_version(); exit(0); 167 case 'V': print_version(); exit(0);
154 case 'v': if (config->verbosity < PSICONV_VERB_PROGRESS) 168 case 'v': if (config->verbosity < PSICONV_VERB_PROGRESS)
155 config->verbosity = PSICONV_VERB_PROGRESS; 169 config->verbosity = PSICONV_VERB_PROGRESS;
156 break; 170 break;
157 case 'd': config->verbosity = PSICONV_VERB_DEBUG; break; 171 case 'd': config->verbosity = PSICONV_VERB_DEBUG; break;
158 case 's': config->verbosity = PSICONV_VERB_FATAL; break; 172 case 's': config->verbosity = PSICONV_VERB_ERROR; break;
159 case 'o': outputfilename = strdup(optarg); break; 173 case 'o': outputfilename = strdup(optarg); break;
160 case 'T': type = strdup(optarg); break; 174 case 'T': type = strdup(optarg); break;
161 case 'u': encoding_type = PSICONV_ENCODING_UTF8; break; 175 case 'e': if(!strcmp(optarg,"UTF8"))
176 encoding_type = ENCODING_UTF8;
177 else if (!strcmp(optarg,"UCS2"))
178 encoding_type = ENCODING_UCS2;
179 else if (!strcmp(optarg,"ASCII"))
180 encoding_type = ENCODING_ASCII;
181 else if (!strcmp(optarg,"Psion"))
182 encoding_type = ENCODING_PSION;
183 else {
184 fputs("Unknown encoding type "
185 "(try '-h' for more information\n",stderr);
186 exit(1);
187 }
188 break;
162 case '?': case ':': fputs("Try `-h' for more information\n",stderr); 189 case '?': case ':': fputs("Try `-h' for more information\n",stderr);
163 exit(1); 190 exit(1);
164 default: fprintf(stderr,"Internal error: getopt_long returned character " 191 default: fprintf(stderr,"Internal error: getopt_long returned character "
165 "code 0%o ?? (contact the author)\n", c); 192 "code 0%o ?? (contact the author)\n", c);
166 exit(1); break; 193 exit(1); break;
169 if (optind < argc-1) { 196 if (optind < argc-1) {
170 fputs("I can only convert one file!\n" 197 fputs("I can only convert one file!\n"
171 "Try `-h' for more information\n",stderr); 198 "Try `-h' for more information\n",stderr);
172 exit(1); 199 exit(1);
173 } else if (optind == argc-1) 200 } else if (optind == argc-1)
174 inputfilename = strdup(argv[optind]); 201 if (!(inputfilename = strdup(argv[optind]))) {
202 fputs("Out of memory error",stderr);
203 exit(1);
204 }
175 205
176 206
177 /* Open inputfile for reading */ 207 /* Open inputfile for reading */
178 208
179 if (strlen(inputfilename) != 0) { 209 if (strlen(inputfilename) != 0) {
187 exit(1); 217 exit(1);
188 } 218 }
189 } else 219 } else
190 f = stdin; 220 f = stdin;
191 221
192 buf = psiconv_buffer_new(); 222 if (!(buf = psiconv_buffer_new())) {
223 fputs("Out of memory error",stderr);
224 exit(1);
225 }
193 psiconv_buffer_fread_all(buf,f); 226 if (psiconv_buffer_fread_all(buf,f)) {
227 fprintf(stderr,"Failure reading file");
228 exit(1);
229 }
194 230
195 if (strlen(inputfilename) != 0) 231 if (strlen(inputfilename) != 0)
196 if (fclose(f)) { 232 if (fclose(f)) {
197 perror(inputfilename); 233 perror(inputfilename);
198 exit(1); 234 exit(1);
202 { 238 {
203 fprintf(stderr,"Parse error\n"); 239 fprintf(stderr,"Parse error\n");
204 exit(1); 240 exit(1);
205 } 241 }
206 242
207 /* Set correct output file */
208 if (strlen(outputfilename) == 0)
209 outputfilename = "/dev/stdout";
210
211 strtoupper(type); 243 strtoupper(type);
212 for (i = 0; i < psiconv_list_length(fileformat_list); i ++) { 244 for (i = 0; i < psiconv_list_length(fileformat_list); i ++) {
213 ff = psiconv_list_get(fileformat_list,i); 245 ff = psiconv_list_get(fileformat_list,i);
214 if (! strcasecmp(type,ff->name)) { 246 if (! strcasecmp(type,ff->name)) {
215 res = ff->output(outputfilename,file,type, encoding_type);
216 if (res) {
217 fprintf(stderr,
218 "Output format `%s' not permitted for this file type\n",type);
219 exit(1);
220 }
221 break; 247 break;
222 } 248 }
223 } 249 }
224 250
225 if (i == psiconv_list_length(fileformat_list)) { 251 if (i == psiconv_list_length(fileformat_list)) {
226 fprintf(stderr,"Unknown output type: `%s'\n",type); 252 fprintf(stderr,"Unknown output type: `%s'\n",type);
227 exit(1); 253 exit(1);
228 } 254 }
229 255
256 if (!(outputlist = psiconv_list_new(sizeof(psiconv_u8)))) {
257 fputs("Out of memory error\n",stderr);
258 exit(1);
259 }
260
261 res = ff->output(config,outputlist,file,type,encoding_type);
262 if (res) {
263 fprintf(stderr,
264 "Output format `%s' not permitted for this file type\n",type);
265 exit(1);
266 }
267
230 psiconv_free_file(file); 268 psiconv_free_file(file);
231 269
270 if (strlen(outputfilename) != 0) {
271 f = fopen(outputfilename,"w");
272 if (! f) {
273 perror(inputfilename);
274 exit(1);
275 }
276 } else
277 f = stdout;
278
279 psiconv_list_fwrite_all(outputlist,f);
280
281 if (fclose(f)) {
282 perror(inputfilename);
283 exit(1);
284 }
285
286 psiconv_list_free(outputlist);
287
232 exit(0); 288 exit(0);
233} 289}

Legend:
Removed from v.184  
changed lines
  Added in v.185

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