Blame tools/djvmcvt.cpp

Packit df99a1
//C-  -*- C++ -*-
Packit df99a1
//C- -------------------------------------------------------------------
Packit df99a1
//C- DjVuLibre-3.5
Packit df99a1
//C- Copyright (c) 2002  Leon Bottou and Yann Le Cun.
Packit df99a1
//C- Copyright (c) 2001  AT&T
Packit df99a1
//C-
Packit df99a1
//C- This software is subject to, and may be distributed under, the
Packit df99a1
//C- GNU General Public License, either Version 2 of the license,
Packit df99a1
//C- or (at your option) any later version. The license should have
Packit df99a1
//C- accompanied the software or you may obtain a copy of the license
Packit df99a1
//C- from the Free Software Foundation at http://www.fsf.org .
Packit df99a1
//C-
Packit df99a1
//C- This program is distributed in the hope that it will be useful,
Packit df99a1
//C- but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit df99a1
//C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit df99a1
//C- GNU General Public License for more details.
Packit df99a1
//C- 
Packit df99a1
//C- DjVuLibre-3.5 is derived from the DjVu(r) Reference Library from
Packit df99a1
//C- Lizardtech Software.  Lizardtech Software has authorized us to
Packit df99a1
//C- replace the original DjVu(r) Reference Library notice by the following
Packit df99a1
//C- text (see doc/lizard2002.djvu and doc/lizardtech2007.djvu):
Packit df99a1
//C-
Packit df99a1
//C-  ------------------------------------------------------------------
Packit df99a1
//C- | DjVu (r) Reference Library (v. 3.5)
Packit df99a1
//C- | Copyright (c) 1999-2001 LizardTech, Inc. All Rights Reserved.
Packit df99a1
//C- | The DjVu Reference Library is protected by U.S. Pat. No.
Packit df99a1
//C- | 6,058,214 and patents pending.
Packit df99a1
//C- |
Packit df99a1
//C- | This software is subject to, and may be distributed under, the
Packit df99a1
//C- | GNU General Public License, either Version 2 of the license,
Packit df99a1
//C- | or (at your option) any later version. The license should have
Packit df99a1
//C- | accompanied the software or you may obtain a copy of the license
Packit df99a1
//C- | from the Free Software Foundation at http://www.fsf.org .
Packit df99a1
//C- |
Packit df99a1
//C- | The computer code originally released by LizardTech under this
Packit df99a1
//C- | license and unmodified by other parties is deemed "the LIZARDTECH
Packit df99a1
//C- | ORIGINAL CODE."  Subject to any third party intellectual property
Packit df99a1
//C- | claims, LizardTech grants recipient a worldwide, royalty-free, 
Packit df99a1
//C- | non-exclusive license to make, use, sell, or otherwise dispose of 
Packit df99a1
//C- | the LIZARDTECH ORIGINAL CODE or of programs derived from the 
Packit df99a1
//C- | LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU 
Packit df99a1
//C- | General Public License.   This grant only confers the right to 
Packit df99a1
//C- | infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to 
Packit df99a1
//C- | the extent such infringement is reasonably necessary to enable 
Packit df99a1
//C- | recipient to make, have made, practice, sell, or otherwise dispose 
Packit df99a1
//C- | of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to 
Packit df99a1
//C- | any greater extent that may be necessary to utilize further 
Packit df99a1
//C- | modifications or combinations.
Packit df99a1
//C- |
Packit df99a1
//C- | The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY
Packit df99a1
//C- | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
Packit df99a1
//C- | TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF
Packit df99a1
//C- | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
Packit df99a1
//C- +------------------------------------------------------------------
Packit df99a1
Packit df99a1
#ifdef HAVE_CONFIG_H
Packit df99a1
# include "config.h"
Packit df99a1
#endif
Packit df99a1
#if NEED_GNUG_PRAGMAS
Packit df99a1
# pragma implementation
Packit df99a1
#endif
Packit df99a1
Packit df99a1
/** @name djvmcvt
Packit df99a1
Packit df99a1
    {\bf Synopsis}\\
Packit df99a1
    \begin{verbatim}
Packit df99a1
        djvmcvt -b[undled] <doc_in.djvu> <doc_out.djvu>
Packit df99a1
Packit df99a1
	or
Packit df99a1
	
Packit df99a1
	djvmcvt -i[ndirect] <doc_in.djvu> <dir_out> <idx_fname.djvu>
Packit df99a1
    \end{verbatim}
Packit df99a1
Packit df99a1
    {\bf Description} ---
Packit df99a1
    File #"djvmcvt.cpp"# and the program #djvmcvt# serve the purpose of
Packit df99a1
    convertion of obsolete DjVu documents into the new formats. The program
Packit df99a1
    can also read documents in the new formats, so you can use it to
Packit df99a1
    perform conversion between #BUNDLED# and #INDIRECT# formats. This is a
Packit df99a1
    simple illustration of the capabilities of \Ref{DjVuDocument} class.
Packit df99a1
Packit df99a1
    As a matter of fact, there are two ways to make conversion between
Packit df99a1
    different formats:
Packit df99a1
    \begin{enumerate}
Packit df99a1
       \item If the input format is one of obsolete formats (#OLD_BUNDLED#
Packit df99a1
             or #OLD_INDEXED#) then the conversion can be done by
Packit df99a1
	     \Ref{DjVuDocument} only.
Packit df99a1
       \item If the input format is one of new formats (#BUNDLED# or
Packit df99a1
             #INDIRECT#) then the best candidate to perform conversion
Packit df99a1
	     is \Ref{DjVmDoc} class. It will do it at the lowest possible
Packit df99a1
	     with the least expenses.
Packit df99a1
    \end{enumerate}
Packit df99a1
    
Packit df99a1
    {\bf Arguments} ---
Packit df99a1
    Depending on the output format, the number and types of arguments
Packit df99a1
    differ. The second argument though (#<doc_in.djvu>#) is the same in both
Packit df99a1
    cases, and depending on the format of input document, it means:
Packit df99a1
    \begin{itemize}
Packit df99a1
       \item {\bf OLD_BUNDLED} format: just name of the document
Packit df99a1
       \item {\bf OLD_INDEXED} format: name of any page of the document
Packit df99a1
       \item {\bf BUNDLED} format: name of the document
Packit df99a1
       \item {\bf INDIRECT} format: name of the top-level file with the
Packit df99a1
             list of all pages of the document.
Packit df99a1
    \end{itemize}.
Packit df99a1
Packit df99a1
    So, in order to do conversion choose one of syntaxes below:
Packit df99a1
    \begin{itemize}
Packit df99a1
       \item To create a new {\em BUNDLED} document
Packit df99a1
Packit df99a1
             #djvmcvt -b[undled] <doc_in.djvu> <doc_out.djvu>#
Packit df99a1
Packit df99a1
	     This will read the document referenced by #<doc_in.djvu># as
Packit df99a1
	     descrived above, will convert it into the #BUNDLED#
Packit df99a1
	     format and will save the results into the #<doc_out.djvu># file.
Packit df99a1
	     
Packit df99a1
       \item To create a new {\em INDIRECT} document
Packit df99a1
Packit df99a1
             #djvmcvt -i[ndirect] <doc_in.djvu> <dir_out> <idx_fname.djvu>#
Packit df99a1
Packit df99a1
	     This will read the input document referenced by #<doc_in.djvu>#
Packit df99a1
	     as described above, will convert it into the #INDIRECT#
Packit df99a1
	     format and will save it into the #<dir_out># directory. Since
Packit df99a1
	     DjVu multipage documents in the #INDIRECT# formats are
Packit df99a1
	     represented by a bunch of files, you have to specify a directory
Packit df99a1
	     name where all of the files will be saved. In addition to these
Packit df99a1
	     files the program will also create a top-level file named
Packit df99a1
	     #<idx_fname.djvu># with the list of all pages and components
Packit df99a1
	     composing the given DjVu document. Whenever you need to open
Packit df99a1
	     this document later, open this top-level file.
Packit df99a1
    \end{itemize}
Packit df99a1
	     
Packit df99a1
    @memo
Packit df99a1
    DjVu multipage document converter.
Packit df99a1
    @author
Packit df99a1
    Andrei Erofeev <eaf@geocities.com>
Packit df99a1
*/
Packit df99a1
Packit df99a1
Packit df99a1
#include "DjVuDocument.h"
Packit df99a1
#include "DjVmDoc.h"
Packit df99a1
#include "ByteStream.h"
Packit df99a1
#include "GOS.h"
Packit df99a1
#include "DjVuMessage.h"
Packit df99a1
#include "debug.h"
Packit df99a1
#include "common.h"
Packit df99a1
Packit df99a1
static const char * progname;
Packit df99a1
Packit df99a1
static void Usage(void)
Packit df99a1
{
Packit df99a1
   DjVuPrintErrorUTF8(
Packit df99a1
#ifdef DJVULIBRE_VERSION
Packit df99a1
     "DJVMCVT --- DjVuLibre-" DJVULIBRE_VERSION "\n"
Packit df99a1
#endif
Packit df99a1
     "DjVu multipage document conversion utility\n"
Packit df99a1
     "\n"
Packit df99a1
     "Usage:\n"
Packit df99a1
     "\n"
Packit df99a1
     "  To convert any DjVu multipage document into the new BUNDLED format:\n"
Packit df99a1
     "	  %s -b[undled] <doc_in.djvu> <doc_out.djvu>\n"
Packit df99a1
     "	  where <doc_out.djvu> is the name of the output file.\n"
Packit df99a1
     "\n"
Packit df99a1
     "  To convert any DjVu multipage document into the new INDIRECT format:\n"
Packit df99a1
     "	  %s -i[ndirect] <doc_in.djvu> <dir_out> <idx_fname.djvu>\n"
Packit df99a1
     "	  where <dir_out> is the name of the output directory, and\n"
Packit df99a1
     "	  <idx_fname.djvu> is the name of the top-level document index file.\n"
Packit df99a1
     "\n"
Packit df99a1
     "The <doc_in.djvu> specifies the document to be converted.\n"
Packit df99a1
     "For OLD_BUNDLED and BUNDLED formats, this is the name of the document file.\n"
Packit df99a1
     "For INDIRECT format, this is the name of the top-level index file.\n"
Packit df99a1
     "For OLD_INDEXED format, this is the name of any page file.\n"
Packit df99a1
     "\n", progname, progname);
Packit df99a1
}
Packit df99a1
Packit df99a1
static void
Packit df99a1
do_bundled(GArray<GUTF8String> &argv)
Packit df99a1
      // <progname> -b[undled] <file_in> <file_out>
Packit df99a1
{
Packit df99a1
   const int argc=argv.hbound()+1;
Packit df99a1
   if (argc!=4) { Usage(); exit(1); }
Packit df99a1
   const GURL::Filename::UTF8 url2(argv[2]);
Packit df99a1
   const GURL::Filename::UTF8 url3(argv[3]);
Packit df99a1
   GP<DjVuDocument> doc = DjVuDocument::create_wait(url2);
Packit df99a1
   GP<ByteStream> str=ByteStream::create(url3, "wb");
Packit df99a1
   doc->write(str);
Packit df99a1
}
Packit df99a1
Packit df99a1
static void
Packit df99a1
do_indirect(GArray<GUTF8String> &argv)
Packit df99a1
      // <progname> -i[ndirect] <file_in> <dir_out> <idx_fname>
Packit df99a1
{
Packit df99a1
   const int argc=argv.hbound()+1;
Packit df99a1
   if (argc!=5) { Usage(); exit(1); }
Packit df99a1
   const GURL::Filename::UTF8 url2(argv[2]);
Packit df99a1
   GP<DjVuDocument> doc = DjVuDocument::create_wait(url2);
Packit df99a1
   const GURL::Filename::UTF8 url3(argv[3]);
Packit df99a1
   doc->expand(url3, argv[4]);
Packit df99a1
}
Packit df99a1
Packit df99a1
int 
Packit df99a1
main(int argc, char ** argv)
Packit df99a1
{
Packit df99a1
  DJVU_LOCALE;
Packit df99a1
  GArray<GUTF8String> dargv(0,argc-1);
Packit df99a1
  for(int i=0;i
Packit df99a1
    dargv[i]=GNativeString(argv[i]);
Packit df99a1
  progname=dargv[0]=GOS::basename(dargv[0]);
Packit df99a1
Packit df99a1
  if (argc<2) { Usage(); exit(1); }
Packit df99a1
Packit df99a1
   bool bundled=true;
Packit df99a1
   G_TRY {
Packit df99a1
      if (!dargv[1].cmp("-b", 2)) bundled=true;
Packit df99a1
      else if (!dargv[1].cmp("-i", 2)) bundled=false;
Packit df99a1
      else { Usage(); exit(1); }
Packit df99a1
Packit df99a1
      if (bundled) do_bundled(dargv);
Packit df99a1
      else do_indirect(dargv);
Packit df99a1
   } G_CATCH(exc) {
Packit df99a1
      DjVuPrintErrorUTF8("%s\n", exc.get_cause());
Packit df99a1
      exit(1);
Packit df99a1
   } G_ENDCATCH;
Packit df99a1
Packit df99a1
   exit(0);
Packit df99a1
#ifdef _WIN32
Packit df99a1
   return 0;
Packit df99a1
#endif
Packit df99a1
}