From 94981da437ab338d0af0532e140d8c837a8f7db4 Mon Sep 17 00:00:00 2001 From: Ghabry Date: Wed, 24 Apr 2024 11:31:49 +0200 Subject: [PATCH 1/2] lmu2png: Make search of Chipset in lmu2png case-insensitive --- lmu2png/src/main.cpp | 44 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/lmu2png/src/main.cpp b/lmu2png/src/main.cpp index 7ade6c8..e015d97 100644 --- a/lmu2png/src/main.cpp +++ b/lmu2png/src/main.cpp @@ -18,9 +18,10 @@ #include #include #include -#include +#include #include #include +#include #include #include #include @@ -50,9 +51,34 @@ std::string GetFileDirectory (const std::string& file) { return found == std::string::npos ? "./" : file.substr(0,found + 1); } -bool Exists(const std::string& filename) { - std::ifstream infile(filename.c_str()); - return infile.good(); +bool Find(std::string& filename) { + namespace fs = std::filesystem; + + fs::path fpath(filename); + + if (!fs::exists(fpath)) { + // Case insensitive search + fs::path fdir = fpath.parent_path(); + if (!fs::is_directory(fdir)) { + return false; + } + + std::string fname_lc = fpath.filename(); + fname_lc = lcf::ReaderUtil::Normalize(fname_lc); + + for (auto const& entry : fs::directory_iterator(fdir)) { + std::string name_lc = entry.path().filename(); + name_lc = lcf::ReaderUtil::Normalize(name_lc); + + if (fname_lc == name_lc) { + fpath = entry.path(); + filename = fpath; + break; + } + } + } + + return fs::is_regular_file(fpath); } std::string path; @@ -71,11 +97,13 @@ std::string FindResource(const std::string& folder, const std::string& base_name for (const auto& dir : dirs) { for (const auto& ext : {".png", ".bmp", ".xyz"}) { - if (Exists(dir + "/" + folder + "/" + base_name + ext)) - return dir + "/" + folder + "/" + base_name + ext; + std::string outfile = dir + "/" + folder + "/" + base_name + ext; + if (Find(outfile)) { + return outfile; + } } } - return ""; + return {}; } SDL_Surface* LoadImage(const char* image_path, bool transparent = false) { @@ -247,7 +275,7 @@ int main(int argc, char** argv) { exit(EXIT_FAILURE); } - if (!Exists(input)) { + if (!Find(input)) { std::cout << "Input map file " << input << " can't be found." << std::endl; exit(EXIT_FAILURE); } From cc4a4cc4990ef1f6d1d98bd0abd87112a4ccc7a4 Mon Sep 17 00:00:00 2001 From: Ghabry Date: Wed, 24 Apr 2024 11:56:28 +0200 Subject: [PATCH 2/2] lmu2png: Only use filesystem api on non-Windows Otherwise requires UTF-16 conversion and Windows doesn't need this search anyway. --- lmu2png/src/main.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lmu2png/src/main.cpp b/lmu2png/src/main.cpp index e015d97..3d11d4b 100644 --- a/lmu2png/src/main.cpp +++ b/lmu2png/src/main.cpp @@ -21,7 +21,6 @@ #include #include #include -#include #include #include #include @@ -31,6 +30,12 @@ #include "chipset.h" #include "sdlxyz.h" +#ifdef _WIN32 +# include +#else +# include +#endif + // prevent SDL main rename #undef main @@ -52,6 +57,10 @@ std::string GetFileDirectory (const std::string& file) { } bool Find(std::string& filename) { +#ifdef _WIN32 + std::ifstream infile(filename.c_str()); + return infile.good(); +#else namespace fs = std::filesystem; fs::path fpath(filename); @@ -79,6 +88,7 @@ bool Find(std::string& filename) { } return fs::is_regular_file(fpath); +#endif } std::string path;