Skip to content

Commit

Permalink
NXDK support
Browse files Browse the repository at this point in the history
Most of libfmt is available except functions that depend on `fileno`,
such as file descriptor duplication.
  • Loading branch information
glebm committed Jul 16, 2022
1 parent 688a627 commit 16d6760
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 12 deletions.
8 changes: 4 additions & 4 deletions include/fmt/chrono.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ FMT_BEGIN_NAMESPACE
# if FMT_HAS_INCLUDE("winapifamily.h")
# include <winapifamily.h>
# 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
Expand Down Expand Up @@ -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_);
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions include/fmt/format-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
# include <locale>
#endif

#ifdef _WIN32
#if defined(_WIN32) && !defined(NXDK)
# include <io.h> // _isatty
#endif

Expand Down Expand Up @@ -1395,7 +1395,7 @@ template <typename T> decimal_fp<T> 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();
Expand Down Expand Up @@ -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<sizeof(long) == 4, unsigned long, unsigned>;
extern "C" __declspec(dllimport) int __stdcall WriteConsoleW( //
Expand All @@ -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()));
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 5 additions & 1 deletion include/fmt/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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.
Expand Down
6 changes: 5 additions & 1 deletion src/os.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<wchar_t> s) {
if (int error_code = convert(s)) {
FMT_THROW(windows_error(error_code,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 16d6760

Please sign in to comment.