source: trunk/libdjvu/GOS.cpp @ 17

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

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

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