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 #include <stdlib.h>
00040 #include <stdio.h>
00041
00042 #include "OSGConfig.h"
00043
00044 #include <iostream>
00045
00046 #include <OSGBaseFunctions.h>
00047
00048 OSG_BEGIN_NAMESPACE
00049
00050
00051 #if defined(__sgi)
00052
00053 const UInt32 OSGDIRFLAG = IFDIR;
00054 const UInt32 OSGREADFLAG = IREAD;
00055 const UInt32 OSGWRITEFLAG = IWRITE;
00056
00057 #elif defined(__linux) || defined(darwin) || defined(__hpux) || defined(__sun)
00058
00059 const UInt32 OSGDIRFLAG = S_IFDIR;
00060 const UInt32 OSGREADFLAG = S_IRUSR;
00061 const UInt32 OSGWRITEFLAG = S_IWUSR;
00062
00063 #elif defined(WIN32)
00064
00065 const UInt32 OSGDIRFLAG = _S_IFDIR;
00066 const UInt32 OSGREADFLAG = _S_IREAD;
00067 const UInt32 OSGWRITEFLAG = _S_IWRITE;
00068
00069 #else
00070 #error "Could not find your system, check your system/compiler combination"
00071 #endif
00072
00077 namespace File
00078 {
00083 inline
00084 bool tstAttr(const Char8 *szFilename,
00085 UInt32 uiAccessFlags)
00086 {
00087 bool returnValue = false;
00088 Int32 rc = 0;
00089
00090 #ifdef WIN32
00091 struct _stat statBuffer;
00092 #else
00093 struct stat statBuffer;
00094 #endif
00095
00096 if(szFilename != NULL)
00097 {
00098 #ifdef WIN32
00099 rc = _stat(szFilename, &statBuffer);
00100 #else
00101 rc = stat(szFilename, &statBuffer);
00102 #endif
00103 if(rc == 0 && ! (statBuffer.st_mode & OSGDIRFLAG))
00104 {
00105 if(uiAccessFlags & AccessFlags::IsReadable)
00106 {
00107 if(statBuffer.st_mode & OSGREADFLAG)
00108 {
00109 returnValue = true;
00110 }
00111 }
00112
00113 if(uiAccessFlags & AccessFlags::IsWriteable)
00114 {
00115 if(statBuffer.st_mode & OSGWRITEFLAG)
00116 {
00117 returnValue = true;
00118 }
00119 }
00120 }
00121 }
00122
00123 return returnValue;
00124 }
00125
00127 }
00128
00133 namespace Directory
00134 {
00140 inline
00141 bool tstAttr(const Char8 *szDirname,
00142 UInt32 uiAccessFlags)
00143 {
00144 bool returnValue = false;
00145 Int32 rc = 0;
00146
00147 #ifdef WIN32
00148 struct _stat statBuffer;
00149 #else
00150 struct stat statBuffer;
00151 #endif
00152
00153 if(szDirname != NULL)
00154 {
00155 #ifdef WIN32
00156 rc = _stat(szDirname, &statBuffer);
00157 #else
00158 rc = stat(szDirname, &statBuffer);
00159 #endif
00160
00161 if(rc == 0 && (statBuffer.st_mode & OSGDIRFLAG))
00162 {
00163 if(uiAccessFlags & AccessFlags::IsReadable)
00164 {
00165 if(statBuffer.st_mode & OSGREADFLAG)
00166 {
00167 returnValue = true;
00168 }
00169 else
00170 {
00171 returnValue = false;
00172 }
00173 }
00174
00175 if(uiAccessFlags & AccessFlags::IsWriteable)
00176 {
00177 if(statBuffer.st_mode & OSGWRITEFLAG)
00178 {
00179 returnValue = true;
00180 }
00181 else
00182 {
00183 returnValue = false;
00184 }
00185 }
00186 }
00187 }
00188
00189 return returnValue;
00190 }
00191
00192 #ifdef __sgi
00193 #pragma set woff 1209
00194 #endif
00195
00199 inline
00200 Char8 *getCurrent(void)
00201 {
00202 UInt32 uiCurrentNameSize = 256;
00203
00204 Char8 *returnValue = new Char8[uiCurrentNameSize];
00205 Char8 *szTmpBuf;
00206
00207 while(1)
00208 {
00209 #ifndef WIN32
00210 szTmpBuf = getcwd(returnValue, uiCurrentNameSize);
00211 #else
00212 szTmpBuf = _getcwd(returnValue, uiCurrentNameSize);
00213 #endif
00214 if(szTmpBuf != NULL)
00215 break;
00216
00217 uiCurrentNameSize *= 2;
00218 delete [] returnValue;
00219
00220 returnValue = new Char8[uiCurrentNameSize];
00221 }
00222
00223 return returnValue;
00224 }
00225
00226 #ifdef __sgi
00227 #pragma reset woff 1209
00228 #endif
00229
00230 inline
00231 bool setCurrent(const Char8 *szDirname)
00232 {
00233 bool returnValue = false;
00234
00235 if(szDirname != NULL)
00236 {
00237 #ifndef WIN32
00238 if(chdir(szDirname) == 0)
00239 returnValue = true;
00240 #else
00241 if(_chdir(szDirname) == 0)
00242 returnValue = true;
00243 #endif
00244 }
00245
00246 return returnValue;
00247 }
00248
00252 inline
00253 std::vector<Char8 *> *getEntries(const Char8 *szDirname,
00254 const Char8 *szPattern)
00255 {
00256 std::vector<Char8 *> *returnValue = NULL;
00257
00258 if(szDirname != NULL)
00259 {
00260 if(tstAttr(szDirname, AccessFlags::IsReadable) == true)
00261 {
00262 #ifndef WIN32
00263 DIR *pDir = opendir(szDirname);
00264 dirent *pDirEntry = NULL;
00265 Char8 *szEntryName = NULL;
00266
00267 if(pDir != NULL)
00268 {
00269 returnValue = new std::vector<Char8 *>;
00270
00271 do
00272 {
00273 pDirEntry = readdir(pDir);
00274
00275 if(pDirEntry != NULL)
00276 {
00277 stringDup(pDirEntry->d_name, szEntryName);
00278
00279 if(szPattern != NULL)
00280 {
00281 if(fnmatch(szPattern, szEntryName, 0) == 0)
00282 {
00283 returnValue->push_back(szEntryName);
00284 }
00285 else
00286 {
00287 delete [] szEntryName;
00288 }
00289 }
00290 else
00291 {
00292 returnValue->push_back(szEntryName);
00293 }
00294
00295 szEntryName = NULL;
00296 }
00297 }
00298 while(pDirEntry != NULL);
00299
00300 closedir(pDir);
00301 }
00302 #else
00303 Char8 *szTmpDirname = NULL;
00304
00305 bool bVal;
00306 WIN32_FIND_DATA pDirEntry;
00307 HANDLE pDir;
00308 Char8 *szEntryName = NULL;
00309
00310 if(szPattern == NULL)
00311 {
00312 szTmpDirname = new Char8[stringlen(szDirname) + 5];
00313
00314 sprintf(szTmpDirname, "%s\\*", szDirname);
00315 }
00316 else
00317 {
00318 szTmpDirname = new Char8[stringlen(szDirname) +
00319 stringlen(szPattern) +
00320 5];
00321
00322 sprintf(szTmpDirname, "%s\\%s", szDirname, szPattern);
00323 }
00324
00325 pDir = FindFirstFile(szTmpDirname, &pDirEntry);
00326
00327 #ifdef OSG_WIN32_ICL
00328 #pragma warning (disable : 171)
00329 #endif
00330
00331 if(INVALID_HANDLE_VALUE != pDir)
00332 {
00333 returnValue = new std::vector<Char8 *>;
00334
00335 do
00336 {
00337 stringDup(pDirEntry.cFileName, szEntryName);
00338
00339 returnValue->push_back(szEntryName);
00340 szEntryName = NULL;
00341
00342 bVal = (FindNextFile(pDir, &pDirEntry) != FALSE);
00343 }
00344 while(bVal == true);
00345
00346 FindClose(pDir);
00347 }
00348
00349 #ifdef OSG_WIN32_ICL
00350 #pragma warning (error : 171)
00351 #endif
00352
00353 delete szTmpDirname;
00354 #endif
00355 }
00356 }
00357
00358 return returnValue;
00359 }
00360
00362 }
00363
00364 namespace Path
00365 {
00366 inline
00367 static void fixWinNetworkPath(std::string &path)
00368 {
00369 #ifdef WIN32
00370
00371
00372
00373 if((path.length() > 2 ) &&
00374 (path[0] == '\\') &&
00375 (path[1] == '\\') )
00376 {
00377 for(Int32 i = 0; i < path.length(); ++i)
00378 {
00379 if(path[i] == '\\')
00380 {
00381 path[i] = '/';
00382 }
00383 }
00384 }
00385 #endif
00386 }
00387 }
00388
00389 #define OSGFILESYSTEM_INLINE_CVSID "@(#)$Id: $"
00390
00391 OSG_END_NAMESPACE