MCPcopy Create free account
hub / github.com/RomanKubiak/ctrlr / getRelativePathFrom

Method getRelativePathFrom

JUCE/modules/juce_core/files/juce_File.cpp:881–942  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

879}
880
881String 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);

Callers 15

getDefaultMethodTreeMethod · 0.80
setSourceFileMethod · 0.80
writeLuaMethodMethod · 0.80
selectionChangedMethod · 0.80
runTestMethod · 0.80
zipModuleFunction · 0.80
fixIncludesFunction · 0.80
encodeBinaryFunction · 0.80
fillInGeneratedCodeMethod · 0.80
fillInCreationCodeMethod · 0.80
setFileMethod · 0.80
setFileMethod · 0.80

Calls 7

countNumberOfSeparatorsFunction · 0.85
endsWithCharMethod · 0.80
dropLastCharactersMethod · 0.80
appendCharPointerMethod · 0.80
existsAsFileMethod · 0.45
getParentDirectoryMethod · 0.45
getAndAdvanceMethod · 0.45

Tested by

no test coverage detected