source: trunk/libdjvu/DjVmNav.cpp @ 280

Last change on this file since 280 was 280, checked in by rbri, 11 years ago

DJVU plugin: djvulibre updated to version 3.5.22

File size: 9.4 KB
Line 
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// $Id: DjVmNav.cpp,v 1.2 2007/03/25 20:48:29 leonb Exp $
57// $Name: release_3_5_22 $
58
59#ifdef HAVE_CONFIG_H
60# include "config.h"
61#endif
62#if NEED_GNUG_PRAGMAS
63# pragma implementation
64#endif
65
66#include <ctype.h>
67
68#include "DjVuDocument.h"
69#include "DjVmNav.h"
70#include "BSByteStream.h"
71#include "GURL.h"
72#include "debug.h"
73
74#ifdef HAVE_NAMESPACES
75namespace DJVU {
76# ifdef NOT_DEFINED // Just to fool emacs c++ mode
77}
78#endif
79#endif
80
81
82GP<DjVmNav::DjVuBookMark>
83DjVmNav::DjVuBookMark::create(void)
84{
85  return new DjVuBookMark();
86}
87
88GP<DjVmNav::DjVuBookMark>
89DjVmNav::DjVuBookMark::create(const unsigned short count,
90                              const GUTF8String &displayname, 
91                              const GUTF8String &url)
92{
93  DjVuBookMark *pvm=new DjVuBookMark();
94  GP<DjVuBookMark> bookmark=pvm;
95  pvm->count=count;
96  pvm->displayname=displayname;
97  pvm->url=url;
98  return bookmark;
99}   
100
101DjVmNav::DjVuBookMark::DjVuBookMark(void)
102  : count(0), displayname(), url()
103{ 
104}
105
106GP<DjVmNav>
107DjVmNav::create(void)
108{
109  return new DjVmNav;
110}
111
112// Decode the input bytestream and populate this object
113void 
114DjVmNav::DjVuBookMark::decode(const GP<ByteStream> &gstr)
115{
116  int textsize=0, readsize=0;
117  char *buffer=0;
118  ByteStream &bs=*gstr;
119  count = bs.read8();
120  displayname.empty();
121#ifdef DJVMNAV_WITH_256LIMIT
122  textsize = bs.read24();
123#else
124  int counthi = bs.read8();
125  count = (counthi<<8)+ count;
126  textsize = bs.read16();
127#endif
128  if (textsize)
129    {
130      buffer = displayname.getbuf(textsize);
131      readsize = bs.read(buffer,textsize);
132      buffer[readsize] = 0;
133    }
134  url.empty();
135  textsize = bs.read24();
136  if (textsize)
137    {
138      buffer = url.getbuf(textsize);
139      readsize = bs.read(buffer,textsize);
140      buffer[readsize] = 0;
141    }
142}
143
144// Serialize this object to the output bytestream
145void 
146DjVmNav::DjVuBookMark::encode(const GP<ByteStream> &gstr) 
147{
148  int textsize=0;
149  ByteStream &bs=*gstr;
150#ifdef DJVMNAV_WITH_256LIMIT
151  if (count>255)
152    G_THROW("Excessive number of children in bookmark tree");
153  bs.write8(count);
154  textsize = displayname.length();
155  bs.write24( textsize );
156#else
157  if (count>65535)
158    G_THROW("Excessive number of children in bookmark tree");
159  bs.write8( count & 0xff );
160  bs.write8( (count>>8) & 0xff );
161  textsize = displayname.length();
162  bs.write16( textsize );
163#endif
164  bs.writestring(displayname);
165  textsize = url.length();
166  bs.write24( textsize );
167  bs.writestring(url);
168}
169
170// Text dump of this object to the output bytestream
171void 
172DjVmNav::DjVuBookMark::dump(const GP<ByteStream> &gstr) 
173{
174  int textsize=0;
175  ByteStream &bs=*gstr;
176  bs.format("\n  count=%d\n",count);
177  textsize = displayname.length();
178  bs.format("  (%d) %s\n",textsize, displayname.getbuf());
179  textsize = url.length();
180  bs.format("  (%d) %s\n",textsize, url.getbuf());
181}
182
183// Decode the input bytestream and populate this object
184void 
185DjVmNav::decode(const GP<ByteStream> &gstr)
186{
187  //ByteStream &str=*gstr;
188  GP<ByteStream> gpBSByteStream = BSByteStream::create(gstr);
189  GCriticalSectionLock lock(&class_lock);
190  bookmark_list.empty();
191  int nbookmarks=gpBSByteStream->read16();
192  if (nbookmarks)
193    {
194      for(int bookmark=0;bookmark<nbookmarks;bookmark++)
195        {
196          GP<DjVuBookMark> pBookMark=DjVuBookMark::create();
197          pBookMark->decode(gpBSByteStream);
198          bookmark_list.append(pBookMark); 
199        }
200    }
201}
202
203// Serialize this object to the output stream
204void 
205DjVmNav::encode(const GP<ByteStream> &gstr)
206{
207  //ByteStream &str=*gstr;
208  GP<ByteStream> gpBSByteStream = BSByteStream::create(gstr, 1024);
209  GCriticalSectionLock lock(&class_lock);
210  int nbookmarks=bookmark_list.size();
211  gpBSByteStream->write16(nbookmarks);
212  if (nbookmarks)
213    {
214      GPosition pos;
215      int cnt=0;
216      for (pos = bookmark_list; pos; ++pos)
217        {
218          bookmark_list[pos]->encode(gpBSByteStream);
219          cnt++;
220        }
221      if (nbookmarks != cnt)
222        {
223          GUTF8String msg;
224          msg.format("Corrupt bookmarks found during encode: %d of %d \n",
225                     cnt, nbookmarks);
226          G_THROW (msg);
227        }
228    }
229}
230
231int 
232DjVmNav::getBookMarkCount()
233{
234  return(bookmark_list.size());
235}
236
237void 
238DjVmNav::append (const GP<DjVuBookMark> &gpBookMark) 
239{
240  bookmark_list.append(gpBookMark);
241}
242
243bool 
244DjVmNav::getBookMark(GP<DjVuBookMark> &gpBookMark, int iPos)
245{
246  GPosition pos = bookmark_list.nth(iPos);
247  if (pos)
248    gpBookMark = bookmark_list[pos];
249  else
250    gpBookMark = 0;
251  return (gpBookMark?true:false);
252}
253
254
255// A text dump of this object
256void 
257DjVmNav::dump(const GP<ByteStream> &gstr)
258{
259  ByteStream &str=*gstr;
260  GCriticalSectionLock lock(&class_lock);
261  int nbookmarks=bookmark_list.size();
262  str.format("%d bookmarks:\n",nbookmarks);
263  if (nbookmarks)
264    {
265      GPosition pos;
266      int cnt=0;
267      for (pos = bookmark_list; pos; ++pos)
268        {
269          bookmark_list[pos]->dump(&str);
270          cnt++;
271        }
272      if (nbookmarks != cnt)
273        {
274          GUTF8String msg;
275          msg.format("Corrupt bookmarks found during encode: %d of %d \n",
276                     cnt,nbookmarks);
277          G_THROW (msg);
278        }
279    }
280}
281
282bool 
283DjVmNav::isValidBookmark()
284{
285  //test if the bookmark is properly given
286  //for example: (4, "A", urla)
287  //             (0, "B", urlb)
288  //             (0, "C", urlc)
289  //is not a bookmark since A suppose to have 4 decendents, it only get one.
290  int bookmark_totalnum=getBookMarkCount();
291  GP<DjVuBookMark> gpBookMark;
292  int* count_array=(int*)malloc(sizeof(int)*bookmark_totalnum);
293  for(int i=0;i<bookmark_totalnum;i++)
294    {
295      getBookMark(gpBookMark, i);
296      count_array[i]=gpBookMark->count;
297    }
298  int index=0;
299  int trees=0;
300  int* treeSizes=(int*)malloc(sizeof(int)*bookmark_totalnum);
301  while(index<bookmark_totalnum)
302    {
303      int treeSize=get_tree(index,count_array,bookmark_totalnum);
304      if(treeSize>0) //is a tree
305        {
306          index+=treeSize;
307          treeSizes[trees++]=treeSize;
308        }
309      else //not a tree
310        break;
311    }
312  free(count_array);
313  free(treeSizes);
314  return true;
315}
316
317int 
318DjVmNav::get_tree(int index, int* count_array, int count_array_size)
319{
320  int i=index;
321  int accumulate_count=0;
322  while(i<count_array_size)
323    {
324      accumulate_count+=count_array[i];
325      if(accumulate_count==0)
326        return 1;
327      else if(accumulate_count == i-index) //get a tree
328        return accumulate_count;
329      i++;
330    }
331  return 0;
332}
333
334
335#ifdef HAVE_NAMESPACES
336}
337# ifndef NOT_USING_DJVU_NAMESPACE
338using namespace DJVU;
339# endif
340#endif
Note: See TracBrowser for help on using the repository browser.