| 1 |
/* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */ |
| 2 |
/* libmwaw |
| 3 |
* Version: MPL 2.0 / LGPLv2+ |
| 4 |
* |
| 5 |
* The contents of this file are subject to the Mozilla Public License Version |
| 6 |
* 2.0 (the "License"); you may not use this file except in compliance with |
| 7 |
* the License or as specified alternatively below. You may obtain a copy of |
| 8 |
* the License at http://www.mozilla.org/MPL/ |
| 9 |
* |
| 10 |
* Software distributed under the License is distributed on an "AS IS" basis, |
| 11 |
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
| 12 |
* for the specific language governing rights and limitations under the |
| 13 |
* License. |
| 14 |
* |
| 15 |
* Major Contributor(s): |
| 16 |
* Copyright (C) 2002 William Lachance ([email protected]) |
| 17 |
* Copyright (C) 2002,2004 Marc Maurer ([email protected]) |
| 18 |
* Copyright (C) 2004-2006 Fridrich Strba ([email protected]) |
| 19 |
* Copyright (C) 2006, 2007 Andrew Ziem |
| 20 |
* Copyright (C) 2011, 2012 Alonso Laurent ([email protected]) |
| 21 |
* |
| 22 |
* |
| 23 |
* All Rights Reserved. |
| 24 |
* |
| 25 |
* For minor contributions see the git repository. |
| 26 |
* |
| 27 |
* Alternatively, the contents of this file may be used under the terms of |
| 28 |
* the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"), |
| 29 |
* in which case the provisions of the LGPLv2+ are applicable |
| 30 |
* instead of those above. |
| 31 |
*/ |
| 32 |
|
| 33 |
#include <stdio.h> |
| 34 |
#include <string.h> |
| 35 |
#include <unistd.h> |
| 36 |
|
| 37 |
#include <librevenge/librevenge.h> |
| 38 |
#include <librevenge-generators/librevenge-generators.h> |
| 39 |
#include <librevenge-stream/librevenge-stream.h> |
| 40 |
|
| 41 |
#include <libmwaw/libmwaw.hxx> |
| 42 |
|
| 43 |
#ifdef HAVE_CONFIG_H |
| 44 |
#include "config.h" |
| 45 |
#endif |
| 46 |
|
| 47 |
#include "helper.h" |
| 48 |
|
| 49 |
#ifndef VERSION |
| 50 |
#define VERSION "UNKNOWN VERSION" |
| 51 |
#endif |
| 52 |
|
| 53 |
static int printUsage() |
| 54 |
{ |
| 55 |
printf("Usage: mwaw2text [OPTION] <Mac Document>\n"); |
| 56 |
printf("\n"); |
| 57 |
printf("Options:\n"); |
| 58 |
printf(" -i Display document metadata instead of the text\n"); |
| 59 |
printf(" -h Shows this help message\n"); |
| 60 |
printf(" -o file.txt Define the output[default stdout]\n"); |
| 61 |
printf(" -v: Output mwaw2text version \n"); |
| 62 |
printf("\n"); |
| 63 |
return -1; |
| 64 |
} |
| 65 |
|
| 66 |
static int printVersion() |
| 67 |
{ |
| 68 |
printf("mwaw2text %s\n", VERSION); |
| 69 |
return 0; |
| 70 |
} |
| 71 |
|
| 72 |
int main(int argc, char *argv[]) |
| 73 |
{ |
| 74 |
if (argc < 2) |
| 75 |
return printUsage(); |
| 76 |
|
| 77 |
char const *output = 0; |
| 78 |
bool isInfo = false; |
| 79 |
bool printHelp=false; |
| 80 |
int ch; |
| 81 |
|
| 82 |
while ((ch = getopt(argc, argv, "hio:v")) != -1) { |
| 83 |
switch (ch) { |
| 84 |
case 'i': |
| 85 |
isInfo=true; |
| 86 |
break; |
| 87 |
case 'o': |
| 88 |
output=optarg; |
| 89 |
break; |
| 90 |
case 'v': |
| 91 |
printVersion(); |
| 92 |
return 0; |
| 93 |
default: |
| 94 |
case 'h': |
| 95 |
printHelp = true; |
| 96 |
break; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
if (argc != 1+optind || printHelp) { |
| 101 |
printUsage(); |
| 102 |
return -1; |
| 103 |
} |
| 104 |
char const *file=argv[optind]; |
| 105 |
|
| 106 |
MWAWDocument::Kind kind; |
| 107 |
auto confidence = MWAWDocument::MWAW_C_NONE; |
| 108 |
auto input=libmwawHelper::isSupported(file, confidence, kind); |
| 109 |
if (!input || confidence != MWAWDocument::MWAW_C_EXCELLENT) { |
| 110 |
printf("ERROR: Unsupported file format!\n"); |
| 111 |
return 1; |
| 112 |
} |
| 113 |
|
| 114 |
librevenge::RVNGString document; |
| 115 |
librevenge::RVNGStringVector pages; |
| 116 |
bool useStringVector=false; |
| 117 |
auto error = MWAWDocument::MWAW_R_OK; |
| 118 |
try { |
| 119 |
if (kind == MWAWDocument::MWAW_K_DRAW || kind == MWAWDocument::MWAW_K_PAINT) { |
| 120 |
if (isInfo) { |
| 121 |
printf("ERROR: can not print info concerning a graphic document!\n"); |
| 122 |
return 1; |
| 123 |
} |
| 124 |
librevenge::RVNGTextDrawingGenerator documentGenerator(pages); |
| 125 |
error=MWAWDocument::parse(input.get(), &documentGenerator); |
| 126 |
if (error == MWAWDocument::MWAW_R_OK && !pages.size()) { |
| 127 |
printf("ERROR: find no graphics!\n"); |
| 128 |
return 1; |
| 129 |
} |
| 130 |
useStringVector=true; |
| 131 |
} |
| 132 |
else if (kind == MWAWDocument::MWAW_K_SPREADSHEET || kind == MWAWDocument::MWAW_K_DATABASE) { |
| 133 |
librevenge::RVNGTextSpreadsheetGenerator documentGenerator(pages, isInfo); |
| 134 |
error=MWAWDocument::parse(input.get(), &documentGenerator); |
| 135 |
if (error == MWAWDocument::MWAW_R_OK && !pages.size()) { |
| 136 |
printf("ERROR: find no sheets!\n"); |
| 137 |
return 1; |
| 138 |
} |
| 139 |
useStringVector=true; |
| 140 |
} |
| 141 |
else if (kind == MWAWDocument::MWAW_K_PRESENTATION) { |
| 142 |
if (isInfo) { |
| 143 |
printf("ERROR: can not print info concerning a presentation document!\n"); |
| 144 |
return 1; |
| 145 |
} |
| 146 |
librevenge::RVNGTextPresentationGenerator documentGenerator(pages); |
| 147 |
error=MWAWDocument::parse(input.get(), &documentGenerator); |
| 148 |
if (error == MWAWDocument::MWAW_R_OK && !pages.size()) { |
| 149 |
printf("ERROR: find no slides!\n"); |
| 150 |
return 1; |
| 151 |
} |
| 152 |
useStringVector=true; |
| 153 |
} |
| 154 |
else { |
| 155 |
librevenge::RVNGTextTextGenerator documentGenerator(document, isInfo); |
| 156 |
error=MWAWDocument::parse(input.get(), &documentGenerator); |
| 157 |
} |
| 158 |
} |
| 159 |
catch (MWAWDocument::Result const &err) { |
| 160 |
error=err; |
| 161 |
} |
| 162 |
catch (...) { |
| 163 |
error = MWAWDocument::MWAW_R_UNKNOWN_ERROR; |
| 164 |
} |
| 165 |
|
| 166 |
if (libmwawHelper::checkErrorAndPrintMessage(error)) |
| 167 |
return 1; |
| 168 |
|
| 169 |
if (!output) { |
| 170 |
if (!useStringVector) |
| 171 |
printf("%s", document.cstr()); |
| 172 |
else { |
| 173 |
for (unsigned i=0; i < pages.size(); ++i) { |
| 174 |
if (i) |
| 175 |
printf("\n\t##### Page %d #####\n\n", int(i)); |
| 176 |
printf("%s\n", pages[i].cstr()); |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
else { |
| 181 |
FILE *out=fopen(output, "wb"); |
| 182 |
if (!out) { |
| 183 |
fprintf(stderr, "ERROR: can not open file %s!\n", output); |
| 184 |
return 1; |
| 185 |
} |
| 186 |
if (!useStringVector) |
| 187 |
fprintf(out, "%s", document.cstr()); |
| 188 |
else { |
| 189 |
for (unsigned i=0; i < pages.size(); ++i) { |
| 190 |
if (i) |
| 191 |
fprintf(out, "\n\t##### Page %d #####\n\n", int(i)); |
| 192 |
fprintf(out, "%s\n", pages[i].cstr()); |
| 193 |
} |
| 194 |
} |
| 195 |
fclose(out); |
| 196 |
} |
| 197 |
|
| 198 |
return 0; |
| 199 |
} |
| 200 |
// vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: |