MCPcopy Create free account
hub / github.com/computationalpathologygroup/ASAP / getPathRelativeToLocation

Function getPathRelativeToLocation

core/filetools.cpp:365–422  ·  view source on GitHub ↗

---------------------------------------------------------------------------

Source from the content-addressed store, hash-verified

363
364//---------------------------------------------------------------------------
365 std::string getPathRelativeToLocation(const std::string &_pathToAlter, const std::string &_fixedPath)
366 {
367//Make copies so we can change them for processing
368 std::string pathToAlter = _pathToAlter;
369 std::string fixedPath = _fixedPath;
370
371//if a relative path cannot be written
372 if ( (rootName(pathToAlter)).compare(rootName(fixedPath)) != 0 )
373 return pathToAlter;
374//if the two paths are the same
375 if ( pathToAlter.compare(fixedPath) == 0)
376 return std::string(".");
377
378//if the path ends with a "\" then cut it off
379 if (pathToAlter.find_last_of(dirsep) == pathToAlter.length() - 1)
380 pathToAlter = pathToAlter.substr(0, pathToAlter.length() - 1);
381 if (fixedPath.find_last_of(dirsep) == fixedPath.length() - 1)
382 fixedPath = fixedPath.substr(0, fixedPath.length() - 1);
383
384
385 std::string relativePath;
386//If the entire fixed path is contained within the path to be given
387 if ( pathToAlter.length() >= fixedPath.length()
388 && pathToAlter.substr(0, fixedPath.length()).compare(fixedPath) == 0 )
389 {
390 //Tben we just need "./" and to append the remainder (non-matched part) of pathToAlter
391 relativePath = std::string(".") + dirsep + pathToAlter.substr(fixedPath.length() + 1, pathToAlter.length() - fixedPath.length());
392 }
393//Otherwise we need at least one "../" step in the relative path
394 else
395 {
396 relativePath = std::string("..") + dirsep;
397 //step up a level in the fixed path and see if it is now contained within the pathToAlter
398 std::string choppedFixed = upOneLevel(fixedPath);
399 //As long as the "choppedFixed" is not contained in pathToAlter
400 while ( pathToAlter.substr(0, choppedFixed.length()).compare(choppedFixed) != 0)
401 {
402 //Keep stepping up one level and appending "../" to relative path
403 relativePath = relativePath + std::string("..") + dirsep;
404 choppedFixed = upOneLevel(choppedFixed);
405 }
406
407 //if choppedFixed ends with a "\" then cut it off (e.g. if it is C:\ then '\' always appended)
408 if (choppedFixed.find_last_of(dirsep) == choppedFixed.length() - 1)
409 choppedFixed = choppedFixed.substr(0, choppedFixed.length() - 1);
410
411 //A string to hold the path that we need to append to the "../../../" we made
412 std::string endOfRelPath = std::string("");
413
414 //Take the end part of "pathToAlter" (The part which doesn't match with choppedFixed)
415 if (pathToAlter.length() > choppedFixed.length())
416 endOfRelPath = pathToAlter.substr(choppedFixed.length() + 1, pathToAlter.length() - choppedFixed.length()) ;
417
418 //Append this to the "../../../" part
419 relativePath = relativePath + endOfRelPath;
420 }
421 return relativePath;
422 }

Callers

nothing calls this directly

Calls 2

rootNameFunction · 0.85
upOneLevelFunction · 0.85

Tested by

no test coverage detected