From 16d67608437f4fb9d293e561b4ddbccf3815a097 Mon Sep 17 00:00:00 2001 From: Gleb Mazovetskiy Date: Sat, 2 Jul 2022 11:30:43 +0100 Subject: [PATCH] NXDK support Most of libfmt is available except functions that depend on `fileno`, such as file descriptor duplication. --- include/fmt/chrono.h | 8 ++++---- include/fmt/format-inl.h | 10 +++++----- include/fmt/format.h | 2 +- include/fmt/os.h | 6 +++++- src/os.cc | 6 +++++- 5 files changed, 20 insertions(+), 12 deletions(-) diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index 4a2c3a3d0a9f..600e53fa423f 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -28,8 +28,8 @@ FMT_BEGIN_NAMESPACE # if FMT_HAS_INCLUDE("winapifamily.h") # include # endif -# if defined(_WIN32) && (!defined(WINAPI_FAMILY) || \ - (WINAPI_FAMILY == WINAPI_FAMILY_DESKTOP_APP)) +# if defined(_WIN32) && !defined(NXDK) && \ + (!defined(WINAPI_FAMILY) || (WINAPI_FAMILY == WINAPI_FAMILY_DESKTOP_APP)) # define FMT_USE_TZSET 1 # else # define FMT_USE_TZSET 0 @@ -469,7 +469,7 @@ inline std::tm localtime(std::time_t time) { bool fallback(int res) { return res == 0; } -#if !FMT_MSC_VERSION +#if !FMT_MSC_VERSION || defined(NXDK) bool fallback(detail::null<>) { using namespace fmt::detail; std::tm* tm = std::localtime(&time_); @@ -515,7 +515,7 @@ inline std::tm gmtime(std::time_t time) { bool fallback(int res) { return res == 0; } -#if !FMT_MSC_VERSION +#if !FMT_MSC_VERSION || defined(NXDK) bool fallback(detail::null<>) { std::tm* tm = std::gmtime(&time_); if (tm) tm_ = *tm; diff --git a/include/fmt/format-inl.h b/include/fmt/format-inl.h index 8391aa9b7414..afc3c472976d 100644 --- a/include/fmt/format-inl.h +++ b/include/fmt/format-inl.h @@ -22,7 +22,7 @@ # include #endif -#ifdef _WIN32 +#if defined(_WIN32) && !defined(NXDK) # include // _isatty #endif @@ -1395,7 +1395,7 @@ template decimal_fp to_decimal(T x) noexcept { } } // namespace dragonbox -#ifdef _MSC_VER +#if defined(_MSC_VER) && !defined(NXDK) FMT_FUNC auto fmt_snprintf(char* buf, size_t size, const char* fmt, ...) -> int { auto args = va_list(); @@ -1473,7 +1473,7 @@ FMT_FUNC std::string vformat(string_view fmt, format_args args) { return to_string(buffer); } -#ifdef _WIN32 +#if defined(_WIN32) && !defined(NXDK) namespace detail { using dword = conditional_t; extern "C" __declspec(dllimport) int __stdcall WriteConsoleW( // @@ -1483,7 +1483,7 @@ extern "C" __declspec(dllimport) int __stdcall WriteConsoleW( // namespace detail { FMT_FUNC void print(std::FILE* f, string_view text) { -#ifdef _WIN32 +#if defined(_WIN32) && !defined(NXDK) auto fd = _fileno(f); if (_isatty(fd)) { detail::utf8_to_utf16 u16(string_view(text.data(), text.size())); @@ -1507,7 +1507,7 @@ FMT_FUNC void vprint(std::FILE* f, string_view format_str, format_args args) { detail::print(f, {buffer.data(), buffer.size()}); } -#ifdef _WIN32 +#if defined(_WIN32) && !defined(NXDK) // Print assuming legacy (non-Unicode) encoding. FMT_FUNC void detail::vprint_mojibake(std::FILE* f, string_view format_str, format_args args) { diff --git a/include/fmt/format.h b/include/fmt/format.h index dad643c624e2..0afb9a96abc2 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -1544,7 +1544,7 @@ FMT_CONSTEXPR inline fp get_cached_power(int min_exponent, return {data::pow10_significands[index], data::pow10_exponents[index]}; } -#ifndef _MSC_VER +#if !defined(_MSC_VER) || defined(NXDK) # define FMT_SNPRINTF snprintf #else FMT_API auto fmt_snprintf(char* buf, size_t size, const char* fmt, ...) -> int; diff --git a/include/fmt/os.h b/include/fmt/os.h index d82be1125a4b..03ce4d5feb0a 100644 --- a/include/fmt/os.h +++ b/include/fmt/os.h @@ -49,7 +49,7 @@ # define FMT_POSIX_CALL(call) FMT_SYSTEM(call) #else # define FMT_SYSTEM(call) ::call -# ifdef _WIN32 +# if defined(_WIN32) && !defined(NXDK) // Fix warnings about deprecated symbols. # define FMT_POSIX_CALL(call) ::_##call # else @@ -260,7 +260,9 @@ class buffered_file { // Returns the pointer to a FILE object representing this file. FILE* get() const noexcept { return file_; } +#ifndef NXDK FMT_API int descriptor() const; +#endif void vprint(string_view format_str, format_args args) { fmt::vprint(file_, format_str, args); @@ -336,6 +338,7 @@ class FMT_API file { // Attempts to write count bytes from the specified buffer to the file. size_t write(const void* buffer, size_t count); +#ifndef NXDK // Duplicates a file descriptor with the dup function and returns // the duplicate as a file object. static file dup(int fd); @@ -351,6 +354,7 @@ class FMT_API file { // Creates a pipe setting up read_end and write_end file objects for reading // and writing respectively. static void pipe(file& read_end, file& write_end); +#endif // Creates a buffered_file object associated with this file and detaches // this file object from the file. diff --git a/src/os.cc b/src/os.cc index f388ead0191b..52080628cf3e 100644 --- a/src/os.cc +++ b/src/os.cc @@ -71,7 +71,7 @@ inline std::size_t convert_rwcount(std::size_t count) { return count; } FMT_BEGIN_NAMESPACE -#ifdef _WIN32 +#if defined(_WIN32) && !defined(NXDK) detail::utf16_to_utf8::utf16_to_utf8(basic_string_view s) { if (int error_code = convert(s)) { FMT_THROW(windows_error(error_code, @@ -202,11 +202,13 @@ void buffered_file::close() { if (result != 0) FMT_THROW(system_error(errno, "cannot close file")); } +#ifndef NXDK int buffered_file::descriptor() const { int fd = FMT_POSIX_CALL(fileno(file_)); if (fd == -1) FMT_THROW(system_error(errno, "cannot get file descriptor")); return fd; } +#endif // !NXDK #if FMT_USE_FCNTL file::file(cstring_view path, int oflag) { @@ -281,6 +283,7 @@ std::size_t file::write(const void* buffer, std::size_t count) { return detail::to_unsigned(result); } +#ifndef NXDK file file::dup(int fd) { // Don't retry as dup doesn't return EINTR. // http://pubs.opengroup.org/onlinepubs/009695399/functions/dup.html @@ -326,6 +329,7 @@ void file::pipe(file& read_end, file& write_end) { read_end = file(fds[0]); write_end = file(fds[1]); } +#endif buffered_file file::fdopen(const char* mode) { // Don't retry as fdopen doesn't return EINTR.