• Skip to content
  • Skip to link menu
KDE 3.5 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

kviewshell

GOS.cpp

Go to the documentation of this file.
00001 //C-  -*- C++ -*-
00002 //C- -------------------------------------------------------------------
00003 //C- DjVuLibre-3.5
00004 //C- Copyright (c) 2002  Leon Bottou and Yann Le Cun.
00005 //C- Copyright (c) 2001  AT&T
00006 //C-
00007 //C- This software is subject to, and may be distributed under, the
00008 //C- GNU General Public License, Version 2. The license should have
00009 //C- accompanied the software or you may obtain a copy of the license
00010 //C- from the Free Software Foundation at http://www.fsf.org .
00011 //C-
00012 //C- This program is distributed in the hope that it will be useful,
00013 //C- but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 //C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 //C- GNU General Public License for more details.
00016 //C- 
00017 //C- DjVuLibre-3.5 is derived from the DjVu(r) Reference Library
00018 //C- distributed by Lizardtech Software.  On July 19th 2002, Lizardtech 
00019 //C- Software authorized us to replace the original DjVu(r) Reference 
00020 //C- Library notice by the following text (see doc/lizard2002.djvu):
00021 //C-
00022 //C-  ------------------------------------------------------------------
00023 //C- | DjVu (r) Reference Library (v. 3.5)
00024 //C- | Copyright (c) 1999-2001 LizardTech, Inc. All Rights Reserved.
00025 //C- | The DjVu Reference Library is protected by U.S. Pat. No.
00026 //C- | 6,058,214 and patents pending.
00027 //C- |
00028 //C- | This software is subject to, and may be distributed under, the
00029 //C- | GNU General Public License, Version 2. The license should have
00030 //C- | accompanied the software or you may obtain a copy of the license
00031 //C- | from the Free Software Foundation at http://www.fsf.org .
00032 //C- |
00033 //C- | The computer code originally released by LizardTech under this
00034 //C- | license and unmodified by other parties is deemed "the LIZARDTECH
00035 //C- | ORIGINAL CODE."  Subject to any third party intellectual property
00036 //C- | claims, LizardTech grants recipient a worldwide, royalty-free, 
00037 //C- | non-exclusive license to make, use, sell, or otherwise dispose of 
00038 //C- | the LIZARDTECH ORIGINAL CODE or of programs derived from the 
00039 //C- | LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU 
00040 //C- | General Public License.   This grant only confers the right to 
00041 //C- | infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to 
00042 //C- | the extent such infringement is reasonably necessary to enable 
00043 //C- | recipient to make, have made, practice, sell, or otherwise dispose 
00044 //C- | of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to 
00045 //C- | any greater extent that may be necessary to utilize further 
00046 //C- | modifications or combinations.
00047 //C- |
00048 //C- | The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY
00049 //C- | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
00050 //C- | TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF
00051 //C- | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
00052 //C- +------------------------------------------------------------------
00053 // 
00054 // $Id: GOS.cpp,v 1.12 2005/04/27 16:34:13 leonb Exp $
00055 // $Name: release_3_5_15 $
00056 
00057 #ifdef HAVE_CONFIG_H
00058 # include "config.h"
00059 #endif
00060 #if NEED_GNUG_PRAGMAS
00061 # pragma implementation
00062 #endif
00063 
00064 #include "GException.h"
00065 #include "GThreads.h"
00066 #include "GOS.h"
00067 #include "GURL.h"
00068 
00069 #include <stdlib.h>
00070 #include <stdio.h>
00071 #include <ctype.h>
00072 #include <math.h>
00073 #include <string.h>
00074 
00075 #ifdef WIN32
00076 # include <atlbase.h>
00077 # include <windows.h>
00078 # include <direct.h>
00079 #endif
00080 
00081 #ifdef OS2
00082 # define INCL_DOS
00083 # include <os2.h>
00084 #endif
00085 
00086 #if defined(UNIX) || defined(OS2)
00087 # include <errno.h>
00088 # include <sys/types.h>
00089 # include <sys/stat.h>
00090 # include <sys/time.h>
00091 # include <fcntl.h>
00092 # include <pwd.h>
00093 # include <stdio.h>
00094 # include <unistd.h>
00095 #endif
00096 
00097 #ifdef macintosh
00098 # include <unix.h>
00099 # include <errno.h>
00100 # include <unistd.h>
00101 #endif
00102 
00103 // -- TRUE FALSE
00104 #undef TRUE
00105 #undef FALSE
00106 #define TRUE 1
00107 #define FALSE 0
00108 
00109 // -- MAXPATHLEN
00110 #ifndef MAXPATHLEN
00111 # ifdef _MAX_PATH
00112 #  define MAXPATHLEN _MAX_PATH
00113 # else
00114 #  define MAXPATHLEN 1024
00115 # endif
00116 #else
00117 # if ( MAXPATHLEN < 1024 )
00118 #  undef MAXPATHLEN
00119 #  define MAXPATHLEN 1024
00120 # endif
00121 #endif
00122 
00123 #ifdef HAVE_NAMESPACES
00124 namespace DJVU {
00125 # ifdef NOT_DEFINED // Just to fool emacs c++ mode
00126 }
00127 #endif
00128 #endif
00129 
00130 
00131 #if defined(AUTOCONF) && !defined(HAVE_STRERROR)
00132 # define NEED_STRERROR
00133 #elif defined(sun) && !defined(__svr4__)
00134 # define NEED_STRERROR
00135 #elif defined(REIMPLEMENT_STRERROR)
00136 # define NEED_STRERROR
00137 #endif
00138 #ifdef NEED_STRERROR
00139 char *
00140 strerror(int errno)
00141 {
00142   extern int sys_nerr;
00143   extern char *sys_errlist[];
00144   if (errno>0 && errno<sys_nerr) 
00145     return sys_errlist[errno];
00146   return "unknown stdio error";
00147 }
00148 #endif
00149 
00150 
00151 static const char slash='/';
00152 static const char percent='%';
00153 static const char backslash='\\';
00154 static const char colon=':';
00155 static const char dot='.';
00156 static const char nillchar=0;
00157 
00158 
00159 // -----------------------------------------
00160 // Functions for dealing with filenames
00161 // -----------------------------------------
00162 
00163 static inline int
00164 finddirsep(const GUTF8String &fname)
00165 {
00166 #if defined(UNIX)
00167   return fname.rsearch('/',0);
00168 #elif defined(WIN32) || defined(OS2)
00169   return fname.rcontains("\\/",0);
00170 #elif defined(macintosh)
00171   return fname.rcontains(":/",0);
00172 #else
00173 #error "Define something here for your operating system"
00174 #endif  
00175 }
00176 
00177 
00178 // basename(filename[, suffix])
00179 // -- returns the last component of filename and removes suffix
00180 //    when present. works like /bin/basename.
00181 GUTF8String 
00182 GOS::basename(const GUTF8String &gfname, const char *suffix)
00183 {
00184   if(!gfname.length())
00185     return gfname;
00186 
00187   const char *fname=gfname;
00188 #if defined(WIN32) || defined(OS2)
00189   // Special cases
00190   if (fname[1] == colon)
00191   {
00192     if(!fname[2])
00193     {
00194       return gfname;
00195     }
00196     if (!fname[3] && (fname[2]== slash || fname[2]== backslash))
00197     {
00198       char string_buffer[4];
00199       string_buffer[0] = fname[0];
00200       string_buffer[1] = colon;
00201       string_buffer[2] = backslash; 
00202       string_buffer[3] = 0; 
00203       return string_buffer;
00204     }
00205   }
00206 #endif
00207 
00208 
00209   // Allocate buffer
00210   GUTF8String retval(gfname,finddirsep(gfname)+1,(unsigned int)(-1));
00211   fname=retval;
00212 
00213   // Process suffix
00214   if (suffix)
00215   {
00216     if (suffix[0]== dot )
00217       suffix ++;
00218     if (suffix[0])
00219     {
00220       const GUTF8String gsuffix(suffix);
00221       const int sl = gsuffix.length();
00222       const char *s = fname + strlen(fname);
00223       if (s > fname + sl)
00224       {
00225         s = s - (sl + 1);
00226         if(*s == dot && (GUTF8String(s+1).downcase() == gsuffix.downcase()))
00227         {
00228           retval.setat((int)((size_t)s-(size_t)fname),0);
00229         }
00230       }
00231     }
00232   }
00233   return retval;
00234 }
00235 
00236 
00237 
00238 // errmsg --
00239 // -- A small helper function returning a 
00240 //    stdio error message in a static buffer.
00241 
00242 static GNativeString 
00243 errmsg()
00244 {
00245   GNativeString buffer;
00246   const char *errname = strerror(errno);
00247   buffer.format("%s (errno = %d)", errname, errno);
00248   return buffer;
00249 }
00250 
00251 
00252 
00253 // -----------------------------------------
00254 // Functions for measuring time
00255 // -----------------------------------------
00256 
00257 // ticks() --
00258 // -- returns the number of milliseconds elapsed since 
00259 //    a system dependent date.
00260 unsigned long 
00261 GOS::ticks()
00262 {
00263 #if defined(UNIX)
00264   struct timeval tv;
00265   if (gettimeofday(&tv, NULL) < 0)
00266     G_THROW(errmsg());
00267   return (unsigned long)( ((tv.tv_sec & 0xfffff)*1000) 
00268                           + (tv.tv_usec/1000) );
00269 #elif defined(WIN32)
00270   DWORD clk = GetTickCount();
00271   return (unsigned long)clk;
00272 #elif defined(OS2)
00273   ULONG clk = 0;
00274   DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, (PVOID)&clk, sizeof(ULONG));
00275   return clk;
00276 #elif defined(macintosh)
00277   return (unsigned long)((double)TickCount()*16.66);
00278 #else
00279 # error "Define something here for your operating system"
00280 #endif
00281 }
00282 
00283 // sleep(int milliseconds) --
00284 // -- sleeps during the specified time (in milliseconds)
00285 void 
00286 GOS::sleep(int milliseconds)
00287 {
00288 #if defined(UNIX)
00289   struct timeval tv;
00290   tv.tv_sec = milliseconds / 1000;
00291   tv.tv_usec = (milliseconds - (tv.tv_sec * 1000)) * 1000;
00292 # if defined(THREADMODEL) && (THREADMODEL==COTHREADS)
00293   GThread::select(0, NULL, NULL, NULL, &tv);
00294 # else
00295   select(0, NULL, NULL, NULL, &tv);
00296 # endif
00297 #elif defined(WIN32)
00298   Sleep(milliseconds);
00299 #elif defined(OS2)
00300   DosSleep(milliseconds);
00301 #elif defined(macintosh)
00302   unsigned long tick = ticks(), now;
00303   while (1) {
00304     now = ticks();
00305     if ((tick+milliseconds) < now)
00306       break;
00307     GThread::yield();
00308   }
00309 #endif
00310 }
00311 
00312 
00313 // -----------------------------------------
00314 // Testing
00315 // -----------------------------------------
00316 
00317 // cwd([dirname])
00318 // -- changes directory to dirname (when specified).
00319 //    returns the full path name of the current directory. 
00320 GUTF8String 
00321 GOS::cwd(const GUTF8String &dirname)
00322 {
00323 #if defined(UNIX) || defined(macintosh) || defined(OS2)
00324   if (dirname.length() && chdir(dirname.getUTF82Native())==-1)//MBCS cvt
00325     G_THROW(errmsg());
00326   char *string_buffer;
00327   GPBuffer<char> gstring_buffer(string_buffer,MAXPATHLEN+1);
00328   char *result = getcwd(string_buffer,MAXPATHLEN);
00329   if (!result)
00330     G_THROW(errmsg());
00331   return GNativeString(result).getNative2UTF8();//MBCS cvt
00332 #elif defined (WIN32)
00333   char drv[2];
00334   if (dirname.length() && _chdir(dirname.getUTF82Native())==-1)//MBCS cvt
00335     G_THROW(errmsg());
00336   drv[0]= dot ; drv[1]=0;
00337   char *string_buffer;
00338   GPBuffer<char> gstring_buffer(string_buffer,MAXPATHLEN+1);
00339   char *result = getcwd(string_buffer,MAXPATHLEN);
00340   GetFullPathName(drv, MAXPATHLEN, string_buffer, &result);
00341   return GNativeString(string_buffer).getNative2UTF8();//MBCS cvt
00342 #else
00343 #error "Define something here for your operating system"
00344 #endif 
00345 }
00346 
00347 GUTF8String
00348 GOS::getenv(const GUTF8String &name)
00349 {
00350   GUTF8String retval;
00351   if(name.length())
00352   {
00353     const char *env=::getenv(name.getUTF82Native());
00354     if(env)
00355     {
00356       retval=GNativeString(env);
00357     }
00358   }
00359   return retval;
00360 }
00361 
00362 
00363 
00364 #ifdef HAVE_NAMESPACES
00365 }
00366 # ifndef NOT_USING_DJVU_NAMESPACE
00367 using namespace DJVU;
00368 # endif
00369 #endif
00370 

kviewshell

Skip menu "kviewshell"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  • kviewshell
Generated for API Reference by doxygen 1.5.9
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal