std::filesystem::relative / proximate

std::filesystem::relative / proximate (상대 경로 만들기)

경로를 기준 경로에 상대적으로 만드는 함수예요. relative는 상대 경로를, proximate는 상대 경로가 "근접하지 않으면" 원래 경로를 돌려줘요. C++17부터 있어요.

출처: cppreference

본문

<filesystem> 헤더에 정의돼 있어요.

path relative( const std::filesystem::path& p, std::error_code& ec ); // (1) C++17
path relative( const std::filesystem::path& p,
               const std::filesystem::path& base = std::filesystem::current_path() ); // (2) C++17
path relative( const std::filesystem::path& p, const std::filesystem::path& base,
               std::error_code& ec );                                // (3) C++17
path proximate( const std::filesystem::path& p, std::error_code& ec ); // (4) C++17
path proximate( const std::filesystem::path& p,
                const std::filesystem::path& base = std::filesystem::current_path() ); // (5) C++17
path proximate( const std::filesystem::path& p, const std::filesystem::path& base,
                std::error_code& ec );                               // (6) C++17
    1. relative(p, current_path(), ec)를 돌려줘요.
  • 2,3) pbase에 상대적으로 만든 경로를 돌려줘요. 다른 처리를 하기 전에 pbase의 심볼릭 링크를 해석하고 정규화해요. 효과적으로 std::filesystem::weakly_canonical(p).lexically_relative(std::filesystem::weakly_canonical(base)) 또는 오류 코드 형식은 첫 오류 시 path()를 돌려주는 것을 제외하고 weakly_canonical(p, ec).lexically_relative(weakly_canonical(base, ec))를 돌려줘요.
  • 4~6) pbase에 상대적으로 만들어진 경로를 돌려줘요. 그 경로가 "근접"(proximate)하지 않으면(즉 다른 드라이브·루트에 있으면) 원래의 p를 돌려줘요.

매개변수

  • p — 상대 경로로 만들 경로
  • base — 기준 경로. 기본값은 현재 작업 디렉터리.
  • ec — 예외 없는 버전에서 오류 보고용 출력 매개변수

반환값

base에 상대적인 p. pbase가 서로 다른 드라이브/루트를 가리키면 proximate는 원래의 p를 돌려줘요.

예외

던지는 오버로드는 OS API 오류가 있으면 std::filesystem::filesystem_error를 던져요. 예외 없는 오버로드는 ec를 설정해요.

더 알아보기 (Learn more)

cppreference