Skip to content

Commit

Permalink
Addressed remaining comments
Browse files Browse the repository at this point in the history
  • Loading branch information
aqnuep authored and aqnuep committed Oct 24, 2023
1 parent 6cc1f26 commit 15c6526
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 72 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,7 @@ add_library( objUtil STATIC
utils/sbufstream.h
utils/scapp.h
utils/stdafx.h
utils/platform_utils.h
utils/unused.h
)
target_include_directories(
Expand Down
3 changes: 2 additions & 1 deletion tools/imageio/imageinput.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "imageio.h"
#include "imageio_utility.h"
#include "platform_utils.h"

#include <iomanip>
#include <map>
Expand Down Expand Up @@ -47,7 +48,7 @@ ImageInput::open(const _tstring& filename,
// Check file exists, before looking for a suitable plugin.
// MS's STL has `open` overloads that accept wchar_t to handle
// Window's Unicode file names.
ifs.open(filename, std::ios::binary | std::ios::in);
ifs.open(DecodeUTF8Path(filename).c_str(), std::ios::binary | std::ios::in);
if (ifs.fail()) {
throw std::runtime_error(
fmt::format("Open of \"{}\" failed. {}",
Expand Down
1 change: 0 additions & 1 deletion tools/ktx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ add_executable(ktxtools
metrics_utils.h
transcode_utils.cpp
transcode_utils.h
platform_utils.h
utility.h
validate.cpp
validate.h
Expand Down
2 changes: 1 addition & 1 deletion tools/ktx/command_create.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1252,7 +1252,7 @@ void CommandCreate::executeCreate() {
}

// Save output file
const auto outputPath = std::filesystem::path(DecodeUTF8Path(options.outputFilepath));
const auto outputPath = std::filesystem::path(options.outputFilepath);
if (outputPath.has_parent_path())
std::filesystem::create_directories(outputPath.parent_path());

Expand Down
2 changes: 1 addition & 1 deletion tools/ktx/command_encode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ void CommandEncode::executeEncode() {
}

// Save output file
const auto outputPath = std::filesystem::path(DecodeUTF8Path(options.outputFilepath));
const auto outputPath = std::filesystem::path(options.outputFilepath);
if (outputPath.has_parent_path())
std::filesystem::create_directories(outputPath.parent_path());

Expand Down
2 changes: 1 addition & 1 deletion tools/ktx/command_extract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ void CommandExtract::executeExtract() {
(!options.fragmentURI.facial.is_undefined() && options.fragmentURI.facial.is_multi()) ||
((options.globalAll || options.depthFlagUsed) && options.depth.is_multi());
try {
const auto outputPath = std::filesystem::path(DecodeUTF8Path(options.outputPath));
const auto outputPath = std::filesystem::path(options.outputPath);
if (isMultiOutput) {
if (std::filesystem::exists(outputPath) && !std::filesystem::is_directory(outputPath))
fatal_usage("Specified output path must be a directory for multi-output extract: \"{}\".", options.outputPath);
Expand Down
6 changes: 3 additions & 3 deletions tools/ktx/command_help.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,15 +168,15 @@ void CommandHelp::executeHelp() {
fatal(rc::RUNTIME_ERROR, "Failed to open the html documentation: ERROR {}", r);

#else
#if defined(__APPLE__)
# if defined(__APPLE__)
char buf[PATH_MAX];
uint32_t bufsize = PATH_MAX;
if (const auto ec = _NSGetExecutablePath(buf, &bufsize))
fatal(rc::RUNTIME_ERROR, "Failed to determine executable path: ERROR {}", ec);
const auto executablePath = std::filesystem::canonical(buf);
#else // Linux
# else // Linux
const auto executablePath = std::filesystem::canonical("/proc/self/exe");
#endif
# endif

const auto executableDir = std::filesystem::path(executablePath).remove_filename();
const auto manFile = fmt::format("{}/../share/man/man1/ktx{}{}.1",
Expand Down
2 changes: 1 addition & 1 deletion tools/ktx/command_transcode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ void CommandTranscode::executeTranscode() {
writer.c_str());

// Save output file
const auto outputPath = std::filesystem::path(DecodeUTF8Path(options.outputFilepath));
const auto outputPath = std::filesystem::path(options.outputFilepath);
if (outputPath.has_parent_path())
std::filesystem::create_directories(outputPath.parent_path());

Expand Down
31 changes: 2 additions & 29 deletions tools/ktx/ktx_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,16 @@


#include "command.h"
#include "platform_utils.h"
#include "stdafx.h"
#include <iostream>
#include <string>
#include <memory>
#include <unordered_map>

#include <cxxopts.hpp>
#include <fmt/ostream.h>
#include <fmt/printf.h>

#if defined(_WIN32)
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#include <shellapi.h>
#endif

// -------------------------------------------------------------------------------------------------

namespace ktx {
Expand Down Expand Up @@ -197,23 +186,7 @@ int _tmain(int argc, _TCHAR* argv[]) {
// // pbxproj file, so it can't be disabled in a generated project.
// // Remove these from the arguments under consideration.

#if defined(_WIN32)
// Windows does not support UTF-8 argv so we have to manually acquire it
LPWSTR commandLine = GetCommandLineW();
LPWSTR* wideArgv = CommandLineToArgvW(commandLine, &argc);
std::vector<std::unique_ptr<_TCHAR[]>> utf8Argv(argc);
for (int i = 0; i < argc; ++i) {
int byteSize = WideCharToMultiByte(CP_UTF8, 0, wideArgv[i], -1, nullptr, 0, nullptr, nullptr);
utf8Argv[i] = std::make_unique<_TCHAR[]>(byteSize);
WideCharToMultiByte(CP_UTF8, 0, wideArgv[i], -1, utf8Argv[i].get(), byteSize, nullptr, nullptr);
argv[i] = utf8Argv[i].get();
}

// Set UTF-8 codepage for the console
if (!SetConsoleOutputCP(CP_UTF8)) {
fmt::print(std::cerr, "{} warning: failed to set UTF-8 code page for console output.\n", argv[0]);
}
#endif
InitUTF8CLI(argc, argv);

if (argc >= 2) {
// Has a subcommand, attempt to lookup
Expand Down
34 changes: 0 additions & 34 deletions tools/ktx/platform_utils.h

This file was deleted.

68 changes: 68 additions & 0 deletions utils/platform_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright 2022-2023 The Khronos Group Inc.
// Copyright 2022-2023 RasterGrid Kft.
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include "stdafx.h"
#include <string>
#include <iostream>
#include <memory>
#include <vector>
#include <fmt/ostream.h>
#include <fmt/printf.h>

#if defined(_WIN32)
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#include <shellapi.h>
#endif

#if defined(_WIN32)
// For Windows, we convert the UTF-8 path to a UTF-16 path to force using the APIs
// that correctly handle unicode characters
inline std::wstring DecodeUTF8Path(std::string path) {
std::wstring result;
int len = MultiByteToWideChar(CP_UTF8, 0, path.c_str(), static_cast<int>(path.length()), NULL, 0);
if (len > 0)
{
result.resize(len);
MultiByteToWideChar(CP_UTF8, 0, path.c_str(), static_cast<int>(path.length()), &result[0], len);
}
return result;
}
#else
// For other platforms there is no need for any conversion, they support UTF-8 natively
inline std::string DecodeUTF8Path(std::string path) {
return path;
}
#endif

inline void InitUTF8CLI(int argc, _TCHAR* argv[]) {
#if defined(_WIN32)
// Windows does not support UTF-8 argv so we have to manually acquire it
static std::vector<std::unique_ptr<_TCHAR[]>> utf8Argv(argc);
LPWSTR commandLine = GetCommandLineW();
LPWSTR* wideArgv = CommandLineToArgvW(commandLine, &argc);
for (int i = 0; i < argc; ++i) {
int byteSize = WideCharToMultiByte(CP_UTF8, 0, wideArgv[i], -1, nullptr, 0, nullptr, nullptr);
utf8Argv[i] = std::make_unique<_TCHAR[]>(byteSize);
WideCharToMultiByte(CP_UTF8, 0, wideArgv[i], -1, utf8Argv[i].get(), byteSize, nullptr, nullptr);
argv[i] = utf8Argv[i].get();
}

// Set UTF-8 codepage for the console
if (!SetConsoleOutputCP(CP_UTF8)) {
fmt::print(std::cerr, "{} warning: failed to set UTF-8 code page for console output.\n", argv[0]);
}
#else
// Nothing to do for other platforms
(void)argc;
(void)argv;
#endif
}

0 comments on commit 15c6526

Please sign in to comment.