]> www.wagner.pp.ru Git - oss/catdoc.git/blob - src/xls2csv.c
Erroneusly inserted data removed
[oss/catdoc.git] / src / xls2csv.c
1 /*****************************************************************/
2 /* Main program for parsing XLS files                            */
3 /*                                                               */
4 /* This file is part of catdoc project                           */
5 /* (c) David Rysdam  1998                                        */
6 /* (c) Victor Wagner 1998-2003, (c) Alex Ott 2003                    */
7 /*****************************************************************/
8 #ifdef HAVE_CONFIG_H
9 #include <config.h>
10 #endif
11 #include <stdio.h>
12 #include <time.h>
13 #include <math.h>
14 #include <ctype.h>
15 #include <string.h>
16 #include "xltypes.h"
17 #include "catdoc.h"
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include "catdoc.h"
21 #include "float.h"
22 #include "xls.h"
23
24 #ifdef __TURBOC__
25 #define strcasecmp(a,b) strcmpi(a,b)
26 #endif
27 extern char *forced_date_format; 
28 extern char number_format[];
29 extern char *sheet_separator;
30 /************************************************************************/
31 /* Displays  help message                                               */
32 /************************************************************************/
33 void help (void) {
34         printf("Usage:\n xls2csv [-xlV] [-g number] [-f date-format] [-b string] [-s charset] [-d charset] [-c char] [ -q number] files\n");
35 }
36 /* Defines unicode chars which should be
37    replaced by strings before UNICODE->target chatset
38    mappigs are applied i.e. TeX special chars like %
39    */
40 char *input_buffer, *output_buffer;
41 int main(int argc, char *argv[])
42 {
43         FILE *input;
44         FILE *new_file, *ole_file;
45         char *filename =NULL;
46         short int *tmp_charset;
47         int c;
48         int i;
49         char *tempname;
50         read_config_file(SYSTEMRC);
51 #ifdef USERRC
52         tempname=find_file(strdup(USERRC),getenv("HOME"));
53         if (tempname) {
54                 read_config_file(tempname);
55                 free(tempname);
56         }
57 #endif
58 #ifdef HAVE_LANGINFO
59                get_locale_charset();
60 #endif
61         
62         check_charset(&dest_csname,dest_csname); 
63                 
64         while ((c=getopt(argc,argv,"Vlf:s:d:xq:c:b:g:p:"))!=-1) {
65                 switch(c)  {
66                         case 'l':
67                                 list_charsets(); exit(0);
68                         case 'x': 
69                                 unknown_as_hex = 1; break;
70                         case 's':
71                                 check_charset(&source_csname,optarg);
72                                 source_charset=read_charset(source_csname);
73                                 break;
74                         case 'b':
75                                 sheet_separator= strdup(optarg);
76                                 break;
77                         case 'd':
78                                 check_charset(&dest_csname,optarg);
79                                 break;
80                         case 'q':
81                                 {   char *errptr;
82                                         quote_mode = strtol(optarg,&errptr,0);
83                                         if ((errptr && *errptr)||quote_mode<0||quote_mode>3) {
84                                                 fprintf(stderr,
85                                                                 "argument of -q should be number from 0 to 3\n");
86                                                 exit(1);
87                                         }
88                                 }    
89                                 break;
90                         case 'c':
91                                 cell_separator = optarg[0];
92                                 break;
93                         case 'f':
94                                 forced_date_format = strdup(optarg);
95                                 break;
96                         case 'g': 
97                                         { char *strend;
98                                           int digits = strtol(optarg,&strend,0);
99                                           if (*strend||digits<0||digits>DBL_DIG) {
100                                                   fprintf(stderr,"value of -g option should be numbe between 0 and %d, not '%s'\n", DBL_DIG, optarg);
101                                                   exit(1);
102                                           }
103                                           sprintf(number_format,"%%.%dg",digits);
104                                         }
105                                         break;
106                         case 'V': printf("Catdoc Version %s\n",CATDOC_VERSION);
107                                           exit(0);
108                         default:
109                                 help();
110                                 exit(1);
111                 }       
112         }
113 /* If we are using system strftime, we need to  set LC_TIME locale
114  * category unless choosen charset is not same as system locale
115  */ 
116 #if defined(HAVE_LANGINFO) && defined(HAVE_STRFTIME) && !defined(__TURB0C__)
117         set_time_locale();
118 #endif  
119         /* charset conversion init*/
120         input_buffer=malloc(FILE_BUFFER);
121         if (strcmp(dest_csname,"utf-8")) {
122                 tmp_charset=read_charset(dest_csname);
123                 if (!tmp_charset) {
124                         fprintf(stderr,"Cannot load target charset %s\n",dest_csname);
125                         exit(1);
126                 }       
127                 target_charset=make_reverse_map(tmp_charset);
128                 free(tmp_charset);
129         } else { 
130                 target_charset=NULL;
131         } 
132         spec_chars=read_substmap(stradd("ascii",SPEC_EXT));
133         if (!spec_chars) {
134                 fprintf(stderr,"Cannod read substitution map ascii%s\n",
135                                 SPEC_EXT);
136                 exit(1);
137         }  
138         replacements=read_substmap(stradd("ascii",REPL_EXT));
139         if (!replacements) {
140                 fprintf(stderr,"Cannod read substitution map ascii%s\n",
141                                 REPL_EXT);
142                 exit(1);
143         }  
144         if (optind>=argc) {
145                 if (isatty(fileno(stdin))) {
146                         help();
147                         exit(0);
148                 }    
149                 do_table(stdin,"STDIN");
150                 exit (0);
151         }       
152         for (i=optind;i<argc;i++) {
153                 filename = argv[i];
154                 input=fopen(filename,"rb");
155                 if (!input) {
156                         perror(filename);
157                         exit(1);
158                 }
159                 if ((new_file=ole_init(input, NULL, 0)) != NULL) {
160                         set_ole_func();
161                         while((ole_file=ole_readdir(new_file)) != NULL) {
162                                 int res=ole_open(ole_file);
163 /*                              fprintf(stderr, "name = %s\n", ((oleEntry*)ole_file)->name); */
164                                 if (res >= 0) {
165                                         if (strcasecmp(((oleEntry*)ole_file)->name , "Workbook") == 0
166                                                 || strcasecmp(((oleEntry*)ole_file)->name,"Book") == 0) {
167                                                 do_table(ole_file,filename);
168                                         }
169                                 } 
170                                 ole_close(ole_file);
171                         }
172                         set_std_func();
173                         ole_finish();
174                         fclose(new_file);
175                 } else {
176                         fprintf(stderr, "%s is not OLE file or Error\n", filename);
177                 }
178         }
179         return 0;
180 }