Skip to content

Instantly share code, notes, and snippets.

@kennykerr
Created January 29, 2016 06:44
Show Gist options
  • Select an option

  • Save kennykerr/d346e15c0f6fc6e1730f to your computer and use it in GitHub Desktop.

Select an option

Save kennykerr/d346e15c0f6fc6e1730f to your computer and use it in GitHub Desktop.

Revisions

  1. kennykerr created this gist Jan 29, 2016.
    62 changes: 62 additions & 0 deletions path_combine.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    #include <windows.h>
    #include <pathcch.h>
    #include <string>

    #pragma comment(lib, "pathcch")

    namespace impl
    {
    void check(HRESULT const result)
    {
    if (result != S_OK)
    {
    throw result;
    }
    }

    void path_append(std::wstring & path, wchar_t const * const more, size_t const size)
    {
    path.resize(path.size() + 5 + size); // \\?\path\more

    check(PathCchAppendEx(&path[0],
    path.size() + 1,
    more,
    PATHCCH_ALLOW_LONG_PATHS));

    path.resize(wcslen(path.c_str()));
    }

    void path_append(std::wstring & path, wchar_t const * const more)
    {
    path_append(path, more, wcslen(more));
    }

    void path_append(std::wstring & path, std::wstring const & more)
    {
    path_append(path, more.c_str(), more.size());
    }

    void path_combine(std::wstring & path) {}

    template <typename First, typename ... Rest>
    void path_combine(std::wstring & path, First const & first, Rest const & ... rest)
    {
    path_append(path, first);
    path_combine(path, rest ...);
    }
    }

    template <typename ... T>
    std::wstring path_combine(T const & ... paths)
    {
    std::wstring path;
    impl::path_combine(path, paths ...);
    return path;
    }

    int main()
    {
    std::wstring path = path_combine(L"C:\\root", L"one", L"two", L"three", L"four");

    printf("%ls\n", path.c_str());
    }