Debian Codegraph @

src/conv/wks2csv/wks2csv.cpp

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /* libwps
3 * Version: MPL 2.0 / LGPLv2.1+
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * Major Contributor(s):
10 * Copyright (C) 2002 William Lachance ([email protected])
11 * Copyright (C) 2002,2004 Marc Maurer ([email protected])
12 *
13 * For minor contributions see the git repository.
14 *
15 * Alternatively, the contents of this file may be used under the terms
16 * of the GNU Lesser General Public License Version 2.1 or later
17 * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are
18 * applicable instead of those above.
19 *
20 * For further information visit http://libwps.sourceforge.net
21 */
22
23 #include <stdio.h>
24 #include <string.h>
25 #include <unistd.h>
26
27 #include <cstdlib>
28 #include <fstream>
29 #include <iostream>
30
31 #include <librevenge/librevenge.h>
32 #include <librevenge-generators/librevenge-generators.h>
33 #include <librevenge-stream/librevenge-stream.h>
34
35 #include <libwps/libwps.h>
36
37 #include "helper.h"
38
39 using namespace libwps;
40
41 #ifdef HAVE_CONFIG_H
42 #include "config.h"
43 #endif
44
45 #ifndef VERSION
46 #define VERSION "UNKNOWN VERSION"
47 #endif
48
49 static int printUsage()
50 {
51 printf("`wks2csv' converts various spreadsheets and database formats to CSV.\n");
52 printf("Supported formats are Microsoft Works Spreadsheet and Database, Lotus Wk1-Wk4 and Quattro Pro Wq1-Wq2.\n");
53 printf("\n");
54 printf("Usage: wks2csv [OPTION] FILE\n");
55 printf("\n");
56 printf("Options:\n");
57 printf("\t-h show this help message\n");
58 printf("\t-d CHAR set the decimal commas to character CHAR: default .\n");
59 printf("\t-e ENCODING Define the file encoding where encoding can be\n");
60 printf("\t\t CP037, CP424, CP437, CP737, CP500, CP775, CP850, CP852, CP855, CP856, CP857,\n");
61 printf("\t\t CP860, CP861, CP862, CP863, CP864, CP865, CP866, CP869, CP874, CP875, CP932,\n");
62 printf("\t\t CP950, CP1006, CP1026, CP1250, CP1251, CP1252, CP1253, CP1254, CP1255, CP1256,\n");
63 printf("\t\t CP1257, CP1258, MacArabic, MacCeltic, MacCEurope, MacCroation, MacCyrillic,\n");
64 printf("\t\t MacDevanage, MacFarsi, MacGaelic, MacGreek, MacGujarati, MacGurmukhi, MacHebrew,\n");
65 printf("\t\t MacIceland, MacInuit, MacRoman, MacRomanian, MacThai, MacTurkish.\n");
66 printf("\t-f CHAR Sets the field separator to character CHAR: default ,\n");
67 printf("\t-t CHAR set the text separator to character CHAR: default \"\n");
68 printf("\t-o FILE set the ouput file\n");
69 printf("\t-p PASSWORD set password to open the file\n");
70 printf("\t-v show version information\n");
71 printf("\t-N show the number of sheets\n");
72 printf("\t-n NUM choose the sheet to convert (1 means first sheet) \n");
73 printf("\t-F set to output the formula which exists in the file\n");
74 printf("\t-D FORMAT set date format: default \"%%m/%%d/%%y\"\n");
75 printf("\t-T FORMAT set time format: default \"%%H:%%M:%%S\"\n");
76 printf("\n");
77 printf("Examples:\n");
78 printf("\twks2cvs -d, -D\"%%d/%%m/%%y\" file convert a file using french locale\n");
79 printf("\n");
80 printf("Note:\n");
81 printf("\tIf -F is present, the formula are generated which english names\n");
82 printf("\n");
83 printf("Report bugs to <https://sourceforge.net/p/libwps/bugs/> or <https://bugs.documentfoundation.org/>.\n");
84 return -1;
85 }
86
87 static int printVersion()
88 {
89 printf("wks2csv %s\n", VERSION);
90 return 0;
91 }
92
93 int main(int argc, char *argv[])
94 {
95 bool printHelp=false;
96 bool printNumberOfSheet=false;
97 bool generateFormula=false;
98 char const *encoding="";
99 char const *password=nullptr;
100 char const *output = nullptr;
101 int sheetToConvert=0;
102 int ch;
103 char decSeparator='.', fieldSeparator=',', textSeparator='"';
104 std::string dateFormat("%m/%d/%y"), timeFormat("%H:%M:%S");
105
106 while ((ch = getopt(argc, argv, "e:hvo:d:f:p:t:D:Nn:FT:")) != -1)
107 {
108 switch (ch)
109 {
110 case 'D':
111 dateFormat=optarg;
112 break;
113 case 'F':
114 generateFormula=true;
115 break;
116 case 'N':
117 printNumberOfSheet=true;
118 break;
119 case 'T':
120 timeFormat=optarg;
121 break;
122 case 'd':
123 decSeparator=optarg[0];
124 break;
125 case 'e':
126 encoding=optarg;
127 break;
128 case 'f':
129 fieldSeparator=optarg[0];
130 break;
131 case 'n':
132 sheetToConvert=std::atoi(optarg);
133 break;
134 case 'o':
135 output=optarg;
136 break;
137 case 'p':
138 password=optarg;
139 break;
140 case 't':
141 textSeparator=optarg[0];
142 break;
143 case 'v':
144 printVersion();
145 return 0;
146 default:
147 case 'h':
148 printHelp = true;
149 break;
150 }
151 }
152 if (argc != 1+optind || printHelp)
153 {
154 printUsage();
155 return -1;
156 }
157 char const *file=argv[optind];
158 WPSConfidence confidence;
159 WPSKind kind;
160 bool needCharEncoding;
161 auto input=libwpsHelper::isSupported(file,confidence, kind,needCharEncoding);
162 if (!input || confidence == WPS_CONFIDENCE_NONE || (kind != WPS_SPREADSHEET && kind != WPS_DATABASE))
163 {
164 printf("ERROR: Unsupported file format!\n");
165 return 1;
166 }
167
168 WPSResult error=WPS_OK;
169 librevenge::RVNGStringVector vec;
170
171 try
172 {
173 librevenge::RVNGCSVSpreadsheetGenerator listenerImpl(vec, generateFormula);
174 listenerImpl.setSeparators(fieldSeparator, textSeparator, decSeparator);
175 listenerImpl.setDTFormats(dateFormat.c_str(),timeFormat.c_str());
176 error= WPSDocument::parse(input.get(), &listenerImpl, password, encoding);
177 }
178 catch (...)
179 {
180 error=WPS_PARSE_ERROR;
181 }
182
183 if (libwpsHelper::checkErrorAndPrintMessage(error))
184 return 1;
185 else if (vec.empty())
186 {
187 fprintf(stderr, "ERROR: bad output!\n");
188 return 1;
189 }
190 else if (sheetToConvert>0 && sheetToConvert>(int) vec.size())
191 {
192 fprintf(stderr, "ERROR: Can not find sheet %d\n", sheetToConvert);
193 return 1;
194 }
195
196 if (printNumberOfSheet)
197 {
198 std::cout << vec.size() << "\n";
199 return 0;
200 }
201 if (!output)
202 std::cout << vec[sheetToConvert>0 ? unsigned(sheetToConvert-1) : 0].cstr() << std::endl;
203 else
204 {
205 std::ofstream out(output);
206 out << vec[sheetToConvert>0 ? unsigned(sheetToConvert-1) : 0].cstr() << std::endl;
207 }
208 return 0;
209 }
210
211 /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */