Blame src/conv/text/mwaw2text.cpp

rpm-build 6f7582
/* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */
rpm-build 6f7582
/* libmwaw
rpm-build 6f7582
* Version: MPL 2.0 / LGPLv2+
rpm-build 6f7582
*
rpm-build 6f7582
* The contents of this file are subject to the Mozilla Public License Version
rpm-build 6f7582
* 2.0 (the "License"); you may not use this file except in compliance with
rpm-build 6f7582
* the License or as specified alternatively below. You may obtain a copy of
rpm-build 6f7582
* the License at http://www.mozilla.org/MPL/
rpm-build 6f7582
*
rpm-build 6f7582
* Software distributed under the License is distributed on an "AS IS" basis,
rpm-build 6f7582
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
rpm-build 6f7582
* for the specific language governing rights and limitations under the
rpm-build 6f7582
* License.
rpm-build 6f7582
*
rpm-build 6f7582
* Major Contributor(s):
rpm-build 6f7582
* Copyright (C) 2002 William Lachance (wrlach@gmail.com)
rpm-build 6f7582
* Copyright (C) 2002,2004 Marc Maurer (uwog@uwog.net)
rpm-build 6f7582
* Copyright (C) 2004-2006 Fridrich Strba (fridrich.strba@bluewin.ch)
rpm-build 6f7582
* Copyright (C) 2006, 2007 Andrew Ziem
rpm-build 6f7582
* Copyright (C) 2011, 2012 Alonso Laurent (alonso@loria.fr)
rpm-build 6f7582
*
rpm-build 6f7582
*
rpm-build 6f7582
* All Rights Reserved.
rpm-build 6f7582
*
rpm-build 6f7582
* For minor contributions see the git repository.
rpm-build 6f7582
*
rpm-build 6f7582
* Alternatively, the contents of this file may be used under the terms of
rpm-build 6f7582
* the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
rpm-build 6f7582
* in which case the provisions of the LGPLv2+ are applicable
rpm-build 6f7582
* instead of those above.
rpm-build 6f7582
*/
rpm-build 6f7582
rpm-build 6f7582
#include <stdio.h>
rpm-build 6f7582
#include <string.h>
rpm-build 6f7582
#include <unistd.h>
rpm-build 6f7582
rpm-build 6f7582
#include <librevenge/librevenge.h>
rpm-build 6f7582
#include <librevenge-generators/librevenge-generators.h>
rpm-build 6f7582
#include <librevenge-stream/librevenge-stream.h>
rpm-build 6f7582
rpm-build 6f7582
#include <libmwaw/libmwaw.hxx>
rpm-build 6f7582
rpm-build 6f7582
#ifdef HAVE_CONFIG_H
rpm-build 6f7582
#include "config.h"
rpm-build 6f7582
#endif
rpm-build 6f7582
rpm-build 6f7582
#include "helper.h"
rpm-build 6f7582
rpm-build 6f7582
#ifndef VERSION
rpm-build 6f7582
#define VERSION "UNKNOWN VERSION"
rpm-build 6f7582
#endif
rpm-build 6f7582
rpm-build 6f7582
static int printUsage()
rpm-build 6f7582
{
rpm-build 6f7582
  printf("Usage: mwaw2text [OPTION] <Mac Document>\n");
rpm-build 6f7582
  printf("\n");
rpm-build 6f7582
  printf("Options:\n");
rpm-build 6f7582
  printf(" -i                Display document metadata instead of the text\n");
rpm-build 6f7582
  printf(" -h                Shows this help message\n");
rpm-build 6f7582
  printf(" -o file.txt       Define the output[default stdout]\n");
rpm-build 6f7582
  printf(" -v:               Output mwaw2text version \n");
rpm-build 6f7582
  printf("\n");
rpm-build 6f7582
  return -1;
rpm-build 6f7582
}
rpm-build 6f7582
rpm-build 6f7582
static int printVersion()
rpm-build 6f7582
{
rpm-build 6f7582
  printf("mwaw2text %s\n", VERSION);
rpm-build 6f7582
  return 0;
rpm-build 6f7582
}
rpm-build 6f7582
rpm-build 6f7582
int main(int argc, char *argv[])
rpm-build 6f7582
{
rpm-build 6f7582
  if (argc < 2)
rpm-build 6f7582
    return printUsage();
rpm-build 6f7582
rpm-build 6f7582
  char const *output = 0;
rpm-build 6f7582
  bool isInfo = false;
rpm-build 6f7582
  bool printHelp=false;
rpm-build 6f7582
  int ch;
rpm-build 6f7582
rpm-build 6f7582
  while ((ch = getopt(argc, argv, "hio:v")) != -1) {
rpm-build 6f7582
    switch (ch) {
rpm-build 6f7582
    case 'i':
rpm-build 6f7582
      isInfo=true;
rpm-build 6f7582
      break;
rpm-build 6f7582
    case 'o':
rpm-build 6f7582
      output=optarg;
rpm-build 6f7582
      break;
rpm-build 6f7582
    case 'v':
rpm-build 6f7582
      printVersion();
rpm-build 6f7582
      return 0;
rpm-build 6f7582
    default:
rpm-build 6f7582
    case 'h':
rpm-build 6f7582
      printHelp = true;
rpm-build 6f7582
      break;
rpm-build 6f7582
    }
rpm-build 6f7582
  }
rpm-build 6f7582
rpm-build 6f7582
  if (argc != 1+optind || printHelp) {
rpm-build 6f7582
    printUsage();
rpm-build 6f7582
    return -1;
rpm-build 6f7582
  }
rpm-build 6f7582
  char const *file=argv[optind];
rpm-build 6f7582
rpm-build 6f7582
  MWAWDocument::Kind kind;
rpm-build 6f7582
  auto confidence = MWAWDocument::MWAW_C_NONE;
rpm-build 6f7582
  auto input=libmwawHelper::isSupported(file, confidence, kind);
rpm-build 6f7582
  if (!input || confidence != MWAWDocument::MWAW_C_EXCELLENT) {
rpm-build 6f7582
    printf("ERROR: Unsupported file format!\n");
rpm-build 6f7582
    return 1;
rpm-build 6f7582
  }
rpm-build 6f7582
rpm-build 6f7582
  librevenge::RVNGString document;
rpm-build 6f7582
  librevenge::RVNGStringVector pages;
rpm-build 6f7582
  bool useStringVector=false;
rpm-build 6f7582
  auto error = MWAWDocument::MWAW_R_OK;
rpm-build 6f7582
  try {
rpm-build 6f7582
    if (kind == MWAWDocument::MWAW_K_DRAW || kind == MWAWDocument::MWAW_K_PAINT) {
rpm-build 6f7582
      if (isInfo) {
rpm-build 6f7582
        printf("ERROR: can not print info concerning a graphic document!\n");
rpm-build 6f7582
        return 1;
rpm-build 6f7582
      }
rpm-build 6f7582
      librevenge::RVNGTextDrawingGenerator documentGenerator(pages);
rpm-build 6f7582
      error=MWAWDocument::parse(input.get(), &documentGenerator);
rpm-build 6f7582
      if (error == MWAWDocument::MWAW_R_OK && !pages.size()) {
rpm-build 6f7582
        printf("ERROR: find no graphics!\n");
rpm-build 6f7582
        return 1;
rpm-build 6f7582
      }
rpm-build 6f7582
      useStringVector=true;
rpm-build 6f7582
    }
rpm-build 6f7582
    else if (kind == MWAWDocument::MWAW_K_SPREADSHEET || kind == MWAWDocument::MWAW_K_DATABASE) {
rpm-build 6f7582
      librevenge::RVNGTextSpreadsheetGenerator documentGenerator(pages, isInfo);
rpm-build 6f7582
      error=MWAWDocument::parse(input.get(), &documentGenerator);
rpm-build 6f7582
      if (error == MWAWDocument::MWAW_R_OK && !pages.size()) {
rpm-build 6f7582
        printf("ERROR: find no sheets!\n");
rpm-build 6f7582
        return 1;
rpm-build 6f7582
      }
rpm-build 6f7582
      useStringVector=true;
rpm-build 6f7582
    }
rpm-build 6f7582
    else if (kind == MWAWDocument::MWAW_K_PRESENTATION) {
rpm-build 6f7582
      if (isInfo) {
rpm-build 6f7582
        printf("ERROR: can not print info concerning a presentation document!\n");
rpm-build 6f7582
        return 1;
rpm-build 6f7582
      }
rpm-build 6f7582
      librevenge::RVNGTextPresentationGenerator documentGenerator(pages);
rpm-build 6f7582
      error=MWAWDocument::parse(input.get(), &documentGenerator);
rpm-build 6f7582
      if (error == MWAWDocument::MWAW_R_OK && !pages.size()) {
rpm-build 6f7582
        printf("ERROR: find no slides!\n");
rpm-build 6f7582
        return 1;
rpm-build 6f7582
      }
rpm-build 6f7582
      useStringVector=true;
rpm-build 6f7582
    }
rpm-build 6f7582
    else {
rpm-build 6f7582
      librevenge::RVNGTextTextGenerator documentGenerator(document, isInfo);
rpm-build 6f7582
      error=MWAWDocument::parse(input.get(), &documentGenerator);
rpm-build 6f7582
    }
rpm-build 6f7582
  }
rpm-build 6f7582
  catch (MWAWDocument::Result const &err) {
rpm-build 6f7582
    error=err;
rpm-build 6f7582
  }
rpm-build 6f7582
  catch (...) {
rpm-build 6f7582
    error = MWAWDocument::MWAW_R_UNKNOWN_ERROR;
rpm-build 6f7582
  }
rpm-build 6f7582
rpm-build 6f7582
  if (libmwawHelper::checkErrorAndPrintMessage(error))
rpm-build 6f7582
    return 1;
rpm-build 6f7582
rpm-build 6f7582
  if (!output) {
rpm-build 6f7582
    if (!useStringVector)
rpm-build 6f7582
      printf("%s", document.cstr());
rpm-build 6f7582
    else {
rpm-build 6f7582
      for (unsigned i=0; i < pages.size(); ++i) {
rpm-build 6f7582
        if (i)
rpm-build 6f7582
          printf("\n\t##### Page %d #####\n\n", int(i));
rpm-build 6f7582
        printf("%s\n", pages[i].cstr());
rpm-build 6f7582
      }
rpm-build 6f7582
    }
rpm-build 6f7582
  }
rpm-build 6f7582
  else {
rpm-build 6f7582
    FILE *out=fopen(output, "wb");
rpm-build 6f7582
    if (!out) {
rpm-build 6f7582
      fprintf(stderr, "ERROR: can not open file %s!\n", output);
rpm-build 6f7582
      return 1;
rpm-build 6f7582
    }
rpm-build 6f7582
    if (!useStringVector)
rpm-build 6f7582
      fprintf(out, "%s", document.cstr());
rpm-build 6f7582
    else {
rpm-build 6f7582
      for (unsigned i=0; i < pages.size(); ++i) {
rpm-build 6f7582
        if (i)
rpm-build 6f7582
          fprintf(out, "\n\t##### Page %d #####\n\n", int(i));
rpm-build 6f7582
        fprintf(out, "%s\n", pages[i].cstr());
rpm-build 6f7582
      }
rpm-build 6f7582
    }
rpm-build 6f7582
    fclose(out);
rpm-build 6f7582
  }
rpm-build 6f7582
rpm-build 6f7582
  return 0;
rpm-build 6f7582
}
rpm-build 6f7582
// vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: