| 879 | } |
| 880 | |
| 881 | String File::getRelativePathFrom (const File& dir) const |
| 882 | { |
| 883 | if (dir == *this) |
| 884 | return "."; |
| 885 | |
| 886 | auto thisPath = fullPath; |
| 887 | |
| 888 | while (thisPath.endsWithChar (getSeparatorChar())) |
| 889 | thisPath = thisPath.dropLastCharacters (1); |
| 890 | |
| 891 | auto dirPath = addTrailingSeparator (dir.existsAsFile() ? dir.getParentDirectory().getFullPathName() |
| 892 | : dir.fullPath); |
| 893 | |
| 894 | int commonBitLength = 0; |
| 895 | auto thisPathAfterCommon = thisPath.getCharPointer(); |
| 896 | auto dirPathAfterCommon = dirPath.getCharPointer(); |
| 897 | |
| 898 | { |
| 899 | auto thisPathIter = thisPath.getCharPointer(); |
| 900 | auto dirPathIter = dirPath.getCharPointer(); |
| 901 | |
| 902 | for (int i = 0;;) |
| 903 | { |
| 904 | auto c1 = thisPathIter.getAndAdvance(); |
| 905 | auto c2 = dirPathIter.getAndAdvance(); |
| 906 | |
| 907 | #if NAMES_ARE_CASE_SENSITIVE |
| 908 | if (c1 != c2 |
| 909 | #else |
| 910 | if ((c1 != c2 && CharacterFunctions::toLowerCase (c1) != CharacterFunctions::toLowerCase (c2)) |
| 911 | #endif |
| 912 | || c1 == 0) |
| 913 | break; |
| 914 | |
| 915 | ++i; |
| 916 | |
| 917 | if (c1 == getSeparatorChar()) |
| 918 | { |
| 919 | thisPathAfterCommon = thisPathIter; |
| 920 | dirPathAfterCommon = dirPathIter; |
| 921 | commonBitLength = i; |
| 922 | } |
| 923 | } |
| 924 | } |
| 925 | |
| 926 | // if the only common bit is the root, then just return the full path.. |
| 927 | if (commonBitLength == 0 || (commonBitLength == 1 && thisPath[1] == getSeparatorChar())) |
| 928 | return fullPath; |
| 929 | |
| 930 | auto numUpDirectoriesNeeded = countNumberOfSeparators (dirPathAfterCommon); |
| 931 | |
| 932 | if (numUpDirectoriesNeeded == 0) |
| 933 | return thisPathAfterCommon; |
| 934 | |
| 935 | #if JUCE_WINDOWS |
| 936 | auto s = String::repeatedString ("..\\", numUpDirectoriesNeeded); |
| 937 | #else |
| 938 | auto s = String::repeatedString ("../", numUpDirectoriesNeeded); |
no test coverage detected