Blame tools/djvm.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 djvm
Packit df99a1
Packit df99a1
    {\bf Synopsis}\\
Packit df99a1
    \begin{verbatim}
Packit df99a1
        djvm [options] <djvu_doc_name> [djvufiles] [pagenum]
Packit df99a1
    \end{verbatim}
Packit df99a1
Packit df99a1
    {\bf Description} ---
Packit df99a1
    File #"djvm.cpp"# and program #djvm# illustrate how class \Ref{DjVuDocEditor}
Packit df99a1
    can be used to create and modify DjVu multipage documents. The program
Packit df99a1
    demonstrates how to pack several DjVu single-page files together,
Packit df99a1
    how to remove pages from a multipage document or how to insert new.
Packit df99a1
    
Packit df99a1
    {\bf Arguments} ---
Packit df99a1
    Depending on the task to be performed, the number and types of arguments
Packit df99a1
    differ:
Packit df99a1
    \begin{itemize}
Packit df99a1
       \item To create a new document
Packit df99a1
Packit df99a1
             #djvm -c[reate] <doc.djvu> <page_1.djvu> ... <page_n.djvu>#
Packit df99a1
Packit df99a1
	     This will package pages represented by files #<page_1.djvu>#
Packit df99a1
	     ... #<page_n.djvu># into #BUNDLED# multipage DjVu document
Packit df99a1
	     #<doc.djvu># (see \Ref{DjVuDocument} for the explanation of
Packit df99a1
	     the #BUNDLED# format). The specified page files may include other
Packit df99a1
	     files (by means of #INCL# chunks). They will also be packed
Packit df99a1
	     into the document.
Packit df99a1
Packit df99a1
       \item To insert a page
Packit df99a1
Packit df99a1
       	     #djvm -i[nsert] <doc.djvu> <page.djvu> [<page_num>]#
Packit df99a1
Packit df99a1
	     This will insert page represented by file #<page.djvu># into
Packit df99a1
	     document #<doc.djvu># as page number #<page_num># (page
Packit df99a1
	     numbering starts from #1#). Negative or missing #<page_num>#
Packit df99a1
	     means to append the page.
Packit df99a1
Packit df99a1
	     The #<page.djvu># file can actually be another
Packit df99a1
	     multipage DjVu document. In this cases, all pages from that
Packit df99a1
	     document will be inserted into #<doc.djvu># starting from
Packit df99a1
	     page #<page_num>#.
Packit df99a1
Packit df99a1
       \item To delete a page
Packit df99a1
Packit df99a1
             #djvm -d[elete] <doc.djvu> <page_num>#
Packit df99a1
Packit df99a1
	     This will remove page number #<page_num># from document
Packit df99a1
	     #<doc.djvu>#. If there is only one page left in #<doc.djvu>#
Packit df99a1
	     after deletion, it will automatically be converted to a
Packit df99a1
	     single page DjVu file format. Page numbering starts from #1#.
Packit df99a1
Packit df99a1
       \item To view document contents
Packit df99a1
Packit df99a1
             #djvm -l[ist] <doc.djvu>#
Packit df99a1
Packit df99a1
	     This will list all files composing the given document #<doc.djvu>#.
Packit df99a1
	     The files list includes the names of page files plus names
Packit df99a1
	     of any files included into the page files by means of
Packit df99a1
	     #INCL# chunk.
Packit df99a1
    \end{itemize}
Packit df99a1
Packit df99a1
    @memo
Packit df99a1
    DjVu multipage documents creator.
Packit df99a1
    @author
Packit df99a1
    Andrei Erofeev <eaf@geocities.com>
Packit df99a1
*/
Packit df99a1
//@{
Packit df99a1
//@}
Packit df99a1
Packit df99a1
#include "GException.h"
Packit df99a1
#include "DjVuDocEditor.h"
Packit df99a1
#include "GOS.h"
Packit df99a1
#include "DjVuMessage.h"
Packit df99a1
#include "common.h"
Packit df99a1
Packit df99a1
static const char * progname;
Packit df99a1
Packit df99a1
static void
Packit df99a1
usage(void)
Packit df99a1
{
Packit df99a1
   DjVuPrintErrorUTF8(
Packit df99a1
#ifdef DJVULIBRE_VERSION
Packit df99a1
           "DJVM --- DjVuLibre-" DJVULIBRE_VERSION "\n"
Packit df99a1
#endif
Packit df99a1
           "DjVu multipage document manipulation utility\n"
Packit df99a1
           "\n"
Packit df99a1
           "Usage:\n"
Packit df99a1
           "   To compose a multipage document:\n"
Packit df99a1
           "      %s -c[reate] <doc.djvu> <page_1.djvu> ... <page_n.djvu>\n"
Packit df99a1
           "      where <doc.djvu> is the name of the BUNDLED document to be\n"
Packit df99a1
           "      created, <page_n.djvu> are the names of the page files to\n"
Packit df99a1
           "      be packed together.\n"
Packit df99a1
           "\n"
Packit df99a1
           "   To insert a new page into an existing document:\n"
Packit df99a1
           "      %s -i[nsert] <doc.djvu> <page.djvu> [<page_num>]\n"
Packit df99a1
           "      where <doc.djvu> is the name of the BUNDLED DjVu document to be\n"
Packit df99a1
           "      modified, <page.djvu> is the name of the single-page DjVu document\n"
Packit df99a1
           "      file to be inserted as page <page_num> (page numbers start from 1).\n"
Packit df99a1
           "      Negative or omitted <page_num> means to append the page.\n"
Packit df99a1
           "      <page.djvu> can be another multipage DjVu document, in which case\n"
Packit df99a1
           "      all pages of that document will be inserted into <doc.djvu>\n"
Packit df99a1
           "      starting starting at page <page_num>\n"
Packit df99a1
           "\n"
Packit df99a1
           "   To delete a page from an existing document:\n"
Packit df99a1
           "      %s -d[elete] <doc.djvu> <page_num>\n"
Packit df99a1
           "      where <doc.djvu> is the name of the docyment to be modified\n"
Packit df99a1
           "      and <page_num> is the number of the page to be deleted\n"
Packit df99a1
           "\n"
Packit df99a1
           "   To list document contents:\n"
Packit df99a1
           "      %s -l[ist] <doc.djvu>\n"
Packit df99a1
           "\n"
Packit df99a1
           "Pages being inserted may reference other files by means of INCL chunks.\n"
Packit df99a1
           "Moreover, files shared between pages will be stored into the document\n"
Packit df99a1
           "only once.\n"
Packit df99a1
           "\n", progname, progname, progname, progname );
Packit df99a1
}
Packit df99a1
Packit df99a1
static void
Packit df99a1
create(GArray<GUTF8String> &argv)
Packit df99a1
      // djvm -c[reate] <doc.djvu> <page_1.djvu> ... <page_n.djvu>
Packit df99a1
      // doc.djvu will be overwritten
Packit df99a1
{
Packit df99a1
   const int argc=argv.hbound()+1;
Packit df99a1
   if (argc<4) { usage(); exit(1); }
Packit df99a1
Packit df99a1
      // Initialize the DjVuDocEditor class
Packit df99a1
   GP<DjVuDocEditor> doc=DjVuDocEditor::create_wait();
Packit df99a1
Packit df99a1
      // Insert pages
Packit df99a1
   GList<GURL> list;
Packit df99a1
   for(int i=3;i
Packit df99a1
      list.append(GURL::Filename::UTF8(argv[i]));
Packit df99a1
   doc->insert_group(list);
Packit df99a1
Packit df99a1
   const GURL::Filename::UTF8 url(argv[2]);
Packit df99a1
      // Save in BUNDLED format
Packit df99a1
   doc->save_as(url, true);
Packit df99a1
}
Packit df99a1
Packit df99a1
static void
Packit df99a1
insert(GArray<GUTF8String> &argv)
Packit df99a1
      // djvm -i[nsert] <doc.djvu> <page.djvu> <page_num>
Packit df99a1
{
Packit df99a1
   const int argc=argv.hbound()+1;
Packit df99a1
   if (argc!=4 && argc!=5) { usage(); exit(1); }
Packit df99a1
Packit df99a1
      // Initialize DjVuDocEditor class
Packit df99a1
   const GURL::Filename::UTF8 url(argv[2]);
Packit df99a1
   GP<DjVuDocEditor> doc=DjVuDocEditor::create_wait(url);
Packit df99a1
Packit df99a1
      // Insert page
Packit df99a1
   int page_num=-1;
Packit df99a1
   if (argc==5) page_num=atoi(argv[4])-1;
Packit df99a1
   doc->insert_page(GURL::Filename::UTF8(argv[3]), page_num);
Packit df99a1
Packit df99a1
      // Save the document
Packit df99a1
   doc->save();
Packit df99a1
}
Packit df99a1
Packit df99a1
static void
Packit df99a1
del(GArray<GUTF8String> &argv)
Packit df99a1
      // djvm -d[elete] <doc.djvu> <page_num>
Packit df99a1
{
Packit df99a1
   const int argc=argv.hbound()+1;
Packit df99a1
   if (argc!=4) { usage(); exit(1); }
Packit df99a1
Packit df99a1
      // Initialize DjVuDocEditor class
Packit df99a1
   const GURL::Filename::UTF8 url(argv[2]);
Packit df99a1
   GP<DjVuDocEditor> doc=DjVuDocEditor::create_wait(url);
Packit df99a1
Packit df99a1
      // Delete the page
Packit df99a1
   int page_num=atoi(argv[3])-1;
Packit df99a1
   if (page_num<0)
Packit df99a1
   {
Packit df99a1
     DjVuPrintErrorUTF8("%s","Page number must be positive.\n");
Packit df99a1
     exit(1);
Packit df99a1
   }
Packit df99a1
   doc->remove_page(page_num);
Packit df99a1
Packit df99a1
      // Save the document
Packit df99a1
   doc->save();
Packit df99a1
}
Packit df99a1
Packit df99a1
static void
Packit df99a1
list(GArray<GUTF8String> &argv)
Packit df99a1
      // djvm -l[ist] <doc.djvu>
Packit df99a1
{
Packit df99a1
   const int argc=argv.hbound()+1;
Packit df99a1
   if (argc!=3) { usage(); exit(1); }
Packit df99a1
Packit df99a1
   const GURL::Filename::UTF8 url(argv[2]);
Packit df99a1
   GP<DjVmDoc> doc=DjVmDoc::create();
Packit df99a1
   doc->read(url);
Packit df99a1
   
Packit df99a1
   GP<DjVmDir> dir=doc->get_djvm_dir();
Packit df99a1
   if (dir)
Packit df99a1
   {
Packit df99a1
      GPList<DjVmDir::File> files_list=dir->get_files_list();
Packit df99a1
      printf("Size     Type        Name\n");
Packit df99a1
      printf("--------------------------\n");
Packit df99a1
      for(GPosition pos=files_list;pos;++pos)
Packit df99a1
      {
Packit df99a1
	 GP<DjVmDir::File> file=files_list[pos];
Packit df99a1
	 printf("%6d   ", file->size);
Packit df99a1
	 if (file->is_page())
Packit df99a1
	 {
Packit df99a1
	    char buffer[128];
Packit df99a1
	    sprintf(buffer, "PAGE #%d", file->get_page_num()+1);
Packit df99a1
	    printf("%s", buffer);
Packit df99a1
	    for(int i=strlen(buffer);i<9;i++)
Packit df99a1
	       putchar(' ');
Packit df99a1
	 } else if (file->is_include())
Packit df99a1
         {
Packit df99a1
	    printf("%s", "INCLUDE  ");
Packit df99a1
	 } else if (file->is_shared_anno())
Packit df99a1
         {
Packit df99a1
	    printf("%s", "SHARED_ANNO  ");
Packit df99a1
	 }else if (file->is_thumbnails())
Packit df99a1
         {
Packit df99a1
	    printf("%s", "THUMBNAIL");
Packit df99a1
	 } else
Packit df99a1
         {
Packit df99a1
           printf("%s", "UNKNOWN  ");
Packit df99a1
         }
Packit df99a1
	 printf("   %s\n", (const char *) file->get_save_name());
Packit df99a1
      }
Packit df99a1
   }
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
   G_TRY {
Packit df99a1
      if (argc<2) { usage(); exit(1); }
Packit df99a1
      if (!dargv[1].cmp("-c", 2)) create(dargv);
Packit df99a1
      else if (!dargv[1].cmp("-i", 2)) insert(dargv);
Packit df99a1
      else if (!dargv[1].cmp("-d", 2)) del(dargv);
Packit df99a1
      else if (!dargv[1].cmp("-l", 2)) list(dargv);
Packit df99a1
      else { usage(); exit(1); }
Packit df99a1
   } G_CATCH(exc) {
Packit df99a1
      exc.perror();
Packit df99a1
      exit(1);
Packit df99a1
   } G_ENDCATCH;
Packit df99a1
   return 0; 
Packit df99a1
}
Packit df99a1