File: djvm.cpp

package info (click to toggle)
djvulibre 3.5.27.1-7
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 8,976 kB
  • ctags: 8,037
  • sloc: cpp: 65,455; xml: 5,723; ansic: 4,640; sh: 846; makefile: 315
file content (314 lines) | stat: -rw-r--r-- 11,062 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
//C-  -*- C++ -*-
//C- -------------------------------------------------------------------
//C- DjVuLibre-3.5
//C- Copyright (c) 2002  Leon Bottou and Yann Le Cun.
//C- Copyright (c) 2001  AT&T
//C-
//C- This software is subject to, and may be distributed under, the
//C- GNU General Public License, either Version 2 of the license,
//C- or (at your option) any later version. The license should have
//C- accompanied the software or you may obtain a copy of the license
//C- from the Free Software Foundation at http://www.fsf.org .
//C-
//C- This program is distributed in the hope that it will be useful,
//C- but WITHOUT ANY WARRANTY; without even the implied warranty of
//C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//C- GNU General Public License for more details.
//C- 
//C- DjVuLibre-3.5 is derived from the DjVu(r) Reference Library from
//C- Lizardtech Software.  Lizardtech Software has authorized us to
//C- replace the original DjVu(r) Reference Library notice by the following
//C- text (see doc/lizard2002.djvu and doc/lizardtech2007.djvu):
//C-
//C-  ------------------------------------------------------------------
//C- | DjVu (r) Reference Library (v. 3.5)
//C- | Copyright (c) 1999-2001 LizardTech, Inc. All Rights Reserved.
//C- | The DjVu Reference Library is protected by U.S. Pat. No.
//C- | 6,058,214 and patents pending.
//C- |
//C- | This software is subject to, and may be distributed under, the
//C- | GNU General Public License, either Version 2 of the license,
//C- | or (at your option) any later version. The license should have
//C- | accompanied the software or you may obtain a copy of the license
//C- | from the Free Software Foundation at http://www.fsf.org .
//C- |
//C- | The computer code originally released by LizardTech under this
//C- | license and unmodified by other parties is deemed "the LIZARDTECH
//C- | ORIGINAL CODE."  Subject to any third party intellectual property
//C- | claims, LizardTech grants recipient a worldwide, royalty-free, 
//C- | non-exclusive license to make, use, sell, or otherwise dispose of 
//C- | the LIZARDTECH ORIGINAL CODE or of programs derived from the 
//C- | LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU 
//C- | General Public License.   This grant only confers the right to 
//C- | infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to 
//C- | the extent such infringement is reasonably necessary to enable 
//C- | recipient to make, have made, practice, sell, or otherwise dispose 
//C- | of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to 
//C- | any greater extent that may be necessary to utilize further 
//C- | modifications or combinations.
//C- |
//C- | The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY
//C- | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
//C- | TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF
//C- | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
//C- +------------------------------------------------------------------

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#if NEED_GNUG_PRAGMAS
# pragma implementation
#endif

/** @name djvm

    {\bf Synopsis}\\
    \begin{verbatim}
        djvm [options] <djvu_doc_name> [djvufiles] [pagenum]
    \end{verbatim}

    {\bf Description} ---
    File #"djvm.cpp"# and program #djvm# illustrate how class \Ref{DjVuDocEditor}
    can be used to create and modify DjVu multipage documents. The program
    demonstrates how to pack several DjVu single-page files together,
    how to remove pages from a multipage document or how to insert new.
    
    {\bf Arguments} ---
    Depending on the task to be performed, the number and types of arguments
    differ:
    \begin{itemize}
       \item To create a new document

             #djvm -c[reate] <doc.djvu> <page_1.djvu> ... <page_n.djvu>#

	     This will package pages represented by files #<page_1.djvu>#
	     ... #<page_n.djvu># into #BUNDLED# multipage DjVu document
	     #<doc.djvu># (see \Ref{DjVuDocument} for the explanation of
	     the #BUNDLED# format). The specified page files may include other
	     files (by means of #INCL# chunks). They will also be packed
	     into the document.

       \item To insert a page

       	     #djvm -i[nsert] <doc.djvu> <page.djvu> [<page_num>]#

	     This will insert page represented by file #<page.djvu># into
	     document #<doc.djvu># as page number #<page_num># (page
	     numbering starts from #1#). Negative or missing #<page_num>#
	     means to append the page.

	     The #<page.djvu># file can actually be another
	     multipage DjVu document. In this cases, all pages from that
	     document will be inserted into #<doc.djvu># starting from
	     page #<page_num>#.

       \item To delete a page

             #djvm -d[elete] <doc.djvu> <page_num>#

	     This will remove page number #<page_num># from document
	     #<doc.djvu>#. If there is only one page left in #<doc.djvu>#
	     after deletion, it will automatically be converted to a
	     single page DjVu file format. Page numbering starts from #1#.

       \item To view document contents

             #djvm -l[ist] <doc.djvu>#

	     This will list all files composing the given document #<doc.djvu>#.
	     The files list includes the names of page files plus names
	     of any files included into the page files by means of
	     #INCL# chunk.
    \end{itemize}

    @memo
    DjVu multipage documents creator.
    @author
    Andrei Erofeev <eaf@geocities.com>
*/
//@{
//@}

#include "GException.h"
#include "DjVuDocEditor.h"
#include "GOS.h"
#include "DjVuMessage.h"
#include "common.h"

static const char * progname;

static void
usage(void)
{
   DjVuPrintErrorUTF8(
#ifdef DJVULIBRE_VERSION
           "DJVM --- DjVuLibre-" DJVULIBRE_VERSION "\n"
#endif
           "DjVu multipage document manipulation utility\n"
           "\n"
           "Usage:\n"
           "   To compose a multipage document:\n"
           "      %s -c[reate] <doc.djvu> <page_1.djvu> ... <page_n.djvu>\n"
           "      where <doc.djvu> is the name of the BUNDLED document to be\n"
           "      created, <page_n.djvu> are the names of the page files to\n"
           "      be packed together.\n"
           "\n"
           "   To insert a new page into an existing document:\n"
           "      %s -i[nsert] <doc.djvu> <page.djvu> [<page_num>]\n"
           "      where <doc.djvu> is the name of the BUNDLED DjVu document to be\n"
           "      modified, <page.djvu> is the name of the single-page DjVu document\n"
           "      file to be inserted as page <page_num> (page numbers start from 1).\n"
           "      Negative or omitted <page_num> means to append the page.\n"
           "      <page.djvu> can be another multipage DjVu document, in which case\n"
           "      all pages of that document will be inserted into <doc.djvu>\n"
           "      starting starting at page <page_num>\n"
           "\n"
           "   To delete a page from an existing document:\n"
           "      %s -d[elete] <doc.djvu> <page_num>\n"
           "      where <doc.djvu> is the name of the docyment to be modified\n"
           "      and <page_num> is the number of the page to be deleted\n"
           "\n"
           "   To list document contents:\n"
           "      %s -l[ist] <doc.djvu>\n"
           "\n"
           "Pages being inserted may reference other files by means of INCL chunks.\n"
           "Moreover, files shared between pages will be stored into the document\n"
           "only once.\n"
           "\n", progname, progname, progname, progname );
}

static void
create(GArray<GUTF8String> &argv)
      // djvm -c[reate] <doc.djvu> <page_1.djvu> ... <page_n.djvu>
      // doc.djvu will be overwritten
{
   const int argc=argv.hbound()+1;
   if (argc<4) { usage(); exit(1); }

      // Initialize the DjVuDocEditor class
   GP<DjVuDocEditor> doc=DjVuDocEditor::create_wait();

      // Insert pages
   GList<GURL> list;
   for(int i=3;i<argc;i++)
      list.append(GURL::Filename::UTF8(argv[i]));
   doc->insert_group(list);

   const GURL::Filename::UTF8 url(argv[2]);
      // Save in BUNDLED format
   doc->save_as(url, true);
}

static void
insert(GArray<GUTF8String> &argv)
      // djvm -i[nsert] <doc.djvu> <page.djvu> <page_num>
{
   const int argc=argv.hbound()+1;
   if (argc!=4 && argc!=5) { usage(); exit(1); }

      // Initialize DjVuDocEditor class
   const GURL::Filename::UTF8 url(argv[2]);
   GP<DjVuDocEditor> doc=DjVuDocEditor::create_wait(url);

      // Insert page
   int page_num=-1;
   if (argc==5) page_num=atoi(argv[4])-1;
   doc->insert_page(GURL::Filename::UTF8(argv[3]), page_num);

      // Save the document
   doc->save();
}

static void
del(GArray<GUTF8String> &argv)
      // djvm -d[elete] <doc.djvu> <page_num>
{
   const int argc=argv.hbound()+1;
   if (argc!=4) { usage(); exit(1); }

      // Initialize DjVuDocEditor class
   const GURL::Filename::UTF8 url(argv[2]);
   GP<DjVuDocEditor> doc=DjVuDocEditor::create_wait(url);

      // Delete the page
   int page_num=atoi(argv[3])-1;
   if (page_num<0)
   {
     DjVuPrintErrorUTF8("%s","Page number must be positive.\n");
     exit(1);
   }
   doc->remove_page(page_num);

      // Save the document
   doc->save();
}

static void
list(GArray<GUTF8String> &argv)
      // djvm -l[ist] <doc.djvu>
{
   const int argc=argv.hbound()+1;
   if (argc!=3) { usage(); exit(1); }

   const GURL::Filename::UTF8 url(argv[2]);
   GP<DjVmDoc> doc=DjVmDoc::create();
   doc->read(url);
   
   GP<DjVmDir> dir=doc->get_djvm_dir();
   if (dir)
   {
      GPList<DjVmDir::File> files_list=dir->get_files_list();
      printf("Size     Type        Name\n");
      printf("--------------------------\n");
      for(GPosition pos=files_list;pos;++pos)
      {
	 GP<DjVmDir::File> file=files_list[pos];
	 printf("%6d   ", file->size);
	 if (file->is_page())
	 {
	    char buffer[128];
	    sprintf(buffer, "PAGE #%d", file->get_page_num()+1);
	    printf("%s", buffer);
	    for(int i=strlen(buffer);i<9;i++)
	       putchar(' ');
	 } else if (file->is_include())
         {
	    printf("%s", "INCLUDE  ");
	 } else if (file->is_shared_anno())
         {
	    printf("%s", "SHARED_ANNO  ");
	 }else if (file->is_thumbnails())
         {
	    printf("%s", "THUMBNAIL");
	 } else
         {
           printf("%s", "UNKNOWN  ");
         }
	 printf("   %s\n", (const char *) file->get_save_name());
      }
   }
}

int
main(int argc, char ** argv)
{
  DJVU_LOCALE;
  GArray<GUTF8String> dargv(0,argc-1);
  for(int i=0;i<argc;++i)
    dargv[i]=GNativeString(argv[i]);
  progname=dargv[0]=GOS::basename(dargv[0]);
   
   G_TRY {
      if (argc<2) { usage(); exit(1); }
      if (!dargv[1].cmp("-c", 2)) create(dargv);
      else if (!dargv[1].cmp("-i", 2)) insert(dargv);
      else if (!dargv[1].cmp("-d", 2)) del(dargv);
      else if (!dargv[1].cmp("-l", 2)) list(dargv);
      else { usage(); exit(1); }
   } G_CATCH(exc) {
      exc.perror();
      exit(1);
   } G_ENDCATCH;
   return 0; 
}