source: trunk/libdjvu/GOS.cpp @ 206

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

DJVU plugin: djvulibre updated to version 3.5.19

File size: 9.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, 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: GOS.cpp,v 1.14 2007/03/25 20:48:32 leonb Exp $
57// $Name: release_3_5_19 $
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 "GException.h"
67#include "GThreads.h"
68#include "GOS.h"
69#include "GURL.h"
70
71#include <stdlib.h>
72#include <stdio.h>
73#include <ctype.h>
74#include <math.h>
75#include <string.h>
76
77#ifdef WIN32
78# include <atlbase.h>
79# include <windows.h>
80# include <direct.h>
81#endif
82
83#ifdef OS2
84# define INCL_DOS
85# include <os2.h>
86#endif
87
88#if defined(UNIX) || defined(OS2)
89# include <errno.h>
90# include <sys/types.h>
91# include <sys/stat.h>
92# include <sys/time.h>
93# include <fcntl.h>
94# include <pwd.h>
95# include <stdio.h>
96# include <unistd.h>
97#endif
98
99#ifdef macintosh
100# include <unix.h>
101# include <errno.h>
102# include <unistd.h>
103#endif
104
105// -- TRUE FALSE
106#undef TRUE
107#undef FALSE
108#define TRUE 1
109#define FALSE 0
110
111// -- MAXPATHLEN
112#ifndef MAXPATHLEN
113# ifdef _MAX_PATH
114#  define MAXPATHLEN _MAX_PATH
115# else
116#  define MAXPATHLEN 1024
117# endif
118#else
119# if ( MAXPATHLEN < 1024 )
120#  undef MAXPATHLEN
121#  define MAXPATHLEN 1024
122# endif
123#endif
124
125#ifdef HAVE_NAMESPACES
126namespace DJVU {
127# ifdef NOT_DEFINED // Just to fool emacs c++ mode
128}
129#endif
130#endif
131
132
133#if defined(AUTOCONF) && !defined(HAVE_STRERROR)
134# define NEED_STRERROR
135#elif defined(sun) && !defined(__svr4__) && !defined(__SVR4)
136# define NEED_STRERROR
137#elif defined(REIMPLEMENT_STRERROR)
138# define NEED_STRERROR
139#endif
140#ifdef NEED_STRERROR
141char *
142strerror(int errno)
143{
144  extern int sys_nerr;
145  extern char *sys_errlist[];
146  if (errno>0 && errno<sys_nerr) 
147    return sys_errlist[errno];
148  return "unknown stdio error";
149}
150#endif
151
152
153static const char slash='/';
154static const char percent='%';
155static const char backslash='\\';
156static const char colon=':';
157static const char dot='.';
158static const char nillchar=0;
159
160
161// -----------------------------------------
162// Functions for dealing with filenames
163// -----------------------------------------
164
165static inline int
166finddirsep(const GUTF8String &fname)
167{
168#if defined(UNIX)
169  return fname.rsearch('/',0);
170#elif defined(WIN32) || defined(OS2)
171  return fname.rcontains("\\/",0);
172#elif defined(macintosh)
173  return fname.rcontains(":/",0);
174#else
175#error "Define something here for your operating system"
176#endif 
177}
178
179
180// basename(filename[, suffix])
181// -- returns the last component of filename and removes suffix
182//    when present. works like /bin/basename.
183GUTF8String
184GOS::basename(const GUTF8String &gfname, const char *suffix)
185{
186  if(!gfname.length())
187    return gfname;
188
189  const char *fname=gfname;
190#if defined(WIN32) || defined(OS2)
191  // Special cases
192  if (fname[1] == colon)
193  {
194    if(!fname[2])
195    {
196      return gfname;
197    }
198    if (!fname[3] && (fname[2]== slash || fname[2]== backslash))
199    {
200      char string_buffer[4];
201      string_buffer[0] = fname[0];
202      string_buffer[1] = colon;
203      string_buffer[2] = backslash; 
204      string_buffer[3] = 0; 
205      return string_buffer;
206    }
207  }
208#endif
209
210
211  // Allocate buffer
212  GUTF8String retval(gfname,finddirsep(gfname)+1,(unsigned int)(-1));
213  fname=retval;
214
215  // Process suffix
216  if (suffix)
217  {
218    if (suffix[0]== dot )
219      suffix ++;
220    if (suffix[0])
221    {
222      const GUTF8String gsuffix(suffix);
223      const int sl = gsuffix.length();
224      const char *s = fname + strlen(fname);
225      if (s > fname + sl)
226      {
227        s = s - (sl + 1);
228        if(*s == dot && (GUTF8String(s+1).downcase() == gsuffix.downcase()))
229        {
230          retval.setat((int)((size_t)s-(size_t)fname),0);
231        }
232      }
233    }
234  }
235  return retval;
236}
237
238
239
240// errmsg --
241// -- A small helper function returning a
242//    stdio error message in a static buffer.
243
244static GNativeString
245errmsg()
246{
247  GNativeString buffer;
248  const char *errname = strerror(errno);
249  buffer.format("%s (errno = %d)", errname, errno);
250  return buffer;
251}
252
253
254
255// -----------------------------------------
256// Functions for measuring time
257// -----------------------------------------
258
259// ticks() --
260// -- returns the number of milliseconds elapsed since
261//    a system dependent date.
262unsigned long 
263GOS::ticks()
264{
265#if defined(UNIX)
266  struct timeval tv;
267  if (gettimeofday(&tv, NULL) < 0)
268    G_THROW(errmsg());
269  return (unsigned long)( ((tv.tv_sec & 0xfffff)*1000) 
270                          + (tv.tv_usec/1000) );
271#elif defined(WIN32)
272  DWORD clk = GetTickCount();
273  return (unsigned long)clk;
274#elif defined(OS2)
275  ULONG clk = 0;
276  DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, (PVOID)&clk, sizeof(ULONG));
277  return clk;
278#elif defined(macintosh)
279  return (unsigned long)((double)TickCount()*16.66);
280#else
281# error "Define something here for your operating system"
282#endif
283}
284
285// sleep(int milliseconds) --
286// -- sleeps during the specified time (in milliseconds)
287void 
288GOS::sleep(int milliseconds)
289{
290#if defined(UNIX)
291  struct timeval tv;
292  tv.tv_sec = milliseconds / 1000;
293  tv.tv_usec = (milliseconds - (tv.tv_sec * 1000)) * 1000;
294# if defined(THREADMODEL) && (THREADMODEL==COTHREADS)
295  GThread::select(0, NULL, NULL, NULL, &tv);
296# else
297  select(0, NULL, NULL, NULL, &tv);
298# endif
299#elif defined(WIN32)
300  Sleep(milliseconds);
301#elif defined(OS2)
302  DosSleep(milliseconds);
303#elif defined(macintosh)
304  unsigned long tick = ticks(), now;
305  while (1) {
306    now = ticks();
307    if ((tick+milliseconds) < now)
308      break;
309    GThread::yield();
310  }
311#endif
312}
313
314
315// -----------------------------------------
316// Testing
317// -----------------------------------------
318
319// cwd([dirname])
320// -- changes directory to dirname (when specified).
321//    returns the full path name of the current directory.
322GUTF8String
323GOS::cwd(const GUTF8String &dirname)
324{
325#if defined(UNIX) || defined(macintosh) || defined(OS2)
326  if (dirname.length() && chdir(dirname.getUTF82Native())==-1)//MBCS cvt
327    G_THROW(errmsg());
328  char *string_buffer;
329  GPBuffer<char> gstring_buffer(string_buffer,MAXPATHLEN+1);
330  char *result = getcwd(string_buffer,MAXPATHLEN);
331  if (!result)
332    G_THROW(errmsg());
333  return GNativeString(result).getNative2UTF8();//MBCS cvt
334#elif defined (WIN32)
335  char drv[2];
336  if (dirname.length() && _chdir(dirname.getUTF82Native())==-1)//MBCS cvt
337    G_THROW(errmsg());
338  drv[0]= dot ; drv[1]=0;
339  char *string_buffer;
340  GPBuffer<char> gstring_buffer(string_buffer,MAXPATHLEN+1);
341  char *result = getcwd(string_buffer,MAXPATHLEN);
342  GetFullPathName(drv, MAXPATHLEN, string_buffer, &result);
343  return GNativeString(string_buffer).getNative2UTF8();//MBCS cvt
344#else
345#error "Define something here for your operating system"
346#endif
347}
348
349GUTF8String
350GOS::getenv(const GUTF8String &name)
351{
352  GUTF8String retval;
353  if(name.length())
354  {
355    const char *env=::getenv(name.getUTF82Native());
356    if(env)
357    {
358      retval=GNativeString(env);
359    }
360  }
361  return retval;
362}
363
364
365
366#ifdef HAVE_NAMESPACES
367}
368# ifndef NOT_USING_DJVU_NAMESPACE
369using namespace DJVU;
370# endif
371#endif
372
Note: See TracBrowser for help on using the repository browser.