| 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 <fstream> |
| 28 | #include <iostream> |
| 29 | |
| 30 | #include <librevenge/librevenge.h> |
| 31 | #include <librevenge-generators/librevenge-generators.h> |
| 32 | #include <librevenge-stream/librevenge-stream.h> |
| 33 | |
| 34 | #include <libwps/libwps.h> |
| 35 | |
| 36 | #include "helper.h" |
| 37 | |
| 38 | #ifdef HAVE_CONFIG_H |
| 39 | #include "config.h" |
| 40 | #endif |
| 41 | |
| 42 | #ifndef VERSION |
| 43 | #define VERSION "UNKNOWN VERSION" |
| 44 | #endif |
| 45 | |
| 46 | using namespace libwps; |
| 47 | |
| 48 | static int printUsage() |
| 49 | { |
| 50 | printf("`wks2text' converts various spreadsheets and database formats to plain text.\n"); |
| 51 | printf("Supported formats are Microsoft Works Spreadsheet and Database, Lotus Wk1-Wk4 and Quattro Pro Wq1-Wq2.\n"); |
| 52 | printf("\n"); |
| 53 | printf("Usage: wks2text [OPTION] FILE\n"); |
| 54 | printf("\n"); |
| 55 | printf("Options:\n"); |
| 56 | printf("\t-e ENCODING define the file encoding, where encoding can be\n"); |
| 57 | printf("\t\t CP037, CP424, CP437, CP737, CP500, CP775, CP850, CP852, CP855, CP856, CP857,\n"); |
| 58 | printf("\t\t CP860, CP861, CP862, CP863, CP864, CP865, CP866, CP869, CP874, CP875, CP1006, CP1026,\n"); |
| 59 | printf("\t\t CP1250, CP1251, CP1252, CP1253, CP1254, CP1255, CP1256, CP1257, CP1258,\n"); |
| 60 | printf("\t\t MacArabic, MacCeltic, MacCEurope, MacCroation, MacCyrillic, MacDevanage,\n"); |
| 61 | printf("\t\t MacFarsi, MacGaelic, MacGreek, MacGujarati, MacGurmukhi, MacHebrew, MacIceland,\n"); |
| 62 | printf("\t\t MacInuit, MacRoman, MacRomanian, MacThai, MacTurkish.\n"); |
| 63 | printf("\t-h show this help message\n"); |
| 64 | printf("\t-o FILE set the output file\n"); |
| 65 | printf("\t-p PASSWORD set password to open the file\n"); |
| 66 | printf("\t-v show version information\n"); |
| 67 | printf("\n"); |
| 68 | printf("Report bugs to <https://sourceforge.net/p/libwps/bugs/> or <https://bugs.documentfoundation.org/>.\n"); |
| 69 | return -1; |
| 70 | } |
| 71 | |
| 72 | static int printVersion() |
| 73 | { |
| 74 | printf("wks2text %s\n", VERSION); |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | int main(int argc, char *argv[]) |
| 79 | { |
| 80 | bool printHelp=false; |
| 81 | char const *encoding=""; |
| 82 | char const *password=nullptr; |
| 83 | char const *output = nullptr; |
| 84 | int ch; |
| 85 | |
| 86 | while ((ch = getopt(argc, argv, "hvo:p:")) != -1) |
| 87 | { |
| 88 | switch (ch) |
| 89 | { |
| 90 | case 'e': |
| 91 | encoding=optarg; |
| 92 | break; |
| 93 | case 'o': |
| 94 | output=optarg; |
| 95 | break; |
| 96 | case 'p': |
| 97 | password=optarg; |
| 98 | break; |
| 99 | case 'v': |
| 100 | printVersion(); |
| 101 | return 0; |
| 102 | default: |
| 103 | case 'h': |
| 104 | printHelp = true; |
| 105 | break; |
| 106 | } |
| 107 | } |
| 108 | if (argc != 1+optind || printHelp) |
| 109 | { |
| 110 | printUsage(); |
| 111 | return -1; |
| 112 | } |
| 113 | char const *file=argv[optind]; |
| 114 | WPSConfidence confidence; |
| 115 | WPSKind kind; |
| 116 | bool needCharEncoding; |
| 117 | auto input=libwpsHelper::isSupported(file,confidence, kind,needCharEncoding); |
| 118 | if (!input || confidence == WPS_CONFIDENCE_NONE || (kind != WPS_SPREADSHEET && kind != WPS_DATABASE)) |
| 119 | { |
| 120 | printf("ERROR: Unsupported file format!\n"); |
| 121 | return 1; |
| 122 | } |
| 123 | |
| 124 | librevenge::RVNGStringVector vec; |
| 125 | auto error=WPS_OK; |
| 126 | try |
| 127 | { |
| 128 | librevenge::RVNGTextSpreadsheetGenerator listenerImpl(vec); |
| 129 | error= WPSDocument::parse(input.get(), &listenerImpl, password, encoding); |
| 130 | } |
| 131 | catch (...) |
| 132 | { |
| 133 | error=WPS_PARSE_ERROR; |
| 134 | } |
| 135 | |
| 136 | if (libwpsHelper::checkErrorAndPrintMessage(error)) |
| 137 | return 1; |
| 138 | else if (vec.empty()) |
| 139 | { |
| 140 | fprintf(stderr, "ERROR: bad output!\n"); |
| 141 | return 1; |
| 142 | } |
| 143 | |
| 144 | if (!output) |
| 145 | { |
| 146 | for (unsigned i=0; i<vec.size(); ++i) |
| 147 | { |
| 148 | if (i) |
| 149 | std::cout << "\n\t############# Sheet " << i+1 << " ################\n\n"; |
| 150 | std::cout << vec[i].cstr() << std::endl; |
| 151 | } |
| 152 | } |
| 153 | else |
| 154 | { |
| 155 | std::ofstream out(output); |
| 156 | for (unsigned i=0; i<vec.size(); ++i) |
| 157 | { |
| 158 | if (i) |
| 159 | out << "\n\t############# Sheet " << i+1 << " ################\n\n"; |
| 160 | out << vec[i].cstr() << std::endl; |
| 161 | } |
| 162 | } |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */ |