source: trunk/libdjvu/DjVuGlobal.cpp @ 199

Last change on this file since 199 was 17, checked in by Eugene Romanenko, 16 years ago

update makefiles, remove absolute paths, update djvulibre to version 3.5.17

File size: 7.7 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: DjVuGlobal.cpp,v 1.7 2003/11/07 22:08:20 leonb Exp $
55// $Name:  $
56
57#ifdef HAVE_CONFIG_H
58# include "config.h"
59#endif
60#if NEED_GNUG_PRAGMAS
61# pragma implementation
62#endif
63
64/** This file impliments the DjVuProgressTask elements.  The memory
65    functions are implimented in a separate file, because only the memory
66    functions should be compiled with out overloading of the memory functions.
67 */
68 
69
70#ifdef NEED_DJVU_PROGRESS
71#include "DjVuGlobal.h"
72
73
74// ----------------------------------------
75
76#include "GOS.h"
77#include "GThreads.h"
78#include "GException.h"
79#include "GContainer.h"
80#include <stdlib.h>
81#include <stdio.h>
82#include <string.h>
83
84#define INITIAL  500
85#define INTERVAL 250
86
87class DjVuProgressTask::Data : public GPEnabled
88{
89public:
90  djvu_progress_callback *callback;
91  DjVuProgressTask *head;
92  const char *gtask;
93  unsigned long lastsigdate;
94  Data(djvu_progress_callback *_callback):
95    callback(_callback), head(0), gtask(0), lastsigdate(0) {}
96};
97
98 
99static GPMap<void *,DjVuProgressTask::Data> &
100get_map(void)
101{
102  static GPMap<void *,DjVuProgressTask::Data> xmap;
103  return xmap;
104}
105
106djvu_progress_callback *
107DjVuProgressTask::set_callback(djvu_progress_callback *_callback)
108{ 
109  djvu_progress_callback *retval=0;
110  if(_callback)
111  {
112    GMap<void *,GP<DjVuProgressTask::Data> > &map=get_map();
113    void *threadID=GThread::current();
114    if(map.contains(threadID))
115    {
116      DjVuProgressTask::Data &data=*(map[threadID]);
117      retval=data.callback;
118      data.callback=_callback;
119      data.head=0;
120      data.gtask=0;
121      data.lastsigdate=0;
122    }else
123    {
124      map[threadID]=new Data(_callback);
125    }
126  }else
127  {
128    GMap<void *,GP<DjVuProgressTask::Data> > &map=get_map();
129    void *threadID=GThread::current();
130    if(map.contains(threadID))
131    {
132      DjVuProgressTask::Data &data=*(map[threadID]);
133      retval=data.callback;
134      data.callback=0;
135      data.head=0;
136      data.gtask=0;
137      data.lastsigdate=0;
138      map.del(threadID);
139    }
140  }
141  return retval;
142}
143
144DjVuProgressTask::DjVuProgressTask(const char *xtask,int nsteps)
145  : task(xtask),parent(0), nsteps(nsteps), runtostep(0), gdata(0), data(0)
146{
147  //  gtask=task;
148  {
149    GMap<void *,GP<DjVuProgressTask::Data> > &map=get_map();
150    void *threadID=GThread::current();
151    if(map.contains(threadID))
152    {
153      gdata=new GP<Data>;
154      Data &d=*(data=((*(GP<Data> *)gdata)=map[threadID]));
155      if(d.callback)
156      {
157        unsigned long curdate = GOS::ticks();
158        startdate = curdate;
159        if (!d.head)
160          d.lastsigdate = curdate + INITIAL;
161        parent = d.head;
162        d.head = this;
163      }
164    }
165  }
166}
167
168DjVuProgressTask::~DjVuProgressTask()
169{
170  if (data && data->callback)
171  {
172    if (data->head != this)
173      G_THROW( ERR_MSG("DjVuGlobal.not_compatible") );
174    data->head = parent;
175    if (!parent)
176    {
177      unsigned long curdate = GOS::ticks();
178      if((*(data->callback))(data->gtask?data->gtask:"",curdate-startdate, curdate-startdate))
179      {
180        G_THROW("INTERRUPT");
181      }
182    }
183  }
184  delete (GP<Data> *)gdata;
185}
186
187void
188DjVuProgressTask::run(int tostep)
189{
190  if(data)
191  {
192    data->gtask=task;
193    if ((data->callback)&&(tostep>runtostep))
194    {
195      unsigned long curdate = GOS::ticks();
196      if (curdate > data->lastsigdate + INTERVAL)
197        signal(curdate, curdate);
198      runtostep = tostep;
199    }
200  }
201}
202
203void
204DjVuProgressTask::signal(unsigned long curdate, unsigned long estdate)
205{
206  int inprogress = runtostep;
207  if (inprogress > nsteps)
208    inprogress = nsteps;
209  if (inprogress > 0)
210    {
211      const unsigned long enddate = startdate+
212        (unsigned long)(((float)(estdate-startdate) * (float)nsteps) / (float)inprogress);
213      if (parent)
214      {
215        parent->signal(curdate, enddate);
216      }
217      else if (data && data->callback && curdate<enddate)
218      {
219        if((*(data->callback))(data->gtask?data->gtask:"",curdate-startdate, enddate-startdate))
220        {
221          G_THROW("INTERRUPT");
222        }
223        data->lastsigdate = curdate;
224      }
225    }
226}
227
228// Progress callback
229//
230djvu_progress_callback *
231djvu_set_progress_callback( djvu_progress_callback *callback )
232{
233   return DjVuProgressTask::set_callback(callback);
234}
235
236int djvu_supports_progress_callback(void) {return 1;}
237
238#else
239
240#ifndef HAS_DJVU_PROGRESS_TYPEDEF
241extern "C"
242{
243  void *djvu_set_progress_callback(void *);
244  int djvu_supports_progress_callback(void);
245}
246void *djvu_set_progress_callback(void *) { return 0; }
247int djvu_supports_progress_callback(void) {return 0;}
248#else
249int djvu_supports_progress_callback(void) {return 0;}
250djvu_progress_callback *
251djvu_set_progress_callback( djvu_progress_callback *) { return 0; }
252#endif
253
254#endif
255
Note: See TracBrowser for help on using the repository browser.