Skip to content

Commit

Permalink
fix problem with path name encoding on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
striezel committed Feb 27, 2024
1 parent 767c1d4 commit 7663b00
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Version history of webp-viewer

## Version 0.?.? (2024-02-??)

A problem with handling of special characters in path names on Windows is fixed.

## Version 0.6.1 (2023-11-07)

The displayed image is now resized when the user resizes the window.
Expand Down
31 changes: 28 additions & 3 deletions webp-viewer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@

#include <algorithm>
#include <cmath>
#if defined(_WIN32) || defined(_WIN64)
#include <cstdlib>
#endif
#include <filesystem>
#include <iostream>
#include <string>
Expand Down Expand Up @@ -167,13 +170,35 @@ int main(int argc, char** argv)
else
{
// Parameter might be a file.
std::filesystem::path file;
try
{
#if !defined(_WIN32) && !defined(_WIN64)
file = param;
#else
std::wstring utf16(param.size(), L'\0');
const auto num_chars = std::mbstowcs(&utf16[0], param.c_str(), param.size());
if (num_chars == static_cast<std::size_t>(-1))
{
std::cerr << "Error: " << param << " is not a valid parameter!\n";
return rcInvalidParameter;
}
file = utf16;
#endif
}
catch (...)
{
std::cerr << "Error: " << param << " is neither a valid parameter "
<< "nor an existing WebP file!\n";
return rcInvalidParameter;
}
std::error_code error;
if (!std::filesystem::exists(param, error) || error)
if (!std::filesystem::exists(file, error) || error)
{
std::cerr << "Error: File " << param << " does not exist!\n";
std::cerr << "Error: File " << file << " does not exist!\n";
return rcInvalidParameter;
}
files.push_back(param);
files.push_back(file);
}
}
} // if arguments are there
Expand Down

0 comments on commit 7663b00

Please sign in to comment.