Blame libdjvu/GOS.cpp

Packit df99a1
//C-  -*- C++ -*-
Packit df99a1
//C- -------------------------------------------------------------------
Packit df99a1
//C- DjVuLibre-3.5
Packit df99a1
//C- Copyright (c) 2002  Leon Bottou and Yann Le Cun.
Packit df99a1
//C- Copyright (c) 2001  AT&T
Packit df99a1
//C-
Packit df99a1
//C- This software is subject to, and may be distributed under, the
Packit df99a1
//C- GNU General Public License, either Version 2 of the license,
Packit df99a1
//C- or (at your option) any later version. The license should have
Packit df99a1
//C- accompanied the software or you may obtain a copy of the license
Packit df99a1
//C- from the Free Software Foundation at http://www.fsf.org .
Packit df99a1
//C-
Packit df99a1
//C- This program is distributed in the hope that it will be useful,
Packit df99a1
//C- but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit df99a1
//C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit df99a1
//C- GNU General Public License for more details.
Packit df99a1
//C- 
Packit df99a1
//C- DjVuLibre-3.5 is derived from the DjVu(r) Reference Library from
Packit df99a1
//C- Lizardtech Software.  Lizardtech Software has authorized us to
Packit df99a1
//C- replace the original DjVu(r) Reference Library notice by the following
Packit df99a1
//C- text (see doc/lizard2002.djvu and doc/lizardtech2007.djvu):
Packit df99a1
//C-
Packit df99a1
//C-  ------------------------------------------------------------------
Packit df99a1
//C- | DjVu (r) Reference Library (v. 3.5)
Packit df99a1
//C- | Copyright (c) 1999-2001 LizardTech, Inc. All Rights Reserved.
Packit df99a1
//C- | The DjVu Reference Library is protected by U.S. Pat. No.
Packit df99a1
//C- | 6,058,214 and patents pending.
Packit df99a1
//C- |
Packit df99a1
//C- | This software is subject to, and may be distributed under, the
Packit df99a1
//C- | GNU General Public License, either Version 2 of the license,
Packit df99a1
//C- | or (at your option) any later version. The license should have
Packit df99a1
//C- | accompanied the software or you may obtain a copy of the license
Packit df99a1
//C- | from the Free Software Foundation at http://www.fsf.org .
Packit df99a1
//C- |
Packit df99a1
//C- | The computer code originally released by LizardTech under this
Packit df99a1
//C- | license and unmodified by other parties is deemed "the LIZARDTECH
Packit df99a1
//C- | ORIGINAL CODE."  Subject to any third party intellectual property
Packit df99a1
//C- | claims, LizardTech grants recipient a worldwide, royalty-free, 
Packit df99a1
//C- | non-exclusive license to make, use, sell, or otherwise dispose of 
Packit df99a1
//C- | the LIZARDTECH ORIGINAL CODE or of programs derived from the 
Packit df99a1
//C- | LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU 
Packit df99a1
//C- | General Public License.   This grant only confers the right to 
Packit df99a1
//C- | infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to 
Packit df99a1
//C- | the extent such infringement is reasonably necessary to enable 
Packit df99a1
//C- | recipient to make, have made, practice, sell, or otherwise dispose 
Packit df99a1
//C- | of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to 
Packit df99a1
//C- | any greater extent that may be necessary to utilize further 
Packit df99a1
//C- | modifications or combinations.
Packit df99a1
//C- |
Packit df99a1
//C- | The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY
Packit df99a1
//C- | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
Packit df99a1
//C- | TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF
Packit df99a1
//C- | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
Packit df99a1
//C- +------------------------------------------------------------------
Packit df99a1
Packit df99a1
#ifdef HAVE_CONFIG_H
Packit df99a1
# include "config.h"
Packit df99a1
#endif
Packit df99a1
#if NEED_GNUG_PRAGMAS
Packit df99a1
# pragma implementation
Packit df99a1
#endif
Packit df99a1
Packit df99a1
#include "GException.h"
Packit df99a1
#include "GThreads.h"
Packit df99a1
#include "GOS.h"
Packit df99a1
#include "GURL.h"
Packit df99a1
Packit df99a1
#include <stddef.h>
Packit df99a1
#include <stdlib.h>
Packit df99a1
#include <stdio.h>
Packit df99a1
#include <ctype.h>
Packit df99a1
#include <math.h>
Packit df99a1
#include <string.h>
Packit df99a1
Packit df99a1
#if defined(__CYGWIN32__)
Packit df99a1
# define UNIX 1
Packit df99a1
#endif
Packit df99a1
Packit df99a1
#if defined(_WIN32) && !defined(UNIX)
Packit df99a1
# include <windows.h>
Packit df99a1
# include <string.h>
Packit df99a1
# include <direct.h>
Packit df99a1
# define getcwd _getcwd
Packit df99a1
#endif
Packit df99a1
Packit df99a1
#if defined(OS2)
Packit df99a1
# define INCL_DOS
Packit df99a1
# include <os2.h>
Packit df99a1
#endif
Packit df99a1
Packit df99a1
#if defined(macintosh) && !defined(UNIX)
Packit df99a1
# include <unix.h>
Packit df99a1
# include <errno.h>
Packit df99a1
# include <unistd.h>
Packit df99a1
#endif
Packit df99a1
Packit df99a1
#if defined(UNIX) || defined(OS2)
Packit df99a1
# include <errno.h>
Packit df99a1
# include <sys/types.h>
Packit df99a1
# include <sys/stat.h>
Packit df99a1
# include <sys/time.h>
Packit df99a1
# include <fcntl.h>
Packit df99a1
# include <pwd.h>
Packit df99a1
# include <stdio.h>
Packit df99a1
# include <unistd.h>
Packit df99a1
#endif
Packit df99a1
Packit df99a1
Packit df99a1
// -- TRUE FALSE
Packit df99a1
#undef TRUE
Packit df99a1
#undef FALSE
Packit df99a1
#define TRUE 1
Packit df99a1
#define FALSE 0
Packit df99a1
Packit df99a1
// -- MAXPATHLEN
Packit df99a1
#ifndef MAXPATHLEN
Packit df99a1
# ifdef _MAX_PATH
Packit df99a1
#  define MAXPATHLEN _MAX_PATH
Packit df99a1
# else
Packit df99a1
#  define MAXPATHLEN 1024
Packit df99a1
# endif
Packit df99a1
#else
Packit df99a1
# if ( MAXPATHLEN < 1024 )
Packit df99a1
#  undef MAXPATHLEN
Packit df99a1
#  define MAXPATHLEN 1024
Packit df99a1
# endif
Packit df99a1
#endif
Packit df99a1
Packit df99a1
#ifdef HAVE_NAMESPACES
Packit df99a1
namespace DJVU {
Packit df99a1
# ifdef NOT_DEFINED // Just to fool emacs c++ mode
Packit df99a1
}
Packit df99a1
#endif
Packit df99a1
#endif
Packit df99a1
Packit df99a1
Packit df99a1
#if defined(AUTOCONF) && !defined(HAVE_STRERROR)
Packit df99a1
# define NEED_STRERROR
Packit df99a1
#elif defined(sun) && !defined(__svr4__) && !defined(__SVR4)
Packit df99a1
# define NEED_STRERROR
Packit df99a1
#elif defined(REIMPLEMENT_STRERROR)
Packit df99a1
# define NEED_STRERROR
Packit df99a1
#endif
Packit df99a1
#ifdef NEED_STRERROR
Packit df99a1
char *
Packit df99a1
strerror(int errno)
Packit df99a1
{
Packit df99a1
  extern int sys_nerr;
Packit df99a1
  extern char *sys_errlist[];
Packit df99a1
  if (errno>0 && errno
Packit df99a1
    return sys_errlist[errno];
Packit df99a1
  return (char*) "unknown stdio error";
Packit df99a1
}
Packit df99a1
#endif
Packit df99a1
Packit df99a1
Packit df99a1
static const char slash='/';
Packit df99a1
static const char percent='%';
Packit df99a1
static const char backslash='\\';
Packit df99a1
static const char colon=':';
Packit df99a1
static const char dot='.';
Packit df99a1
static const char nillchar=0;
Packit df99a1
Packit df99a1
Packit df99a1
// -----------------------------------------
Packit df99a1
// Functions for dealing with filenames
Packit df99a1
// -----------------------------------------
Packit df99a1
Packit df99a1
static inline int
Packit df99a1
finddirsep(const GUTF8String &fname)
Packit df99a1
{
Packit df99a1
#if defined(_WIN32)
Packit df99a1
  return fname.rcontains("\\/",0);
Packit df99a1
#elif defined(UNIX)
Packit df99a1
  return fname.rsearch('/',0);
Packit df99a1
#elif defined(macintosh)
Packit df99a1
  return fname.rcontains(":/",0);
Packit df99a1
#else
Packit df99a1
# error "Define something here for your operating system"
Packit df99a1
#endif  
Packit df99a1
}
Packit df99a1
Packit df99a1
Packit df99a1
// basename(filename[, suffix])
Packit df99a1
// -- returns the last component of filename and removes suffix
Packit df99a1
//    when present. works like /bin/basename.
Packit df99a1
GUTF8String 
Packit df99a1
GOS::basename(const GUTF8String &gfname, const char *suffix)
Packit df99a1
{
Packit df99a1
  if(!gfname.length())
Packit df99a1
    return gfname;
Packit df99a1
Packit df99a1
  const char *fname=gfname;
Packit df99a1
#if defined(_WIN32) || defined(OS2)
Packit df99a1
  // Special cases
Packit df99a1
  if (fname[1] == colon)
Packit df99a1
  {
Packit df99a1
    if(!fname[2])
Packit df99a1
    {
Packit df99a1
      return gfname;
Packit df99a1
    }
Packit df99a1
    if (!fname[3] && (fname[2]== slash || fname[2]== backslash))
Packit df99a1
    {
Packit df99a1
      char string_buffer[4];
Packit df99a1
      string_buffer[0] = fname[0];
Packit df99a1
      string_buffer[1] = colon;
Packit df99a1
      string_buffer[2] = backslash; 
Packit df99a1
      string_buffer[3] = 0; 
Packit df99a1
      return string_buffer;
Packit df99a1
    }
Packit df99a1
  }
Packit df99a1
#endif
Packit df99a1
Packit df99a1
Packit df99a1
  // Allocate buffer
Packit df99a1
  GUTF8String retval(gfname,finddirsep(gfname)+1,(unsigned int)(-1));
Packit df99a1
  fname=retval;
Packit df99a1
Packit df99a1
  // Process suffix
Packit df99a1
  if (suffix)
Packit df99a1
  {
Packit df99a1
    if (suffix[0]== dot )
Packit df99a1
      suffix ++;
Packit df99a1
    if (suffix[0])
Packit df99a1
    {
Packit df99a1
      const GUTF8String gsuffix(suffix);
Packit df99a1
      const int sl = gsuffix.length();
Packit df99a1
      const char *s = fname + strlen(fname);
Packit df99a1
      if (s > fname + sl)
Packit df99a1
      {
Packit df99a1
        s = s - (sl + 1);
Packit df99a1
        if(*s == dot && (GUTF8String(s+1).downcase() == gsuffix.downcase()))
Packit df99a1
        {
Packit df99a1
          retval.setat((int)((size_t)s-(size_t)fname),0);
Packit df99a1
        }
Packit df99a1
      }
Packit df99a1
    }
Packit df99a1
  }
Packit df99a1
  return retval;
Packit df99a1
}
Packit df99a1
Packit df99a1
Packit df99a1
Packit df99a1
// errmsg --
Packit df99a1
// -- A small helper function returning a 
Packit df99a1
//    stdio error message in a static buffer.
Packit df99a1
Packit df99a1
static GNativeString 
Packit df99a1
errmsg()
Packit df99a1
{
Packit df99a1
  GNativeString buffer;
Packit df99a1
  const char *errname = strerror(errno);
Packit df99a1
  buffer.format("%s (errno = %d)", errname, errno);
Packit df99a1
  return buffer;
Packit df99a1
}
Packit df99a1
Packit df99a1
Packit df99a1
Packit df99a1
// -----------------------------------------
Packit df99a1
// Functions for measuring time
Packit df99a1
// -----------------------------------------
Packit df99a1
Packit df99a1
// ticks() --
Packit df99a1
// -- returns the number of milliseconds elapsed since 
Packit df99a1
//    a system dependent date.
Packit df99a1
unsigned long 
Packit df99a1
GOS::ticks()
Packit df99a1
{
Packit df99a1
#if defined(UNIX)
Packit df99a1
  struct timeval tv;
Packit df99a1
  if (gettimeofday(&tv, NULL) < 0)
Packit df99a1
    G_THROW(errmsg());
Packit df99a1
  return (unsigned long)( ((tv.tv_sec & 0xfffff)*1000) 
Packit df99a1
                          + (tv.tv_usec/1000) );
Packit df99a1
#elif defined(_WIN32)
Packit df99a1
  DWORD clk = GetTickCount();
Packit df99a1
  return (unsigned long)clk;
Packit df99a1
#elif defined(OS2)
Packit df99a1
  ULONG clk = 0;
Packit df99a1
  DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, (PVOID)&clk, sizeof(ULONG));
Packit df99a1
  return clk;
Packit df99a1
#elif defined(macintosh)
Packit df99a1
  return (unsigned long)((double)TickCount()*16.66);
Packit df99a1
#else
Packit df99a1
# error "Define something here for your operating system"
Packit df99a1
#endif
Packit df99a1
}
Packit df99a1
Packit df99a1
// sleep(int milliseconds) --
Packit df99a1
// -- sleeps during the specified time (in milliseconds)
Packit df99a1
void 
Packit df99a1
GOS::sleep(int milliseconds)
Packit df99a1
{
Packit df99a1
#if defined(UNIX)
Packit df99a1
  struct timeval tv;
Packit df99a1
  tv.tv_sec = milliseconds / 1000;
Packit df99a1
  tv.tv_usec = (milliseconds - (tv.tv_sec * 1000)) * 1000;
Packit df99a1
  ::select(0, NULL, NULL, NULL, &tv;;
Packit df99a1
#elif defined(_WIN32)
Packit df99a1
  Sleep(milliseconds);
Packit df99a1
#elif defined(OS2)
Packit df99a1
  DosSleep(milliseconds);
Packit df99a1
#elif defined(macintosh)
Packit df99a1
  unsigned long tick = ticks(), now;
Packit df99a1
  while (1) {
Packit df99a1
    now = ticks();
Packit df99a1
    if ((tick+milliseconds) < now)
Packit df99a1
      break;
Packit df99a1
    GThread::yield();
Packit df99a1
  }
Packit df99a1
#endif
Packit df99a1
}
Packit df99a1
Packit df99a1
Packit df99a1
// -----------------------------------------
Packit df99a1
// Testing
Packit df99a1
// -----------------------------------------
Packit df99a1
Packit df99a1
// cwd([dirname])
Packit df99a1
// -- changes directory to dirname (when specified).
Packit df99a1
//    returns the full path name of the current directory. 
Packit df99a1
GUTF8String 
Packit df99a1
GOS::cwd(const GUTF8String &dirname)
Packit df99a1
{
Packit df99a1
#if defined(UNIX) || defined(macintosh) || defined(OS2)
Packit df99a1
  if (dirname.length() && chdir(dirname.getUTF82Native())==-1)//MBCS cvt
Packit df99a1
    G_THROW(errmsg());
Packit df99a1
  char *string_buffer;
Packit df99a1
  GPBuffer<char> gstring_buffer(string_buffer,MAXPATHLEN+1);
Packit df99a1
  char *result = getcwd(string_buffer,MAXPATHLEN);
Packit df99a1
  if (!result)
Packit df99a1
    G_THROW(errmsg());
Packit df99a1
  return GNativeString(result).getNative2UTF8();//MBCS cvt
Packit df99a1
#elif defined(_WIN32)
Packit df99a1
  char drv[2];
Packit df99a1
  if (dirname.length() && _chdir(dirname.getUTF82Native())==-1)//MBCS cvt
Packit df99a1
    G_THROW(errmsg());
Packit df99a1
  drv[0]= dot ; drv[1]=0;
Packit df99a1
  char *string_buffer;
Packit df99a1
  GPBuffer<char> gstring_buffer(string_buffer,MAXPATHLEN+1);
Packit df99a1
  char *result = getcwd(string_buffer,MAXPATHLEN);
Packit df99a1
  GetFullPathName(drv, MAXPATHLEN, string_buffer, &result);
Packit df99a1
  return GNativeString(string_buffer).getNative2UTF8();//MBCS cvt
Packit df99a1
#else
Packit df99a1
# error "Define something here for your operating system"
Packit df99a1
#endif 
Packit df99a1
}
Packit df99a1
Packit df99a1
GUTF8String
Packit df99a1
GOS::getenv(const GUTF8String &name)
Packit df99a1
{
Packit df99a1
  GUTF8String retval;
Packit df99a1
  if(name.length())
Packit df99a1
  {
Packit df99a1
    const char *env=::getenv(name.getUTF82Native());
Packit df99a1
    if(env)
Packit df99a1
    {
Packit df99a1
      retval=GNativeString(env);
Packit df99a1
    }
Packit df99a1
  }
Packit df99a1
  return retval;
Packit df99a1
}
Packit df99a1
Packit df99a1
Packit df99a1
Packit df99a1
#ifdef HAVE_NAMESPACES
Packit df99a1
}
Packit df99a1
# ifndef NOT_USING_DJVU_NAMESPACE
Packit df99a1
using namespace DJVU;
Packit df99a1
# endif
Packit df99a1
#endif
Packit df99a1