kviewshell
GOS.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
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
00104 #undef TRUE
00105 #undef FALSE
00106 #define TRUE 1
00107 #define FALSE 0
00108
00109
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
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
00179
00180
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
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
00210 GUTF8String retval(gfname,finddirsep(gfname)+1,(unsigned int)(-1));
00211 fname=retval;
00212
00213
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
00239
00240
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
00255
00256
00257
00258
00259
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
00284
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
00315
00316
00317
00318
00319
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)
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();
00332 #elif defined (WIN32)
00333 char drv[2];
00334 if (dirname.length() && _chdir(dirname.getUTF82Native())==-1)
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();
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