Skip to content

Add wil::println() - C++23's std::println() but for HANDLEs #492

Open
@fredemmott

Description

e.g (needs C++20):

void println(
  HANDLE handle,
  std::format_string<Args...> format,
  Args&&... args) {
  const auto buffer = std::format(format, std::forward<Args>(args)...) + '\n';
  DWORD bytesWritten = {};
  while (bytesWritten < buffer.size()) {
    const auto remaining = buffer.size() - bytesWritten;
    DWORD bytesWrittenThisBatch {};
    const auto result = WriteFile(
      handle,
      buffer.data() + bytesWritten,
      remaining,
      &bytesWrittenThisBatch,
      nullptr);
    if (!SUCCEEDED(result)) {
      throw std::system_error(result, std::system_category());
    }
    bytesWritten += bytesWrittenThisBatch;
  }
}

Would also probably want a wchar version taking an std::wformat_string

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions