1 //C-  -*- C++ -*-
2 //C- -------------------------------------------------------------------
3 //C- DjVuLibre-3.5
4 //C- Copyright (c) 2002  Leon Bottou and Yann Le Cun.
5 //C- Copyright (c) 2001  AT&T
6 //C-
7 //C- This software is subject to, and may be distributed under, the
8 //C- GNU General Public License, either Version 2 of the license,
9 //C- or (at your option) any later version. The license should have
10 //C- accompanied the software or you may obtain a copy of the license
11 //C- from the Free Software Foundation at http://www.fsf.org .
12 //C-
13 //C- This program is distributed in the hope that it will be useful,
14 //C- but WITHOUT ANY WARRANTY; without even the implied warranty of
15 //C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 //C- GNU General Public License for more details.
17 //C-
18 //C- DjVuLibre-3.5 is derived from the DjVu(r) Reference Library from
19 //C- Lizardtech Software.  Lizardtech Software has authorized us to
20 //C- replace the original DjVu(r) Reference Library notice by the following
21 //C- text (see doc/lizard2002.djvu and doc/lizardtech2007.djvu):
22 //C-
23 //C-  ------------------------------------------------------------------
24 //C- | DjVu (r) Reference Library (v. 3.5)
25 //C- | Copyright (c) 1999-2001 LizardTech, Inc. All Rights Reserved.
26 //C- | The DjVu Reference Library is protected by U.S. Pat. No.
27 //C- | 6,058,214 and patents pending.
28 //C- |
29 //C- | This software is subject to, and may be distributed under, the
30 //C- | GNU General Public License, either Version 2 of the license,
31 //C- | or (at your option) any later version. The license should have
32 //C- | accompanied the software or you may obtain a copy of the license
33 //C- | from the Free Software Foundation at http://www.fsf.org .
34 //C- |
35 //C- | The computer code originally released by LizardTech under this
36 //C- | license and unmodified by other parties is deemed "the LIZARDTECH
37 //C- | ORIGINAL CODE."  Subject to any third party intellectual property
38 //C- | claims, LizardTech grants recipient a worldwide, royalty-free,
39 //C- | non-exclusive license to make, use, sell, or otherwise dispose of
40 //C- | the LIZARDTECH ORIGINAL CODE or of programs derived from the
41 //C- | LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU
42 //C- | General Public License.   This grant only confers the right to
43 //C- | infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to
44 //C- | the extent such infringement is reasonably necessary to enable
45 //C- | recipient to make, have made, practice, sell, or otherwise dispose
46 //C- | of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to
47 //C- | any greater extent that may be necessary to utilize further
48 //C- | modifications or combinations.
49 //C- |
50 //C- | The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY
51 //C- | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
52 //C- | TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF
53 //C- | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
54 //C- +------------------------------------------------------------------
55 
56 #ifndef _DJVMDIR0_H
57 #define _DJVMDIR0_H
58 #ifdef HAVE_CONFIG_H
59 #include "config.h"
60 #endif
61 #if NEED_GNUG_PRAGMAS
62 # pragma interface
63 #endif
64 
65 
66 #include "GString.h"
67 
68 #ifdef HAVE_NAMESPACES
69 namespace DJVU {
70 # ifdef NOT_DEFINED // Just to fool emacs c++ mode
71 }
72 #endif
73 #endif
74 
75 class ByteStream;
76 
77 /** @name DjVmDir0.h
78 
79     Files #"DjVmDir0.h"# and #"DjVmDir0.cpp"# contain implementation of
80     \Ref{DjVmDir0} class responsible for reading and writing the directory
81     of a multipage all-in-one-file DjVu document (usually referred to as
82     {\bf DjVm} document.
83 
84     This is {\bf not} a navigation directory, which lists all the pages
85     in a multipage document. The navigation directory is supported by class
86     \Ref{DjVuNavDir}. This is the directory of a DjVm archive.
87 
88 
89     @memo Directory of DjVu all-in-one-file DjVu documents.
90     @author Andrei Erofeev <eaf@geocities.com>
91 */
92 
93 //@{
94 
95 /** Directory of all-in-one-file DjVu documents (also known as DjVm documents).
96     This class can read and write the directory (table of contents, in other
97     words) of a DjVm document. This table of contents lists all {\bf files}
98     included into the document, not {\bf pages} like \Ref{DjVuNavDir} does.
99     It is normally stored in the document inside {\bf DIR0} chunk where
100     {\bf "0"} refers to the version number.
101 
102     An object of this class can be created either as a result of the decoding
103     of an existing DjVm file, or manually by calling the default constructor
104     and adding later directory entries by means of \Ref{add_file}() function.
105 
106     You normally will not want to create or decode this directory manually.
107     \Ref{DjVmFile} class will do it for you. */
108 class DjVmDir0 : public GPEnabled
109 {
110 public:
111       /** Describes a file record inside a DjVm document (archive) */
112    class FileRec;
113 private:
114    GMap<GUTF8String, GP<FileRec> >	name2file;
115    GPArray<FileRec>		num2file;
116 protected:
117       /// Default constructor
DjVmDir0(void)118    DjVmDir0(void) {};
119 public:
120       /// Copy constructor
121    DjVmDir0(const DjVmDir0 & d);
122 
create(void)123    static GP<DjVmDir0> create(void) {return new DjVmDir0;}
124 
~DjVmDir0(void)125    virtual ~DjVmDir0(void) {};
126 
127       /// Returns the number of files in the DjVm archive
128    int		get_files_num(void) const;
129 
130       /// Returns the file record with name #name#
131    GP<FileRec>	get_file(const GUTF8String &name);
132 
133       /// Returns the file record number #file_num#
134    GP<FileRec>	get_file(int file_num);
135 
136       /** Creates a new file record with name #name# at offset
137 	  #offset# and size #size#, which is in IFF format if
138 	  #iff_file# is #TRUE#. */
139    void		add_file(const GUTF8String &name, bool iff_file,
140 			 int offset=-1, int size=-1);
141 
142       /// Returns the size of the directory if it were encoded in #DIR0# chunk
143    int		get_size(void) const;
144 
145       /** Encodes the directory in #DIR0# chunk into the specified
146 	  \Ref{ByteStream} */
147    void		encode(ByteStream & bs);
148 
149       /** Decodes the directory from #DIR0# chunk from the specified
150 	  \Ref{ByteStream} */
151    void		decode(ByteStream & bs);
152 
153 };
154 
155       /** Describes a file record inside a DjVm document (archive) */
156 class DjVmDir0::FileRec : public GPEnabled
157 {
158 public:
159   /// Name of the file.
160   GUTF8String		name;
161   /// 1 if the file is in IFF format.
162   bool		iff_file;
163   /// Offset of the file in the archive.
164   int		offset;
165   /// Size of the file
166   int		size;
167 
168   friend int	operator==(const FileRec & f1, const FileRec & f2);
169 
170   /// Constructs the #FileRec# object
171   FileRec(const GUTF8String &name, bool iff_file,
172 	      int offset=-1, int size=-1);
173   /// Default constructor
174   FileRec(void);
175   virtual ~FileRec(void);
176 };
177 
178 inline
FileRec(const GUTF8String & name_in,bool iff_file_in,int offset_in,int size_in)179 DjVmDir0::FileRec::FileRec(
180   const GUTF8String &name_in, bool iff_file_in, int offset_in, int size_in)
181 : name(name_in), iff_file(iff_file_in), offset(offset_in), size(size_in)
182 {
183 }
184 
185 inline
FileRec(void)186 DjVmDir0::FileRec::FileRec(void) : iff_file(0), offset(-1), size(-1)
187 {
188 }
189 
190 inline
~FileRec(void)191 DjVmDir0::FileRec::~FileRec(void)
192 {
193 }
194 
195 inline int
get_files_num(void)196 DjVmDir0::get_files_num(void) const
197 {
198    return num2file.size();
199 }
200 
201 inline
DjVmDir0(const DjVmDir0 & d)202 DjVmDir0::DjVmDir0(const DjVmDir0 & d) :
203       name2file(d.name2file), num2file(d.num2file)
204 {
205 }
206 
207 //@}
208 
209 
210 #ifdef HAVE_NAMESPACES
211 }
212 # ifndef NOT_USING_DJVU_NAMESPACE
213 using namespace DJVU;
214 # endif
215 #endif
216 #endif
217