| 1513 | |
| 1514 | template <typename... Args> |
| 1515 | inline ssize_t Stream::write_format(const char *fmt, const Args &...args) { |
| 1516 | const auto bufsiz = 2048; |
| 1517 | std::array<char, bufsiz> buf{}; |
| 1518 | |
| 1519 | #if defined(_MSC_VER) && _MSC_VER < 1900 |
| 1520 | auto sn = _snprintf_s(buf.data(), bufsiz, _TRUNCATE, fmt, args...); |
| 1521 | #else |
| 1522 | auto sn = snprintf(buf.data(), buf.size() - 1, fmt, args...); |
| 1523 | #endif |
| 1524 | if (sn <= 0) { return sn; } |
| 1525 | |
| 1526 | auto n = static_cast<size_t>(sn); |
| 1527 | |
| 1528 | if (n >= buf.size() - 1) { |
| 1529 | std::vector<char> glowable_buf(buf.size()); |
| 1530 | |
| 1531 | while (n >= glowable_buf.size() - 1) { |
| 1532 | glowable_buf.resize(glowable_buf.size() * 2); |
| 1533 | #if defined(_MSC_VER) && _MSC_VER < 1900 |
| 1534 | n = static_cast<size_t>(_snprintf_s(&glowable_buf[0], glowable_buf.size(), |
| 1535 | glowable_buf.size() - 1, fmt, |
| 1536 | args...)); |
| 1537 | #else |
| 1538 | n = static_cast<size_t>( |
| 1539 | snprintf(&glowable_buf[0], glowable_buf.size() - 1, fmt, args...)); |
| 1540 | #endif |
| 1541 | } |
| 1542 | return write(&glowable_buf[0], n); |
| 1543 | } else { |
| 1544 | return write(buf.data(), n); |
| 1545 | } |
| 1546 | } |
| 1547 | |
| 1548 | inline void default_socket_options(socket_t sock) { |
| 1549 | int yes = 1; |
no test coverage detected