Skip to content

Commit

Permalink
Resize instead of reserve
Browse files Browse the repository at this point in the history
Signed-off-by: Julien Jerphanion <[email protected]>
  • Loading branch information
jjerphan committed Jan 30, 2025
1 parent 9ab42d9 commit 457efb7
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion libmamba/src/core/util_os.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,35 @@ static_assert(std::is_same_v<mamba::DWORD, ::DWORD>);

namespace mamba
{
#ifdef _WIN32
fs::u8path get_path_of_hmodule(HMODULE hModule)
{
std::wstring buffer(MAX_PATH, '\0');
DWORD new_size = MAX_PATH;
DWORD size = 0;
while (true)
{
size = GetModuleFileNameW(hModule, buffer.data(), static_cast<DWORD>(buffer.size()));
if (size == 0)
{
throw mamba::mamba_error(
"Could find the filename of the libmamba's module handle. (GetModuleFileNameW failed)",
mamba_error_code::internal_failure
);
}
if (size < new_size)
{
break;
}

new_size *= 2;
buffer.reserve(new_size);
}
buffer.resize(size);
return fs::absolute(buffer);
}
#endif

// Heavily inspired by https://github.com/gpakosz/whereami/
// check their source to add support for other OS
fs::u8path get_self_exe_path()
Expand Down Expand Up @@ -126,7 +155,7 @@ namespace mamba
}

new_size *= 2;
buffer.reserve(new_size);
buffer.resize(new_size);
}
buffer.resize(size);
return fs::absolute(buffer);
Expand Down

0 comments on commit 457efb7

Please sign in to comment.