| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | #ifndef TCHAR |
|---|
| 19 | typedef char TCHAR; |
|---|
| 20 | #endif |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | #if defined(WIN32) && !defined(__CYGWIN__) |
|---|
| 28 | #include <io.h> |
|---|
| 29 | #define WINBASE_DECLARE_GET_MODULE_HANDLE_EX |
|---|
| 30 | #include <windows.h> |
|---|
| 31 | #include <winbase.h> |
|---|
| 32 | #include <sys/types.h> |
|---|
| 33 | #include <sys/stat.h> |
|---|
| 34 | #include <direct.h> |
|---|
| 35 | |
|---|
| 36 | #define mkdir(x,y) _mkdir((x)) |
|---|
| 37 | #define stat64 _stati64 |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | #ifndef F_OK |
|---|
| 41 | #define F_OK 4 |
|---|
| 42 | #endif |
|---|
| 43 | |
|---|
| 44 | #else // unix |
|---|
| 45 | |
|---|
| 46 | #if defined( __APPLE__ ) |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | #include <AvailabilityMacros.h> |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | #include "TargetConditionals.h" |
|---|
| 54 | |
|---|
| 55 | #if (TARGET_OS_IPHONE) |
|---|
| 56 | #include <Availability.h> |
|---|
| 57 | |
|---|
| 58 | #ifdef __IPHONE_4_0 && (__IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0) |
|---|
| 59 | #define stat64 stat |
|---|
| 60 | #else |
|---|
| 61 | #if !TARGET_IPHONE_SIMULATOR |
|---|
| 62 | #define stat64 stat |
|---|
| 63 | #endif |
|---|
| 64 | #endif |
|---|
| 65 | #endif |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | #if (MAC_OS_X_VERSION_MAX_ALLOWED <= 1040) |
|---|
| 77 | #define stat64 stat |
|---|
| 78 | #endif |
|---|
| 79 | #elif defined(__CYGWIN__) || defined(__FreeBSD__) || (defined(__hpux) && !defined(_LARGEFILE64_SOURCE)) |
|---|
| 80 | #define stat64 stat |
|---|
| 81 | #endif |
|---|
| 82 | |
|---|
| 83 | #include <stdlib.h> |
|---|
| 84 | #include <unistd.h> |
|---|
| 85 | #include <sys/types.h> |
|---|
| 86 | #include <sys/stat.h> |
|---|
| 87 | #endif |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | #if !defined(S_ISDIR) |
|---|
| 91 | # if defined( _S_IFDIR) && !defined( __S_IFDIR) |
|---|
| 92 | # define __S_IFDIR _S_IFDIR |
|---|
| 93 | # endif |
|---|
| 94 | # define S_ISDIR(mode) (mode&__S_IFDIR) |
|---|
| 95 | #endif |
|---|
| 96 | |
|---|
| 97 | #include <osg/Config> |
|---|
| 98 | #include <osgDB/ConvertUTF> |
|---|
| 99 | #include <osg/Notify> |
|---|
| 100 | |
|---|
| 101 | #include <osgDB/FileUtils> |
|---|
| 102 | #include <osgDB/FileNameUtils> |
|---|
| 103 | #include <osgDB/Registry> |
|---|
| 104 | |
|---|
| 105 | #include <errno.h> |
|---|
| 106 | #include <string.h> |
|---|
| 107 | |
|---|
| 108 | #include <stack> |
|---|
| 109 | |
|---|
| 110 | namespace osgDB |
|---|
| 111 | { |
|---|
| 112 | #ifdef OSG_USE_UTF8_FILENAME |
|---|
| 113 | #define OSGDB_STRING_TO_FILENAME(s) osgDB::convertUTF8toUTF16(s) |
|---|
| 114 | #define OSGDB_FILENAME_TO_STRING(s) osgDB::convertUTF16toUTF8(s) |
|---|
| 115 | #define OSGDB_FILENAME_TEXT(x) L ## x |
|---|
| 116 | #define OSGDB_WINDOWS_FUNCT(x) x ## W |
|---|
| 117 | #define OSGDB_WINDOWS_FUNCT_STRING(x) #x "W" |
|---|
| 118 | typedef wchar_t filenamechar; |
|---|
| 119 | typedef std::wstring filenamestring; |
|---|
| 120 | #else |
|---|
| 121 | #define OSGDB_STRING_TO_FILENAME(s) s |
|---|
| 122 | #define OSGDB_FILENAME_TO_STRING(s) s |
|---|
| 123 | #define OSGDB_FILENAME_TEXT(x) x |
|---|
| 124 | #define OSGDB_WINDOWS_FUNCT(x) x ## A |
|---|
| 125 | #define OSGDB_WINDOWS_FUNCT_STRING(x) #x "A" |
|---|
| 126 | typedef char filenamechar; |
|---|
| 127 | typedef std::string filenamestring; |
|---|
| 128 | #endif |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | FILE* osgDB::fopen(const char* filename, const char* mode) |
|---|
| 132 | { |
|---|
| 133 | #ifdef OSG_USE_UTF8_FILENAME |
|---|
| 134 | return ::_wfopen(convertUTF8toUTF16(filename).c_str(), convertUTF8toUTF16(mode).c_str()); |
|---|
| 135 | #else |
|---|
| 136 | return ::fopen(filename, mode); |
|---|
| 137 | #endif |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | bool osgDB::makeDirectory( const std::string &path ) |
|---|
| 141 | { |
|---|
| 142 | if (path.empty()) |
|---|
| 143 | { |
|---|
| 144 | OSG_DEBUG << "osgDB::makeDirectory(): cannot create an empty directory" << std::endl; |
|---|
| 145 | return false; |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | struct stat64 stbuf; |
|---|
| 149 | #ifdef OSG_USE_UTF8_FILENAME |
|---|
| 150 | if( _wstat64( OSGDB_STRING_TO_FILENAME(path).c_str(), &stbuf ) == 0 ) |
|---|
| 151 | #else |
|---|
| 152 | if( stat64( path.c_str(), &stbuf ) == 0 ) |
|---|
| 153 | #endif |
|---|
| 154 | { |
|---|
| 155 | if( S_ISDIR(stbuf.st_mode)) |
|---|
| 156 | return true; |
|---|
| 157 | else |
|---|
| 158 | { |
|---|
| 159 | OSG_DEBUG << "osgDB::makeDirectory(): " << |
|---|
| 160 | path << " already exists and is not a directory!" << std::endl; |
|---|
| 161 | return false; |
|---|
| 162 | } |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | std::string dir = path; |
|---|
| 166 | std::stack<std::string> paths; |
|---|
| 167 | while( true ) |
|---|
| 168 | { |
|---|
| 169 | if( dir.empty() ) |
|---|
| 170 | break; |
|---|
| 171 | |
|---|
| 172 | #ifdef OSG_USE_UTF8_FILENAME |
|---|
| 173 | if( _wstat64( OSGDB_STRING_TO_FILENAME(dir).c_str(), &stbuf ) < 0 ) |
|---|
| 174 | #else |
|---|
| 175 | if( stat64( dir.c_str(), &stbuf ) < 0 ) |
|---|
| 176 | #endif |
|---|
| 177 | { |
|---|
| 178 | switch( errno ) |
|---|
| 179 | { |
|---|
| 180 | case ENOENT: |
|---|
| 181 | case ENOTDIR: |
|---|
| 182 | paths.push( dir ); |
|---|
| 183 | break; |
|---|
| 184 | |
|---|
| 185 | default: |
|---|
| 186 | OSG_DEBUG << "osgDB::makeDirectory(): " << strerror(errno) << std::endl; |
|---|
| 187 | return false; |
|---|
| 188 | } |
|---|
| 189 | } |
|---|
| 190 | dir = getFilePath(std::string(dir)); |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | while( !paths.empty() ) |
|---|
| 194 | { |
|---|
| 195 | std::string dir = paths.top(); |
|---|
| 196 | |
|---|
| 197 | #if defined(WIN32) |
|---|
| 198 | |
|---|
| 199 | if (dir.size() == 2 && dir.c_str()[1] == ':') { |
|---|
| 200 | paths.pop(); |
|---|
| 201 | continue; |
|---|
| 202 | } |
|---|
| 203 | #endif |
|---|
| 204 | |
|---|
| 205 | #ifdef OSG_USE_UTF8_FILENAME |
|---|
| 206 | if ( _wmkdir(OSGDB_STRING_TO_FILENAME(dir).c_str())< 0 ) |
|---|
| 207 | #else |
|---|
| 208 | if( mkdir( dir.c_str(), 0755 )< 0 ) |
|---|
| 209 | #endif |
|---|
| 210 | { |
|---|
| 211 | OSG_DEBUG << "osgDB::makeDirectory(): " << strerror(errno) << std::endl; |
|---|
| 212 | return false; |
|---|
| 213 | } |
|---|
| 214 | paths.pop(); |
|---|
| 215 | } |
|---|
| 216 | return true; |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | bool osgDB::makeDirectoryForFile( const std::string &path ) |
|---|
| 220 | { |
|---|
| 221 | return makeDirectory( getFilePath( path )); |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | std::string osgDB::getCurrentWorkingDirectory( void ) |
|---|
| 226 | { |
|---|
| 227 | |
|---|
| 228 | #ifndef MAX_PATH |
|---|
| 229 | #define MAX_PATH 1024 |
|---|
| 230 | #endif |
|---|
| 231 | TCHAR rootdir[MAX_PATH]; |
|---|
| 232 | if(getcwd(rootdir,MAX_PATH-1)) |
|---|
| 233 | { |
|---|
| 234 | return(rootdir); |
|---|
| 235 | } |
|---|
| 236 | return(""); |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 241 | bool osgDB::setCurrentWorkingDirectory( const std::string &newCurrentWorkingDirectory ) |
|---|
| 242 | { |
|---|
| 243 | if (newCurrentWorkingDirectory.empty()) |
|---|
| 244 | { |
|---|
| 245 | OSG_DEBUG << "osgDB::setCurrentWorkingDirectory(): called with empty string." << std::endl; |
|---|
| 246 | return false; |
|---|
| 247 | } |
|---|
| 248 | |
|---|
| 249 | #ifdef OSG_USE_UTF8_FILENAME |
|---|
| 250 | return _wchdir( OSGDB_STRING_TO_FILENAME(newCurrentWorkingDirectory).c_str()) == 0; |
|---|
| 251 | #else |
|---|
| 252 | return chdir( newCurrentWorkingDirectory.c_str()) == 0; |
|---|
| 253 | #endif |
|---|
| 254 | |
|---|
| 255 | return true; |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | |
|---|
| 259 | |
|---|
| 260 | void osgDB::convertStringPathIntoFilePathList(const std::string& paths,FilePathList& filepath) |
|---|
| 261 | { |
|---|
| 262 | #if defined(WIN32) && !defined(__CYGWIN__) |
|---|
| 263 | char delimitor = ';'; |
|---|
| 264 | #else |
|---|
| 265 | char delimitor = ':'; |
|---|
| 266 | #endif |
|---|
| 267 | |
|---|
| 268 | if (!paths.empty()) |
|---|
| 269 | { |
|---|
| 270 | std::string::size_type start = 0; |
|---|
| 271 | std::string::size_type end; |
|---|
| 272 | while ((end = paths.find_first_of(delimitor,start))!=std::string::npos) |
|---|
| 273 | { |
|---|
| 274 | filepath.push_back(std::string(paths,start,end-start)); |
|---|
| 275 | start = end+1; |
|---|
| 276 | } |
|---|
| 277 | |
|---|
| 278 | std::string lastPath(paths,start,std::string::npos); |
|---|
| 279 | if (!lastPath.empty()) |
|---|
| 280 | filepath.push_back(lastPath); |
|---|
| 281 | } |
|---|
| 282 | |
|---|
| 283 | } |
|---|
| 284 | |
|---|
| 285 | bool osgDB::fileExists(const std::string& filename) |
|---|
| 286 | { |
|---|
| 287 | #ifdef OSG_USE_UTF8_FILENAME |
|---|
| 288 | return _waccess( OSGDB_STRING_TO_FILENAME(filename).c_str(), F_OK ) == 0; |
|---|
| 289 | #else |
|---|
| 290 | return access( filename.c_str(), F_OK ) == 0; |
|---|
| 291 | #endif |
|---|
| 292 | } |
|---|
| 293 | |
|---|
| 294 | osgDB::FileType osgDB::fileType(const std::string& filename) |
|---|
| 295 | { |
|---|
| 296 | struct stat64 fileStat; |
|---|
| 297 | #ifdef OSG_USE_UTF8_FILENAME |
|---|
| 298 | if ( _wstat64(OSGDB_STRING_TO_FILENAME(filename).c_str(), &fileStat) != 0 ) |
|---|
| 299 | #else |
|---|
| 300 | if ( stat64(filename.c_str(), &fileStat) != 0 ) |
|---|
| 301 | #endif |
|---|
| 302 | { |
|---|
| 303 | return FILE_NOT_FOUND; |
|---|
| 304 | } |
|---|
| 305 | |
|---|
| 306 | if ( fileStat.st_mode & S_IFDIR ) |
|---|
| 307 | return DIRECTORY; |
|---|
| 308 | else if ( fileStat.st_mode & S_IFREG ) |
|---|
| 309 | return REGULAR_FILE; |
|---|
| 310 | |
|---|
| 311 | return FILE_NOT_FOUND; |
|---|
| 312 | } |
|---|
| 313 | |
|---|
| 314 | std::string osgDB::findFileInPath(const std::string& filename, const FilePathList& filepath,CaseSensitivity caseSensitivity) |
|---|
| 315 | { |
|---|
| 316 | if (filename.empty()) |
|---|
| 317 | return filename; |
|---|
| 318 | |
|---|
| 319 | if (!isFileNameNativeStyle(filename)) |
|---|
| 320 | return findFileInPath(convertFileNameToNativeStyle(filename), filepath, caseSensitivity); |
|---|
| 321 | |
|---|
| 322 | |
|---|
| 323 | for(FilePathList::const_iterator itr=filepath.begin(); |
|---|
| 324 | itr!=filepath.end(); |
|---|
| 325 | ++itr) |
|---|
| 326 | { |
|---|
| 327 | OSG_DEBUG << "itr='" <<*itr<< "'\n"; |
|---|
| 328 | std::string path = itr->empty() ? filename : concatPaths(*itr, filename); |
|---|
| 329 | |
|---|
| 330 | path = getRealPath(path); |
|---|
| 331 | |
|---|
| 332 | OSG_DEBUG << "FindFileInPath() : trying " << path << " ...\n"; |
|---|
| 333 | if(fileExists(path)) |
|---|
| 334 | { |
|---|
| 335 | OSG_DEBUG << "FindFileInPath() : USING " << path << "\n"; |
|---|
| 336 | return path; |
|---|
| 337 | } |
|---|
| 338 | #ifndef WIN32 |
|---|
| 339 | |
|---|
| 340 | else if (caseSensitivity==CASE_INSENSITIVE) |
|---|
| 341 | { |
|---|
| 342 | std::string foundfile = findFileInDirectory(filename,*itr,CASE_INSENSITIVE); |
|---|
| 343 | if (!foundfile.empty()) return foundfile; |
|---|
| 344 | } |
|---|
| 345 | #endif |
|---|
| 346 | |
|---|
| 347 | } |
|---|
| 348 | |
|---|
| 349 | return std::string(); |
|---|
| 350 | } |
|---|
| 351 | |
|---|
| 352 | |
|---|
| 353 | std::string osgDB::findDataFile(const std::string& filename,CaseSensitivity caseSensitivity) |
|---|
| 354 | { |
|---|
| 355 | return findDataFile(filename,static_cast<Options*>(0),caseSensitivity); |
|---|
| 356 | } |
|---|
| 357 | |
|---|
| 358 | OSGDB_EXPORT std::string osgDB::findDataFile(const std::string& filename,const Options* options, CaseSensitivity caseSensitivity) |
|---|
| 359 | { |
|---|
| 360 | return Registry::instance()->findDataFile(filename, options, caseSensitivity); |
|---|
| 361 | } |
|---|
| 362 | |
|---|
| 363 | std::string osgDB::findLibraryFile(const std::string& filename,CaseSensitivity caseSensitivity) |
|---|
| 364 | { |
|---|
| 365 | return Registry::instance()->findLibraryFile(filename, osgDB::Registry::instance()->getOptions(), caseSensitivity); |
|---|
| 366 | } |
|---|
| 367 | |
|---|
| 368 | std::string osgDB::findFileInDirectory(const std::string& fileName,const std::string& dirName,CaseSensitivity caseSensitivity) |
|---|
| 369 | { |
|---|
| 370 | bool needFollowingBackslash = false; |
|---|
| 371 | bool needDirectoryName = true; |
|---|
| 372 | osgDB::DirectoryContents dc; |
|---|
| 373 | |
|---|
| 374 | std::string realDirName = dirName; |
|---|
| 375 | std::string realFileName = fileName; |
|---|
| 376 | |
|---|
| 377 | |
|---|
| 378 | #ifdef WIN32 |
|---|
| 379 | bool win32 = true; |
|---|
| 380 | #else |
|---|
| 381 | bool win32 = false; |
|---|
| 382 | #endif |
|---|
| 383 | |
|---|
| 384 | |
|---|
| 385 | |
|---|
| 386 | if (fileName != getSimpleFileName(fileName)) |
|---|
| 387 | { |
|---|
| 388 | |
|---|
| 389 | if (realDirName.empty()) |
|---|
| 390 | { |
|---|
| 391 | realDirName = getFilePath(fileName); |
|---|
| 392 | } |
|---|
| 393 | else if (realDirName=="." || realDirName=="./" || realDirName==".\\") |
|---|
| 394 | { |
|---|
| 395 | realDirName = "./" + getFilePath(fileName); |
|---|
| 396 | } |
|---|
| 397 | else |
|---|
| 398 | { |
|---|
| 399 | char lastChar = dirName[dirName.size()-1]; |
|---|
| 400 | if ((lastChar == '/') || (lastChar == '\\')) |
|---|
| 401 | realDirName = dirName + getFilePath(fileName); |
|---|
| 402 | else |
|---|
| 403 | realDirName = dirName + "/" + getFilePath(fileName); |
|---|
| 404 | } |
|---|
| 405 | |
|---|
| 406 | |
|---|
| 407 | realFileName = getSimpleFileName(fileName); |
|---|
| 408 | } |
|---|
| 409 | |
|---|
| 410 | OSG_DEBUG << "findFileInDirectory() : looking for " << realFileName << " in " << realDirName << "...\n"; |
|---|
| 411 | |
|---|
| 412 | if (realDirName.empty()) |
|---|
| 413 | { |
|---|
| 414 | dc = osgDB::getDirectoryContents("."); |
|---|
| 415 | needFollowingBackslash = false; |
|---|
| 416 | needDirectoryName = false; |
|---|
| 417 | } |
|---|
| 418 | else if (realDirName=="." || realDirName=="./" || realDirName==".\\") |
|---|
| 419 | { |
|---|
| 420 | dc = osgDB::getDirectoryContents("."); |
|---|
| 421 | needFollowingBackslash = false; |
|---|
| 422 | needDirectoryName = false; |
|---|
| 423 | } |
|---|
| 424 | else if (realDirName=="/") |
|---|
| 425 | { |
|---|
| 426 | dc = osgDB::getDirectoryContents("/"); |
|---|
| 427 | needFollowingBackslash = false; |
|---|
| 428 | needDirectoryName = true; |
|---|
| 429 | } |
|---|
| 430 | else |
|---|
| 431 | { |
|---|
| 432 | |
|---|
| 433 | |
|---|
| 434 | |
|---|
| 435 | if ((caseSensitivity == CASE_INSENSITIVE) && (!win32)) |
|---|
| 436 | { |
|---|
| 437 | |
|---|
| 438 | std::string parentPath = getFilePath(realDirName); |
|---|
| 439 | std::string lastElement = getSimpleFileName(realDirName); |
|---|
| 440 | |
|---|
| 441 | |
|---|
| 442 | if ((parentPath.empty()) && (!lastElement.empty())) |
|---|
| 443 | { |
|---|
| 444 | std::string directoryStringToUse = (realDirName[0]=='/' || realDirName[0]=='\\') ? std::string("/") : std::string("."); |
|---|
| 445 | |
|---|
| 446 | |
|---|
| 447 | |
|---|
| 448 | realDirName = findFileInDirectory(lastElement, directoryStringToUse, |
|---|
| 449 | CASE_INSENSITIVE); |
|---|
| 450 | |
|---|
| 451 | dc = osgDB::getDirectoryContents(realDirName); |
|---|
| 452 | needFollowingBackslash = true; |
|---|
| 453 | needDirectoryName = true; |
|---|
| 454 | } |
|---|
| 455 | else |
|---|
| 456 | { |
|---|
| 457 | |
|---|
| 458 | |
|---|
| 459 | realDirName = findFileInDirectory(lastElement, parentPath, |
|---|
| 460 | CASE_INSENSITIVE); |
|---|
| 461 | |
|---|
| 462 | dc = osgDB::getDirectoryContents(realDirName); |
|---|
| 463 | char lastChar = realDirName[realDirName.size()-1]; |
|---|
| 464 | if (lastChar=='/') needFollowingBackslash = false; |
|---|
| 465 | else if (lastChar=='\\') needFollowingBackslash = false; |
|---|
| 466 | else needFollowingBackslash = true; |
|---|
| 467 | needDirectoryName = true; |
|---|
| 468 | } |
|---|
| 469 | } |
|---|
| 470 | else |
|---|
| 471 | { |
|---|
| 472 | |
|---|
| 473 | dc = osgDB::getDirectoryContents(realDirName); |
|---|
| 474 | char lastChar = realDirName[realDirName.size()-1]; |
|---|
| 475 | if (lastChar=='/') needFollowingBackslash = false; |
|---|
| 476 | else if (lastChar=='\\') needFollowingBackslash = false; |
|---|
| 477 | else needFollowingBackslash = true; |
|---|
| 478 | needDirectoryName = true; |
|---|
| 479 | } |
|---|
| 480 | } |
|---|
| 481 | |
|---|
| 482 | for(osgDB::DirectoryContents::iterator itr=dc.begin(); |
|---|
| 483 | itr!=dc.end(); |
|---|
| 484 | ++itr) |
|---|
| 485 | { |
|---|
| 486 | if ((caseSensitivity==CASE_INSENSITIVE && osgDB::equalCaseInsensitive(realFileName,*itr)) || |
|---|
| 487 | (realFileName==*itr)) |
|---|
| 488 | { |
|---|
| 489 | if (!needDirectoryName) return *itr; |
|---|
| 490 | else if (needFollowingBackslash) return realDirName+'/'+*itr; |
|---|
| 491 | else return realDirName+*itr; |
|---|
| 492 | } |
|---|
| 493 | } |
|---|
| 494 | return ""; |
|---|
| 495 | } |
|---|
| 496 | |
|---|
| 497 | static void appendInstallationLibraryFilePaths(osgDB::FilePathList& filepath) |
|---|
| 498 | { |
|---|
| 499 | #ifdef OSG_DEFAULT_LIBRARY_PATH |
|---|
| 500 | |
|---|
| 501 | |
|---|
| 502 | filepath.push_back(ADDQUOTES(OSG_DEFAULT_LIBRARY_PATH)); |
|---|
| 503 | #endif |
|---|
| 504 | } |
|---|
| 505 | |
|---|
| 506 | #if defined(WIN32) && !defined(__CYGWIN__) |
|---|
| 507 | #include <io.h> |
|---|
| 508 | #include <direct.h> |
|---|
| 509 | |
|---|
| 510 | osgDB::DirectoryContents osgDB::getDirectoryContents(const std::string& dirName) |
|---|
| 511 | { |
|---|
| 512 | osgDB::DirectoryContents contents; |
|---|
| 513 | |
|---|
| 514 | OSGDB_WINDOWS_FUNCT(WIN32_FIND_DATA) data; |
|---|
| 515 | HANDLE handle = OSGDB_WINDOWS_FUNCT(FindFirstFile)((OSGDB_STRING_TO_FILENAME(dirName) + OSGDB_FILENAME_TEXT("\\*")).c_str(), &data); |
|---|
| 516 | if (handle != INVALID_HANDLE_VALUE) |
|---|
| 517 | { |
|---|
| 518 | do |
|---|
| 519 | { |
|---|
| 520 | contents.push_back(OSGDB_FILENAME_TO_STRING(data.cFileName)); |
|---|
| 521 | } |
|---|
| 522 | while (OSGDB_WINDOWS_FUNCT(FindNextFile)(handle, &data) != 0); |
|---|
| 523 | |
|---|
| 524 | FindClose(handle); |
|---|
| 525 | } |
|---|
| 526 | return contents; |
|---|
| 527 | } |
|---|
| 528 | |
|---|
| 529 | #else |
|---|
| 530 | |
|---|
| 531 | #include <dirent.h> |
|---|
| 532 | osgDB::DirectoryContents osgDB::getDirectoryContents(const std::string& dirName) |
|---|
| 533 | { |
|---|
| 534 | osgDB::DirectoryContents contents; |
|---|
| 535 | |
|---|
| 536 | DIR *handle = opendir(dirName.c_str()); |
|---|
| 537 | if (handle) |
|---|
| 538 | { |
|---|
| 539 | dirent *rc; |
|---|
| 540 | while((rc = readdir(handle))!=NULL) |
|---|
| 541 | { |
|---|
| 542 | contents.push_back(rc->d_name); |
|---|
| 543 | } |
|---|
| 544 | closedir(handle); |
|---|
| 545 | } |
|---|
| 546 | |
|---|
| 547 | return contents; |
|---|
| 548 | } |
|---|
| 549 | |
|---|
| 550 | #endif // unix getDirectoryContexts |
|---|
| 551 | |
|---|
| 552 | |
|---|
| 553 | osgDB::DirectoryContents osgDB::expandWildcardsInFilename(const std::string& filename) |
|---|
| 554 | { |
|---|
| 555 | osgDB::DirectoryContents contents; |
|---|
| 556 | |
|---|
| 557 | std::string dir = osgDB::getFilePath(filename); |
|---|
| 558 | std::string filenameOnly = dir.empty() ? filename : filename.substr(dir.length()+1, std::string::npos); |
|---|
| 559 | std::string left = filenameOnly.substr(0, filenameOnly.find('*')); |
|---|
| 560 | std::string right = filenameOnly.substr(filenameOnly.find('*')+1, std::string::npos); |
|---|
| 561 | |
|---|
| 562 | if (dir.empty()) |
|---|
| 563 | dir = osgDB::getCurrentWorkingDirectory(); |
|---|
| 564 | |
|---|
| 565 | osgDB::DirectoryContents dirContents = osgDB::getDirectoryContents(dir); |
|---|
| 566 | for (unsigned int i = 0; i < dirContents.size(); ++i) |
|---|
| 567 | { |
|---|
| 568 | std::string filenameInDir = dirContents[i]; |
|---|
| 569 | |
|---|
| 570 | if (filenameInDir == "." || |
|---|
| 571 | filenameInDir == "..") |
|---|
| 572 | { |
|---|
| 573 | continue; |
|---|
| 574 | } |
|---|
| 575 | |
|---|
| 576 | if ((filenameInDir.find(left) == 0 || left.empty()) && |
|---|
| 577 | (filenameInDir.find(right) == filenameInDir.length() - right.length() || right.empty())) |
|---|
| 578 | { |
|---|
| 579 | contents.push_back( dir + osgDB::getNativePathSeparator() + filenameInDir ); |
|---|
| 580 | } |
|---|
| 581 | } |
|---|
| 582 | |
|---|
| 583 | return contents; |
|---|
| 584 | } |
|---|
| 585 | |
|---|
| 586 | osgDB::FileOpResult::Value osgDB::copyFile(const std::string & source, const std::string & destination) |
|---|
| 587 | { |
|---|
| 588 | if (source.empty() || destination.empty()) |
|---|
| 589 | { |
|---|
| 590 | OSG_INFO << "copyFile(): Empty file name." << std::endl; |
|---|
| 591 | return FileOpResult::BAD_ARGUMENT; |
|---|
| 592 | } |
|---|
| 593 | |
|---|
| 594 | |
|---|
| 595 | if (source == destination || osgDB::getRealPath(source) == osgDB::getRealPath(destination)) |
|---|
| 596 | { |
|---|
| 597 | OSG_INFO << "copyFile(): Source and destination point to the same file: source=" << source << ", destination=" << destination << std::endl; |
|---|
| 598 | return FileOpResult::SOURCE_EQUALS_DESTINATION; |
|---|
| 599 | } |
|---|
| 600 | |
|---|
| 601 | |
|---|
| 602 | if (!osgDB::fileExists(source)) |
|---|
| 603 | { |
|---|
| 604 | OSG_INFO << "copyFile(): Source file does not exist: " << source << std::endl; |
|---|
| 605 | return FileOpResult::SOURCE_MISSING; |
|---|
| 606 | } |
|---|
| 607 | |
|---|
| 608 | |
|---|
| 609 | osgDB::ifstream fin(source.c_str(), std::ios::in | std::ios::binary); |
|---|
| 610 | if (!fin) |
|---|
| 611 | { |
|---|
| 612 | OSG_NOTICE << "copyFile(): Can't read source file: " << source << std::endl; |
|---|
| 613 | return FileOpResult::SOURCE_NOT_OPENED; |
|---|
| 614 | } |
|---|
| 615 | |
|---|
| 616 | |
|---|
| 617 | if (!osgDB::makeDirectoryForFile(destination)) |
|---|
| 618 | { |
|---|
| 619 | OSG_INFO << "Can't create directory for file '" << destination << "'. Copy may fail creating the file." << std::endl; |
|---|
| 620 | } |
|---|
| 621 | |
|---|
| 622 | |
|---|
| 623 | osgDB::ofstream fout(destination.c_str(), std::ios::out | std::ios::binary | std::ios::trunc); |
|---|
| 624 | if (!fout) |
|---|
| 625 | { |
|---|
| 626 | OSG_NOTICE << "copyFile(): Can't write destination file: " << destination << std::endl; |
|---|
| 627 | return FileOpResult::DESTINATION_NOT_OPENED; |
|---|
| 628 | } |
|---|
| 629 | |
|---|
| 630 | |
|---|
| 631 | const unsigned int BUFFER_SIZE = 10240; |
|---|
| 632 | osgDB::ifstream::char_type buffer[BUFFER_SIZE]; |
|---|
| 633 | for(; fin.good() && fout.good() && !fin.eof(); ) |
|---|
| 634 | { |
|---|
| 635 | fin.read(buffer, BUFFER_SIZE); |
|---|
| 636 | fout.write(buffer, fin.gcount()); |
|---|
| 637 | } |
|---|
| 638 | |
|---|
| 639 | if (!fout.good()) |
|---|
| 640 | { |
|---|
| 641 | OSG_NOTICE << "copyFile(): Error writing destination file: " << destination << std::endl; |
|---|
| 642 | return FileOpResult::WRITE_ERROR; |
|---|
| 643 | } |
|---|
| 644 | |
|---|
| 645 | if (!fin.eof()) |
|---|
| 646 | { |
|---|
| 647 | OSG_NOTICE << "copyFile(): Error reading source file: " << source << std::endl; |
|---|
| 648 | return FileOpResult::READ_ERROR; |
|---|
| 649 | } |
|---|
| 650 | |
|---|
| 651 | return FileOpResult::OK; |
|---|
| 652 | } |
|---|
| 653 | |
|---|
| 654 | |
|---|
| 655 | bool osgDB::containsCurrentWorkingDirectoryReference(const FilePathList& paths) |
|---|
| 656 | { |
|---|
| 657 | const std::string cwd("."); |
|---|
| 658 | for(FilePathList::const_iterator itr = paths.begin(); |
|---|
| 659 | itr != paths.end(); |
|---|
| 660 | ++itr) |
|---|
| 661 | { |
|---|
| 662 | if (itr->empty()) return true; |
|---|
| 663 | if (*itr==cwd) return true; |
|---|
| 664 | } |
|---|
| 665 | return false; |
|---|
| 666 | } |
|---|
| 667 | |
|---|
| 668 | |
|---|
| 669 | |
|---|
| 670 | |
|---|
| 671 | |
|---|
| 672 | #ifdef __sgi |
|---|
| 673 | |
|---|
| 674 | void osgDB::appendPlatformSpecificLibraryFilePaths(FilePathList& filepath) |
|---|
| 675 | { |
|---|
| 676 | convertStringPathIntoFilePathList("/usr/lib32/:/usr/local/lib32/",filepath); |
|---|
| 677 | |
|---|
| 678 | |
|---|
| 679 | char* ptr; |
|---|
| 680 | |
|---|
| 681 | #if (_MIPS_SIM == _MIPS_SIM_ABI32) |
|---|
| 682 | if( (ptr = getenv( "LD_LIBRARY_PATH" ))) |
|---|
| 683 | { |
|---|
| 684 | convertStringPathIntoFilePathList(ptr,filepath); |
|---|
| 685 | } |
|---|
| 686 | |
|---|
| 687 | #elif (_MIPS_SIM == _MIPS_SIM_NABI32) |
|---|
| 688 | |
|---|
| 689 | if( !(ptr = getenv( "LD_LIBRARYN32_PATH" ))) |
|---|
| 690 | ptr = getenv( "LD_LIBRARY_PATH" ); |
|---|
| 691 | |
|---|
| 692 | if( ptr ) |
|---|
| 693 | { |
|---|
| 694 | convertStringPathIntoFilePathList(ptr,filepath); |
|---|
| 695 | } |
|---|
| 696 | |
|---|
| 697 | #elif (_MIPS_SIM == _MIPS_SIM_ABI64) |
|---|
| 698 | |
|---|
| 699 | if( !(ptr = getenv( "LD_LIBRARY64_PATH" ))) |
|---|
| 700 | ptr = getenv( "LD_LIBRARY_PATH" ); |
|---|
| 701 | |
|---|
| 702 | if( ptr ) |
|---|
| 703 | { |
|---|
| 704 | convertStringPathIntoFilePathList(ptr,filepath); |
|---|
| 705 | } |
|---|
| 706 | #endif |
|---|
| 707 | |
|---|
| 708 | appendInstallationLibraryFilePaths(filepath); |
|---|
| 709 | } |
|---|
| 710 | |
|---|
| 711 | |
|---|
| 712 | #elif defined(__CYGWIN__) |
|---|
| 713 | |
|---|
| 714 | void osgDB::appendPlatformSpecificLibraryFilePaths(FilePathList& filepath) |
|---|
| 715 | { |
|---|
| 716 | char* ptr; |
|---|
| 717 | if ((ptr = getenv( "PATH" ))) |
|---|
| 718 | { |
|---|
| 719 | convertStringPathIntoFilePathList(ptr,filepath); |
|---|
| 720 | } |
|---|
| 721 | |
|---|
| 722 | appendInstallationLibraryFilePaths(filepath); |
|---|
| 723 | |
|---|
| 724 | convertStringPathIntoFilePathList("/usr/bin/:/usr/local/bin/",filepath); |
|---|
| 725 | } |
|---|
| 726 | |
|---|
| 727 | #elif defined(WIN32) |
|---|
| 728 | |
|---|
| 729 | void osgDB::appendPlatformSpecificLibraryFilePaths(FilePathList& filepath) |
|---|
| 730 | { |
|---|
| 731 | |
|---|
| 732 | |
|---|
| 733 | |
|---|
| 734 | |
|---|
| 735 | |
|---|
| 736 | |
|---|
| 737 | |
|---|
| 738 | |
|---|
| 739 | |
|---|
| 740 | |
|---|
| 741 | |
|---|
| 742 | |
|---|
| 743 | DWORD retval = 0; |
|---|
| 744 | const DWORD size = MAX_PATH; |
|---|
| 745 | filenamechar path[size]; |
|---|
| 746 | retval = OSGDB_WINDOWS_FUNCT(GetModuleFileName)(NULL, path, size); |
|---|
| 747 | if (retval != 0 && retval < size) |
|---|
| 748 | { |
|---|
| 749 | filenamestring pathstr(path); |
|---|
| 750 | filenamestring executableDir(pathstr, 0, |
|---|
| 751 | pathstr.find_last_of(OSGDB_FILENAME_TEXT("\\/"))); |
|---|
| 752 | convertStringPathIntoFilePathList(OSGDB_FILENAME_TO_STRING(executableDir), filepath); |
|---|
| 753 | } |
|---|
| 754 | else |
|---|
| 755 | { |
|---|
| 756 | OSG_WARN << "Could not get application directory " |
|---|
| 757 | "using Win32 API. It will not be searched." << std::endl; |
|---|
| 758 | } |
|---|
| 759 | |
|---|
| 760 | |
|---|
| 761 | |
|---|
| 762 | |
|---|
| 763 | #if defined(_MSC_VER) |
|---|
| 764 | |
|---|
| 765 | |
|---|
| 766 | |
|---|
| 767 | OSGDB_WINDOWS_FUNCT(PGET_MODULE_HANDLE_EX) pGetModuleHandleEx = reinterpret_cast<OSGDB_WINDOWS_FUNCT(PGET_MODULE_HANDLE_EX)> |
|---|
| 768 | (GetProcAddress( GetModuleHandleA("kernel32.dll"), OSGDB_WINDOWS_FUNCT_STRING(GetModuleHandleEx))); |
|---|
| 769 | if( pGetModuleHandleEx ) |
|---|
| 770 | { |
|---|
| 771 | HMODULE thisModule = 0; |
|---|
| 772 | static filenamechar static_variable = 0; |
|---|
| 773 | |
|---|
| 774 | if( pGetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, &static_variable, &thisModule) ) |
|---|
| 775 | { |
|---|
| 776 | retval = OSGDB_WINDOWS_FUNCT(GetModuleFileName)(thisModule, path, size); |
|---|
| 777 | if (retval != 0 && retval < size) |
|---|
| 778 | { |
|---|
| 779 | filenamestring pathstr(path); |
|---|
| 780 | filenamestring dllDir(pathstr, 0, |
|---|
| 781 | pathstr.find_last_of(OSGDB_FILENAME_TEXT("\\/"))); |
|---|
| 782 | convertStringPathIntoFilePathList(OSGDB_FILENAME_TO_STRING(dllDir), filepath); |
|---|
| 783 | } |
|---|
| 784 | else |
|---|
| 785 | { |
|---|
| 786 | OSG_WARN << "Could not get dll directory " |
|---|
| 787 | "using Win32 API. It will not be searched." << std::endl; |
|---|
| 788 | } |
|---|
| 789 | FreeLibrary(thisModule); |
|---|
| 790 | } |
|---|
| 791 | else |
|---|
| 792 | { |
|---|
| 793 | OSG_WARN << "Could not get dll module handle " |
|---|
| 794 | "using Win32 API. Dll directory will not be searched." << std::endl; |
|---|
| 795 | } |
|---|
| 796 | } |
|---|
| 797 | #endif |
|---|
| 798 | |
|---|
| 799 | |
|---|
| 800 | |
|---|
| 801 | filenamechar systemDir[(UINT)size]; |
|---|
| 802 | retval = OSGDB_WINDOWS_FUNCT(GetSystemDirectory)(systemDir, (UINT)size); |
|---|
| 803 | |
|---|
| 804 | if (retval != 0 && retval < size) |
|---|
| 805 | { |
|---|
| 806 | convertStringPathIntoFilePathList(OSGDB_FILENAME_TO_STRING(systemDir), |
|---|
| 807 | filepath); |
|---|
| 808 | } |
|---|
| 809 | else |
|---|
| 810 | { |
|---|
| 811 | OSG_WARN << "Could not get system directory using " |
|---|
| 812 | "Win32 API, using default directory." << std::endl; |
|---|
| 813 | convertStringPathIntoFilePathList("C:\\Windows\\System32", |
|---|
| 814 | filepath); |
|---|
| 815 | } |
|---|
| 816 | |
|---|
| 817 | |
|---|
| 818 | |
|---|
| 819 | |
|---|
| 820 | |
|---|
| 821 | filenamechar windowsDir[(UINT)size]; |
|---|
| 822 | retval = OSGDB_WINDOWS_FUNCT(GetWindowsDirectory)(windowsDir, (UINT)size); |
|---|
| 823 | if (retval != 0 && retval < size) |
|---|
| 824 | { |
|---|
| 825 | convertStringPathIntoFilePathList(std::string(OSGDB_FILENAME_TO_STRING(windowsDir)) + |
|---|
| 826 | "\\System", filepath); |
|---|
| 827 | convertStringPathIntoFilePathList(OSGDB_FILENAME_TO_STRING(windowsDir), |
|---|
| 828 | filepath); |
|---|
| 829 | } |
|---|
| 830 | else |
|---|
| 831 | { |
|---|
| 832 | OSG_WARN << "Could not get Windows directory using " |
|---|
| 833 | "Win32 API, using default directory." << std::endl; |
|---|
| 834 | convertStringPathIntoFilePathList("C:\\Windows", filepath); |
|---|
| 835 | convertStringPathIntoFilePathList("C:\\Windows\\System", filepath); |
|---|
| 836 | } |
|---|
| 837 | |
|---|
| 838 | |
|---|
| 839 | |
|---|
| 840 | convertStringPathIntoFilePathList(".", filepath); |
|---|
| 841 | |
|---|
| 842 | |
|---|
| 843 | |
|---|
| 844 | |
|---|
| 845 | filenamechar* ptr; |
|---|
| 846 | #ifdef OSG_USE_UTF8_FILENAME |
|---|
| 847 | if ((ptr = _wgetenv(OSGDB_FILENAME_TEXT("PATH")))) |
|---|
| 848 | #else |
|---|
| 849 | if ((ptr = getenv("PATH"))) |
|---|
| 850 | #endif |
|---|
| 851 | { |
|---|
| 852 | |
|---|
| 853 | |
|---|
| 854 | |
|---|
| 855 | |
|---|
| 856 | convertStringPathIntoFilePathList(OSGDB_FILENAME_TO_STRING(ptr), filepath); |
|---|
| 857 | } |
|---|
| 858 | |
|---|
| 859 | appendInstallationLibraryFilePaths(filepath); |
|---|
| 860 | } |
|---|
| 861 | |
|---|
| 862 | #elif defined(__APPLE__) |
|---|
| 863 | #if (TARGET_OS_IPHONE) |
|---|
| 864 | #define COMPILE_COCOA_VERSION |
|---|
| 865 | #else |
|---|
| 866 | #define COMPILE_CARBON_VERSION |
|---|
| 867 | #endif |
|---|
| 868 | |
|---|
| 869 | #ifdef COMPILE_COCOA_VERSION |
|---|
| 870 | #include <Foundation/Foundation.h> |
|---|
| 871 | #endif |
|---|
| 872 | #ifdef COMPILE_CARBON_VERSION |
|---|
| 873 | #include <CoreServices/CoreServices.h> |
|---|
| 874 | #include <CoreFoundation/CoreFoundation.h> |
|---|
| 875 | #include <Carbon/Carbon.h> |
|---|
| 876 | #endif |
|---|
| 877 | #include <iostream> |
|---|
| 878 | |
|---|
| 879 | |
|---|
| 880 | |
|---|
| 881 | std::string GetShortenedPath(std::string path, int numToShorten) |
|---|
| 882 | { |
|---|
| 883 | unsigned int i = path.length() - 1; |
|---|
| 884 | if(path[i] == '/') i--; |
|---|
| 885 | while(i > 1 && numToShorten) |
|---|
| 886 | { |
|---|
| 887 | if(path[i] == '/') |
|---|
| 888 | numToShorten--; |
|---|
| 889 | i--; |
|---|
| 890 | } |
|---|
| 891 | return path.substr(0,i + 1); |
|---|
| 892 | } |
|---|
| 893 | |
|---|
| 894 | |
|---|
| 895 | std::string GetPathFromCFURLRef(CFURLRef urlRef) |
|---|
| 896 | { |
|---|
| 897 | char buffer[1024]; |
|---|
| 898 | std::string path; |
|---|
| 899 | if(CFURLGetFileSystemRepresentation(urlRef, true, (UInt8*)buffer, 1024)) |
|---|
| 900 | path = std::string(buffer); |
|---|
| 901 | return path; |
|---|
| 902 | } |
|---|
| 903 | |
|---|
| 904 | |
|---|
| 905 | std::string GetApplicationBundlePath(CFBundleRef mainBundle) |
|---|
| 906 | { |
|---|
| 907 | std::string path; |
|---|
| 908 | CFURLRef urlRef = CFBundleCopyBundleURL(mainBundle); |
|---|
| 909 | if(urlRef) |
|---|
| 910 | { |
|---|
| 911 | path = GetPathFromCFURLRef(urlRef); |
|---|
| 912 | CFRelease(urlRef); |
|---|
| 913 | } |
|---|
| 914 | return path; |
|---|
| 915 | |
|---|
| 916 | } |
|---|
| 917 | |
|---|
| 918 | std::string GetApplicationParentPath(CFBundleRef mainBundle) |
|---|
| 919 | { |
|---|
| 920 | return GetShortenedPath(GetApplicationBundlePath(mainBundle), 1); |
|---|
| 921 | } |
|---|
| 922 | |
|---|
| 923 | std::string GetApplicationPluginsPath(CFBundleRef mainBundle) |
|---|
| 924 | { |
|---|
| 925 | std::string path; |
|---|
| 926 | CFURLRef urlRef = CFBundleCopyBuiltInPlugInsURL(mainBundle); |
|---|
| 927 | if(urlRef) |
|---|
| 928 | { |
|---|
| 929 | path = GetPathFromCFURLRef(urlRef); |
|---|
| 930 | CFRelease(urlRef); |
|---|
| 931 | } |
|---|
| 932 | return path; |
|---|
| 933 | |
|---|
| 934 | } |
|---|
| 935 | |
|---|
| 936 | std::string GetApplicationResourcesPath(CFBundleRef mainBundle) |
|---|
| 937 | { |
|---|
| 938 | std::string path; |
|---|
| 939 | CFURLRef urlRef = CFBundleCopyResourcesDirectoryURL(mainBundle); |
|---|
| 940 | if(urlRef) |
|---|
| 941 | { |
|---|
| 942 | path = GetPathFromCFURLRef(urlRef); |
|---|
| 943 | CFRelease(urlRef); |
|---|
| 944 | } |
|---|
| 945 | return path; |
|---|
| 946 | } |
|---|
| 947 | |
|---|
| 948 | |
|---|
| 949 | |
|---|
| 950 | |
|---|
| 951 | |
|---|
| 952 | |
|---|
| 953 | |
|---|
| 954 | |
|---|
| 955 | |
|---|
| 956 | |
|---|
| 957 | |
|---|
| 958 | |
|---|
| 959 | |
|---|
| 960 | |
|---|
| 961 | |
|---|
| 962 | |
|---|
| 963 | |
|---|
| 964 | #ifdef COMPILE_COCOA_VERSION |
|---|
| 965 | |
|---|
| 966 | |
|---|
| 967 | |
|---|
| 968 | |
|---|
| 969 | |
|---|
| 970 | |
|---|
| 971 | |
|---|
| 972 | |
|---|
| 973 | |
|---|
| 974 | |
|---|
| 975 | |
|---|
| 976 | |
|---|
| 977 | |
|---|
| 978 | |
|---|
| 979 | |
|---|
| 980 | |
|---|
| 981 | |
|---|
| 982 | |
|---|
| 983 | |
|---|
| 984 | |
|---|
| 985 | |
|---|
| 986 | |
|---|
| 987 | |
|---|
| 988 | |
|---|
| 989 | |
|---|
| 990 | |
|---|
| 991 | |
|---|
| 992 | void osgDB::appendPlatformSpecificLibraryFilePaths(FilePathList& filepath) |
|---|
| 993 | { |
|---|
| 994 | char* ptr; |
|---|
| 995 | if ((ptr = getenv( "DYLD_LIBRARY_PATH" )) ) |
|---|
| 996 | { |
|---|
| 997 | convertStringPathIntoFilePathList(ptr, filepath); |
|---|
| 998 | } |
|---|
| 999 | |
|---|
| 1000 | appendInstallationLibraryFilePaths(filepath); |
|---|
| 1001 | |
|---|
| 1002 | |
|---|
| 1003 | |
|---|
| 1004 | |
|---|
| 1005 | |
|---|
| 1006 | |
|---|
| 1007 | |
|---|
| 1008 | NSAutoreleasePool* mypool = [[NSAutoreleasePool alloc] init]; |
|---|
| 1009 | |
|---|
| 1010 | NSString* myBundlePlugInPath; |
|---|
| 1011 | NSString* userSupportDir; |
|---|
| 1012 | |
|---|
| 1013 | |
|---|
| 1014 | |
|---|
| 1015 | |
|---|
| 1016 | myBundlePlugInPath = [[NSBundle mainBundle] builtInPlugInsPath]; |
|---|
| 1017 | |
|---|
| 1018 | |
|---|
| 1019 | |
|---|
| 1020 | |
|---|
| 1021 | |
|---|
| 1022 | userSupportDir = [@"~/Library/Application Support/OpenSceneGraph/PlugIns" stringByExpandingTildeInPath]; |
|---|
| 1023 | |
|---|
| 1024 | |
|---|
| 1025 | |
|---|
| 1026 | |
|---|
| 1027 | |
|---|
| 1028 | |
|---|
| 1029 | filepath.push_back( [myBundlePlugInPath UTF8String] ); |
|---|
| 1030 | filepath.push_back( [userSupportDir UTF8String] ); |
|---|
| 1031 | |
|---|
| 1032 | filepath.push_back( "/Library/Application Support/OpenSceneGraph/PlugIns" ); |
|---|
| 1033 | filepath.push_back( "/Network/Library/Application Support/OpenSceneGraph/PlugIns" ); |
|---|
| 1034 | |
|---|
| 1035 | |
|---|
| 1036 | [mypool release]; |
|---|
| 1037 | } |
|---|
| 1038 | |
|---|
| 1039 | #elif defined(COMPILE_CARBON_VERSION) |
|---|
| 1040 | |
|---|
| 1041 | |
|---|
| 1042 | |
|---|
| 1043 | |
|---|
| 1044 | |
|---|
| 1045 | |
|---|
| 1046 | |
|---|
| 1047 | |
|---|
| 1048 | |
|---|
| 1049 | |
|---|
| 1050 | |
|---|
| 1051 | |
|---|
| 1052 | |
|---|
| 1053 | |
|---|
| 1054 | |
|---|
| 1055 | |
|---|
| 1056 | |
|---|
| 1057 | |
|---|
| 1058 | |
|---|
| 1059 | |
|---|
| 1060 | |
|---|
| 1061 | |
|---|
| 1062 | |
|---|
| 1063 | |
|---|
| 1064 | void osgDB::appendPlatformSpecificLibraryFilePaths(FilePathList& filepath) |
|---|
| 1065 | { |
|---|
| 1066 | char* ptr; |
|---|
| 1067 | if ((ptr = getenv( "DYLD_LIBRARY_PATH" )) ) |
|---|
| 1068 | { |
|---|
| 1069 | convertStringPathIntoFilePathList(ptr, filepath); |
|---|
| 1070 | } |
|---|
| 1071 | |
|---|
| 1072 | appendInstallationLibraryFilePaths(filepath); |
|---|
| 1073 | |
|---|
| 1074 | const std::string OSG_PLUGIN_PATH("/OpenSceneGraph/PlugIns"); |
|---|
| 1075 | CFURLRef url; |
|---|
| 1076 | CFBundleRef myBundle; |
|---|
| 1077 | FSRef f; |
|---|
| 1078 | OSErr errCode; |
|---|
| 1079 | |
|---|
| 1080 | |
|---|
| 1081 | |
|---|
| 1082 | |
|---|
| 1083 | |
|---|
| 1084 | myBundle = CFBundleGetMainBundle(); |
|---|
| 1085 | |
|---|
| 1086 | if(myBundle != NULL) |
|---|
| 1087 | { |
|---|
| 1088 | |
|---|
| 1089 | |
|---|
| 1090 | |
|---|
| 1091 | |
|---|
| 1092 | std::string bundlePath = GetApplicationBundlePath(myBundle); |
|---|
| 1093 | if( bundlePath.substr(bundlePath.length() - 4, 4) == std::string(".app") ) |
|---|
| 1094 | filepath.push_back(GetApplicationPluginsPath(myBundle)); |
|---|
| 1095 | } |
|---|
| 1096 | else |
|---|
| 1097 | { |
|---|
| 1098 | OSG_NOTIFY( osg::DEBUG_INFO ) << "Couldn't find the Application Bundle" << std::endl; |
|---|
| 1099 | } |
|---|
| 1100 | |
|---|
| 1101 | |
|---|
| 1102 | errCode = FSFindFolder( kUserDomain, kApplicationSupportFolderType, kDontCreateFolder, &f ); |
|---|
| 1103 | if(noErr == errCode) |
|---|
| 1104 | { |
|---|
| 1105 | |
|---|
| 1106 | url = CFURLCreateFromFSRef( 0, &f ); |
|---|
| 1107 | if(url) |
|---|
| 1108 | { |
|---|
| 1109 | filepath.push_back(GetPathFromCFURLRef(url) + OSG_PLUGIN_PATH); |
|---|
| 1110 | CFRelease( url ); |
|---|
| 1111 | } |
|---|
| 1112 | else |
|---|
| 1113 | OSG_NOTIFY( osg::DEBUG_INFO ) << "Couldn't create CFURLRef for User's application support Path" << std::endl; |
|---|
| 1114 | |
|---|
| 1115 | url = NULL; |
|---|
| 1116 | } |
|---|
| 1117 | else |
|---|
| 1118 | { |
|---|
| 1119 | OSG_NOTIFY( osg::DEBUG_INFO ) << "Couldn't find the User's Application Support Path" << std::endl; |
|---|
| 1120 | } |
|---|
| 1121 | |
|---|
| 1122 | |
|---|
| 1123 | errCode = FSFindFolder( kLocalDomain, kApplicationSupportFolderType, kDontCreateFolder, &f ); |
|---|
| 1124 | if(noErr == errCode) |
|---|
| 1125 | { |
|---|
| 1126 | |
|---|
| 1127 | url = CFURLCreateFromFSRef( 0, &f ); |
|---|
| 1128 | |
|---|
| 1129 | if(url) |
|---|
| 1130 | { |
|---|
| 1131 | filepath.push_back(GetPathFromCFURLRef(url) + OSG_PLUGIN_PATH); |
|---|
| 1132 | CFRelease( url ); |
|---|
| 1133 | } |
|---|
| 1134 | else |
|---|
| 1135 | OSG_NOTIFY( osg::DEBUG_INFO ) << "Couldn't create CFURLRef for local System's ApplicationSupport Path" << std::endl; |
|---|
| 1136 | |
|---|
| 1137 | url = NULL; |
|---|
| 1138 | } |
|---|
| 1139 | else |
|---|
| 1140 | { |
|---|
| 1141 | OSG_NOTIFY( osg::DEBUG_INFO ) << "Couldn't find the Local System's Application Support Path" << std::endl; |
|---|
| 1142 | } |
|---|
| 1143 | |
|---|
| 1144 | |
|---|
| 1145 | |
|---|
| 1146 | |
|---|
| 1147 | errCode = FSFindFolder( kNetworkDomain, kApplicationSupportFolderType, kDontCreateFolder, &f ); |
|---|
| 1148 | if(noErr == errCode) |
|---|
| 1149 | { |
|---|
| 1150 | |
|---|
| 1151 | url = CFURLCreateFromFSRef( 0, &f ); |
|---|
| 1152 | |
|---|
| 1153 | if(url) |
|---|
| 1154 | { |
|---|
| 1155 | filepath.push_back(GetPathFromCFURLRef(url) + OSG_PLUGIN_PATH); |
|---|
| 1156 | CFRelease( url ); |
|---|
| 1157 | } |
|---|
| 1158 | else |
|---|
| 1159 | OSG_NOTIFY( osg::DEBUG_INFO ) << "Couldn't create CFURLRef for network Application Support Path" << std::endl; |
|---|
| 1160 | |
|---|
| 1161 | url = NULL; |
|---|
| 1162 | } |
|---|
| 1163 | else |
|---|
| 1164 | { |
|---|
| 1165 | |
|---|
| 1166 | |
|---|
| 1167 | } |
|---|
| 1168 | } |
|---|
| 1169 | #else |
|---|
| 1170 | void osgDB::appendPlatformSpecificLibraryFilePaths(FilePathList& filepath) |
|---|
| 1171 | { |
|---|
| 1172 | char* ptr; |
|---|
| 1173 | if ((ptr = getenv( "DYLD_LIBRARY_PATH" )) ) |
|---|
| 1174 | { |
|---|
| 1175 | convertStringPathIntoFilePathList(ptr, filepath); |
|---|
| 1176 | } |
|---|
| 1177 | |
|---|
| 1178 | appendInstallationLibraryFilePaths(filepath); |
|---|
| 1179 | } |
|---|
| 1180 | #endif |
|---|
| 1181 | |
|---|
| 1182 | #else |
|---|
| 1183 | |
|---|
| 1184 | void osgDB::appendPlatformSpecificLibraryFilePaths(FilePathList& filepath) |
|---|
| 1185 | { |
|---|
| 1186 | |
|---|
| 1187 | char* ptr; |
|---|
| 1188 | if( (ptr = getenv( "LD_LIBRARY_PATH" )) ) |
|---|
| 1189 | { |
|---|
| 1190 | convertStringPathIntoFilePathList(ptr,filepath); |
|---|
| 1191 | } |
|---|
| 1192 | |
|---|
| 1193 | appendInstallationLibraryFilePaths(filepath); |
|---|
| 1194 | |
|---|
| 1195 | #if defined(__ia64__) || defined(__x86_64__) |
|---|
| 1196 | convertStringPathIntoFilePathList("/usr/lib/:/usr/lib64/:/usr/local/lib/:/usr/local/lib64/",filepath); |
|---|
| 1197 | #else |
|---|
| 1198 | convertStringPathIntoFilePathList("/usr/lib/:/usr/local/lib/",filepath); |
|---|
| 1199 | #endif |
|---|
| 1200 | |
|---|
| 1201 | } |
|---|
| 1202 | |
|---|
| 1203 | #endif |
|---|
| 1204 | |
|---|
| 1205 | |
|---|
| 1206 | |
|---|
| 1207 | |
|---|
| 1208 | #ifdef __APPLE__ |
|---|
| 1209 | void osgDB::appendPlatformSpecificResourceFilePaths(FilePathList& filepath) |
|---|
| 1210 | { |
|---|
| 1211 | |
|---|
| 1212 | CFBundleRef mainBundle = CFBundleGetMainBundle(); |
|---|
| 1213 | |
|---|
| 1214 | if (mainBundle != NULL) { |
|---|
| 1215 | |
|---|
| 1216 | std::string bundlePath = GetApplicationBundlePath(mainBundle); |
|---|
| 1217 | std::string resourcesPath = GetApplicationResourcesPath(mainBundle); |
|---|
| 1218 | |
|---|
| 1219 | |
|---|
| 1220 | if(bundlePath.substr(bundlePath.length() - 4, 4) == std::string(".app")) |
|---|
| 1221 | { |
|---|
| 1222 | if(resourcesPath != std::string("")) |
|---|
| 1223 | filepath.push_back( resourcesPath ); |
|---|
| 1224 | |
|---|
| 1225 | std::string parentPath = GetShortenedPath(bundlePath, 1); |
|---|
| 1226 | if(parentPath != std::string("")) |
|---|
| 1227 | filepath.push_back( parentPath ); |
|---|
| 1228 | } |
|---|
| 1229 | } |
|---|
| 1230 | else |
|---|
| 1231 | { |
|---|
| 1232 | OSG_NOTIFY( osg::DEBUG_INFO ) << "Couldn't find the Application Bundle." << std::endl; |
|---|
| 1233 | } |
|---|
| 1234 | } |
|---|
| 1235 | #else |
|---|
| 1236 | void osgDB::appendPlatformSpecificResourceFilePaths(FilePathList& ) |
|---|
| 1237 | { |
|---|
| 1238 | } |
|---|
| 1239 | #endif |
|---|
| 1240 | |
|---|