--------------------------------- FileUtil::GetRelativePath Converts inPath to a path relative to rootPath - assumes both arguments paths are relative to the executable directory
| 427 | // Converts inPath to a path relative to rootPath - assumes both arguments paths are relative to the executable directory |
| 428 | // |
| 429 | std::string FileUtil::GetRelativePath(std::string const& inPath, std::string const& rootPath) |
| 430 | { |
| 431 | // Make sure both paths have the same format |
| 432 | std::string absIn = GetAbsolutePath(inPath); |
| 433 | std::string absRoot = GetAbsolutePath(rootPath); |
| 434 | |
| 435 | // Find the first letter that is different |
| 436 | size_t idx = 0u; |
| 437 | for (;idx < std::min(absIn.size(), absRoot.size()); ++idx) |
| 438 | { |
| 439 | if (absIn[idx] != absRoot[idx]) |
| 440 | { |
| 441 | break; |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | // trace backwards to the last path delimiter - delimiters should be all the same character (s_PathDelimiter) now due to GetAbsolutePath() |
| 446 | while (idx > 0u) |
| 447 | { |
| 448 | --idx; |
| 449 | if (absIn[idx] == s_PathDelimiter) |
| 450 | { |
| 451 | break; |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | // figure out how many times we need to go to the parent directory |
| 456 | size_t searchStart = idx; |
| 457 | size_t numParentDirs = 0u; |
| 458 | for (;;) |
| 459 | { |
| 460 | searchStart = absRoot.find(s_PathDelimiter, ++searchStart); // idx is on a path delimiter right now, so we start from the next character |
| 461 | if (searchStart != std::string::npos) // if we found another delimiter we have more parent directories |
| 462 | { |
| 463 | ++numParentDirs; |
| 464 | } |
| 465 | else |
| 466 | { |
| 467 | break; // if we can't find any more we have reached the end of the string |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | // build our output path |
| 472 | |
| 473 | // first escape the root directory into the first common directory |
| 474 | std::string retVal; |
| 475 | while (numParentDirs > 0u) |
| 476 | { |
| 477 | retVal += "../"; |
| 478 | --numParentDirs; |
| 479 | } |
| 480 | |
| 481 | // append the rest of our input path |
| 482 | // if the current common index is a delimiter we skip that |
| 483 | if (absIn[idx] == s_PathDelimiter) |
| 484 | { |
| 485 | ++idx; |
| 486 | } |
nothing calls this directly
no outgoing calls
no test coverage detected