Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

another one wide char file API recover #3117

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/exiv2/basicio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ class EXIV2API FileIo : public BasicIo {
@param path The full path of a file
*/
explicit FileIo(const std::string& path);
explicit FileIo(const std::wstring& path);

//! Destructor. Flushes and closes an open file.
~FileIo() override;
Expand Down Expand Up @@ -433,6 +434,9 @@ class EXIV2API FileIo : public BasicIo {
@brief close the file source and set a new path.
*/
virtual void setPath(const std::string& path);
#ifdef _WIN32
virtual void setPath(const std::wstring& path);
#endif

//@}
//! @name Accessors
Expand Down
8 changes: 6 additions & 2 deletions include/exiv2/image.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,9 @@ class EXIV2API ImageFactory {
read the remote file.
*/
static BasicIo::UniquePtr createIo(const std::string& path, bool useCurl = true);

#ifdef _WIN32
static BasicIo::UniquePtr createIo(const std::wstring& path);
#endif
/*!
@brief Create an Image subclass of the appropriate type by reading
the specified file. %Image type is derived from the file
Expand All @@ -537,7 +539,9 @@ class EXIV2API ImageFactory {
unknown image type.
*/
static Image::UniquePtr open(const std::string& path, bool useCurl = true);

#ifdef _WIN32
static Image::UniquePtr open(const std::wstring& path);
#endif
/*!
@brief Create an Image subclass of the appropriate type by reading
the provided memory. %Image type is derived from the memory
Expand Down
55 changes: 55 additions & 0 deletions src/basicio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,18 @@ class FileIo::Impl {
public:
//! Constructor
explicit Impl(std::string path);
#ifdef _WIN32
explicit Impl(std::wstring path);
#endif
~Impl() = default;
// Enumerations
//! Mode of operation
enum OpMode { opRead, opWrite, opSeek };
// DATA
std::string path_; //!< (Standard) path
#ifdef _WIN32
std::wstring wpath_; //!< UCS2 path
#endif
std::string openMode_; //!< File open mode
FILE* fp_{}; //!< File stream pointer
OpMode opMode_{opSeek}; //!< File open mode
Expand Down Expand Up @@ -110,7 +116,19 @@ class FileIo::Impl {
};

FileIo::Impl::Impl(std::string path) : path_(std::move(path)) {
#ifdef _WIN32
wchar_t t[512];
const auto nw = MultiByteToWideChar(CP_UTF8, 0, path_.data(), (int)path_.size(), t, 512);
wpath_.assign(t, nw);
#endif
}
#ifdef _WIN32
FileIo::Impl::Impl(std::wstring path) : wpath_(std::move(path)) {
char t[1024];
const auto nc = WideCharToMultiByte(CP_UTF8, 0, wpath_.data(), (int)wpath_.size(), t, 1024, nullptr, nullptr);
path_.assign(t, nc);
}
#endif

int FileIo::Impl::switchMode(OpMode opMode) {
if (opMode_ == opMode)
Expand Down Expand Up @@ -159,7 +177,11 @@ int FileIo::Impl::switchMode(OpMode opMode) {
std::fclose(fp_);
openMode_ = "r+b";
opMode_ = opSeek;
#ifdef _WIN32
fp_ = _wfopen(wpath_.c_str(), L"r+b");
#else
fp_ = std::fopen(path_.c_str(), openMode_.c_str());
#endif
if (!fp_)
return 1;
#ifdef _WIN32
Expand All @@ -170,17 +192,29 @@ int FileIo::Impl::switchMode(OpMode opMode) {
} // FileIo::Impl::switchMode

int FileIo::Impl::stat(StructStat& buf) const {
#ifdef _WIN32
struct _stat64 st;
auto ret = _wstat64(wpath_.c_str(), &st);
if (ret == 0) {
buf.st_size = st.st_size;
buf.st_mode = st.st_mode;
}
return ret;
#else
try {
buf.st_size = fs::file_size(path_);
buf.st_mode = fs::status(path_).permissions();
return 0;
} catch (const fs::filesystem_error&) {
return -1;
}
#endif
} // FileIo::Impl::stat

FileIo::FileIo(const std::string& path) : p_(std::make_unique<Impl>(path)) {
}
FileIo::FileIo(const std::wstring& path) : p_(std::make_unique<Impl>(path)) {
}

FileIo::~FileIo() {
close();
Expand Down Expand Up @@ -296,8 +330,23 @@ byte* FileIo::mmap(bool isWriteable) {
void FileIo::setPath(const std::string& path) {
close();
p_->path_ = path;
#ifdef _WIN32
wchar_t t[512];
const auto nw = MultiByteToWideChar(CP_UTF8, 0, p_->path_.data(), (int)p_->path_.size(), t, 512);
p_->wpath_.assign(t, nw);
#endif
}

#ifdef _WIN32
void FileIo::setPath(const std::wstring& path) {
close();
p_->wpath_ = path;
char t[1024];
const auto nc = WideCharToMultiByte(CP_UTF8, 0, p_->wpath_.data(), (int)p_->wpath_.size(), t, 1024, nullptr, nullptr);
w17 marked this conversation as resolved.
Show resolved Hide resolved
p_->path_.assign(t, nc);
}
#endif

size_t FileIo::write(const byte* data, size_t wcount) {
if (p_->switchMode(Impl::opWrite) != 0)
return 0;
Expand Down Expand Up @@ -480,7 +529,13 @@ int FileIo::open(const std::string& mode) {
close();
p_->openMode_ = mode;
p_->opMode_ = Impl::opSeek;
#ifdef _WIN32
wchar_t wmode[10];
MultiByteToWideChar(CP_UTF8, 0, mode.c_str(), -1, wmode, 10);
p_->fp_ = _wfopen(p_->wpath_.c_str(), wmode);
#else
p_->fp_ = ::fopen(path().c_str(), mode.c_str());
#endif
if (!p_->fp_)
return 1;
return 0;
Expand Down
26 changes: 26 additions & 0 deletions src/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@
#include <bit>
#endif

#ifdef _WIN32
#include <windows.h>
#endif

// *****************************************************************************
namespace {
using namespace Exiv2;
Expand Down Expand Up @@ -830,13 +834,35 @@ BasicIo::UniquePtr ImageFactory::createIo(const std::string& path, [[maybe_unuse
#endif
} // ImageFactory::createIo

#ifdef _WIN32
BasicIo::UniquePtr ImageFactory::createIo(const std::wstring& path) {
#ifdef EXV_ENABLE_FILESYSTEM
return std::make_unique<FileIo>(path);
#else
return nullptr;
#endif
}
#endif

Image::UniquePtr ImageFactory::open(const std::string& path, bool useCurl) {
auto image = open(ImageFactory::createIo(path, useCurl)); // may throw
if (!image)
throw Error(ErrorCode::kerFileContainsUnknownImageType, path);
return image;
}

#ifdef _WIN32
Image::UniquePtr ImageFactory::open(const std::wstring& path) {
auto image = open(ImageFactory::createIo(path)); // may throw
if (!image) {
char t[1024];
WideCharToMultiByte(CP_UTF8, 0, path.c_str(), -1, t, 1024, nullptr, nullptr);
throw Error(ErrorCode::kerFileContainsUnknownImageType, t);
}
return image;
}
#endif

Image::UniquePtr ImageFactory::open(const byte* data, size_t size) {
auto image = open(std::make_unique<MemIo>(data, size)); // may throw
if (!image)
Expand Down