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