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

Contents of /psiconv/trunk/program/psiconv/psiconv.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 20 - (show annotations)
Wed Oct 27 15:05:03 1999 UTC (20 years, 5 months ago) by frodo
File MIME type: text/plain
File size: 5422 byte(s)
(Frodo) Some IRIX compatibility issues fixed, as well as some compiler
        warnings

1 /*
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 #include "compat.h"
24 #include <getopt.h>
25 #include <stdio.h>
26 #include <sys/stat.h>
27 #include <stdlib.h>
28
29 #ifdef HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
32
33 #include <string.h>
34 #include <ctype.h>
35 #include "data.h"
36 #include "parse.h"
37 #include "gen.h"
38
39 static void print_help(void);
40 static void print_version(void);
41 static void strtoupper(char *str);
42
43 int psiconv_verbosity = PSICONV_VERB_WARN;
44
45 void print_help(void)
46 {
47 puts("Syntax: psiconv [OPTIONS..] [FILE]");
48 puts("Convert the psion Word file FILE to a HTML file");
49 puts("If FILE is not specified, use stdin");
50 puts(" -d, --debug Show debug information on stderr");
51 puts(" -e, --exact Quit on any parse errors or warnings");
52 puts(" -h, --help Display this help and exit");
53 puts(" -o, --outputfile Output to file instead of stdout");
54 puts(" -s, --silent Do not even show warnings on stderr");
55 puts(" -T, --type=FILETYPE Output type");
56 puts(" -V, --version Display the program version and exit");
57 puts(" -v, --verbose Show progress indicators on stderr");
58 puts("");
59 puts("The following output types are known:");
60 puts(" HTML (default)");
61 puts(" HTML4");
62 puts(" ASCII");
63 puts(" RTF");
64 }
65
66 void print_version(void)
67 {
68 printf("Version %s\n",VERSION);
69 }
70
71 void strtoupper(char *str)
72 {
73 int i;
74 for (i = 0; i < strlen(str); i ++)
75 str[i] = toupper(str[i]);
76 }
77
78 int main(int argc, char *argv[])
79 {
80 struct option long_options[] =
81 {
82 {"help",no_argument,NULL,'h'},
83 {"version",no_argument,NULL,'V'},
84 {"verbose",no_argument,NULL,'v'},
85 {"debug",no_argument,NULL,'d'},
86 {"silent",no_argument,NULL,'s'},
87 {"outputfile",required_argument,NULL,'o'},
88 {"type",required_argument,NULL,'T'},
89 {"exact",no_argument,NULL,'e'},
90 {0,0,0,0}
91 };
92 const char* short_options = "hVvsdo:eT:";
93 int option_index;
94 FILE * f;
95 struct stat fbuf;
96
97 const char *inputfilename = "";
98 const char *outputfilename = "";
99 char *type = strdup("HTML");
100 int exact=0;
101
102 int c;
103 psiconv_buffer buf;
104 psiconv_file file;
105
106 while(1) {
107 c = getopt_long(argc,argv,short_options, long_options, &option_index);
108 if (c == -1)
109 break;
110 switch(c) {
111 case 'h': print_help(); exit(0);
112 case 'V': print_version(); exit(0);
113 case 'v': if (psiconv_verbosity < PSICONV_VERB_PROGRESS)
114 psiconv_verbosity = PSICONV_VERB_PROGRESS;
115 break;
116 case 'd': psiconv_verbosity = PSICONV_VERB_DEBUG; break;
117 case 's': psiconv_verbosity = PSICONV_VERB_SILENT; break;
118 case 'e': exact = 1; break;
119 case 'o': outputfilename = strdup(optarg); break;
120 case 'T': type = strdup(optarg); break;
121 case '?': case ':': fputs("Try `-h' for more information",stderr);
122 exit(1);
123 default: fprintf(stderr,"Internal error: getopt_long returned character "
124 "code 0%o ?? (contact the author)\n", c);
125 exit(1); break;
126 }
127 }
128 if (optind < argc-1) {
129 fputs("I can only convert one file!\n"
130 "Try `-h' for more information\n",stderr);
131 exit(1);
132 } else if (optind == argc-1)
133 inputfilename = strdup(argv[optind]);
134
135 /* Open inputfile for reading */
136
137 if (strlen(inputfilename) != 0) {
138 if(stat(inputfilename,&fbuf) < 0) {
139 perror(inputfilename);
140 exit(1);
141 }
142 f = fopen(inputfilename,"r");
143 if (! f) {
144 perror(inputfilename);
145 exit(1);
146 }
147 } else
148 f = stdin;
149
150 buf = psiconv_list_new(sizeof(psiconv_u8));
151 while (! feof(f))
152 psiconv_list_fread(buf,1024,f);
153
154 if (strlen(inputfilename) != 0)
155 if (fclose(f)) {
156 perror(inputfilename);
157 exit(1);
158 }
159
160 if (psiconv_parse(buf,&file))
161 if(exact)
162 exit(1);
163
164 /* Open outputfile for writing */
165 if (strlen(outputfilename) != 0) {
166 f = fopen(outputfilename,"w");
167 if (! f) {
168 perror(outputfilename);
169 exit(1);
170 }
171 } else
172 f = stdout;
173
174 strtoupper(type);
175 if (! strcmp(type,"HTML"))
176 psiconv_gen_html(f,file);
177 else if (! strcmp(type,"HTML4"))
178 psiconv_gen_html4(f,file);
179 else if (! strcmp(type,"ASCII"))
180 psiconv_gen_txt(f,file);
181 else if (! strcmp(type,"RTF"))
182 psiconv_gen_rtf(f,file);
183 else if (! strcmp(type,"IMAGE"))
184 psiconv_gen_image(f,file);
185 else {
186 fprintf(stderr,"Unknown output type: `%s'\n",type);
187 exit(1);
188 }
189
190 if (strlen(outputfilename) != 0)
191 if (fclose(f)) {
192 perror(outputfilename);
193 exit(1);
194 }
195
196 psiconv_free_file(file);
197
198 exit(0);
199 }

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