module: use cf_FindRealFileNameCaseInsensitive() instead duplicated

In mod_GetRealModuleName() there was error - arguments of mod_FindRealFileNameCaseInsensitive() is misplaced.
This commit is contained in:
Azamat H. Hackimov 2024-09-15 23:35:00 +03:00
parent dc0cd880e4
commit 85cceef2af
2 changed files with 3 additions and 46 deletions

View File

@ -7,6 +7,7 @@ set(CPPS
add_library(module STATIC ${HEADERS} ${CPPS})
target_link_libraries(module PRIVATE
cfile
ddebug
logger
)

View File

@ -101,55 +101,11 @@
#include <dlfcn.h>
#endif
#include "cfile.h"
#include "crossplat.h"
#include "log.h"
#include "module.h"
/**
* Returns fixed case file name to actual case on disk for case-sensitive filesystems (Linux).
* This is actually copy of cf_FindRealFileNameCaseInsensitive() from CFILE.
* @param fname the fixed case name to map to reality
* @param directory optional directory to search within (default - current path)
* @return filename with actual case name or empty path if there no mapping in filesystem
* @note This function returns only filename without directory part, i.e.
* mod_FindRealFileNameCaseInsensitive("test/test.txt") will return only "test.txt" on success.
*/
std::filesystem::path mod_FindRealFileNameCaseInsensitive(const std::filesystem::path &fname,
const std::filesystem::path &directory = ".") {
// Dumb check, maybe there already all ok?
if (std::filesystem::exists((directory / fname))) {
return fname.filename();
}
std::filesystem::path result, search_path, search_file;
search_path = directory / fname.parent_path();
search_file = fname.filename();
// If directory does not exist, nothing to search.
if (!std::filesystem::is_directory(search_path) || search_file.empty()) {
return {};
}
// Search component in search_path
auto const &it = std::filesystem::directory_iterator(search_path);
auto found = std::find_if(it, end(it), [&search_file, &search_path, &result](const auto &dir_entry) {
return stricmp(dir_entry.path().filename().u8string().c_str(), search_file.u8string().c_str()) == 0;
});
if (found != end(it)) {
// Match, append to result
result = found->path();
search_path = result;
} else {
// Component not found, mission failed
return {};
}
return result.filename();
}
int ModLastError = MODERR_NOERROR;
std::filesystem::path mod_GetRealModuleName(const std::filesystem::path &mod_filename) {
@ -228,7 +184,7 @@ bool mod_LoadModule(module *handle, const std::filesystem::path &imodfilename, i
if (!handle->handle) {
// ok we couldn't find the given name...try other ways
std::filesystem::path parent_path = modfilename.parent_path();
std::filesystem::path new_filename = mod_FindRealFileNameCaseInsensitive(parent_path, modfilename.filename());
std::filesystem::path new_filename = cf_FindRealFileNameCaseInsensitive(modfilename.filename(), parent_path);
if (new_filename.empty()) {
LOG_ERROR.printf("Module Load Err: %s", dlerror());