| 1 | /* libwps |
| 2 | * Version: MPL 2.0 / LGPLv2.1+ |
| 3 | * |
| 4 | * This Source Code Form is subject to the terms of the Mozilla Public |
| 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 7 | * |
| 8 | * Major Contributor(s): |
| 9 | * Copyright (C) 2002-2003 William Lachance ([email protected]) |
| 10 | * Copyright (C) 2002-2004 Marc Maurer ([email protected]) |
| 11 | * |
| 12 | * For minor contributions see the git repository. |
| 13 | * |
| 14 | * Alternatively, the contents of this file may be used under the terms |
| 15 | * of the GNU Lesser General Public License Version 2.1 or later |
| 16 | * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are |
| 17 | * applicable instead of those above. |
| 18 | * |
| 19 | * For further information visit http://libwps.sourceforge.net |
| 20 | */ |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <string.h> |
| 24 | #include <unistd.h> |
| 25 | |
| 26 | #include <librevenge/librevenge.h> |
| 27 | #include <librevenge-generators/librevenge-generators.h> |
| 28 | #include <librevenge-stream/librevenge-stream.h> |
| 29 | |
| 30 | #include <libwps/libwps.h> |
| 31 | |
| 32 | #include "helper.h" |
| 33 | |
| 34 | using namespace libwps; |
| 35 | |
| 36 | #ifdef HAVE_CONFIG_H |
| 37 | #include "config.h" |
| 38 | #endif |
| 39 | |
| 40 | #ifndef VERSION |
| 41 | #define VERSION "UNKNOWN VERSION" |
| 42 | #endif |
| 43 | |
| 44 | static int printUsage() |
| 45 | { |
| 46 | printf("`wps2text' converts various Microsoft text document formats to plain text.\n"); |
| 47 | printf("Supported formats are Microsoft Works, Write and Word for DOS.\n"); |
| 48 | printf("\n"); |
| 49 | printf("Usage: wps2text [OPTION] FILE\n"); |
| 50 | printf("\n"); |
| 51 | printf("Options:\n"); |
| 52 | printf("\t-e ENCODING define the file encoding, where encoding can be\n"); |
| 53 | printf("\t\t CP037, CP424, CP437, CP737, CP500, CP775, CP850, CP852, CP855, CP856, CP857,\n"); |
| 54 | printf("\t\t CP860, CP861, CP862, CP863, CP864, CP865, CP866, CP869, CP874, CP875, CP932,\n"); |
| 55 | printf("\t\t CP950, CP1006, CP1026, CP1250, CP1251, CP1252, CP1253, CP1254, CP1255, CP1256,\n"); |
| 56 | printf("\t\t CP1257, CP1258, MacArabic, MacCeltic, MacCEurope, MacCroation, MacCyrillic,\n"); |
| 57 | printf("\t\t MacDevanage, MacFarsi, MacGaelic, MacGreek, MacGujarati, MacGurmukhi, MacHebrew,\n"); |
| 58 | printf("\t\t MacIceland, MacInuit, MacRoman, MacRomanian, MacThai, MacTurkish.\n"); |
| 59 | printf("\t-h show this help message\n"); |
| 60 | printf("\t-p PASSWORD set password to open the file\n"); |
| 61 | printf("\t-v show version information\n"); |
| 62 | printf("\n"); |
| 63 | printf("Report bugs to <https://sourceforge.net/p/libwps/bugs/> or <https://bugs.documentfoundation.org/>.\n"); |
| 64 | return -1; |
| 65 | } |
| 66 | |
| 67 | static int printVersion() |
| 68 | { |
| 69 | printf("wps2text %s\n", VERSION); |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | int main(int argc, char *argv[]) |
| 74 | { |
| 75 | bool printHelp=false; |
| 76 | int ch; |
| 77 | char const *encoding=""; |
| 78 | char const *password=nullptr; |
| 79 | |
| 80 | while ((ch = getopt(argc, argv, "e:hp:v")) != -1) |
| 81 | { |
| 82 | switch (ch) |
| 83 | { |
| 84 | case 'e': |
| 85 | encoding=optarg; |
| 86 | break; |
| 87 | case 'p': |
| 88 | password=optarg; |
| 89 | break; |
| 90 | case 'v': |
| 91 | printVersion(); |
| 92 | return 0; |
| 93 | default: |
| 94 | case 'h': |
| 95 | printHelp = true; |
| 96 | break; |
| 97 | } |
| 98 | } |
| 99 | if (argc != 1+optind || printHelp) |
| 100 | { |
| 101 | printUsage(); |
| 102 | return -1; |
| 103 | } |
| 104 | |
| 105 | librevenge::RVNGFileStream input(argv[optind]); |
| 106 | |
| 107 | WPSCreator creator; |
| 108 | WPSKind kind; |
| 109 | bool needCharEncoding; |
| 110 | auto confidence = WPSDocument::isFileFormatSupported(&input,kind,creator, needCharEncoding); |
| 111 | if (confidence == WPS_CONFIDENCE_NONE || kind != WPS_TEXT) |
| 112 | { |
| 113 | printf("ERROR: Unsupported file format!\n"); |
| 114 | return 1; |
| 115 | } |
| 116 | |
| 117 | librevenge::RVNGString document; |
| 118 | librevenge::RVNGTextTextGenerator listenerImpl(document); |
| 119 | auto error = WPSDocument::parse(&input, &listenerImpl, password, encoding); |
| 120 | |
| 121 | if (libwpsHelper::checkErrorAndPrintMessage(error)) |
| 122 | return 1; |
| 123 | |
| 124 | printf("%s", document.cstr()); |
| 125 | |
| 126 | return 0; |
| 127 | } |