| 1 |
/* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */ |
| 2 |
|
| 3 |
/* libmwaw |
| 4 |
* Version: MPL 2.0 / LGPLv2+ |
| 5 |
* |
| 6 |
* The contents of this file are subject to the Mozilla Public License Version |
| 7 |
* 2.0 (the "License"); you may not use this file except in compliance with |
| 8 |
* the License or as specified alternatively below. You may obtain a copy of |
| 9 |
* the License at http://www.mozilla.org/MPL/ |
| 10 |
* |
| 11 |
* Software distributed under the License is distributed on an "AS IS" basis, |
| 12 |
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
| 13 |
* for the specific language governing rights and limitations under the |
| 14 |
* License. |
| 15 |
* |
| 16 |
* Major Contributor(s): |
| 17 |
* Copyright (C) 2002 William Lachance ([email protected]) |
| 18 |
* Copyright (C) 2002,2004 Marc Maurer ([email protected]) |
| 19 |
* Copyright (C) 2004-2006 Fridrich Strba ([email protected]) |
| 20 |
* Copyright (C) 2006, 2007 Andrew Ziem |
| 21 |
* Copyright (C) 2011, 2012 Alonso Laurent ([email protected]) |
| 22 |
* |
| 23 |
* |
| 24 |
* All Rights Reserved. |
| 25 |
* |
| 26 |
* For minor contributions see the git repository. |
| 27 |
* |
| 28 |
* Alternatively, the contents of this file may be used under the terms of |
| 29 |
* the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"), |
| 30 |
* in which case the provisions of the LGPLv2+ are applicable |
| 31 |
* instead of those above. |
| 32 |
*/ |
| 33 |
|
| 34 |
#include <stdio.h> |
| 35 |
#include <string.h> |
| 36 |
#include <unistd.h> |
| 37 |
|
| 38 |
#include <cstdlib> |
| 39 |
#include <cstring> |
| 40 |
#include <fstream> |
| 41 |
#include <iostream> |
| 42 |
|
| 43 |
#include <librevenge/librevenge.h> |
| 44 |
#include <librevenge-generators/librevenge-generators.h> |
| 45 |
#include <librevenge-stream/librevenge-stream.h> |
| 46 |
|
| 47 |
#include <libmwaw/libmwaw.hxx> |
| 48 |
|
| 49 |
#ifdef HAVE_CONFIG_H |
| 50 |
#include "config.h" |
| 51 |
#endif |
| 52 |
|
| 53 |
#include "helper.h" |
| 54 |
|
| 55 |
#ifndef VERSION |
| 56 |
#define VERSION "UNKNOWN VERSION" |
| 57 |
#endif |
| 58 |
|
| 59 |
static int printUsage() |
| 60 |
{ |
| 61 |
printf("Usage: mwaw2csv [OPTION] <Mac Spreadsheet Document>\n"); |
| 62 |
printf("\n"); |
| 63 |
printf("Options:\n"); |
| 64 |
printf("\t-h: Shows this help message\n"); |
| 65 |
printf("\t-dc: Sets the decimal commas to character c: default .\n"); |
| 66 |
printf("\t-fc: Sets the field separator to character c: default ,\n"); |
| 67 |
printf("\t-tc: Sets the text separator to character c: default \"\n"); |
| 68 |
printf("\t-F: Sets to output the formula which exists in the file\n"); |
| 69 |
printf("\t-Dformat: Sets the date format: default \"%%m/%%d/%%y\"\n"); |
| 70 |
printf("\t-Tformat: Sets the time format: default \"%%H:%%M:%%S\"\n"); |
| 71 |
printf("\t-N: Output the number of sheets \n"); |
| 72 |
printf("\t-n num: Sets the choose the sheet to convert (1: means first sheet) \n"); |
| 73 |
printf("\t-o file.csv: Defines the ouput file\n"); |
| 74 |
printf("\t-v: Output mwaw2csv version\n"); |
| 75 |
printf("\n"); |
| 76 |
printf("Example:\n"); |
| 77 |
printf("\tmwaw2cvs -d, -D\"%%d/%%m/%%y\" file : Converts a file using french locale\n"); |
| 78 |
printf("\n"); |
| 79 |
printf("Note:\n"); |
| 80 |
printf("\t If -F is present, the formula are generated which english names\n"); |
| 81 |
return -1; |
| 82 |
} |
| 83 |
|
| 84 |
static int printVersion() |
| 85 |
{ |
| 86 |
printf("mwaw2csv %s\n", VERSION); |
| 87 |
return 0; |
| 88 |
} |
| 89 |
|
| 90 |
int main(int argc, char *argv[]) |
| 91 |
{ |
| 92 |
bool printHelp=false; |
| 93 |
bool printNumberOfSheet=false; |
| 94 |
bool generateFormula=false; |
| 95 |
char const *output = 0; |
| 96 |
int sheetToConvert=0; |
| 97 |
int ch; |
| 98 |
char decSeparator='.', fieldSeparator=',', textSeparator='"'; |
| 99 |
std::string dateFormat("%m/%d/%y"), timeFormat("%H:%M:%S"); |
| 100 |
|
| 101 |
while ((ch = getopt(argc, argv, "hvo:d:f:t:D:FNn:T:")) != -1) { |
| 102 |
switch (ch) { |
| 103 |
case 'D': |
| 104 |
dateFormat=optarg; |
| 105 |
break; |
| 106 |
case 'F': |
| 107 |
generateFormula=true; |
| 108 |
break; |
| 109 |
case 'N': |
| 110 |
printNumberOfSheet=true; |
| 111 |
break; |
| 112 |
case 'T': |
| 113 |
timeFormat=optarg; |
| 114 |
break; |
| 115 |
case 'd': |
| 116 |
decSeparator=optarg[0]; |
| 117 |
break; |
| 118 |
case 'f': |
| 119 |
fieldSeparator=optarg[0]; |
| 120 |
break; |
| 121 |
case 'n': |
| 122 |
sheetToConvert=std::atoi(optarg); |
| 123 |
break; |
| 124 |
case 't': |
| 125 |
textSeparator=optarg[0]; |
| 126 |
break; |
| 127 |
case 'o': |
| 128 |
output=optarg; |
| 129 |
break; |
| 130 |
case 'v': |
| 131 |
printVersion(); |
| 132 |
return 0; |
| 133 |
default: |
| 134 |
case 'h': |
| 135 |
printHelp = true; |
| 136 |
break; |
| 137 |
} |
| 138 |
} |
| 139 |
if (argc != 1+optind || printHelp) { |
| 140 |
printUsage(); |
| 141 |
return -1; |
| 142 |
} |
| 143 |
char const *file=argv[optind]; |
| 144 |
|
| 145 |
MWAWDocument::Kind kind; |
| 146 |
auto confidence = MWAWDocument::MWAW_C_NONE; |
| 147 |
auto input=libmwawHelper::isSupported(file, confidence, kind); |
| 148 |
if (!input || confidence != MWAWDocument::MWAW_C_EXCELLENT) { |
| 149 |
fprintf(stderr,"ERROR: Unsupported file format!\n"); |
| 150 |
return 1; |
| 151 |
} |
| 152 |
if (kind != MWAWDocument::MWAW_K_SPREADSHEET && kind != MWAWDocument::MWAW_K_DATABASE) { |
| 153 |
fprintf(stderr,"ERROR: not a spreadsheet!\n"); |
| 154 |
return 1; |
| 155 |
} |
| 156 |
auto error=MWAWDocument::MWAW_R_OK; |
| 157 |
librevenge::RVNGStringVector vec; |
| 158 |
|
| 159 |
try { |
| 160 |
librevenge::RVNGCSVSpreadsheetGenerator listenerImpl(vec, generateFormula); |
| 161 |
listenerImpl.setSeparators(fieldSeparator, textSeparator, decSeparator); |
| 162 |
listenerImpl.setDTFormats(dateFormat.c_str(),timeFormat.c_str()); |
| 163 |
error= MWAWDocument::parse(input.get(), &listenerImpl); |
| 164 |
} |
| 165 |
catch (MWAWDocument::Result const &err) { |
| 166 |
error=err; |
| 167 |
} |
| 168 |
catch (...) { |
| 169 |
error=MWAWDocument::MWAW_R_UNKNOWN_ERROR; |
| 170 |
} |
| 171 |
|
| 172 |
if (libmwawHelper::checkErrorAndPrintMessage(error)) |
| 173 |
return 1; |
| 174 |
if (vec.empty()) { |
| 175 |
fprintf(stderr, "ERROR: can not find any sheet!\n"); |
| 176 |
error = MWAWDocument::MWAW_R_PARSE_ERROR; |
| 177 |
} |
| 178 |
else if (sheetToConvert>0 && sheetToConvert>int(vec.size())) { |
| 179 |
fprintf(stderr, "ERROR: Can not find sheet %d\n", sheetToConvert); |
| 180 |
error = MWAWDocument::MWAW_R_PARSE_ERROR; |
| 181 |
} |
| 182 |
|
| 183 |
if (error != MWAWDocument::MWAW_R_OK) |
| 184 |
return 1; |
| 185 |
|
| 186 |
if (printNumberOfSheet) { |
| 187 |
std::cout << vec.size() << "\n"; |
| 188 |
return 0; |
| 189 |
} |
| 190 |
|
| 191 |
if (!output) |
| 192 |
std::cout << vec[sheetToConvert>0 ? unsigned(sheetToConvert-1) : 0].cstr() << std::endl; |
| 193 |
else { |
| 194 |
std::ofstream out(output); |
| 195 |
out << vec[sheetToConvert>0 ? unsigned(sheetToConvert-1) : 0].cstr() << std::endl; |
| 196 |
} |
| 197 |
return 0; |
| 198 |
} |
| 199 |
// vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: |