platformFileIO.cpp
Engine/source/platform/platformFileIO.cpp
Public Typedefs
Vector< char * >
CharVector
Public Variables
char
filePathBuffer [1024]
gPlatformDirectoryExcludeList (__FILE__, __LINE__)
Public Functions
DefineEngineFunction(getTemporaryDirectory , const char * , () , "@brief Returns the OS temporary directory, \"C:/Users/Mich/AppData/Local/Temp\" for <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">example\n\n</a>" "@note This can be useful <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> adhering <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> OS standards and practices, " "but not really used in Torque 3D right <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">now.\n</a>" " @note Be very careful when getting into OS level <a href="/coding/class/classfile/">File</a> I/O." " @return <a href="/coding/class/classstring/">String</a> containing path <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> OS temp <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">directory\n</a>" " @note This is legacy function brought over from TGB, and does not appear " "<a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> have much use. Possibly deprecate?\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" " @ingroup <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">FileSystem\n</a>" " @internal" )
DefineEngineFunction(getTemporaryFileName , const char * , () , "@brief Creates <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> name and extension <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> potential temporary <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">file\n\n</a>" "This does not create the actual file. It simply creates <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> random name " "<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a702945180aa732857b380a007a7e2a21">file</a> that does not <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">exist.\n\n</a>" "@note This is legacy function brought over from TGB, and does not appear " "<a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> have much use. Possibly deprecate?\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" " @ingroup <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">FileSystem\n</a>" " @internal" )
DefineEngineFunction(getUserDataDirectory , const char * , () , "getUserDataDirectory()" )
DefineEngineFunction(getUserHomeDirectory , const char * , () , "getUserHomeDirectory()" )
DefineEngineFunction(setMainDotCsDir , void , (const char *path) , "setMainDotCsDir()" )
bool
deleteDirectoryRecusrive(const char * pPath)
makeCleanPathInPlace(char * path)
tryStripBasePath(const char * path, const char * base)
Detailed Description
Public Typedefs
typedef Vector< char * > CharVector
Public Variables
char filePathBuffer [1024]
CharVector gPlatformDirectoryExcludeList (__FILE__, __LINE__)
StringTableEntry sgMainCSDir
Public Functions
catPath(char * dst, const char * src, U32 len)
DefineEngineFunction(getTemporaryDirectory , const char * , () , "@brief Returns the OS temporary directory, \"C:/Users/Mich/AppData/Local/Temp\" for <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">example\n\n</a>" "@note This can be useful <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> adhering <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> OS standards and practices, " "but not really used in Torque 3D right <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">now.\n</a>" " @note Be very careful when getting into OS level <a href="/coding/class/classfile/">File</a> I/O." " @return <a href="/coding/class/classstring/">String</a> containing path <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> OS temp <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">directory\n</a>" " @note This is legacy function brought over from TGB, and does not appear " "<a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> have much use. Possibly deprecate?\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" " @ingroup <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">FileSystem\n</a>" " @internal" )
DefineEngineFunction(getTemporaryFileName , const char * , () , "@brief Creates <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> name and extension <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> potential temporary <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">file\n\n</a>" "This does not create the actual file. It simply creates <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> random name " "<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a702945180aa732857b380a007a7e2a21">file</a> that does not <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">exist.\n\n</a>" "@note This is legacy function brought over from TGB, and does not appear " "<a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> have much use. Possibly deprecate?\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" " @ingroup <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">FileSystem\n</a>" " @internal" )
DefineEngineFunction(getUserDataDirectory , const char * , () , "getUserDataDirectory()" )
DefineEngineFunction(getUserHomeDirectory , const char * , () , "getUserHomeDirectory()" )
DefineEngineFunction(setMainDotCsDir , void , (const char *path) , "setMainDotCsDir()" )
deleteDirectoryRecusrive(const char * pPath)
makeCleanPathInPlace(char * path)
tryStripBasePath(const char * path, const char * base)
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#include "core/strings/stringFunctions.h" 25#include "util/tempAlloc.h" 26#include "console/console.h" 27#include "console/engineAPI.h" 28#include "core/stringTable.h" 29 30//----------------------------------------------------------------------------- 31 32StringTableEntry Platform::getTemporaryDirectory() 33{ 34 StringTableEntry path = osGetTemporaryDirectory(); 35 36 if(! Platform::isDirectory(path)) 37 path = Platform::getCurrentDirectory(); 38 39 return path; 40} 41 42DefineEngineFunction( getTemporaryDirectory, const char *, (), , 43 "@brief Returns the OS temporary directory, \"C:/Users/Mich/AppData/Local/Temp\" for example\n\n" 44 "@note This can be useful to adhering to OS standards and practices, " 45 "but not really used in Torque 3D right now.\n" 46 "@note Be very careful when getting into OS level File I/O." 47 "@return String containing path to OS temp directory\n" 48 "@note This is legacy function brought over from TGB, and does not appear " 49 "to have much use. Possibly deprecate?\n" 50 "@ingroup FileSystem\n" 51 "@internal") 52{ 53 return Platform::getTemporaryDirectory(); 54} 55 56StringTableEntry Platform::getTemporaryFileName() 57{ 58 char buf[512]; 59 StringTableEntry path = Platform::getTemporaryDirectory(); 60 61 dSprintf(buf, sizeof(buf), "%s/tgb.%08x.%02x.tmp", path, Platform::getRealMilliseconds(), U32(Platform::getRandom() * 255)); 62 63 // [tom, 9/7/2006] This shouldn't be needed, but just in case 64 if(Platform::isFile(buf)) 65 return Platform::getTemporaryFileName(); 66 67 return StringTable->insert(buf); 68} 69 70DefineEngineFunction( getTemporaryFileName, const char *, (), , 71 "@brief Creates a name and extension for a potential temporary file\n\n" 72 "This does not create the actual file. It simply creates a random name " 73 "for a file that does not exist.\n\n" 74 "@note This is legacy function brought over from TGB, and does not appear " 75 "to have much use. Possibly deprecate?\n" 76 "@ingroup FileSystem\n" 77 "@internal") 78{ 79 return Platform::getTemporaryFileName(); 80} 81 82//----------------------------------------------------------------------------- 83static char filePathBuffer[1024]; 84static bool deleteDirectoryRecusrive(const char* pPath) 85{ 86 // Sanity! 87 AssertFatal(pPath != NULL, "Cannot delete directory that is NULL."); 88 89 // Find directories. 90 Vector<StringTableEntry> directories; 91 if (!Platform::dumpDirectories(pPath, directories, 0)) 92 { 93 // Warn. 94 Con::warnf("Could not retrieve sub-directories of '%s'.", pPath); 95 return false; 96 } 97 98 // Iterate directories. 99 for (Vector<StringTableEntry>::iterator basePathItr = directories.begin(); basePathItr != directories.end(); ++basePathItr) 100 { 101 // Fetch base path. 102 StringTableEntry basePath = *basePathItr; 103 104 // Skip if the base path. 105 if (basePathItr == directories.begin() && String::compare(pPath, basePath) == 0) 106 continue; 107 108 // Delete any directories recursively. 109 if (!deleteDirectoryRecusrive(basePath)) 110 return false; 111 } 112 113 // Find files. 114 Vector<Platform::FileInfo> files; 115 if (!Platform::dumpPath(pPath, files, 0)) 116 { 117 // Warn. 118 Con::warnf("Could not retrieve files for directory '%s'.", pPath); 119 return false; 120 } 121 122 // Iterate files. 123 for (Vector<Platform::FileInfo>::iterator fileItr = files.begin(); fileItr != files.end(); ++fileItr) 124 { 125 // Format file. 126 dSprintf(filePathBuffer, sizeof(filePathBuffer), "%s/%s", fileItr->pFullPath, fileItr->pFileName); 127 128 // Delete file. 129 if (!Platform::fileDelete(filePathBuffer)) 130 { 131 // Warn. 132 Con::warnf("Could not delete file '%s'.", filePathBuffer); 133 return false; 134 } 135 } 136 137 // Delete the directory. 138 if (!Platform::fileDelete(pPath)) 139 { 140 // Warn. 141 Con::warnf("Could not delete directory '%s'.", pPath); 142 return false; 143 } 144 145 return true; 146} 147 148//----------------------------------------------------------------------------- 149 150bool Platform::deleteDirectory(const char* pPath) 151{ 152 // Sanity! 153 AssertFatal(pPath != NULL, "Cannot delete directory that is NULL."); 154 155 // Is the path a file? 156 if (Platform::isFile(pPath)) 157 { 158 // Yes, so warn. 159 Con::warnf("Cannot delete directory '%s' as it specifies a file.", pPath); 160 return false; 161 } 162 163 // Expand module location. 164 char pathBuffer[1024]; 165 Con::expandPath(pathBuffer, sizeof(pathBuffer), pPath, NULL, true); 166 167 // Delete directory recursively. 168 return deleteDirectoryRecusrive(pathBuffer); 169} 170 171//----------------------------------------------------------------------------- 172 173static StringTableEntry sgMainCSDir = NULL; 174 175StringTableEntry Platform::getMainDotCsDir() 176{ 177 if(sgMainCSDir == NULL) 178 sgMainCSDir = Platform::getExecutablePath(); 179 180 return sgMainCSDir; 181} 182 183void Platform::setMainDotCsDir(const char *dir) 184{ 185 sgMainCSDir = StringTable->insert(dir); 186} 187 188//----------------------------------------------------------------------------- 189 190typedef Vector<char*> CharVector; 191static CharVector gPlatformDirectoryExcludeList( __FILE__, __LINE__ ); 192 193void Platform::addExcludedDirectory(const char *pDir) 194{ 195 gPlatformDirectoryExcludeList.push_back(dStrdup(pDir)); 196} 197 198void Platform::clearExcludedDirectories() 199{ 200 while(gPlatformDirectoryExcludeList.size()) 201 { 202 dFree(gPlatformDirectoryExcludeList.last()); 203 gPlatformDirectoryExcludeList.pop_back(); 204 } 205} 206 207bool Platform::isExcludedDirectory(const char *pDir) 208{ 209 for(CharVector::iterator i=gPlatformDirectoryExcludeList.begin(); i!=gPlatformDirectoryExcludeList.end(); i++) 210 if(!String::compare(pDir, *i)) 211 return true; 212 213 return false; 214} 215 216//----------------------------------------------------------------------------- 217 218inline void catPath(char *dst, const char *src, U32 len) 219{ 220 if(*dst != '/') 221 { 222 ++dst; --len; 223 *dst = '/'; 224 } 225 226 ++dst; --len; 227 228 dStrncpy(dst, src, len); 229 dst[len - 1] = 0; 230} 231 232// converts the posix root path "/" to "c:/" for win32 233// FIXME: this is not ideal. the c: drive is not guaranteed to exist. 234#if defined(TORQUE_OS_WIN) 235static inline void _resolveLeadingSlash(char* buf, U32 size) 236{ 237 if(buf[0] != '/') 238 return; 239 240 AssertFatal(dStrlen(buf) + 2 < size, "Expanded path would be too long"); 241 dMemmove(buf + 2, buf, dStrlen(buf)); 242 buf[0] = 'c'; 243 buf[1] = ':'; 244} 245#endif 246 247static void makeCleanPathInPlace( char* path ) 248{ 249 U32 pathDepth = 0; 250 char* fromPtr = path; 251 char* toPtr = path; 252 253 bool isAbsolute = false; 254 if( *fromPtr == '/' ) 255 { 256 fromPtr ++; 257 toPtr ++; 258 isAbsolute = true; 259 } 260 else if( fromPtr[ 0 ] != '\0' && fromPtr[ 1 ] == ':' ) 261 { 262 toPtr += 3; 263 fromPtr += 3; 264 isAbsolute = true; 265 } 266 267 while( *fromPtr ) 268 { 269 if( fromPtr[ 0 ] == '.' && fromPtr[ 1 ] == '.' && fromPtr[ 2 ] == '/' ) 270 { 271 // Back up from '../' 272 273 if( pathDepth > 0 ) 274 { 275 pathDepth --; 276 toPtr -= 2; 277 while( toPtr >= path && *toPtr != '/' ) 278 toPtr --; 279 toPtr ++; 280 } 281 else if( !isAbsolute ) 282 { 283 dMemcpy( toPtr, fromPtr, 3 ); 284 toPtr += 3; 285 } 286 287 fromPtr += 3; 288 } 289 else if( fromPtr[ 0 ] == '.' && fromPtr[ 1 ] == '/' ) 290 { 291 // Ignore. 292 fromPtr += 2; 293 } 294 else 295 { 296 if( fromPtr[ 0 ] == '/' ) 297 pathDepth ++; 298 299 *toPtr ++ = *fromPtr ++; 300 } 301 } 302 303 *toPtr = '\0'; 304} 305 306char * Platform::makeFullPathName(const char *path, char *buffer, U32 size, const char *cwd /* = NULL */) 307{ 308 char bspath[1024]; 309 dStrncpy(bspath, path, sizeof(bspath)); 310 bspath[sizeof(bspath)-1] = 0; 311 312 for(S32 i = 0;i < dStrlen(bspath);++i) 313 { 314 if(bspath[i] == '\\') 315 bspath[i] = '/'; 316 } 317 318 if(Platform::isFullPath(bspath)) 319 { 320 // Already a full path 321 #if defined(TORQUE_OS_WIN) 322 _resolveLeadingSlash(bspath, sizeof(bspath)); 323 #endif 324 dStrncpy(buffer, bspath, size); 325 buffer[size-1] = 0; 326 return buffer; 327 } 328 329 // [rene, 05/05/2008] Based on overall file handling in Torque, it does not seem to make 330 // that much sense to me to base things off the current working directory here. 331 332 if(cwd == NULL) 333 cwd = Con::isCurrentScriptToolScript() ? Platform::getMainDotCsDir() : Platform::getCurrentDirectory(); 334 335 dStrncpy(buffer, cwd, size); 336 buffer[size-1] = 0; 337 338 const char* defaultDir = Con::getVariable("defaultGame"); 339 340 char *ptr = bspath; 341 char *slash = NULL; 342 char *endptr = buffer + dStrlen(buffer) - 1; 343 344 do 345 { 346 slash = dStrchr(ptr, '/'); 347 if(slash) 348 { 349 *slash = 0; 350 351 // Directory 352 353 if(String::compare(ptr, "..") == 0) 354 { 355 // Parent 356 endptr = dStrrchr(buffer, '/'); 357 if (endptr) 358 *endptr-- = 0; 359 } 360 else if(String::compare(ptr, ".") == 0) 361 { 362 // Current dir 363 } 364 else if(String::compare(ptr, "~") == 0) 365 { 366 catPath(endptr, defaultDir, size - (endptr - buffer)); 367 endptr += dStrlen(endptr) - 1; 368 } 369 else if(endptr) 370 { 371 catPath(endptr, ptr, size - (endptr - buffer)); 372 endptr += dStrlen(endptr) - 1; 373 } 374 375 ptr = slash + 1; 376 } 377 else if(endptr) 378 { 379 // File 380 381 catPath(endptr, ptr, size - (endptr - buffer)); 382 endptr += dStrlen(endptr) - 1; 383 } 384 385 } while(slash); 386 387 return buffer; 388} 389 390bool Platform::isFullPath(const char *path) 391{ 392 // Quick way out 393 if(path[0] == '/' || path[1] == ':') 394 return true; 395 396 return false; 397} 398 399//----------------------------------------------------------------------------- 400 401/// Return "fileName" stripped of its extension. Only extensions contained 402/// in "validExtensions" will be stripped from the filename. 403/// 404/// @note Extensions in "validExtension" should include the dot. 405String Platform::stripExtension( String fileName, Vector< String>& validExtensions ) 406{ 407 // See if we have a valid extension to strip off 408 String ext; 409 S32 dotPos = fileName.find( '.', 0, String::Right ); 410 if( dotPos != String::NPos ) 411 ext = fileName.substr( dotPos ); 412 413 U32 numValidExt = validExtensions.size(); 414 if( ext.isNotEmpty() && numValidExt ) 415 { 416 bool validExt = false; 417 for( U32 i = 0; i < numValidExt; i++ ) 418 { 419 if( ext.equal( validExtensions[ i ], String::NoCase ) ) 420 { 421 validExt = true; 422 break; 423 } 424 } 425 426 if( !validExt ) 427 ext = String::EmptyString; 428 } 429 430 if( ext.isEmpty() ) 431 return fileName; 432 else 433 return fileName.substr( 0, fileName.length() - ext.length() ); 434} 435 436//----------------------------------------------------------------------------- 437// TODO: wow really shouldn't be adding everything to the string table, use the string class! 438StringTableEntry Platform::makeRelativePathName(const char *path, const char *to) 439{ 440 // Make sure 'to' is a proper absolute path terminated with a forward slash. 441 442 char buffer[ 2048 ]; 443 if( !to ) 444 { 445 dSprintf( buffer, sizeof( buffer ), "%s/", Platform::getMainDotCsDir() ); 446 to = buffer; 447 } 448 else if( !Platform::isFullPath( to ) ) 449 { 450 dSprintf( buffer, sizeof( buffer ), "%s/%s/", Platform::getMainDotCsDir(), to ); 451 makeCleanPathInPlace( buffer ); 452 to = buffer; 453 } 454 else if( to[ dStrlen( to ) - 1 ] != '/' ) 455 { 456 U32 length = getMin( (U32)dStrlen( to ), sizeof( buffer ) - 2 ); 457 dMemcpy( buffer, to, length ); 458 buffer[ length ] = '/'; 459 buffer[ length + 1 ] = '\0'; 460 to = buffer; 461 } 462 463 // If 'path' isn't absolute, make it now. Let's us use a single 464 // absolute/absolute merge path from here on. 465 466 char buffer2[ 1024 ]; 467 if( !Platform::isFullPath( path ) ) 468 { 469 dSprintf( buffer2, sizeof( buffer2 ), "%s/%s", Platform::getMainDotCsDir(), path ); 470 makeCleanPathInPlace( buffer2 ); 471 path = buffer2; 472 } 473 474 // First, find the common prefix and see where 'path' branches off from 'to'. 475 476 const char *pathPtr, *toPtr, *branch = path; 477 for(pathPtr = path, toPtr = to;*pathPtr && *toPtr && dTolower(*pathPtr) == dTolower(*toPtr);++pathPtr, ++toPtr) 478 { 479 if(*pathPtr == '/') 480 branch = pathPtr; 481 } 482 483 // If there's no common part, the two paths are on different drives and 484 // there's nothing we can do. 485 486 if( pathPtr == path ) 487 return StringTable->insert( path ); 488 489 // If 'path' and 'to' are identical (minus trailing slash or so), we can just return './'. 490 491 else if((*pathPtr == 0 || (*pathPtr == '/' && *(pathPtr + 1) == 0)) && 492 (*toPtr == 0 || (*toPtr == '/' && *(toPtr + 1) == 0))) 493 { 494 char* bufPtr = buffer; 495 *bufPtr ++ = '.'; 496 497 if(*pathPtr == '/' || *(pathPtr - 1) == '/') 498 *bufPtr++ = '/'; 499 500 *bufPtr = 0; 501 return StringTable->insert(buffer); 502 } 503 504 // If 'to' is a proper prefix of 'path', the remainder of 'path' is our relative path. 505 506 else if( *toPtr == '\0' && toPtr[ -1 ] == '/' ) 507 return StringTable->insert( pathPtr ); 508 509 // Otherwise have to step up the remaining directories in 'to' and then 510 // append the remainder of 'path'. 511 512 else 513 { 514 // FIXME: This condition is clearly wrong 515 if((*pathPtr == 0 && *toPtr == '/') || (*toPtr == '/' && *pathPtr == 0)) 516 branch = pathPtr; 517 518 // Allocate a new temp so we aren't prone to buffer overruns. 519 520 TempAlloc< char> temp( dStrlen( toPtr ) + dStrlen( branch ) + 1 ); 521 char* bufPtr = temp; 522 523 // Figure out parent dirs 524 525 for(toPtr = to + (branch - path);*toPtr;++toPtr) 526 { 527 if(*toPtr == '/' && *(toPtr + 1) != 0) 528 { 529 *bufPtr++ = '.'; 530 *bufPtr++ = '.'; 531 *bufPtr++ = '/'; 532 } 533 } 534 *bufPtr = 0; 535 536 // Copy the rest 537 if(*branch) 538 dStrcpy(bufPtr, branch + 1, temp.size - (bufPtr - temp.ptr)); 539 else 540 *--bufPtr = 0; 541 542 return StringTable->insert( temp ); 543 } 544} 545 546//----------------------------------------------------------------------------- 547 548static StringTableEntry tryStripBasePath(const char *path, const char *base) 549{ 550 U32 len = dStrlen(base); 551 if(dStrnicmp(path, base, len) == 0) 552 { 553 if(*(path + len) == '/') ++len; 554 return StringTable->insert(path + len, true); 555 } 556 return NULL; 557} 558 559StringTableEntry Platform::stripBasePath(const char *path) 560{ 561 if(path == NULL) 562 return NULL; 563 564 StringTableEntry str = tryStripBasePath(path, Platform::getMainDotCsDir()); 565 566 if(str != NULL ) 567 return str; 568 569 str = tryStripBasePath(path, Platform::getCurrentDirectory()); 570 if(str != NULL ) 571 return str; 572 573 str = tryStripBasePath(path, Platform::getPrefsPath()); 574 if(str != NULL ) 575 return str; 576 577 return path; 578} 579 580//----------------------------------------------------------------------------- 581 582StringTableEntry Platform::getPrefsPath(const char *file /* = NULL */) 583{ 584#ifndef TORQUE2D_TOOLS_FIXME 585 return StringTable->insert(file ? file : ""); 586#else 587 char buf[1024]; 588 const char *company = Con::getVariable("$Game::CompanyName"); 589 if(company == NULL || *company == 0) 590 company = "GarageGames"; 591 592 const char *appName = Con::getVariable("$Game::GameName"); 593 if(appName == NULL || *appName == 0) 594 appName = TORQUE_APP_NAME; 595 596 if(file) 597 { 598 if(dStrstr(file, "..")) 599 { 600 Con::errorf("getPrefsPath - filename cannot be relative"); 601 return NULL; 602 } 603 604 dSprintf(buf, sizeof(buf), "%s/%s/%s/%s", Platform::getUserDataDirectory(), company, appName, file); 605 } 606 else 607 dSprintf(buf, sizeof(buf), "%s/%s/%s", Platform::getUserDataDirectory(), company, appName); 608 609 return StringTable->insert(buf, true); 610#endif 611} 612 613//----------------------------------------------------------------------------- 614 615DefineEngineFunction( getUserDataDirectory, const char *, (), , "getUserDataDirectory()") 616{ 617 return Platform::getUserDataDirectory(); 618} 619 620DefineEngineFunction( getUserHomeDirectory, const char *, (), , "getUserHomeDirectory()") 621{ 622 return Platform::getUserHomeDirectory(); 623} 624 625DefineEngineFunction(setMainDotCsDir, void, (const char* path), , "setMainDotCsDir()") 626{ 627 Platform::setMainDotCsDir(StringTable->insert(path)); 628} 629