source: trunk/libdjvu/DjVuNavDir.cpp @ 15

Last change on this file since 15 was 15, checked in by Eugene Romanenko, 15 years ago

needed libs update

File size: 6.6 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, Version 2. The license should have
9//C- accompanied the software or you may obtain a copy of the license
10//C- from the Free Software Foundation at http://www.fsf.org .
11//C-
12//C- This program is distributed in the hope that it will be useful,
13//C- but WITHOUT ANY WARRANTY; without even the implied warranty of
14//C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15//C- GNU General Public License for more details.
16//C-
17//C- DjVuLibre-3.5 is derived from the DjVu(r) Reference Library
18//C- distributed by Lizardtech Software.  On July 19th 2002, Lizardtech
19//C- Software authorized us to replace the original DjVu(r) Reference
20//C- Library notice by the following text (see doc/lizard2002.djvu):
21//C-
22//C-  ------------------------------------------------------------------
23//C- | DjVu (r) Reference Library (v. 3.5)
24//C- | Copyright (c) 1999-2001 LizardTech, Inc. All Rights Reserved.
25//C- | The DjVu Reference Library is protected by U.S. Pat. No.
26//C- | 6,058,214 and patents pending.
27//C- |
28//C- | This software is subject to, and may be distributed under, the
29//C- | GNU General Public License, Version 2. The license should have
30//C- | accompanied the software or you may obtain a copy of the license
31//C- | from the Free Software Foundation at http://www.fsf.org .
32//C- |
33//C- | The computer code originally released by LizardTech under this
34//C- | license and unmodified by other parties is deemed "the LIZARDTECH
35//C- | ORIGINAL CODE."  Subject to any third party intellectual property
36//C- | claims, LizardTech grants recipient a worldwide, royalty-free,
37//C- | non-exclusive license to make, use, sell, or otherwise dispose of
38//C- | the LIZARDTECH ORIGINAL CODE or of programs derived from the
39//C- | LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU
40//C- | General Public License.   This grant only confers the right to
41//C- | infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to
42//C- | the extent such infringement is reasonably necessary to enable
43//C- | recipient to make, have made, practice, sell, or otherwise dispose
44//C- | of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to
45//C- | any greater extent that may be necessary to utilize further
46//C- | modifications or combinations.
47//C- |
48//C- | The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY
49//C- | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
50//C- | TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF
51//C- | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
52//C- +------------------------------------------------------------------
53//
54// $Id: DjVuNavDir.cpp,v 1.8 2003/11/07 22:08:21 leonb Exp $
55// $Name: release_3_5_16 $
56
57#ifdef HAVE_CONFIG_H
58# include "config.h"
59#endif
60#if NEED_GNUG_PRAGMAS
61# pragma implementation
62#endif
63
64#include "DjVuNavDir.h"
65#include "debug.h"
66#include "GException.h"
67#include "GOS.h"
68#include "ByteStream.h"
69#include "GURL.h"
70#include <ctype.h>
71
72
73
74#ifdef HAVE_NAMESPACES
75namespace DJVU {
76# ifdef NOT_DEFINED // Just to fool emacs c++ mode
77}
78#endif
79#endif
80
81
82
83DjVuNavDir::DjVuNavDir(const GURL &dirURL)
84{
85   if (!dirURL) G_THROW( ERR_MSG("DjVuNavDir.zero_dir") );
86   baseURL=dirURL.base();
87}
88
89DjVuNavDir::DjVuNavDir(ByteStream & str, const GURL &dirURL)
90{
91   if (!dirURL) G_THROW( ERR_MSG("DjVuNavDir.zero_dir") );
92   
93   baseURL=GURL(dirURL).base();
94   
95   decode(str);
96}
97
98void
99DjVuNavDir::decode(ByteStream & str)
100{
101   GCriticalSectionLock lk(&lock);
102   
103   GList<GUTF8String> tmp_page2name;
104   int eof=0;
105   while(!eof)
106   {
107      char buffer[1024];
108      char * ptr;
109      for(ptr=buffer;ptr-buffer<1024;ptr++)
110         if ((eof=!str.read(ptr, 1)) || *ptr=='\n') break;
111      if (ptr-buffer==1024) G_THROW( ERR_MSG("DjVuNavDir.long_line") );
112      *ptr=0;
113      if (!strlen(buffer)) continue;
114
115      if (!tmp_page2name.contains(buffer))
116         tmp_page2name.append(buffer);
117   };
118
119   // Now copying lists to arrays for faster access later
120   int pages=tmp_page2name.size();
121   page2name.resize(pages-1);
122
123   int cnt;
124   GPosition pos;
125   for(pos=tmp_page2name, cnt=0;pos;++pos, cnt++)
126      page2name[cnt]=tmp_page2name[pos];
127   
128   // Now creating reverse mapping (strings => numbers)
129   for(cnt=0;cnt<pages;cnt++)
130   {
131      name2page[page2name[cnt]]=cnt;
132      url2page[GURL::UTF8(page2name[cnt],baseURL)]=cnt;
133   }
134}
135
136#ifndef NEED_DECODER_ONLY
137void
138DjVuNavDir::encode(ByteStream & str)
139{
140   GCriticalSectionLock lk(&lock);
141
142   for(int i=0;i<page2name.size();i++)
143   {
144      GUTF8String & name=page2name[i];
145      str.writall((const char*)name, name.length());
146      str.writall("\n", 1);
147   };
148}
149#endif //NEED_DECODER_ONLY
150
151int
152DjVuNavDir::get_pages_num(void) const
153{
154   GCriticalSectionLock lk((GCriticalSection *)&lock);
155   
156   return page2name.size();
157}
158
159int
160DjVuNavDir::name_to_page(const char * name) const
161{
162   GCriticalSectionLock lk((GCriticalSection *)&lock);
163
164   if (!name2page.contains(name)) return -1;
165   return name2page[name];
166}
167
168int
169DjVuNavDir::url_to_page(const GURL & url) const
170{
171   GCriticalSectionLock lk((GCriticalSection *)&lock);
172
173   if (!url2page.contains(url)) return -1;
174   return url2page[url];
175}
176
177GUTF8String
178DjVuNavDir::page_to_name(int page) const
179{
180   GCriticalSectionLock lk((GCriticalSection *)&lock);
181   
182   if (page<0) 
183      G_THROW( ERR_MSG("DjVuNavDir.neg_page") );
184   if (page>=page2name.size())
185      G_THROW( ERR_MSG("DjVuNavDir.large_page") );
186   return page2name[page];
187}
188
189GURL
190DjVuNavDir::page_to_url(int page) const
191{
192   GCriticalSectionLock lk((GCriticalSection *)&lock);
193   
194   return GURL::UTF8(page_to_name(page),baseURL);
195}
196
197void
198DjVuNavDir::insert_page(int where, const char * name)
199{
200   GCriticalSectionLock lk((GCriticalSection *)&lock);
201
202   int pages=page2name.size();
203   if (where<0) where=pages;
204   
205   page2name.resize(pages);
206   for(int i=pages;i>where;i--)
207      page2name[i]=page2name[i-1];
208   page2name[where]=name;
209   name2page[name]=where;
210   url2page[GURL::UTF8(name,baseURL)]=where;
211}
212
213#ifndef NEED_DECODER_ONLY
214void
215DjVuNavDir::delete_page(int page_num)
216{
217   GCriticalSectionLock lk((GCriticalSection *)&lock);
218
219   int pages=page2name.size();
220   
221   if (page_num<0 || page_num>=pages)
222      G_THROW( ERR_MSG("DjVuNavDir.bad_page") );
223
224   for(int i=page_num;i<pages-1;i++)
225      page2name[i]=page2name[i+1];
226   page2name.resize(--pages-1);
227}
228#endif
229
230
231
232#ifdef HAVE_NAMESPACES
233}
234# ifndef NOT_USING_DJVU_NAMESPACE
235using namespace DJVU;
236# endif
237#endif
Note: See TracBrowser for help on using the repository browser.